async def start_server(self, on_connect: Callable = None): if self._local_type in [NodeType.WALLET, NodeType.HARVESTER, NodeType.TIMELORD]: return None self.app = web.Application() self.on_connect = on_connect routes = [ web.get("/ws", self.incoming_connection), ] self.app.add_routes(routes) self.runner = web.AppRunner(self.app, access_log=None, logger=self.log) await self.runner.setup() authenticate = self._local_type not in (NodeType.FULL_NODE, NodeType.INTRODUCER) if authenticate: ssl_context = ssl_context_for_server( self.ca_private_crt_path, self.ca_private_key_path, self._private_cert_path, self._private_key_path ) else: self.p2p_crt_path, self.p2p_key_path = public_ssl_paths(self.root_path, self.config) ssl_context = ssl_context_for_server( self.chia_ca_crt_path, self.chia_ca_key_path, self.p2p_crt_path, self.p2p_key_path ) self.site = web.TCPSite( self.runner, port=self._port, shutdown_timeout=3, ssl_context=ssl_context, ) await self.site.start() self.log.info(f"Started listening on port: {self._port}")
def __init__( self, port: int, node: Any, api: Any, local_type: NodeType, ping_interval: int, network_id: str, inbound_rate_limit_percent: int, outbound_rate_limit_percent: int, root_path: Path, config: Dict, private_ca_crt_key: Tuple[Path, Path], chia_ca_crt_key: Tuple[Path, Path], name: str = None, introducer_peers: Optional[IntroducerPeers] = None, ): # Keeps track of all connections to and from this node. logging.basicConfig(level=logging.DEBUG) self.all_connections: Dict[bytes32, WSChiaConnection] = {} self.tasks: Set[asyncio.Task] = set() self.connection_by_type: Dict[NodeType, Dict[bytes32, WSChiaConnection]] = { NodeType.FULL_NODE: {}, NodeType.WALLET: {}, NodeType.HARVESTER: {}, NodeType.FARMER: {}, NodeType.TIMELORD: {}, NodeType.INTRODUCER: {}, } self._port = port # TCP port to identify our node self._local_type: NodeType = local_type self._ping_interval = ping_interval self._network_id = network_id self._inbound_rate_limit_percent = inbound_rate_limit_percent self._outbound_rate_limit_percent = outbound_rate_limit_percent # Task list to keep references to tasks, so they don't get GCd self._tasks: List[asyncio.Task] = [] self.log = logging.getLogger(name if name else __name__) # Our unique random node id that we will send to other peers, regenerated on launch self.api = api self.node = node self.root_path = root_path self.config = config self.on_connect: Optional[Callable] = None self.incoming_messages: asyncio.Queue = asyncio.Queue() self.shut_down_event = asyncio.Event() if self._local_type is NodeType.INTRODUCER: self.introducer_peers = IntroducerPeers() if self._local_type is not NodeType.INTRODUCER: self._private_cert_path, self._private_key_path = private_ssl_paths( root_path, config) if self._local_type is not NodeType.HARVESTER: self.p2p_crt_path, self.p2p_key_path = public_ssl_paths( root_path, config) else: self.p2p_crt_path, self.p2p_key_path = None, None self.ca_private_crt_path, self.ca_private_key_path = private_ca_crt_key self.chia_ca_crt_path, self.chia_ca_key_path = chia_ca_crt_key self.node_id = self.my_id() self.incoming_task = asyncio.create_task(self.incoming_api_task()) self.gc_task: asyncio.Task = asyncio.create_task( self.garbage_collect_connections_task()) self.app: Optional[Application] = None self.runner: Optional[web.AppRunner] = None self.site: Optional[TCPSite] = None self.connection_close_task: Optional[asyncio.Task] = None self.site_shutdown_task: Optional[asyncio.Task] = None self.app_shut_down_task: Optional[asyncio.Task] = None self.received_message_callback: Optional[Callable] = None self.api_tasks: Dict[bytes32, asyncio.Task] = {} self.execute_tasks: Set[bytes32] = set() self.tasks_from_peer: Dict[bytes32, Set[bytes32]] = {} self.banned_peers: Dict[str, float] = {} self.invalid_protocol_ban_seconds = 10 self.api_exception_ban_seconds = 10 self.exempt_peer_networks: List[Union[IPv4Network, IPv6Network]] = [ ip_network(net, strict=False) for net in config.get("exempt_peer_networks", []) ]