[tor-commits] [bridgedb/develop] Implement `client` and `getHashringPlacement()` interfaces.

isis at torproject.org isis at torproject.org
Thu Feb 19 02:21:13 UTC 2015


commit eb28b43d69ea3d91b8ccab6fbc7569817fc25ebb
Author: Isis Lovecruft <isis at torproject.org>
Date:   Fri Dec 5 23:57:36 2014 +0000

    Implement `client` and `getHashringPlacement()` interfaces.
    
     * ADD base class implementations of IRequestBridges.client and
       IRequestBridges.getHashringPlacement() to BridgeRequestBase class.
---
 lib/bridgedb/bridgerequest.py |   27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lib/bridgedb/bridgerequest.py b/lib/bridgedb/bridgerequest.py
index 840b2fc..d7b4d18 100644
--- a/lib/bridgedb/bridgerequest.py
+++ b/lib/bridgedb/bridgerequest.py
@@ -20,6 +20,7 @@ from zope.interface import Attribute
 from zope.interface import Interface
 
 from bridgedb import Filters
+from bridgedb.crypto import getHMACFunc
 
 
 class IRequestBridges(Interface):
@@ -93,8 +94,34 @@ class BridgeRequestBase(object):
         self.filters = list()
         self.transports = list()
         self.notBlockedIn = list()
+        #: This should be some information unique to the client making the
+        #: request for bridges, such that we are able to HMAC this unique data
+        #: in order to place the client into a hashring (determining which
+        #: bridge addresses they get in the request response). It defaults to
+        #: the string ``'default'``.
+        self.client = 'default'
         self.valid = False
 
+    def getHashringPlacement(self, key, client=None):
+        """Create an HMAC of some **client** info using a **key**.
+
+        :param str key: The key to use for HMACing.
+        :param str client: Some (hopefully unique) information about the
+            client who is requesting bridges, such as an IP or email address.
+        :rtype: long
+        :returns: A long specifying index of the first node in a hashring to
+            be distributed to the client. This value should obviously be used
+            mod the number of nodes in the hashring.
+        """
+        if not client:
+            client = self.client
+
+        # Get an HMAC with the key of the client identifier:
+        digest = getHMACFunc(key)(client)
+        # Take the lower 8 bytes of the digest and convert to a long:
+        position = long(digest[:8], 16)
+        return position
+
     def isValid(self, valid=None):
         """Set or determine if this request was valid.
 





More information about the tor-commits mailing list