[tor-commits] [onionoo/master] Fix workaround for fingerprint parameter.

karsten at torproject.org karsten at torproject.org
Mon Jul 21 10:09:04 UTC 2014


commit 0028a1407b8e33f691defcfca95613d75f3a6780
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Mon Jul 21 11:48:11 2014 +0200

    Fix workaround for fingerprint parameter.
    
    This workaround broke when we switched from formatting details documents
    manually to using Gson for that.
---
 src/org/torproject/onionoo/DocumentStore.java |  104 ++++++++-----------------
 1 file changed, 32 insertions(+), 72 deletions(-)

diff --git a/src/org/torproject/onionoo/DocumentStore.java b/src/org/torproject/onionoo/DocumentStore.java
index e8f8878..e164b0d 100644
--- a/src/org/torproject/onionoo/DocumentStore.java
+++ b/src/org/torproject/onionoo/DocumentStore.java
@@ -13,7 +13,6 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Map;
-import java.util.Scanner;
 import java.util.SortedMap;
 import java.util.SortedSet;
 import java.util.Stack;
@@ -272,83 +271,44 @@ public class DocumentStore {
      * 500,000 NodeStatus instances into memory.  Maybe there's a better
      * way?  Or do we need to switch to a real database for this? */
     DetailsDocument detailsDocument = this.retrieveDocumentFile(
-        DetailsDocument.class, false, fingerprint);
+        DetailsDocument.class, true, fingerprint);
     if (detailsDocument == null) {
       return null;
     }
-    try {
-      boolean isRelay = true, running = false;
-      String nickname = null, address = null, countryCode = null,
-          hostName = null, defaultPolicy = null, portList = null,
-          aSNumber = null, contact = null;
-      SortedSet<String> orAddressesAndPorts = new TreeSet<String>(),
-          exitAddresses = new TreeSet<String>(),
-          relayFlags = new TreeSet<String>(), family = null;
-      long lastSeenMillis = -1L, consensusWeight = -1L,
-          lastRdnsLookup = -1L, firstSeenMillis = -1L,
-          lastChangedAddresses = -1L;
-      int orPort = 0, dirPort = 0;
-      Boolean recommendedVersion = null;
-      Scanner s = new Scanner(detailsDocument.getDocumentString());
-      while (s.hasNextLine()) {
-        String line = s.nextLine();
-        if (!line.contains(":")) {
-          continue;
-        }
-        String[] parts = line.split(":", 2);
-        String key = parts[0], value = parts[1];
-        if (key.equals("\"nickname\"")) {
-          if (!value.startsWith("\"") || !value.endsWith("\",")) {
-            return null;
-          }
-          nickname = value.substring(1, value.length() - 2);
-        } else if (key.equals("\"hashed_fingerprint\"")) {
-          isRelay = false;
-        } else if (key.equals("\"or_addresses\"")) {
-          if (!value.startsWith("[") || !value.endsWith("],")) {
-            return null;
-          }
-          for (String addressAndPort :
-              value.substring(1, value.length() - 2).split(",")) {
-            if (addressAndPort.length() < 2 ||
-                !addressAndPort.contains(":")) {
-              return null;
-            }
-            if (address == null) {
-              address = addressAndPort.substring(1,
-                  addressAndPort.lastIndexOf(":"));
-            } else {
-              orAddressesAndPorts.add(addressAndPort);
-            }
-          }
-        } else if (key.equals("\"exit_addresses\"")) {
-          if (!value.startsWith("[") || !value.endsWith("],")) {
-            return null;
-          }
-          for (String addressPart :
-              value.substring(1, value.length() - 2).split(",")) {
-            exitAddresses.add(addressPart);
-          }
-        } else if (key.equals("\"running\"")) {
-          if (value.equals("true,")) {
-            running = true;
-          } else if (!value.equals("false,")) {
-            return null;
-          }
+    boolean isRelay = detailsDocument.getHashedFingerprint() == null;
+    String nickname = detailsDocument.getNickname();
+    String address = null, countryCode = null, hostName = null,
+        defaultPolicy = null, portList = null, aSNumber = null,
+        contact = null;
+    SortedSet<String> orAddressesAndPorts = new TreeSet<String>();
+    for (String orAddressAndPort : detailsDocument.getOrAddresses()) {
+      if (address == null) {
+        if (!orAddressAndPort.contains(":")) {
+          return null;
         }
+        address = orAddressAndPort.substring(0,
+            orAddressAndPort.lastIndexOf(":"));
+      } else {
+        orAddressesAndPorts.add(orAddressAndPort);
       }
-      NodeStatus nodeStatus = new NodeStatus(isRelay, nickname,
-          fingerprint, address, orAddressesAndPorts, exitAddresses,
-          lastSeenMillis, orPort, dirPort, relayFlags, consensusWeight,
-          countryCode, hostName, lastRdnsLookup, defaultPolicy, portList,
-          firstSeenMillis, lastChangedAddresses, aSNumber, contact,
-          recommendedVersion, family);
-      nodeStatus.setRunning(running);
-      return nodeStatus;
-    } catch (Exception e) {
-      /* Play it safe and fall back to returning nothing. */
-      return null;
     }
+    SortedSet<String> exitAddresses = new TreeSet<String>();
+    if (detailsDocument.getExitAddresses() != null) {
+      exitAddresses.addAll(detailsDocument.getExitAddresses());
+    }
+    SortedSet<String> relayFlags = new TreeSet<String>(), family = null;
+    long lastSeenMillis = -1L, consensusWeight = -1L,
+        lastRdnsLookup = -1L, firstSeenMillis = -1L,
+        lastChangedAddresses = -1L;
+    int orPort = 0, dirPort = 0;
+    Boolean recommendedVersion = null;
+    NodeStatus nodeStatus = new NodeStatus(isRelay, nickname, fingerprint,
+        address, orAddressesAndPorts, exitAddresses, lastSeenMillis,
+        orPort, dirPort, relayFlags, consensusWeight, countryCode,
+        hostName, lastRdnsLookup, defaultPolicy, portList,
+        firstSeenMillis, lastChangedAddresses, aSNumber, contact,
+        recommendedVersion, family);
+    return nodeStatus;
   }
 
   private <T extends Document> T retrieveDocumentFile(



More information about the tor-commits mailing list