Exemplo n.º 1
0
def make_socks4(loop,
                *,
                addr=None,
                auth=None,
                rr=True,
                dst=None,
                r=b'',
                ap_factory=None,
                whiter=None):
    addr = addr or aiosocks.Socks4Addr('localhost', 1080)
    auth = auth or aiosocks.Socks4Auth('user')
    dst = dst or ('python.org', 80)

    proto = aiosocks.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
Exemplo n.º 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)
Exemplo n.º 3
0
async def test_socks4_dst_ip_without_user(loop):
    proto = make_socks4(loop, auth=aiosocks.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')
Exemplo n.º 4
0
    def test_init(self):
        addr = aiosocks.Socks5Addr('localhost')
        auth = aiosocks.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        # proxy argument
        with self.assertRaises(AssertionError) as ct:
            conn = aiosocks.create_connection(None, None, auth, dst)
            self.loop.run_until_complete(conn)
        self.assertEqual(str(ct.exception),
                         'proxy must be Socks4Addr() or Socks5Addr() tuple')

        with self.assertRaises(AssertionError) as ct:
            conn = aiosocks.create_connection(None, auth, auth, dst)
            self.loop.run_until_complete(conn)
        self.assertEqual(str(ct.exception),
                         'proxy must be Socks4Addr() or Socks5Addr() tuple')

        # proxy_auth
        with self.assertRaises(AssertionError) as ct:
            conn = aiosocks.create_connection(None, addr, addr, dst)
            self.loop.run_until_complete(conn)
        self.assertIn('proxy_auth must be None or Socks4Auth()',
                      str(ct.exception))

        # dst
        with self.assertRaises(AssertionError) as ct:
            conn = aiosocks.create_connection(None, addr, auth, None)
            self.loop.run_until_complete(conn)
        self.assertIn('invalid dst format, tuple("dst_host", dst_port))',
                      str(ct.exception))

        # addr and auth compatibility
        with self.assertRaises(ValueError) as ct:
            conn = aiosocks.create_connection(None, addr,
                                              aiosocks.Socks4Auth(''), dst)
            self.loop.run_until_complete(conn)
        self.assertIn('proxy is Socks5Addr but proxy_auth is not Socks5Auth',
                      str(ct.exception))

        with self.assertRaises(ValueError) as ct:
            conn = aiosocks.create_connection(None, aiosocks.Socks4Addr(''),
                                              auth, dst)
            self.loop.run_until_complete(conn)
        self.assertIn('proxy is Socks4Addr but proxy_auth is not Socks4Auth',
                      str(ct.exception))

        # test ssl, server_hostname
        with self.assertRaises(ValueError) as ct:
            conn = aiosocks.create_connection(None,
                                              addr,
                                              auth,
                                              dst,
                                              server_hostname='python.org')
            self.loop.run_until_complete(conn)
        self.assertIn('server_hostname is only meaningful with ssl',
                      str(ct.exception))
Exemplo n.º 5
0
    def test_dst_ip_without_user(self):
        proto = make_socks4(self.loop,
                            auth=aiosocks.Socks4Auth(''),
                            dst=('127.0.0.1', 8800),
                            r=b'\x00\x5a\x00P\x7f\x00\x00\x01')
        req = proto.socks_request(c.SOCKS_CMD_CONNECT)
        self.loop.run_until_complete(req)

        proto._stream_writer.write.assert_called_with(
            b'\x04\x01"`\x7f\x00\x00\x01\x00')
Exemplo n.º 6
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 = aiosocks.Socks4Addr('127.0.0.1', srv.port)
        auth = aiosocks.Socks4Auth('usr')
        dst = ('python.org', 80)

        with pytest.raises(aiosocks.SocksError) as ct:
            await aiosocks.create_connection(None, addr, auth, dst, loop=loop)
        assert '0x5b' in str(ct.value)
Exemplo n.º 7
0
    def test_init(self):
        addr = aiosocks.Socks4Addr('localhost', 1080)
        auth = aiosocks.Socks4Auth('user')
        dst = ('python.org', 80)

        with self.assertRaises(ValueError):
            aiosocks.Socks4Protocol(None,
                                    None,
                                    dst,
                                    loop=self.loop,
                                    waiter=None,
                                    app_protocol_factory=None)

        with self.assertRaises(ValueError):
            aiosocks.Socks4Protocol(None,
                                    auth,
                                    dst,
                                    loop=self.loop,
                                    waiter=None,
                                    app_protocol_factory=None)

        with self.assertRaises(ValueError):
            aiosocks.Socks4Protocol(aiosocks.Socks5Addr('host'),
                                    auth,
                                    dst,
                                    loop=self.loop,
                                    waiter=None,
                                    app_protocol_factory=None)

        with self.assertRaises(ValueError):
            aiosocks.Socks4Protocol(addr,
                                    aiosocks.Socks5Auth('l', 'p'),
                                    dst,
                                    loop=self.loop,
                                    waiter=None,
                                    app_protocol_factory=None)

        aiosocks.Socks4Protocol(addr,
                                None,
                                dst,
                                loop=self.loop,
                                waiter=None,
                                app_protocol_factory=None)
        aiosocks.Socks4Protocol(addr,
                                auth,
                                dst,
                                loop=self.loop,
                                waiter=None,
                                app_protocol_factory=None)
Exemplo n.º 8
0
    def test_socks_srv_error(self):
        with fake_socks_srv(self.loop,
                            b'\x00\x5b\x04W\x01\x01\x01\x01') as port:
            addr = aiosocks.Socks4Addr('127.0.0.1', port)
            auth = aiosocks.Socks4Auth('usr')
            dst = ('python.org', 80)

            with self.assertRaises(aiosocks.SocksError) as ct:
                coro = aiosocks.create_connection(None,
                                                  addr,
                                                  auth,
                                                  dst,
                                                  loop=self.loop)
                self.loop.run_until_complete(coro)
            self.assertIn('0x5b', str(ct.exception))
Exemplo n.º 9
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 = aiosocks.Socks4Addr('127.0.0.1', srv.port)
        auth = aiosocks.Socks4Auth('usr')
        dst = ('python.org', 80)

        transport, protocol = await aiosocks.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()
Exemplo n.º 10
0
async def test_create_connection_init():
    addr = aiosocks.Socks5Addr('localhost')
    auth = aiosocks.Socks5Auth('usr', 'pwd')
    dst = ('python.org', 80)

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

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

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

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

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

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

    # test ssl, server_hostname
    with pytest.raises(ValueError) as ct:
        await aiosocks.create_connection(None,
                                         addr,
                                         auth,
                                         dst,
                                         server_hostname='python.org')
    assert 'server_hostname is only meaningful with ssl' in str(ct.value)
Exemplo n.º 11
0
    def test_connect_success(self):
        with fake_socks_srv(self.loop,
                            b'\x00\x5a\x04W\x01\x01\x01\x01test') as port:
            addr = aiosocks.Socks4Addr('127.0.0.1', port)
            auth = aiosocks.Socks4Auth('usr')
            dst = ('python.org', 80)

            coro = aiosocks.create_connection(None,
                                              addr,
                                              auth,
                                              dst,
                                              loop=self.loop)
            transport, protocol = self.loop.run_until_complete(coro)

            self.assertEqual(protocol.proxy_sockname, ('1.1.1.1', 1111))

            data = self.loop.run_until_complete(
                protocol._stream_reader.read(4))
            self.assertEqual(data, b'test')

            transport.close()
Exemplo n.º 12
0
def proxied_connection(dst,
                       proxy_type=None,
                       addr=None,
                       port=None,
                       rdns=True,
                       username=None,
                       password=None):
    if proxy_type == 'SOCKS4':
        socks4_addr = aiosocks.Socks4Addr(addr, port)
        socks4_auth = aiosocks.Socks4Auth(username)
        return aiosocks.open_connection(socks4_addr,
                                        socks4_auth,
                                        dst,
                                        remote_resolve=rdns)
    elif proxy_type == 'SOCKS5':
        socks5_addr = aiosocks.Socks5Addr(addr, port)
        socks5_auth = aiosocks.Socks5Auth(username, password)
        return aiosocks.open_connection(socks5_addr,
                                        socks5_auth,
                                        dst,
                                        remote_resolve=rdns)
    else:
        return asyncio.open_connection(*dst)
Exemplo n.º 13
0
 def test_properties(self):
     addr = aiosocks.Socks4Addr('localhost')
     auth = aiosocks.Socks4Auth('login')
     conn = SocksConnector(addr, auth, loop=self.loop)
     self.assertIs(conn.proxy, addr)
     self.assertIs(conn.proxy_auth, auth)
Exemplo n.º 14
0
def test_socks4_auth2():
    auth = aiosocks.Socks4Auth('usr', encoding='ascii')
    assert auth.login == b'usr'
Exemplo n.º 15
0
def test_socks4_auth1():
    with pytest.raises(ValueError):
        aiosocks.Socks4Auth(None)
Exemplo n.º 16
0
def test_socks4_auth3():
    auth = aiosocks.Socks4Auth('usrё', encoding='utf-8')
    assert auth.login == b'usr\xd1\x91'