Ejemplo n.º 1
0
def get_reg(facilitator_addr, proxy_addr, proxy_transport_list):
    """
    Get a client registration for proxy proxy_addr from the
    facilitator at facilitator_addr using a one-time
    socket. proxy_transport_list is a list containing the transport names that
    the flashproxy supports.

    Returns a dict with keys "client", "client-transport", "relay",
    and "relay-transport" if successful, or a dict with the key "client"
    mapped to the value "" if there are no registrations available for
    proxy_addr. Raises an exception otherwise."""
    f = fac_socket(facilitator_addr)

    # Form a list (in transact() format) with the transports that we
    # should send to the facilitator.  Then pass that list to the
    # transact() function.
    # For example, PROXY-TRANSPORT=obfs2 PROXY-TRANSPORT=obfs3.
    transports = [("PROXY-TRANSPORT", tp) for tp in proxy_transport_list]

    try:
        command, params = transact(f, "GET", ("FROM", format_addr(proxy_addr)),
                                   *transports)
    finally:
        f.close()
    response = {}
    check_back_in = param_first("CHECK-BACK-IN", params)
    if check_back_in is not None:
        try:
            float(check_back_in)
        except ValueError:
            raise ValueError(
                "Facilitator returned non-numeric polling interval.")
        response["check-back-in"] = check_back_in
    if command == "NONE":
        response["client"] = ""
        return response
    elif command == "OK":
        client_spec = param_first("CLIENT", params)
        client_transport = param_first("CLIENT-TRANSPORT", params)
        relay_spec = param_first("RELAY", params)
        relay_transport = param_first("RELAY-TRANSPORT", params)
        if not client_spec:
            raise ValueError("Facilitator did not return CLIENT")
        if not client_transport:
            raise ValueError("Facilitator did not return CLIENT-TRANSPORT")
        if not relay_spec:
            raise ValueError("Facilitator did not return RELAY")
        if not relay_transport:
            raise ValueError("Facilitator did not return RELAY-TRANSPORT")
        # Check the syntax returned by the facilitator.
        client = parse_addr_spec(client_spec)
        relay = parse_addr_spec(relay_spec)
        response["client"] = format_addr(client)
        response["client-transport"] = client_transport
        response["relay"] = format_addr(relay)
        response["relay-transport"] = relay_transport
        return response
    else:
        raise ValueError("Facilitator response was not \"OK\"")
Ejemplo n.º 2
0
def get_reg(facilitator_addr, proxy_addr, proxy_transport_list):
    """
    Get a client registration for proxy proxy_addr from the
    facilitator at facilitator_addr using a one-time
    socket. proxy_transport_list is a list containing the transport names that
    the flashproxy supports.

    Returns a dict with keys "client", "client-transport", "relay",
    and "relay-transport" if successful, or a dict with the key "client"
    mapped to the value "" if there are no registrations available for
    proxy_addr. Raises an exception otherwise."""
    f = fac_socket(facilitator_addr)

    # Form a list (in transact() format) with the transports that we
    # should send to the facilitator.  Then pass that list to the
    # transact() function.
    # For example, PROXY-TRANSPORT=obfs2 PROXY-TRANSPORT=obfs3.
    transports = [("PROXY-TRANSPORT", tp) for tp in proxy_transport_list]

    try:
        command, params = transact(f, "GET", ("FROM", format_addr(proxy_addr)), *transports)
    finally:
        f.close()
    response = {}
    check_back_in = param_first("CHECK-BACK-IN", params)
    if check_back_in is not None:
        try:
            float(check_back_in)
        except ValueError:
            raise ValueError("Facilitator returned non-numeric polling interval.")
        response["check-back-in"] = check_back_in
    if command == "NONE":
        response["client"] = ""
        return response
    elif command == "OK":
        client_spec = param_first("CLIENT", params)
        client_transport = param_first("CLIENT-TRANSPORT", params)
        relay_spec = param_first("RELAY", params)
        relay_transport = param_first("RELAY-TRANSPORT", params)
        if not client_spec:
            raise ValueError("Facilitator did not return CLIENT")
        if not client_transport:
            raise ValueError("Facilitator did not return CLIENT-TRANSPORT")
        if not relay_spec:
            raise ValueError("Facilitator did not return RELAY")
        if not relay_transport:
            raise ValueError("Facilitator did not return RELAY-TRANSPORT")
        # Check the syntax returned by the facilitator.
        client = parse_addr_spec(client_spec)
        relay = parse_addr_spec(relay_spec)
        response["client"] = format_addr(client)
        response["client-transport"] = client_transport
        response["relay"] = format_addr(relay)
        response["relay-transport"] = relay_transport
        return response
    else:
        raise ValueError("Facilitator response was not \"OK\"")
Ejemplo n.º 3
0
 def setUp(self):
     self.relay_file = tempfile.NamedTemporaryFile()
     self.relay_file.write("%s %s\n" % (RELAY_TP, format_addr(self.IPV4_RELAY_ADDR)))
     self.relay_file.write("%s %s\n" % (RELAY_TP, format_addr(self.IPV6_RELAY_ADDR)))
     self.relay_file.flush()
     self.relay_file.seek(0)
     fn = os.path.join(os.path.dirname(__file__), "./facilitator")
     self.process = subprocess.Popen(["python", fn, "-d", "-p", str(FACILITATOR_PORT), "-r", self.relay_file.name, "-l", "/dev/null"])
     time.sleep(0.1)
Ejemplo n.º 4
0
 def test_fields(self):
     """Test that facilitator responses contain all the required fields."""
     fac.put_reg(FACILITATOR_ADDR, self.IPV4_CLIENT_ADDR, CLIENT_TP)
     reg = fac.get_reg(FACILITATOR_ADDR, self.IPV4_PROXY_ADDR, PROXY_TPS)
     self.assertEqual(reg["client"], format_addr(self.IPV4_CLIENT_ADDR))
     self.assertEqual(reg["client-transport"], CLIENT_TP)
     self.assertEqual(reg["relay"], format_addr(self.IPV4_RELAY_ADDR))
     self.assertEqual(reg["relay-transport"], RELAY_TP)
     self.assertGreater(int(reg["check-back-in"]), 0)
Ejemplo n.º 5
0
 def test_fields(self):
     """Test that facilitator responses contain all the required fields."""
     fac.put_reg(FACILITATOR_ADDR, self.IPV4_CLIENT_ADDR, CLIENT_TP)
     reg = fac.get_reg(FACILITATOR_ADDR, self.IPV4_PROXY_ADDR, PROXY_TPS)
     self.assertEqual(reg["client"], format_addr(self.IPV4_CLIENT_ADDR))
     self.assertEqual(reg["client-transport"], CLIENT_TP)
     self.assertEqual(reg["relay"], format_addr(self.IPV4_RELAY_ADDR))
     self.assertEqual(reg["relay-transport"], RELAY_TP)
     self.assertGreater(int(reg["check-back-in"]), 0)
Ejemplo n.º 6
0
 def setUp(self):
     self.relay_file = tempfile.NamedTemporaryFile()
     self.relay_file.write("%s %s\n" %
                           (RELAY_TP, format_addr(self.IPV4_RELAY_ADDR)))
     self.relay_file.write("%s %s\n" %
                           (RELAY_TP, format_addr(self.IPV6_RELAY_ADDR)))
     self.relay_file.flush()
     self.relay_file.seek(0)
     fn = os.path.join(os.path.dirname(__file__), "./facilitator")
     self.process = subprocess.Popen([
         "python", fn, "-d", "-p",
         str(FACILITATOR_PORT), "-r", self.relay_file.name, "-l",
         "/dev/null"
     ])
     time.sleep(0.1)
Ejemplo n.º 7
0
def add_registration_args(parser):
    add_module_opts(parser)
    parser.add_argument("remote_addr",
                        metavar="ADDR:PORT",
                        help="external addr+port to register, default %s" %
                        format_addr(DEFAULT_REMOTE),
                        default="",
                        nargs="?",
                        type=lambda x: parse_addr_spec(x, *DEFAULT_REMOTE))
Ejemplo n.º 8
0
def put_reg(facilitator_addr, client_addr, transport):
    """Send a registration to the facilitator using a one-time socket. Returns
    true iff the command was successful. transport is a transport string such as
    "websocket" or "obfs3|websocket"."""
    f = fac_socket(facilitator_addr)
    params = [("CLIENT", format_addr(client_addr))]
    params.append(("TRANSPORT", transport))
    try:
        command, params = transact(f, "PUT", *params)
    finally:
        f.close()
    return command == "OK"
Ejemplo n.º 9
0
def put_reg(facilitator_addr, client_addr, transport):
    """Send a registration to the facilitator using a one-time socket. Returns
    true iff the command was successful. transport is a transport string such as
    "websocket" or "obfs3|websocket"."""
    f = fac_socket(facilitator_addr)
    params = [("CLIENT", format_addr(client_addr))]
    params.append(("TRANSPORT", transport))
    try:
        command, params = transact(f, "PUT", *params)
    finally:
        f.close()
    return command == "OK"
Ejemplo n.º 10
0
 def test_none_port(self):
     self.assertEqual(format_addr((None, 1234)), ":1234")
Ejemplo n.º 11
0
 def test_af_v6_v6(self):
     """Test that IPv6 proxies can get IPv6 clients."""
     fac.put_reg(FACILITATOR_ADDR, self.IPV4_CLIENT_ADDR, CLIENT_TP)
     fac.put_reg(FACILITATOR_ADDR, self.IPV6_CLIENT_ADDR, CLIENT_TP)
     reg = fac.get_reg(FACILITATOR_ADDR, self.IPV6_PROXY_ADDR, PROXY_TPS)
     self.assertEqual(reg["client"], format_addr(self.IPV6_CLIENT_ADDR))
Ejemplo n.º 12
0
 def test_empty_port(self):
     self.assertEqual(format_addr(("", 1234)), ":1234")
Ejemplo n.º 13
0
 def test_ipv6_port(self):
     self.assertEqual(format_addr(("1:2::3:4", 1234)), "[1:2::3:4]:1234")
Ejemplo n.º 14
0
 def test_name_none(self):
     self.assertEqual(format_addr(("localhost", None)), "localhost")
Ejemplo n.º 15
0
 def test_ipv6_none(self):
     self.assertEqual(format_addr(("1:2::3:4", None)), "[1:2::3:4]")
Ejemplo n.º 16
0
 def test_ipv4_none(self):
     self.assertEqual(format_addr(("1.2.3.4", None)), "1.2.3.4")
Ejemplo n.º 17
0
def build_reg(addr, transport):
    return urllib.urlencode((
        ("client", format_addr(addr)),
        ("client-transport", transport),
    ))
Ejemplo n.º 18
0
def add_registration_args(parser):
    add_module_opts(parser)
    parser.add_argument("remote_addr", metavar="ADDR:PORT",
        help="external addr+port to register, default %s" %
        format_addr(DEFAULT_REMOTE), default="", nargs="?",
        type=lambda x: parse_addr_spec(x, *DEFAULT_REMOTE))
Ejemplo n.º 19
0
 def test_ipv4_port(self):
     self.assertEqual(format_addr(("1.2.3.4", 1234)), "1.2.3.4:1234")
Ejemplo n.º 20
0
 def test_name_port(self):
     self.assertEqual(format_addr(("localhost", 1234)), "localhost:1234")
Ejemplo n.º 21
0
 def test_name_none(self):
     self.assertEqual(format_addr(("localhost", None)), "localhost")
Ejemplo n.º 22
0
 def test_none_port(self):
     self.assertEqual(format_addr((None, 1234)), ":1234")
Ejemplo n.º 23
0
 def test_empty_port(self):
     self.assertEqual(format_addr(("", 1234)), ":1234")
Ejemplo n.º 24
0
 def test_af_v6_v6(self):
     """Test that IPv6 proxies can get IPv6 clients."""
     fac.put_reg(FACILITATOR_ADDR, self.IPV4_CLIENT_ADDR, CLIENT_TP)
     fac.put_reg(FACILITATOR_ADDR, self.IPV6_CLIENT_ADDR, CLIENT_TP)
     reg = fac.get_reg(FACILITATOR_ADDR, self.IPV6_PROXY_ADDR, PROXY_TPS)
     self.assertEqual(reg["client"], format_addr(self.IPV6_CLIENT_ADDR))
Ejemplo n.º 25
0
 def test_ipv4_port(self):
     self.assertEqual(format_addr(("1.2.3.4", 1234)), "1.2.3.4:1234")
Ejemplo n.º 26
0
def build_reg(addr, transport):
    return urllib.urlencode((
        ("client", format_addr(addr)),
        ("client-transport", transport),
    ))
Ejemplo n.º 27
0
 def test_ipv6_port(self):
     self.assertEqual(format_addr(("1:2::3:4", 1234)), "[1:2::3:4]:1234")
Ejemplo n.º 28
0
 def test_ipv4_none(self):
     self.assertEqual(format_addr(("1.2.3.4", None)), "1.2.3.4")
Ejemplo n.º 29
0
 def test_name_port(self):
     self.assertEqual(format_addr(("localhost", 1234)), "localhost:1234")
Ejemplo n.º 30
0
 def test_ipv6_none(self):
     self.assertEqual(format_addr(("1:2::3:4", None)), "[1:2::3:4]")