[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/master] 2678 - filter bridges in same /16 from response



commit 75b386c8d2df617359eb21d7c2e3e147be6df363
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Mon Jul 25 16:27:00 2011 -0700

    2678 - filter bridges in same /16 from response
---
 lib/bridgedb/Bridges.py |   14 +++++++++++++-
 lib/bridgedb/Dist.py    |    6 +++---
 lib/bridgedb/Server.py  |    2 +-
 lib/bridgedb/Storage.py |    4 ++--
 lib/bridgedb/Tests.py   |   25 +++++++++++++++++++++++++
 5 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/lib/bridgedb/Bridges.py b/lib/bridgedb/Bridges.py
index eca6db5..229cc5a 100644
--- a/lib/bridgedb/Bridges.py
+++ b/lib/bridgedb/Bridges.py
@@ -340,7 +340,19 @@ class BridgeRing(BridgeHolder):
                 keys.append(k)
         keys = keys[:N]
         keys.sort()
-        return [ self.bridges[k] for k in keys ]
+
+        #Do not return bridges from the same /16
+        bridges = [ self.bridges[k] for k in keys ]
+        filteredbridges = []
+        slash16s = dict()
+
+        for bridge in bridges:
+            m = re.match(r'(\d+\.\d+)\.\d+\.\d+', bridge.ip)
+            upper16 = m.group(1)
+            if upper16 not in slash16s:
+                filteredbridges.append(bridge)
+                slash16s[upper16] = True
+        return filteredbridges 
 
     def getBridgeByID(self, fp):
         """Return the bridge whose identity digest is fp, or None if no such
diff --git a/lib/bridgedb/Dist.py b/lib/bridgedb/Dist.py
index e11d21e..f2274c7 100644
--- a/lib/bridgedb/Dist.py
+++ b/lib/bridgedb/Dist.py
@@ -249,10 +249,10 @@ class EmailBasedDistributor(bridgedb.Bridges.BridgeHolder):
         """
         now = time.time()
         try:
-          emailaddress = normalizeEmail(emailaddress, self.domainmap,
-                                      self.domainrules)
+            emailaddress = normalizeEmail(emailaddress, self.domainmap,
+                                          self.domainrules)
         except BadEmail:
-          return [] #XXXX log the exception
+            return [] #XXXX log the exception
         if emailaddress is None:
             return [] #XXXX raise an exception.
 
diff --git a/lib/bridgedb/Server.py b/lib/bridgedb/Server.py
index 20b349b..e2e0222 100644
--- a/lib/bridgedb/Server.py
+++ b/lib/bridgedb/Server.py
@@ -219,7 +219,7 @@ def getMailResponse(lines, ctx):
     try:
         _, addrdomain = bridgedb.Dist.extractAddrSpec(clientAddr.lower())
     except bridgedb.Dist.BadEmail:
-	logging.info("Ignoring bad address on incoming email.")
+        logging.info("Ignoring bad address on incoming email.")
         return None,None
     if not addrdomain:
         logging.info("Couldn't parse domain from %r", clientAddr)
diff --git a/lib/bridgedb/Storage.py b/lib/bridgedb/Storage.py
index 62ed71c..30b4c57 100644
--- a/lib/bridgedb/Storage.py
+++ b/lib/bridgedb/Storage.py
@@ -76,7 +76,7 @@ class SqliteDict:
         self._cursor.execute(self._getStmt, (k,))
         val = self._cursor.fetchone()
         if val == None:
-            return v;
+            return v
         else:
             return val[0]
     def setdefault(self, k, v):
@@ -216,7 +216,7 @@ class Database:
         cur = self._cur
         t = timeToStr(expireBefore)
 
-        cur.execute("DELETE FROM EmailedBridges WHERE when_mailed < ?", (t,));
+        cur.execute("DELETE FROM EmailedBridges WHERE when_mailed < ?", (t,))
 
     def getEmailTime(self, addr):
         cur = self._cur
diff --git a/lib/bridgedb/Tests.py b/lib/bridgedb/Tests.py
index 1353658..9dea4b4 100644
--- a/lib/bridgedb/Tests.py
+++ b/lib/bridgedb/Tests.py
@@ -16,6 +16,7 @@ import bridgedb.Main
 import bridgedb.Dist
 import bridgedb.Time
 import bridgedb.Storage
+import re
 
 def suppressWarnings():
     warnings.filterwarnings('ignore', '.*tmpnam.*')
@@ -23,12 +24,23 @@ def suppressWarnings():
 def randomIP():
     return ".".join([str(random.randrange(1,256)) for _ in xrange(4)])
 
+def random16IP():
+    upper = "123.123." # same 16
+    lower = ".".join([str(random.randrange(1,256)) for _ in xrange(2)]) 
+    return upper+lower
+
 def fakeBridge(orport=8080):
     nn = "bridge-%s"%random.randrange(0,1000000)
     ip = randomIP()
     fp = "".join([random.choice("0123456789ABCDEF") for _ in xrange(40)])
     return bridgedb.Bridges.Bridge(nn,ip,orport,fingerprint=fp)
 
+def fake16Bridge(orport=8080):
+    nn = "bridge-%s"%random.randrange(0,1000000)
+    ip = random16IP()
+    fp = "".join([random.choice("0123456789ABCDEF") for _ in xrange(40)])
+    return bridgedb.Bridges.Bridge(nn,ip,orport,fingerprint=fp)  
+
 class RhymesWith255Category:
     def contains(self, ip):
         return ip.endswith(".255")
@@ -83,6 +95,19 @@ class IPBridgeDistTests(unittest.TestCase):
             self.assertEquals(len(fps), 5)
             self.assertTrue(count >= 1)
 
+    def testDistWithFilter16(self):
+        d = bridgedb.Dist.IPBasedDistributor(self.dumbAreaMapper, 3, "Foo")
+        for _ in xrange(256):
+            d.insert(fake16Bridge())
+        n = d.getBridgesForIP("1.2.3.4", "x", 10)
+
+        slash16s = dict()
+        for bridge in n:
+            m = re.match(r'(\d+\.\d+)\.\d+\.\d+', bridge.ip)
+            upper16 = m.group(1)
+            self.assertTrue(upper16 not in slash16s)
+            slash16s[upper16] = True 
+
 class DictStorageTests(unittest.TestCase):
     def setUp(self):
         self.fd, self.fname = tempfile.mkstemp()



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits