示例#1
0
def make_socks5(loop,
                *,
                addr=None,
                auth=None,
                rr=True,
                dst=None,
                r=None,
                ap_factory=None,
                whiter=None):
    addr = addr or aiosocks.Socks5Addr('localhost', 1080)
    auth = auth or aiosocks.Socks5Auth('user', 'pwd')
    dst = dst or ('python.org', 80)

    proto = aiosocks.Socks5Protocol(proxy=addr,
                                    proxy_auth=auth,
                                    dst=dst,
                                    remote_resolve=rr,
                                    loop=loop,
                                    app_protocol_factory=ap_factory,
                                    waiter=whiter)
    proto._stream_writer = mock.Mock()
    proto._stream_writer.drain = fake_coroutine(True)

    if not isinstance(r, (list, tuple)):
        proto.read_response = mock.Mock(
            side_effect=coro(mock.Mock(return_value=r)))
    else:
        proto.read_response = mock.Mock(
            side_effect=coro(mock.Mock(side_effect=r)))

    proto._get_dst_addr = mock.Mock(
        side_effect=coro(mock.Mock(return_value=(socket.AF_INET,
                                                 '127.0.0.1'))))

    return proto
示例#2
0
def test_socks5_ctor(loop):
    addr = aiosocks.Socks5Addr('localhost', 1080)
    auth = aiosocks.Socks5Auth('user', 'pwd')
    dst = ('python.org', 80)

    with pytest.raises(ValueError):
        aiosocks.Socks5Protocol(None, None, dst, loop=loop,
                                waiter=None, app_protocol_factory=None)

    with pytest.raises(ValueError):
        aiosocks.Socks5Protocol(None, auth, dst, loop=loop,
                                waiter=None, app_protocol_factory=None)

    with pytest.raises(ValueError):
        aiosocks.Socks5Protocol(aiosocks.Socks4Addr('host'),
                                auth, dst, loop=loop,
                                waiter=None, app_protocol_factory=None)

    with pytest.raises(ValueError):
        aiosocks.Socks5Protocol(addr, aiosocks.Socks4Auth('l'),
                                dst, loop=loop,
                                waiter=None, app_protocol_factory=None)

    aiosocks.Socks5Protocol(addr, None, dst, loop=loop,
                            waiter=None, app_protocol_factory=None)
    aiosocks.Socks5Protocol(addr, auth, dst, loop=loop,
                            waiter=None, app_protocol_factory=None)