def _open_one(self): best_peer = self.get_best_peer() if best_peer is not None: host = best_peer.host port = best_peer.port print('trying', ipaddress.ip_address(host), port) best_peer.set_pending() c = PacketConnection(net.Connection(net.ipv6(host), port)) conn = Connection2(om=self.om, peers=self, connection=c) self._connections.append(conn) conn.on_connect.append(lambda: self._on_connect(host)) conn.on_disconnect.append(lambda: self._on_disconnect(host)) asyncio.get_event_loop().create_task(conn.run())
def run(self): if self._core.get_config('network.proxy') == 'tor': if self._core.get_config('network.proxy') == 'tor': host = self._core.get_config('network.tor_host') port = self._core.get_config('network.tor_port') socks.set_default_proxy(socks.PROXY_TYPE_SOCKS5, host, port, True) socket.socket = socks.socksocket elif self._core.get_config('network.proxy') == 'disabled': asyncio.get_event_loop().create_task(self._run2()) if self._core.get_config('network.proxy') == 'trusted': while True: host = self._core.get_config('network.trusted_host') port = self._core.get_config('network.trusted_port') print('trying', host, port) self._trusted_status = 1 c = PacketConnection(net.Connection(net.ipv6(host), port)) conn = Connection2(om=self.om, peers=self, connection=c) self._connections.append(conn) def on_connect(): self._trusted_status = 2 for func in self.on_stats_changed: func() conn.on_connect.append(on_connect) yield from conn.run() self._trusted_status = 0 for func in self.on_stats_changed: func() yield from asyncio.sleep(10) else: while True: if self.count_connected() < config.CONNECTION_COUNT\ and self.count_pending_and_connected() < self.count_all(): self._open_one() while self.count_pending_and_connected() < config.CONNECTION_COUNT\ and self.count_pending_and_connected() < self.count_all(): self._open_one() yield from asyncio.sleep(10)