def test_simple(self): a = tcp.Address(("localhost", 80), True) assert a.use_ipv6 b = tcp.Address(("foo.com", 80), True) assert not a == b c = tcp.Address(("localhost", 80), True) assert a == c assert not a != c assert repr(a) == "localhost:80"
def test_echo(self): testval = b"echo!\n" c = tcp.TCPClient(tcp.Address(("::1", self.port), use_ipv6=True)) with c.connect(): c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval
def from_file(cls, f): ver, msg, rsv, atyp = struct.unpack("!BBBB", f.safe_read(4)) if rsv != 0x00: raise SocksError(REP.GENERAL_SOCKS_SERVER_FAILURE, "Socks Request: Invalid reserved byte: %s" % rsv) if atyp == ATYP.IPV4_ADDRESS: # We use tnoa here as ntop is not commonly available on Windows. host = ipaddress.IPv4Address(f.safe_read(4)).compressed use_ipv6 = False elif atyp == ATYP.IPV6_ADDRESS: host = ipaddress.IPv6Address(f.safe_read(16)).compressed use_ipv6 = True elif atyp == ATYP.DOMAINNAME: length, = struct.unpack("!B", f.safe_read(1)) host = f.safe_read(length) if not check.is_valid_host(host): raise SocksError(REP.GENERAL_SOCKS_SERVER_FAILURE, "Invalid hostname: %s" % host) host = host.decode("idna") use_ipv6 = False else: raise SocksError(REP.ADDRESS_TYPE_NOT_SUPPORTED, "Socks Request: Unknown ATYP: %s" % atyp) port, = struct.unpack("!H", f.safe_read(2)) addr = tcp.Address((host, port), use_ipv6=use_ipv6) return cls(ver, msg, atyp, addr)
def test_message_unknown_atyp(): raw = tutils.treader(b"\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") with pytest.raises(socks.SocksError): socks.Message.from_file(raw) m = socks.Message(5, 1, 0x02, tcp.Address(("example.com", 5050))) with pytest.raises(socks.SocksError): m.to_file(BytesIO())
class TestServerIPv6(tservers.ServerTestBase): handler = EchoHandler addr = tcp.Address(("localhost", 0), use_ipv6=True) def test_echo(self): testval = b"echo!\n" c = tcp.TCPClient(tcp.Address(("::1", self.port), use_ipv6=True)) with c.connect(): c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval
def parse_server_spec(spec): try: p = url.parse(spec) if p[0] not in (b"http", b"https"): raise ValueError() except ValueError: raise exceptions.OptionsError("Invalid server specification: %s" % spec) host, port = p[1:3] address = tcp.Address((host.decode("ascii"), port)) scheme = p[0].decode("ascii").lower() return ServerSpec(scheme, address)
def establish_server_connection(self, host: str, port: int, scheme: str): address = tcp.Address((host, port)) tls = (scheme == "https") if self.mode is HTTPMode.regular or self.mode is HTTPMode.transparent: # If there's an existing connection that doesn't match our expectations, kill it. if address != self.server_conn.address or tls != self.server_tls: self.set_server(address) self.set_server_tls(tls, address.host) # Establish connection is neccessary. if not self.server_conn.connected(): self.connect() else: if not self.server_conn.connected(): self.connect() if tls: raise exceptions.HttpProtocolException("Cannot change scheme in upstream proxy mode.")