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)
async def start(self): self.log.debug('connecting to %s:%s' % (self.ip, self.port)) try: if self.proxy is not None: r = await asyncio.wait_for( aiosocks.open_connection(proxy=self.proxy, proxy_auth=None, dst=(self.ip, int(self.port)), remote_resolve=True), self.settings["connect_timeout"]) else: r = await asyncio.wait_for( asyncio.open_connection(self.ip, int(self.port)), self.settings["connect_timeout"]) self.reader, self.writer = r except Exception as err: self.log.debug('connecting filed %s:%s %s' % (self.ip, self.port, err)) return self.tasks.append(self.loop.create_task(self.get_next_message())) self.log.debug('start handshake with %s:%s' % (self.ip, self.port)) await self.send_msg( self.create_message('version', self.create_version())) try: await asyncio.wait_for(self.handshake, timeout=self.settings["handshake_timeout"]) except: self.log.debug('Bitcoin protocol handshake error') if self.handshake.result() == True: self.log.debug('handshake success %s:%s' % (self.ip, self.port)) self.tasks.append(self.loop.create_task(self.ping_pong_task())) await self.send_msg(self.create_message('getaddr', b'')) else: self.writer.close()
def test_open_connection(self, future_mock): addr = aiosocks.Socks5Addr('localhost') auth = aiosocks.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 = fake_coroutine((transp, proto)) fut = fake_coroutine(True) future_mock.side_effect = fut.side_effect conn = aiosocks.open_connection(addr, auth, dst, loop=loop_mock) r, w = self.loop.run_until_complete(conn) self.assertIs(reader, r) self.assertIs(writer, w)