[tor-commits] [flashproxy/master] Add abstractions for using IPv4/IPv6 reg pools.

dcf at torproject.org dcf at torproject.org
Sun Dec 23 09:11:59 UTC 2012


commit c3f883a72a5a9972cad507eb0479073a04dab6c3
Author: David Fifield <david at bamsoftware.com>
Date:   Wed Dec 19 19:20:36 2012 -0800

    Add abstractions for using IPv4/IPv6 reg pools.
    
    Still always going to the IPv4 pool.
---
 facilitator/facilitator |   45 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/facilitator/facilitator b/facilitator/facilitator
index c1cf89a..a0538d2 100755
--- a/facilitator/facilitator
+++ b/facilitator/facilitator
@@ -234,7 +234,24 @@ class Handler(SocketServer.StreamRequestHandler):
         print >> self.wfile, "ERROR"
 
     def do_GET(self, params):
-        reg = REGS_IPV4.fetch()
+        proxy_spec = fac.param_first("FROM", params)
+        if proxy_spec is None:
+            log(u"GET missing FROM param")
+            self.send_error()
+            return False
+        try:
+            proxy_addr = fac.parse_addr_spec(proxy_spec, defport=0)
+        except ValueError, e:
+            log(u"syntax error in proxy address %s: %s" % (safe_str(repr(proxy_spec)), safe_str(repr(str(e)))))
+            self.send_error()
+            return False
+
+        try:
+            reg = get_reg_for_proxy(proxy_addr)
+        except Exception, e:
+            log(u"error getting reg for proxy address %s: %s" % (safe_str(repr(proxy_spec)), safe_str(repr(str(e)))))
+            self.send_error()
+            return False
         if reg:
             log(u"proxy gets %s, relay %s (now %d)" %
                 (safe_str(unicode(reg)), options.relay_spec, num_regs()))
@@ -260,7 +277,13 @@ class Handler(SocketServer.StreamRequestHandler):
             self.send_error()
             return False
 
-        if REGS_IPV4.add(reg):
+        try:
+            ok = put_reg(reg)
+        except Exception, e:
+            log(u"error putting reg %s: %s" % (safe_str(repr(client_spec)), safe_str(repr(str(e)))))
+            self.send_error()
+            return False
+        if ok:
             log(u"client %s (now %d)" % (safe_str(unicode(reg)), num_regs()))
         else:
             log(u"client %s (already present, now %d)" % (safe_str(unicode(reg)), num_regs()))
@@ -281,6 +304,24 @@ def num_regs():
     """Return the total number of registrations."""
     return len(REGS_IPV4) + len(REGS_IPV6)
 
+def regs_for_af(af):
+    """Return the correct regs pool for the given address family."""
+    if af == socket.AF_INET:
+        return REGS_IPV4
+    elif af == socket.AF_INET6:
+        return REGS_IPV6
+    else:
+        raise ValueError("unknown address family %d" % af)
+
+def get_reg_for_proxy(proxy_addr):
+    """Get a client registration appropriate for the given proxy (one of a
+    matching address family)."""
+    return REGS_IPV4.fetch()
+
+def put_reg(reg):
+    """Add a registration."""
+    REGS_IPV4.add(reg)
+
 def main():
     opts, args = getopt.gnu_getopt(sys.argv[1:], "dhl:p:r:",
         ["debug", "help", "log=", "port=", "pidfile=", "relay=", "unsafe-logging"])





More information about the tor-commits mailing list