Exemplo n.º 1
0
def test_server_socks_bad_socks_version_but_allowed():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write(b"\x06")
        yield from io.passthrough()

    with pytest.raises(ValueError):
        rotor(client(), SocksServer(allowed_versions={6}))
Exemplo n.º 2
0
def test_server_socks4_auth_required():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write(b"\x04")
        yield from io.passthrough()

    with pytest.raises(ValueError):
        rotor(client(), SocksServer(username="******"))
Exemplo n.º 3
0
def test_server_socks5_no_auth_methods():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("B", 5)
        yield from io.write_struct("B", 0)
        yield from io.passthrough()

    with pytest.raises(ValueError):
        rotor(client(), SocksServer())
Exemplo n.º 4
0
def test_server_socks4_unsupported_command():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("BBH4s", 4, 2, 123, b"\x7f\x00\x00\x01")
        yield from io.write_c_string("yoba")
        yield from io.passthrough()

    with pytest.raises(ValueError):
        rotor(client(), SocksServer())
Exemplo n.º 5
0
def test_server_socks4_success_by_ipv4():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("BBH4s", 4, 1, 123, b"\x7f\x00\x00\x01")
        yield from io.write_c_string("yoba")
        prefix, code, port, ipv4 = yield from io.read_struct("BBH4s")
        assert prefix == 0
        assert code == 0x5a
        assert port == 0
        assert ipv4 == b"\x00" * 4
        yield from io.passthrough()

    rotor(client(), SocksServer())
Exemplo n.º 6
0
def test_server_socks5_bad_username_auth_version():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("B", 5)
        yield from io.write_struct("BB", 1, 2)
        version, auth_method = yield from io.read_struct("BB")
        assert (version, auth_method) == (5, 2)
        yield from io.write_struct("B", 0)
        yield from io.passthrough()

    with pytest.raises(ValueError):
        rotor(
            client(),
            SocksServer(allowed_versions={5}, username="******", password="******"))
Exemplo n.º 7
0
def test_server_socks4_connect_failed():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("BBH4s", 4, 1, 123, b"\x7f\x00\x00\x01")
        yield from io.write_c_string("yoba")
        prefix, code, port, ipv4 = yield from io.read_struct("BBH4s")
        assert prefix == 0
        assert code == 0x5b
        assert port == 0
        assert ipv4 == b"\x00" * 4
        raise RuntimeError("connection failed")

    with pytest.raises(RuntimeError):
        rotor(client(), SocksServer(), fail_connection=True)
Exemplo n.º 8
0
def test_server_socks5_connection_failed():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("B", 5)
        yield from io.write_struct("BB", 1, 0)
        version, auth_method = yield from io.read_struct("BB")
        assert (version, auth_method) == (5, 0)
        yield from io.write_struct("4B", 5, 1, 0, 1)
        yield from io.write_struct("4sH", b"\x00" * 4, 666)
        version, command, zero, address_type = yield from io.read_struct("4B")
        assert (version, command, zero, address_type) == (5, 1, 0, 1)
        raise RuntimeError("connection failed")

    with pytest.raises(RuntimeError):
        rotor(client(), SocksServer(), fail_connection=True)
Exemplo n.º 9
0
def test_server_socks5_connection_ipv6_success():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("B", 5)
        yield from io.write_struct("BB", 1, 0)
        version, auth_method = yield from io.read_struct("BB")
        assert (version, auth_method) == (5, 0)
        yield from io.write_struct("4B", 5, 1, 0, 4)
        yield from io.write_struct("16sH", b"\x00" * 16, 666)
        version, command, zero, address_type = yield from io.read_struct("4B")
        assert (version, command, zero, address_type) == (5, 0, 0, 1)
        ipv4, port = yield from io.read_struct("4sH")
        assert (ipv4, port) == (b"\x00" * 4, 0)
        yield from io.passthrough()

    rotor(client(), SocksServer())
Exemplo n.º 10
0
def test_server_socks5_command_not_supported():
    def client():
        io = SansIORW(encoding="utf-8")
        yield from io.write_struct("B", 5)
        yield from io.write_struct("BB", 1, 0)
        version, auth_method = yield from io.read_struct("BB")
        assert (version, auth_method) == (5, 0)
        yield from io.write_struct("4B", 5, 2, 0, 1)
        yield from io.write_struct("4sH", b"\x00" * 4, 666)
        version, command, zero, address_type = yield from io.read_struct("4B")
        assert (version, command, zero, address_type) == (5, 7, 0, 1)
        ipv4, port = yield from io.read_struct("4sH")
        assert (ipv4, port) == (b"\x00" * 4, 0)
        yield from io.passthrough()

    with pytest.raises(ValueError):
        rotor(client(), SocksServer())
Exemplo n.º 11
0
async def socks_server_handler(reader, writer, *, io_factory, **kwargs):
    try:
        async with io_factory(reader, writer) as io:
            await async_engine(SocksServer(**kwargs), io)
    finally:
        writer.close()