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

    proto = aiosocksy.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 = make_mocked_coro(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
Example #2
0
async def test_socks5_invalid_address_type(loop):
    async with FakeSocksSrv(loop, b'\x05\x02\x01\x00\x05\x00\x00\xFF') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'invalid data' in str(ct)
Example #3
0
async def test_socks5_cmd_not_granted(loop):
    async with FakeSocksSrv(loop, b'\x05\x02\x01\x00\x05\x01\x00') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'General SOCKS server failure' in str(ct)
Example #4
0
async def test_socks5_auth_failed(loop):
    async with FakeSocksSrv(loop, b'\x05\x02\x01\x01') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'authentication failed' in str(ct)
Example #5
0
async def test_socks5_auth_ver_err(loop):
    async with FakeSocksSrv(loop, b'\x04\x02') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'invalid version' in str(ct)
Example #6
0
async def test_connection_fail():
    addr = aiosocksy.Socks5Addr('localhost')
    auth = aiosocksy.Socks5Auth('usr', 'pwd')
    dst = ('python.org', 80)

    loop_mock = mock.Mock()
    loop_mock.create_connection = make_mocked_coro(raise_exception=OSError())

    with pytest.raises(aiosocksy.SocksConnectionError):
        await aiosocksy.create_connection(None,
                                          addr,
                                          auth,
                                          dst,
                                          loop=loop_mock)
Example #7
0
def test_socks5_ctor(loop):
    addr = aiosocksy.Socks5Addr('localhost', 1080)
    auth = aiosocksy.Socks5Auth('user', 'pwd')
    dst = ('python.org', 80)

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

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

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

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

    aiosocksy.Socks5Protocol(addr,
                             None,
                             dst,
                             loop=loop,
                             waiter=None,
                             app_protocol_factory=None)
    aiosocksy.Socks5Protocol(addr,
                             auth,
                             dst,
                             loop=loop,
                             waiter=None,
                             app_protocol_factory=None)
Example #8
0
 def __init__(self,
              address,
              port,
              login=None,
              password=None,
              timeout=10,
              loop=None):
     addr = aiosocksy.Socks5Addr(address, port)
     if login and password:
         auth = aiosocksy.Socks5Auth(login, password=password)
     else:
         auth = None
     conn = self.connector(proxy=addr, proxy_auth=auth, loop=loop)
     session = aiohttp.ClientSession(connector=conn)
     super().__init__(timeout, loop, session)
Example #9
0
async def test_socks5_atype_domain(loop):
    pld = b'\x05\x02\x01\x00\x05\x00\x00\x03\x0apython.org\x04W'

    async with FakeSocksSrv(loop, pld) as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        transport, protocol = await aiosocksy.create_connection(None,
                                                                addr,
                                                                auth,
                                                                dst,
                                                                loop=loop)
        assert protocol.proxy_sockname == (b'python.org', 1111)

        transport.close()
Example #10
0
async def test_negotiate_fail():
    addr = aiosocksy.Socks5Addr('localhost')
    auth = aiosocksy.Socks5Auth('usr', 'pwd')
    dst = ('python.org', 80)

    loop_mock = mock.Mock()
    loop_mock.create_connection = make_mocked_coro((mock.Mock(), mock.Mock()))

    with mock.patch('aiosocksy.asyncio.Future') as future_mock:
        future_mock.side_effect = make_mocked_coro(
            raise_exception=aiosocksy.SocksError())

        with pytest.raises(aiosocksy.SocksError):
            await aiosocksy.create_connection(None,
                                              addr,
                                              auth,
                                              dst,
                                              loop=loop_mock)
Example #11
0
async def test_socks5_connect_success_usr_pwd(loop):
    pld = b'\x05\x02\x01\x00\x05\x00\x00\x01\x01\x01\x01\x01\x04Wtest'

    async with FakeSocksSrv(loop, pld) as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        transport, protocol = await aiosocksy.create_connection(None,
                                                                addr,
                                                                auth,
                                                                dst,
                                                                loop=loop)
        assert protocol.proxy_sockname == ('1.1.1.1', 1111)

        data = await protocol._stream_reader.read(4)
        assert data == b'test'
        transport.close()
Example #12
0
async def test_open_connection():
    addr = aiosocksy.Socks5Addr('localhost')
    auth = aiosocksy.Socks5Auth('usr', 'pwd')
    dst = ('python.org', 80)

    transp, proto = mock.Mock(), mock.Mock()
    reader, writer = mock.Mock(), mock.Mock()

    proto.app_protocol.reader, proto.app_protocol.writer = reader, writer

    loop_mock = mock.Mock()
    loop_mock.create_connection = make_mocked_coro((transp, proto))

    with mock.patch('aiosocksy.asyncio.Future') as future_mock:
        future_mock.side_effect = make_mocked_coro(True)
        r, w = await aiosocksy.open_connection(addr, auth, dst, loop=loop_mock)

    assert reader is r
    assert writer is w
Example #13
0
async def test_create_connection_init():
    addr = aiosocksy.Socks5Addr('localhost')
    auth = aiosocksy.Socks5Auth('usr', 'pwd')
    dst = ('python.org', 80)

    # proxy argument
    with pytest.raises(AssertionError) as ct:
        await aiosocksy.create_connection(None, None, auth, dst)
    assert 'proxy must be Socks4Addr() or Socks5Addr() tuple' in str(ct)

    with pytest.raises(AssertionError) as ct:
        await aiosocksy.create_connection(None, auth, auth, dst)
    assert 'proxy must be Socks4Addr() or Socks5Addr() tuple' in str(ct)

    # proxy_auth
    with pytest.raises(AssertionError) as ct:
        await aiosocksy.create_connection(None, addr, addr, dst)
    assert 'proxy_auth must be None or Socks4Auth()' in str(ct)

    # dst
    with pytest.raises(AssertionError) as ct:
        await aiosocksy.create_connection(None, addr, auth, None)
    assert 'invalid dst format, tuple("dst_host", dst_port))' in str(ct)

    # addr and auth compatibility
    with pytest.raises(ValueError) as ct:
        await aiosocksy.create_connection(None, addr, aiosocksy.Socks4Auth(''),
                                          dst)
    assert 'proxy is Socks5Addr but proxy_auth is not Socks5Auth' in str(ct)

    with pytest.raises(ValueError) as ct:
        await aiosocksy.create_connection(None, aiosocksy.Socks4Addr(''), auth,
                                          dst)
    assert 'proxy is Socks4Addr but proxy_auth is not Socks4Auth' in str(ct)

    # test ssl, server_hostname
    with pytest.raises(ValueError) as ct:
        await aiosocksy.create_connection(None,
                                          addr,
                                          auth,
                                          dst,
                                          server_hostname='python.org')
    assert 'server_hostname is only meaningful with ssl' in str(ct)
Example #14
0
def test_socks5_addr4():
    addr = aiosocksy.Socks5Addr('localhost', None)
    assert addr.host == 'localhost'
    assert addr.port == 1080
Example #15
0
def test_socks5_addr3():
    addr = aiosocksy.Socks5Addr('localhost', 1)
    assert addr.host == 'localhost'
    assert addr.port == 1
Example #16
0
def test_socks5_addr1():
    with pytest.raises(ValueError):
        aiosocksy.Socks5Addr(None)