[tor-commits] [flashproxy/master] Implement GET in facilitator.cgi.

dcf at torproject.org dcf at torproject.org
Fri Aug 31 11:39:36 UTC 2012


commit 1fb78b99e0a63247d2ef3177ac973528206cefd3
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Jul 28 09:46:13 2012 -0700

    Implement GET in facilitator.cgi.
---
 facilitator.cgi |   50 +++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/facilitator.cgi b/facilitator.cgi
index 061b847..31c632e 100755
--- a/facilitator.cgi
+++ b/facilitator.cgi
@@ -1,10 +1,13 @@
 #!/usr/bin/env python
 
 import cgi
-import sys
 import os
+import socket
+import sys
 import urllib
 
+import fac
+
 FACILITATOR_ADDR = ("127.0.0.1", 9002)
 
 def exit_error(status):
@@ -13,16 +16,49 @@ Status: %d\r
 \r""" % status
     sys.exit()
 
+def fac_socket():
+    return socket.create_connection(FACILITATOR_ADDR, 1.0).makefile()
+
+def transact(f, command, *params):
+    transaction = fac.render_transaction(command, *params)
+    print >> f, transaction
+    f.flush()
+    line = f.readline()
+    if not (len(line) > 0 and line[-1] == '\n'):
+        raise ValueError("No newline at end of string returned by facilitator")
+    return fac.parse_transaction(line[:-1])
+
 def put_reg(client_addr, registrant_addr):
     # Pretending to register client_addr as reported by registrant_addr.
     pass
 
 def get_reg(proxy_addr):
-    # Pretending to ask for a client for the proxy at proxy_addr.
-    return {
-        "client": "2.2.2.2:2222",
-        "relay": "199.1.1.1:9001",
-    }
+    f = fac_socket()
+    try:
+        command, params = transact(f, "GET", ("FROM", fac.format_addr(proxy_addr)))
+    finally:
+        f.close()
+    if command == "NONE":
+        return {
+            "client": ""
+        }
+    elif command == "OK":
+        client_spec = fac.param_first("CLIENT", params)
+        relay_spec = fac.param_first("RELAY", params)
+        if not client_spec or not relay_spec:
+            exit_error(500)
+        try:
+            # Check the syntax returned by the backend.
+            client = fac.parse_addr_spec(client_spec)
+            relay = fac.parse_addr_spec(relay_spec)
+        except ValueError:
+            exit_error(500)
+        return {
+            "client": fac.format_addr(client),
+            "relay": fac.format_addr(relay),
+        }
+    else:
+        exit_error(500)
 
 method = os.environ.get("REQUEST_METHOD")
 proxy_addr = (os.environ.get("REMOTE_ADDR"), None)
@@ -42,7 +78,7 @@ Content-Type: application/x-www-form-urlencoded\r
 Cache-Control: no-cache\r
 Access-Control-Allow-Origin: *\r
 \r"""
-    print urllib.urlencode(reg)
+    sys.stdout.write(urllib.urlencode(reg))
 
 if method == "GET":
     do_get()





More information about the tor-commits mailing list