[tor-commits] [onionoo/release] Allow for URLs that specify list of fingerprints.

karsten at torproject.org karsten at torproject.org
Fri Aug 3 14:35:55 UTC 2018


commit c9218fbd3b8f029857cca1cde4c16dc5099f129d
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Tue Jul 24 09:11:18 2018 +0200

    Allow for URLs that specify list of fingerprints.
    
    Implements #25448.
---
 CHANGELOG.md                                       |  8 ++++++
 .../torproject/onionoo/server/RequestHandler.java  | 25 +++++++++--------
 .../torproject/onionoo/server/ResourceServlet.java | 31 +++++++++-------------
 .../onionoo/server/ResourceServletTest.java        | 14 ++++++++++
 4 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index cd11502..00a0c6a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+# Changes in version 6.2-1.16.0 - 2018-07-??
+
+ * Medium changes
+   - Support a comma-separated list of fingerprints in the lookup
+     parameter to allow for URLs that specify a list of relays or
+     bridges.
+
+
 # Changes in version 6.1-1.15.0 - 2018-07-16
 
  * Medium changes
diff --git a/src/main/java/org/torproject/onionoo/server/RequestHandler.java b/src/main/java/org/torproject/onionoo/server/RequestHandler.java
index b9d9b23..344178b 100644
--- a/src/main/java/org/torproject/onionoo/server/RequestHandler.java
+++ b/src/main/java/org/torproject/onionoo/server/RequestHandler.java
@@ -54,9 +54,9 @@ public class RequestHandler {
     System.arraycopy(search, 0, this.search, 0, search.length);
   }
 
-  private String lookup;
+  private String[] lookup;
 
-  public void setLookup(String lookup) {
+  public void setLookup(String[] lookup) {
     this.lookup = lookup;
   }
 
@@ -340,17 +340,20 @@ public class RequestHandler {
       /* Not filtering by looking up relay or bridge. */
       return;
     }
-    String fingerprint = this.lookup;
-    SummaryDocument relayLine = this.filteredRelays.get(fingerprint);
-    this.filteredRelays.clear();
-    if (relayLine != null) {
-      this.filteredRelays.put(fingerprint, relayLine);
+    Map<String, SummaryDocument> matchingRelays = new HashMap<>();
+    for (String fingerprint : this.lookup) {
+      if (this.filteredRelays.containsKey(fingerprint)) {
+        matchingRelays.put(fingerprint, this.filteredRelays.get(fingerprint));
+      }
     }
-    SummaryDocument bridgeLine = this.filteredBridges.get(fingerprint);
-    this.filteredBridges.clear();
-    if (bridgeLine != null) {
-      this.filteredBridges.put(fingerprint, bridgeLine);
+    this.filteredRelays = matchingRelays;
+    Map<String, SummaryDocument> matchingBridges = new HashMap<>();
+    for (String fingerprint : this.lookup) {
+      if (this.filteredBridges.containsKey(fingerprint)) {
+        matchingBridges.put(fingerprint, this.filteredBridges.get(fingerprint));
+      }
     }
+    this.filteredBridges = matchingBridges;
   }
 
   private void filterByFingerprint() {
diff --git a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
index e119518..534dc0c 100644
--- a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
+++ b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
@@ -211,24 +211,22 @@ public class ResourceServlet extends HttpServlet {
       rh.setRunning(runningRequested ? "true" : "false");
     }
     if (parameterMap.containsKey("lookup")) {
-      String lookupParameter = this.parseFingerprintParameter(
+      String[] lookupParameter = this.parseFingerprintParameter(
           parameterMap.get("lookup"));
       if (lookupParameter == null) {
         response.sendError(HttpServletResponse.SC_BAD_REQUEST);
         return;
       }
-      String fingerprint = lookupParameter.toUpperCase();
-      rh.setLookup(fingerprint);
+      rh.setLookup(lookupParameter);
     }
     if (parameterMap.containsKey("fingerprint")) {
-      String fingerprintParameter = this.parseFingerprintParameter(
+      String[] fingerprintParameter = this.parseFingerprintParameter(
           parameterMap.get("fingerprint"));
-      if (fingerprintParameter == null) {
+      if (null == fingerprintParameter || 1 != fingerprintParameter.length) {
         response.sendError(HttpServletResponse.SC_BAD_REQUEST);
         return;
       }
-      String fingerprint = fingerprintParameter.toUpperCase();
-      rh.setFingerprint(fingerprint);
+      rh.setFingerprint(fingerprintParameter[0]);
     }
     if (parameterMap.containsKey("country")) {
       String countryCodeParameter = this.parseCountryCodeParameter(
@@ -360,14 +358,13 @@ public class ResourceServlet extends HttpServlet {
       rh.setLimit(limitParameter);
     }
     if (parameterMap.containsKey("family")) {
-      String familyParameter = this.parseFingerprintParameter(
+      String[] familyParameter = this.parseFingerprintParameter(
           parameterMap.get("family"));
-      if (familyParameter == null) {
-        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
+      if (null == familyParameter || 1 != familyParameter.length) {
+          response.sendError(HttpServletResponse.SC_BAD_REQUEST);
         return;
       }
-      String family = familyParameter.toUpperCase();
-      rh.setFamily(family);
+      rh.setFamily(familyParameter[0]);
     }
     rh.handleRequest();
     long parsedRequestMillis = System.currentTimeMillis();
@@ -473,18 +470,14 @@ public class ResourceServlet extends HttpServlet {
   }
 
   private static Pattern fingerprintParameterPattern =
-      Pattern.compile("^[0-9a-zA-Z]{1,40}$");
+      Pattern.compile("((^|,)[0-9a-zA-Z]{40})+$");
 
-  private String parseFingerprintParameter(String parameter) {
+  private String[] parseFingerprintParameter(String parameter) {
     if (!fingerprintParameterPattern.matcher(parameter).matches()) {
       /* Fingerprint contains non-hex character(s). */
       return null;
     }
-    if (parameter.length() != 40) {
-      /* Only full fingerprints are accepted. */
-      return null;
-    }
-    return parameter;
+    return parameter.toUpperCase().split(",");
   }
 
   private static Pattern countryCodeParameterPattern =
diff --git a/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java b/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java
index 2bc8203..8100c00 100644
--- a/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java
+++ b/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java
@@ -1042,6 +1042,20 @@ public class ResourceServletTest {
   }
 
   @Test(timeout = 100)
+  public void testLookupTwoFingerprints() {
+    this.assertSummaryDocument(
+        "/summary?lookup=000C5F55BD4814B917CC474BD537F1A3B33CCE2A,"
+        + "1FEDE50ED8DBA1DD9F9165F78C8131E4A44AB756", 1,
+        new String[] { "TorkaZ" }, 1,   new String[] { "gummy" });
+  }
+
+  @Test(timeout = 100)
+  public void testLookupFingerprintTrailingComma() {
+    this.assertErrorStatusCode(
+        "/summary?lookup=000C5F55BD4814B917CC474BD537F1A3B33CCE2A,", 400);
+  }
+
+  @Test(timeout = 100)
   public void testFingerprintRelayFingerprint() {
     this.assertSummaryDocument(
         "/summary?fingerprint=000C5F55BD4814B917CC474BD537F1A3B33CCE2A",





More information about the tor-commits mailing list