From ab55f27ea48eaabfe05d2cfb1e057b85afcd53a5 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 26 Oct 2020 15:29:59 +0000
Subject: [PATCH] DoH: Add more CLI options for testing

---
 core/java/src/net/i2p/util/DNSOverHTTPS.java | 37 +++++++++++++++-----
 core/java/src/net/i2p/util/SSLEepGet.java    | 12 +++++--
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/core/java/src/net/i2p/util/DNSOverHTTPS.java b/core/java/src/net/i2p/util/DNSOverHTTPS.java
index 3d3167a765..cd79c56a7b 100644
--- a/core/java/src/net/i2p/util/DNSOverHTTPS.java
+++ b/core/java/src/net/i2p/util/DNSOverHTTPS.java
@@ -141,6 +141,16 @@ public class DNSOverHTTPS implements EepGet.StatusListener {
      *  @return null if not found
      */
     public String lookup(String host, Type type) {
+        return lookup(host, type, null);
+    }
+
+    /**
+     *  Lookup in cache, then query servers
+     *  @param url null to query several default servers, or specify single server
+     *  @return null if not found
+     *  @since 0.9.48
+     */
+    private String lookup(String host, Type type, String url) {
         if (Addresses.isIPAddress(host))
             return host;
         if (host.startsWith("["))
@@ -175,7 +185,7 @@ public class DNSOverHTTPS implements EepGet.StatusListener {
             if (rv != null)
                 return rv;
         }
-        return query(host, type);
+        return query(host, type, url);
     }
 
     public static void clearCaches() {
@@ -206,10 +216,15 @@ public class DNSOverHTTPS implements EepGet.StatusListener {
 
     /**
      *  Query servers
+     *  @param url null to query several default servers, or specify single server
      *  @return null if not found
      */
-    private String query(String host, Type type) {
-        List<String> toQuery = new ArrayList<String>((type == Type.V6_ONLY) ? v6urls : v4urls);
+    private String query(String host, Type type, String url) {
+        List<String> toQuery;
+        if (url != null)
+            toQuery = Collections.singletonList(url);
+        else
+            toQuery = new ArrayList<String>((type == Type.V6_ONLY) ? v6urls : v4urls);
         Collections.shuffle(toQuery);
         final long timeout = System.currentTimeMillis() + OVERALL_TIMEOUT;
         if (type == Type.V4_ONLY || type == Type.V4_PREFERRED) {
@@ -430,7 +445,8 @@ public class DNSOverHTTPS implements EepGet.StatusListener {
     public static void main(String[] args) {
         Type type = Type.V4_PREFERRED;
         boolean error = false;
-        Getopt g = new Getopt("dnsoverhttps", args, "46fs");
+        String url = null;
+        Getopt g = new Getopt("dnsoverhttps", args, "46fsu:");
         try {
             int c;
             while ((c = g.getopt()) != -1) {
@@ -451,6 +467,10 @@ public class DNSOverHTTPS implements EepGet.StatusListener {
                     type = Type.V6_PREFERRED;
                     break;
 
+                case 'u':
+                    url = g.getOptarg();
+                    break;
+
                 case '?':
                 case ':':
                 default:
@@ -467,18 +487,19 @@ public class DNSOverHTTPS implements EepGet.StatusListener {
             System.exit(1);
         }
 
-        String url = args[g.getOptind()];
-        String result = (new DNSOverHTTPS(I2PAppContext.getGlobalContext())).lookup(url, type);
+        String hostname = args[g.getOptind()];
+        String result = (new DNSOverHTTPS(I2PAppContext.getGlobalContext())).lookup(hostname, type, url);
         if (result != null)
-            System.out.println(type + " lookup for " + url + " is " + result);
+            System.out.println(type + " lookup for " + hostname + " is " + result);
         else
-            System.err.println(type + " lookup failed for " + url);
+            System.err.println(type + " lookup failed for " + hostname);
     }
     
     private static void usage() {
         System.err.println("DNSOverHTTPS [-fs46] hostname\n" +
                            "             [-f] (IPv4 preferred) (default)\n" +
                            "             [-s] (IPv6 preferred)\n" +
+                           "             [-u 'https://host/dns-query?...&'] (request from this URL only)\n" +
                            "             [-4] (IPv4 only)\n" +
                            "             [-6] (IPv6 only)");
     }
diff --git a/core/java/src/net/i2p/util/SSLEepGet.java b/core/java/src/net/i2p/util/SSLEepGet.java
index 6aef33b567..e841e6f66b 100644
--- a/core/java/src/net/i2p/util/SSLEepGet.java
+++ b/core/java/src/net/i2p/util/SSLEepGet.java
@@ -263,8 +263,9 @@ public class SSLEepGet extends EepGet {
         String proxyHost = "127.0.0.1";
         int proxyPort = 0;
         ProxyType ptype = ProxyType.NONE;
+        boolean doh = false;
         boolean error = false;
-        Getopt g = new Getopt("ssleepget", args, "p:y:sz");
+        Getopt g = new Getopt("ssleepget", args, "dp:y:sz");
         try {
             int c;
             while ((c = g.getopt()) != -1) {
@@ -308,6 +309,10 @@ public class SSLEepGet extends EepGet {
                     noVerify = true;
                     break;
 
+                case 'd':
+                    doh = true;
+                    break;
+
                 case '?':
                 case ':':
                 default:
@@ -347,6 +352,8 @@ public class SSLEepGet extends EepGet {
             get._saveCerts = saveCerts;
         if (noVerify)
             get._bypassVerification = true;
+        if (doh)
+            get.forceDNSOverHTTPS(true);
         get._commandLine = true;
         get.addStatusListener(get.new CLIStatusListener(1024, 40));
         if(!get.fetch(45*1000, -1, 60*1000))
@@ -354,7 +361,8 @@ public class SSLEepGet extends EepGet {
     }
     
     private static void usage() {
-        System.err.println("Usage: SSLEepGet [-psyz] https://url\n" +
+        System.err.println("Usage: SSLEepGet [-dpsyz] https://url\n" +
+                           "  -d use DNSOverHTTPS\n" +
                            "  -p proxyHost[:proxyPort]    // default port 8080 for HTTPS and 1080 for SOCKS; default localhost:4444 for I2P\n" +
                            "  -y HTTPS|SOCKS4|SOCKS5|I2P  // proxy type, default HTTPS if proxyHost is set\n" +
                            "  -s save unknown certs\n" +
-- 
GitLab