[tor-commits] [bridgedb/master] 5947 - Parse "a" lines from networkstatus

aagbsn at torproject.org aagbsn at torproject.org
Sat Mar 16 23:46:31 UTC 2013


commit 677272c11ea1a4b86fb143a12a76d03a6384b032
Author: aagbsn <aagbsn at extc.org>
Date:   Wed May 30 21:03:47 2012 -0700

    5947 - Parse "a" lines from networkstatus
    
    The "a" lines have been tested by the Bridge Authority.
    The or-addresses in the bridge descriptors have not.
    
    This fix makes BridgeDB read or-addresses from the networkstatus
    instead of the bridge descriptors
---
 lib/bridgedb/Bridges.py |   46 +++++++++++++++++++++-------------------------
 lib/bridgedb/Main.py    |    9 ++++++---
 2 files changed, 27 insertions(+), 28 deletions(-)

diff --git a/lib/bridgedb/Bridges.py b/lib/bridgedb/Bridges.py
index 1e15157..9af5dfb 100644
--- a/lib/bridgedb/Bridges.py
+++ b/lib/bridgedb/Bridges.py
@@ -268,37 +268,14 @@ def parseDescFile(f, bridge_purpose='bridge'):
                 orport = int(items[3])
         elif line.startswith("fingerprint "):
             fingerprint = line[12:].replace(" ", "")
-        elif line.startswith("or-address "):
-            if num_or_address_lines < 8:
-                line = line[11:]
-                try:
-                    address,portlist = parseORAddressLine(line)
-                except ParseORAddressError: 
-                    logging.warn("Invalid or-address line "\
-                            "from bridge with ID %r" %fingerprint)
-                    continue
-                try:
-                    # distinct ports only
-                    portlist.add(or_addresses[address])
-                except KeyError:
-                    pass
-                finally:
-                    or_addresses[address] = portlist
-            else:
-                logging.warn("Skipping extra or-address line "\
-                             "from Bridge with ID %r" % id)
-            num_or_address_lines += 1
         elif line.startswith("router-signature"):
             purposeMatches = (purpose == bridge_purpose or
                               bridge_purpose is None)
             if purposeMatches and nickname and ip and orport and fingerprint:
-                b = Bridge(nickname, ip, orport, fingerprint,
-                           or_addresses=or_addresses)
+                b = Bridge(nickname, ip, orport, fingerprint)
                 b.assertOK()
                 yield b
             nickname = ip = orport = fingerprint = purpose = None 
-            num_or_address_lines = 0
-            or_addresses = {}
 
 class PortList:
     """ container class for port ranges
@@ -374,6 +351,8 @@ def parseORAddressLine(line):
 def parseStatusFile(f):
     """DOCDOC"""
     ID = None
+    num_or_address_lines = 0
+    or_addresses = {}
     for line in f:
         line = line.strip()
         if line.startswith("opt "):
@@ -384,9 +363,26 @@ def parseStatusFile(f):
                 ID = binascii.a2b_base64(line.split()[2]+"=")
             except binascii.Error:
                 logging.warn("Unparseable base64 ID %r", line.split()[2])
+
+        elif ID and line.startswith("a "):
+            if num_or_address_lines < 8:
+                line = line[2:]
+                address,portlist = parseORAddressLine(line)
+                try:
+                    or_addresses[address].add(portlist)
+                except KeyError:
+                    or_addresses[address] = portlist
+            else:
+                logging.warn("Skipping extra or-address line "\
+                             "from Bridge with ID %r" % id)
+            num_or_address_lines += 1
+
         elif ID and line.startswith("s "):
             flags = line.split()
-            yield ID, ("Running" in flags), ("Stable" in flags)
+            yield ID, ("Running" in flags), ("Stable" in flags), or_addresses
+            ID = None
+            num_or_address_lines = 0
+            or_addresses = {}
 
 def parseCountryBlockFile(f):
     """Generator. Parses a blocked-bridges file 'f', and yields a
diff --git a/lib/bridgedb/Main.py b/lib/bridgedb/Main.py
index fe34cb0..befb4ed 100644
--- a/lib/bridgedb/Main.py
+++ b/lib/bridgedb/Main.py
@@ -166,10 +166,12 @@ def load(cfg, splitter, clear=False):
         countryblock.clear() 
     logging.info("Loading bridges")
     status = {}
+    addresses = {}
     if hasattr(cfg, "STATUS_FILE"):
         f = open(cfg.STATUS_FILE, 'r')
-        for ID, running, stable in Bridges.parseStatusFile(f):
+        for ID, running, stable, or_addresses in Bridges.parseStatusFile(f):
             status[ID] = running, stable
+            addresses[ID] = or_addresses
         f.close()
     if hasattr(cfg, "COUNTRY_BLOCK_FILE"):
         f = open(cfg.COUNTRY_BLOCK_FILE, 'r')
@@ -183,6 +185,7 @@ def load(cfg, splitter, clear=False):
             if s is not None:
                 running, stable = s
                 bridge.setStatus(running=running, stable=stable)
+            bridge.or_addresses = addresses.get(bridge.getID())
             bridge.setBlockingCountries(
                     countryblock.getBlockingCountries(bridge.fingerprint)) 
             splitter.insert(bridge)
@@ -324,8 +327,8 @@ def startup(cfg):
             logging.info("%d for email", len(emailDistributor.splitter))
         if ipDistributor:
             logging.info("%d for web:", len(ipDistributor.splitter))
-	    for (n,(f,r)) in ipDistributor.splitter.filterRings.items():
-                logging.info(" by filter set %s, %d" % (n, len(r)))
+            for (n,(f,r)) in ipDistributor.splitter.filterRings.items():
+                    logging.info(" by filter set %s, %d" % (n, len(r)))
             #logging.info("  by location set: %s",
             #             " ".join(str(len(r)) for r in ipDistributor.rings))
             #logging.info("  by category set: %s",





More information about the tor-commits mailing list