def test_client_bad_socks_version(): def server(): io = SansIORW(encoding="utf-8") yield from io.read_exactly(1) with pytest.raises(ValueError): rotor(SocksClient("abc", 123, 6), server())
def test_client_socks4_and_auth(): def server(): io = SansIORW(encoding="utf-8") yield from io.read_exactly(1) with pytest.raises(ValueError): rotor(SocksClient("abc", 123, 4, username="******", password="******"), server())
def test_client_socks5_request_auth_not_accepted(): def server(): io = SansIORW(encoding="utf-8") version, one, auth_method = yield from io.read_struct("BBB") assert version == 5 assert one == 1 assert auth_method == 0 yield from io.write_struct("BB", 5, 1) yield from io.passthrough() with pytest.raises(ValueError): rotor(SocksClient("abc", 666, 5), server())
async def open_connection(host=None, port=None, *, socks_host=None, socks_port=None, socks_version=None): reader, writer = await asyncio.open_connection(socks_host, socks_port) reader = IncomingDecoder(reader) writer = OutgoingEncoder(writer) protocol = SocksClient(host, port, socks_version) io = ClientIO(reader, writer) await async_engine(protocol, io) return reader, writer
def test_client_socks5_command_bad_version(): def server(): io = SansIORW(encoding="utf-8") version, one, auth_method = yield from io.read_struct("BBB") assert (version, one, auth_method) == (5, 1, 0) yield from io.write_struct("BB", 5, 0) version, command, zero, address_type = yield from io.read_struct("4B") assert (version, command, zero, address_type) == (5, 1, 0, 1) ipv4, port = yield from io.read_struct("4sH") assert (ipv4, port) == (b"\x7f\x00\x00\x01", 666) yield from io.write_struct("4B", 6, 0, 0, 0) yield from io.passthrough() with pytest.raises(ValueError): rotor(SocksClient("127.0.0.1", 666, 5), server())
def test_client_socks4_success_by_ipv4(): def server(): io = SansIORW(encoding="utf-8") version, command, port, ipv4 = yield from io.read_struct("BBH4s") assert version == 4 assert command == 1 assert port == 123 assert ipv4 == b"\x7f\x00\x00\x01" user_id = yield from io.read_c_string() assert user_id == "" yield from io.connect(ipv4, port) yield from io.write_struct("BBH4s", 0, 0x5a, 0, b"\x00" * 4) yield from io.passthrough() rotor(SocksClient("127.0.0.1", 123, 4), server())
def test_client_socks5_success_ipv6(): def server(): io = SansIORW(encoding="utf-8") version, one, auth_method = yield from io.read_struct("BBB") assert (version, one, auth_method) == (5, 1, 0) yield from io.write_struct("BB", 5, 0) version, command, zero, address_type = yield from io.read_struct("4B") assert (version, command, zero, address_type) == (5, 1, 0, 4) ipv6, port = yield from io.read_struct("16sH") assert (ipv6, port) == ((b"\x00" * 15) + b"\x01", 666) yield from io.write_struct("4B", 5, 0, 0, 1) yield from io.write(b"\x00" * 4) yield from io.write_struct("H", 0) yield from io.passthrough() rotor(SocksClient("::1", 666, 5), server())
def test_client_socks5_success_domain(): def server(): io = SansIORW(encoding="utf-8") version, one, auth_method = yield from io.read_struct("BBB") assert (version, one, auth_method) == (5, 1, 0) yield from io.write_struct("BB", 5, 0) version, command, zero, address_type = yield from io.read_struct("4B") assert (version, command, zero, address_type) == (5, 1, 0, 3) domain = yield from io.read_pascal_string() port = yield from io.read_struct("H") assert (domain, port) == ("python.org", 666) yield from io.write_struct("4B", 5, 0, 0, 1) yield from io.write(b"\x00" * 4) yield from io.write_struct("H", 0) yield from io.passthrough() rotor(SocksClient("python.org", 666, 5), server())
def test_client_socks4_redirect_not_supported_by_port(): def server(): io = SansIORW(encoding="utf-8") version, command, port, ipv4 = yield from io.read_struct("BBH4s") assert version == 4 assert command == 1 assert port == 123 assert ipv4 == b"\x7f\x00\x00\x01" user_id = yield from io.read_c_string() assert user_id == "yoba" yield from io.connect(ipv4, port) yield from io.write_struct("BBH4s", 0, 0x5a, 666, b"\x00" * 4) yield from io.passthrough() with pytest.raises(ValueError): rotor( SocksClient("127.0.0.1", 123, 4, socks4_extras=dict(user_id="yoba")), server())
def test_client_socks5_request_auth_username_failed(): def server(): io = SansIORW(encoding="utf-8") version, one, auth_method = yield from io.read_struct("BBB") assert version == 5 assert one == 1 assert auth_method == 2 yield from io.write_struct("BB", 5, 2) auth_version = yield from io.read_struct("B") assert auth_version == 1 username = yield from io.read_pascal_string() password = yield from io.read_pascal_string() assert username == "yoba" assert password == "foo" yield from io.write_struct("BB", 1, 1) yield from io.passthrough() with pytest.raises(ValueError): rotor(SocksClient("abc", 666, 5, username="******", password="******"), server())