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

[tor-commits] [bridgedb/master] 4297 - Move filter functions to Filters.py



commit 1bcaf8328e5285b3648f9714d0cd15d3ba183d32
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Thu May 24 16:20:54 2012 -0700

    4297 - Move filter functions to Filters.py
---
 lib/bridgedb/Dist.py    |   30 ++++-------------------
 lib/bridgedb/Filters.py |   59 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/bridgedb/Server.py  |   25 ++-----------------
 lib/bridgedb/Tests.py   |   45 ++++-------------------------------
 4 files changed, 73 insertions(+), 86 deletions(-)

diff --git a/lib/bridgedb/Dist.py b/lib/bridgedb/Dist.py
index d393ee2..c151bbc 100644
--- a/lib/bridgedb/Dist.py
+++ b/lib/bridgedb/Dist.py
@@ -14,6 +14,9 @@ import re
 import time
 from ipaddr import IPv6Address, IPAddress
 
+from bridgedb.Filters import filterAssignBridgesToRing
+from bridgedb.Filters import filterBridgesByRules
+
 def uniformMap(ip):
     """Map an IP to an arbitrary 'area' string, such that any two /24 addresses
        get the same string.
@@ -54,24 +57,15 @@ class IPBasedDistributor(bridgedb.Bridges.BridgeHolder):
         key4 = bridgedb.Bridges.get_hmac(key, "Assign-Areas-To-Rings")
         self.areaClusterHmac = bridgedb.Bridges.get_hmac_fn(key4, hex=True)
 
-    # add splitter and cache the default rings
-    # plus leave room for dynamic filters
+        # add splitter and cache the default rings
+        # plus leave room for dynamic filters
         ring_cache_size  = nClusters + len(ipCategories) + 5
         self.splitter = bridgedb.Bridges.FilteredBridgeSplitter(key2,
                                                max_cached_rings=ring_cache_size)
 
         logging.debug("added splitter %s" % self.splitter)
 
-    # assign bridges using a filter function
-        def filterAssignBridgesToRing(hmac, numRings, assignedRing):
-            def f(bridge):
-                digest = hmac(bridge.getID())
-                pos = long( digest[:8], 16 )
-                which = pos % numRings
-                if which == assignedRing: return True
-                return False
-            return f
-
+        # assign bridges using a filter function
         for n in xrange(nClusters):
             key1 = bridgedb.Bridges.get_hmac(key, "Order-Bridges-In-Ring-%d"%n)
             ring = bridgedb.Bridges.BridgeRing(key1, answerParameters)
@@ -139,12 +133,6 @@ class IPBasedDistributor(bridgedb.Bridges.BridgeHolder):
                 _,ring = self.splitter.filterRings[ruleset]
             else:
                 logging.debug("Cache miss %s" % ruleset)
-                def filterBridgesByRules(rules):
-                    def g(x):
-                        r = [f(x) for f in rules]
-                        if False in r: return False
-                        return True
-                    return g
                 # add new ring 
                 #XXX what key do we use here? does it matter? 
                 key1 = bridgedb.Bridges.get_hmac(self.splitter.key, str(bridgeFilterRules))
@@ -365,12 +353,6 @@ class EmailBasedDistributor(bridgedb.Bridges.BridgeHolder):
             else:
                 # cache miss, add new ring
                 logging.debug("Cache miss %s" % ruleset)
-                def filterBridgesByRules(rules):
-                    def g(x):
-                        r = [f(x) for f in rules]
-                        if False in r: return False
-                        return True
-                    return g
                 # add new ring 
                 #XXX what key do we use here? does it matter? 
                 key1 = bridgedb.Bridges.get_hmac(self.splitter.key,
diff --git a/lib/bridgedb/Filters.py b/lib/bridgedb/Filters.py
new file mode 100644
index 0000000..72e9004
--- /dev/null
+++ b/lib/bridgedb/Filters.py
@@ -0,0 +1,59 @@
+# BridgeDB by Nick Mathewson.
+# Copyright (c) 2007-2012, The Tor Project, Inc.
+# See LICENSE for licensing information 
+
+from ipaddr import IPv6Address, IPv4Address
+
+def filterAssignBridgesToRing(hmac, numRings, assignedRing):
+    def f(bridge):
+        digest = hmac(bridge.getID())
+        pos = long( digest[:8], 16 )
+        which = pos % numRings
+        if which == assignedRing: return True
+        return False
+    return f
+
+def filterBridgesByRules(rules):
+    def g(x):
+        r = [f(x) for f in rules]
+        if False in r: return False
+        return True
+    return g
+
+def filterBridgesByIP4(bridge):
+    try:
+        if IPv4Address(bridge.ip): return True
+    except ValueError:
+        pass
+
+    for k in bridge.or_addresses.keys():
+        if type(k) is IPv4Address:
+            return True
+    return False
+
+def filterBridgesByIP6(bridge):
+    try:
+        if IPv6Address(bridge.ip): return True
+    except ValueError:
+        pass
+
+    for k in bridge.or_addresses.keys():
+        if type(k) is IPv6Address:
+            return True
+    return False
+
+def filterBridgesByOnlyIP4(bridge):
+    for k in bridge.or_addresses.keys():
+        if type(k) is IPv6Address:
+            return False
+    if type(k) is IPv4Address:
+        return True
+    return False
+
+def filterBridgesByOnlyIP6(bridge):
+    for k in bridge.or_addresses.keys():
+        if type(k) is IPv4Address:
+            return False
+    if type(k) is IPv6Address:
+        return True
+    return False
diff --git a/lib/bridgedb/Server.py b/lib/bridgedb/Server.py
index a2460b8..2de1289 100644
--- a/lib/bridgedb/Server.py
+++ b/lib/bridgedb/Server.py
@@ -33,6 +33,9 @@ import base64
 import textwrap
 from ipaddr import IPv4Address, IPv6Address
 from bridgedb.Dist import BadEmail, TooSoonEmail, IgnoreEmail
+
+from bridgedb.Filters import filterBridgesByIP6
+from bridgedb.Filters import filterBridgesByIP4
  
 try:
     import GeoIP
@@ -676,25 +679,3 @@ def getCCFromRequest(request):
     if len(path) ==  2:
         return path.lower()
     return None 
-
-def filterBridgesByIP4(bridge):
-    try:
-        if IPv4Address(bridge.ip): return True
-    except ValueError:
-        pass
-
-    for k in bridge.or_addresses.keys():
-        if type(k) is IPv4Address:
-            return True
-    return False
-
-def filterBridgesByIP6(bridge):
-    try:
-        if IPv6Address(bridge.ip): return True
-    except ValueError:
-        pass
-
-    for k in bridge.or_addresses.keys():
-        if type(k) is IPv6Address:
-            return True
-    return False
diff --git a/lib/bridgedb/Tests.py b/lib/bridgedb/Tests.py
index ead36fc..d989f7d 100644
--- a/lib/bridgedb/Tests.py
+++ b/lib/bridgedb/Tests.py
@@ -19,6 +19,11 @@ import bridgedb.Storage
 import re
 import ipaddr
 
+from bridgedb.Filters import filterBridgesByIP4
+from bridgedb.Filters import filterBridgesByIP6
+from bridgedb.Filters import filterBridgesByOnlyIP4
+from bridgedb.Filters import filterBridgesByOnlyIP6
+
 def suppressWarnings():
     warnings.filterwarnings('ignore', '.*tmpnam.*')
 
@@ -201,12 +206,6 @@ class IPBridgeDistTests(unittest.TestCase):
             d.insert(fakeBridge6(or_addresses=True))
             d.insert(fakeBridge(or_addresses=True))
 
-        def filterBridgesByIP6(bridge):
-            for k in bridge.or_addresses.keys():
-                if type(k) is ipaddr.IPv6Address:
-                    return True
-            return False
-
         for i in xrange(500):
             b = d.getBridgesForIP(randomIP(), "x", 1, bridgeFilterRules=[filterBridgesByIP6])
             assert filterBridgesByIP6(random.choice(b))
@@ -217,12 +216,6 @@ class IPBridgeDistTests(unittest.TestCase):
             d.insert(fakeBridge6(or_addresses=True))
             d.insert(fakeBridge(or_addresses=True))
 
-        def filterBridgesByIP4(bridge):
-            for k in bridge.or_addresses.keys():
-                if type(k) is ipaddr.IPv4Address:
-                    return True
-            return False
-
         for i in xrange(500):
             b = d.getBridgesForIP(randomIP(), "x", 1, bridgeFilterRules=[filterBridgesByIP4])
             assert filterBridgesByIP4(random.choice(b))
@@ -233,18 +226,6 @@ class IPBridgeDistTests(unittest.TestCase):
             d.insert(fakeBridge6(or_addresses=True))
             d.insert(fakeBridge(or_addresses=True))
 
-        def filterBridgesByIP4(bridge):
-            for k in bridge.or_addresses.keys():
-                if type(k) is ipaddr.IPv4Address:
-                    return True
-            return False
-
-        def filterBridgesByIP6(bridge):
-            for k in bridge.or_addresses.keys():
-                if type(k) is ipaddr.IPv6Address:
-                    return True
-            return False
-
         for i in xrange(50):
             b = d.getBridgesForIP(randomIP(), "x", 1, bridgeFilterRules=[
                 filterBridgesByIP4, filterBridgesByIP6])
@@ -259,22 +240,6 @@ class IPBridgeDistTests(unittest.TestCase):
             d.insert(fakeBridge6(or_addresses=True))
             d.insert(fakeBridge(or_addresses=True))
 
-        def filterBridgesByOnlyIP4(bridge):
-            for k in bridge.or_addresses.keys():
-                if type(k) is ipaddr.IPv6Address:
-                    return False
-            if type(k) is ipaddr.IPv4Address:
-                return True
-            return False
-
-        def filterBridgesByOnlyIP6(bridge):
-            for k in bridge.or_addresses.keys():
-                if type(k) is ipaddr.IPv4Address:
-                    return False
-            if type(k) is ipaddr.IPv6Address:
-                return True
-            return False
-
         for i in xrange(5):
             b = d.getBridgesForIP(randomIP(), "x", 1, bridgeFilterRules=[
                 filterBridgesByOnlyIP4, filterBridgesByOnlyIP6])



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