async def open_session(self, sslc, exit_early=False): async with aiorpcx.Connector(NotificationSession, host=self.host, port=self.port, ssl=sslc, proxy=self.proxy) as session: self.session = session # type: NotificationSession self.session.interface = self self.session.set_default_timeout(self.network.get_network_timeout_seconds(NetworkTimeout.Generic)) try: ver = await session.send_request('server.version', [self.client_name(), version.PROTOCOL_VERSION]) except aiorpcx.jsonrpc.RPCError as e: raise GracefulDisconnect(e) # probably 'unsupported protocol version' if exit_early: return if not self.network.check_interface_against_healthy_spread_of_connected_servers(self): raise GracefulDisconnect(f'too many connected servers already ' f'in bucket {self.bucket_based_on_ipaddress()}') self.logger.info(f"connection established. version: {ver}") try: async with self.group as group: await group.spawn(self.ping) await group.spawn(self.run_fetch_blocks) await group.spawn(self.monitor_connection) except aiorpcx.jsonrpc.RPCError as e: if e.code in (JSONRPC.EXCESSIVE_RESOURCE_USAGE, JSONRPC.SERVER_BUSY): raise GracefulDisconnect(e, log_level=logging.ERROR) from e raise
async def open_session(self, sslc, exit_early=False): async with aiorpcx.Connector(NotificationSession, host=self.host, port=self.port, ssl=sslc, proxy=self.proxy) as session: self.session = session # type: NotificationSession self.session.interface = self self.session.default_timeout = self.network.get_network_timeout_seconds( NetworkTimeout.Generic) try: ver = await session.send_request( 'server.version', [version.ELECTRUM_VERSION, version.PROTOCOL_VERSION]) except aiorpcx.jsonrpc.RPCError as e: raise GracefulDisconnect( e) # probably 'unsupported protocol version' if exit_early: return self.print_error("connection established. version: {}".format(ver)) async with self.group as group: await group.spawn(self.ping) await group.spawn(self.run_fetch_blocks) await group.spawn(self.monitor_connection)
async def open_session(self, sslc, exit_early=False): async with aiorpcx.Connector(NotificationSession, host=self.host, port=self.port, ssl=sslc, proxy=self.proxy) as session: self.session = session # type: NotificationSession self.session.interface = self self.session.default_timeout = self.network.get_network_timeout_seconds( NetworkTimeout.Generic) try: ver = await session.send_request( 'server.version', [self.client_name(), version.PROTOCOL_VERSION]) except aiorpcx.jsonrpc.RPCError as e: raise GracefulDisconnect( e) # probably 'unsupported protocol version' if exit_early: return if not self.network.check_interface_against_healthy_spread_of_connected_servers( self): raise GracefulDisconnect( f'too many connected servers already ' f'in bucket {self.bucket_based_on_ipaddress()}') self.print_error("connection established. version: {}".format(ver)) async with self.group as group: await group.spawn(self.ping) await group.spawn(self.run_fetch_blocks) await group.spawn(self.monitor_connection)
async def get_certificate(self): sslc = ssl.SSLContext() try: async with aiorpcx.Connector(RPCSession, host=self.host, port=self.port, ssl=sslc, proxy=self.proxy) as session: return session.transport._ssl_protocol._sslpipe._sslobj.getpeercert(True) except ValueError: return None
async def gather_info(server): try: host, port = server.split(':') sslc = get_ssl_context() res = {'server': server} async with aiorpcx.Connector(RPCSession, host=host, port=int(port), ssl=sslc) as session: ver = await session.send_request('server.version', [CLIENT_NAME, MIN_PROTO_VERSION]) peers = await session.send_request('server.peers.subscribe') peers = [peer_info_as_dict(p) for p in peers] res.update({'version': ver[0], 'proto': ver[1], 'peers': peers}) return res except aiorpcx.jsonrpc.RPCError: return res except (ConnectionError, TimeoutError): return res except socket.error as e: if e.errno == errno.ECONNREFUSED: return res raise