Пример #1
0
def make_socks4(loop,
                *,
                addr=None,
                auth=None,
                rr=True,
                dst=None,
                r=b'',
                ap_factory=None,
                whiter=None):
    addr = addr or aiosocksy.Socks4Addr('localhost', 1080)
    auth = auth or aiosocksy.Socks4Auth('user')
    dst = dst or ('python.org', 80)

    proto = aiosocksy.Socks4Protocol(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.read_response = mock.Mock(side_effect=coro(mock.Mock(
        return_value=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
async def test_socks4_dst_ip_without_user(loop):
    proto = make_socks4(loop,
                        auth=aiosocksy.Socks4Auth(''),
                        dst=('127.0.0.1', 8800),
                        r=b'\x00\x5a\x00P\x7f\x00\x00\x01')

    await proto.socks_request(c.SOCKS_CMD_CONNECT)
    proto._stream_writer.write.assert_called_with(
        b'\x04\x01"`\x7f\x00\x00\x01\x00')
Пример #3
0
async def test_socks4_srv_error(loop):
    pld = b'\x00\x5b\x04W\x01\x01\x01\x01'

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

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert '0x5b' in str(ct)
Пример #4
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)
Пример #5
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)
Пример #6
0
async def test_socks4_connect_success(loop):
    pld = b'\x00\x5a\x04W\x01\x01\x01\x01test'

    async with FakeSocksSrv(loop, pld) as srv:
        addr = aiosocksy.Socks4Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks4Auth('usr')
        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()
Пример #7
0
def test_socks4_auth2():
    auth = aiosocksy.Socks4Auth('usr', encoding='ascii')
    assert auth.login == b'usr'
Пример #8
0
def test_socks4_auth1():
    with pytest.raises(ValueError):
        aiosocksy.Socks4Auth(None)
Пример #9
0
def test_socks4_auth3():
    auth = aiosocksy.Socks4Auth('usrё', encoding='utf-8')
    assert auth.login == b'usr\xd1\x91'