def test_fingerprint_success(self): with fake_socks4_srv(self.loop) as proxy_port: addr = aiosocks.Socks4Addr('127.0.0.1', proxy_port) fp = (b's\x93\xfd:\xed\x08\x1do\xa9\xaeq9' b'\x1a\xe3\xc5\x7f\x89\xe7l\xf9') conn = SocksConnector(proxy=addr, proxy_auth=None, loop=self.loop, remote_resolve=False, verify_ssl=False, fingerprint=fp) with http_srv(self.loop, use_ssl=True) as url: with aiohttp.ClientSession(connector=conn, loop=self.loop) as ses: @asyncio.coroutine def make_req(): return (yield from ses.request('get', url=url)) resp = self.loop.run_until_complete(make_req()) self.assertEqual(resp.status, 200) content = self.loop.run_until_complete(resp.text()) self.assertEqual(content, 'Test message') resp.close()
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)
def test_https_connect(self): with fake_socks4_srv(self.loop) as proxy_port: addr = aiosocks.Socks4Addr('127.0.0.1', proxy_port) conn = SocksConnector(proxy=addr, proxy_auth=None, loop=self.loop, remote_resolve=False, verify_ssl=False) with http_srv(self.loop, use_ssl=True) as url: with aiohttp.ClientSession(connector=conn, loop=self.loop) as ses: @asyncio.coroutine def make_req(): return (yield from ses.request('get', url=url)) resp = self.loop.run_until_complete(make_req()) self.assertEqual(resp.status, 200) content = self.loop.run_until_complete(resp.text()) self.assertEqual(content, 'Test message') resp.close()
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
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))
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)
def test_proxy_connector(): socks4_addr = aiosocks.Socks4Addr('h') socks5_addr = aiosocks.Socks5Addr('h') http_addr = aiosocks.HttpProxyAddr('http://proxy') loop = asyncio.new_event_loop() assert isinstance(proxy_connector(socks4_addr, loop=loop), SocksConnector) assert isinstance(proxy_connector(socks5_addr, loop=loop), SocksConnector) assert isinstance(proxy_connector(http_addr, loop=loop), aiohttp.ProxyConnector) with pytest.raises(ValueError): proxy_connector(None)
def test_proxy_connector(self): socks4_addr = aiosocks.Socks4Addr('h') socks5_addr = aiosocks.Socks5Addr('h') http_addr = HttpProxyAddr('http://proxy') self.assertIsInstance(proxy_connector(socks4_addr, loop=self.loop), SocksConnector) self.assertIsInstance(proxy_connector(socks5_addr, loop=self.loop), SocksConnector) self.assertIsInstance(proxy_connector(http_addr, loop=self.loop), aiohttp.ProxyConnector) with self.assertRaises(ValueError): proxy_connector(None)
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)
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))
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()
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)
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()
def test_fingerprint_fail(self): with fake_socks4_srv(self.loop) as proxy_port: addr = aiosocks.Socks4Addr('127.0.0.1', proxy_port) fp = (b's\x93\xfd:\xed\x08\x1do\xa9\xaeq9' b'\x1a\xe3\xc5\x7f\x89\xe7l\x10') conn = SocksConnector(proxy=addr, proxy_auth=None, loop=self.loop, remote_resolve=False, verify_ssl=False, fingerprint=fp) with http_srv(self.loop, use_ssl=True) as url: with aiohttp.ClientSession(connector=conn, loop=self.loop) as ses: @asyncio.coroutine def make_req(): return (yield from ses.request('get', url=url)) with self.assertRaises(aiohttp.FingerprintMismatch): self.loop.run_until_complete(make_req())
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)
def test_socks4_addr3(): addr = aiosocks.Socks4Addr('localhost', 1) assert addr.host == 'localhost' assert addr.port == 1
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)
def test_socks4_addr1(): with pytest.raises(ValueError): aiosocks.Socks4Addr(None)
def test_socks4_addr4(): addr = aiosocks.Socks4Addr('localhost', None) assert addr.host == 'localhost' assert addr.port == 1080