def __init__( self, # pyre-fixme[11]: Annotation `AbstractNode` is not defined as a type. node: "AbstractNode", endpoint: Optional[IpEndpoint] = None, is_ssl: bool = True, ): AbstractSocketConnectionProtocol.__init__(self, node, endpoint, is_ssl)
def __init__( self, node: "AbstractNode", endpoint: Optional[IpEndpoint] = None, is_ssl: bool = True, ): AbstractSocketConnectionProtocol.__init__(self, node, endpoint, is_ssl) self._buffer_request_time: Optional[float] = None self._buffer_update_time: Optional[float] = None
def _get_socket_peer_info( self, sock: AbstractSocketConnectionProtocol) -> AuthenticatedPeerInfo: assert sock.is_ssl assert self.NODE_TYPE is not None cert = sock.get_peer_certificate() node_type = extensions_factory.get_node_type(cert) try: connection_type = convert.peer_node_to_connection_type( # pyre-fixme[6]: Expected `NodeType` for 1st param but got # `Optional[NodeType]`. self.NODE_TYPE, node_type) except (KeyError, ValueError): raise ConnectionAuthenticationError( f"Peer ssl certificate ({cert}) has an invalid node type: {node_type}!" ) peer_id = extensions_factory.get_node_id(cert) if peer_id is None: raise ConnectionAuthenticationError( f"Peer ssl certificate ({cert}) does not contain a node id!") account_id = extensions_factory.get_account_id(cert) node_privileges = extensions_factory.get_node_privileges(cert) return AuthenticatedPeerInfo(connection_type, peer_id, account_id, node_privileges)
def _initialize_connection( self, socket_connection: AbstractSocketConnectionProtocol ) -> Optional[AbstractConnection]: conn_obj = self.build_connection(socket_connection) ip, port = socket_connection.endpoint if conn_obj is not None: logger.debug("Connecting to: {}...", conn_obj) self.alarm_queue.register_alarm(constants.CONNECTION_TIMEOUT, self._connection_timeout, conn_obj) self.connection_pool.add(socket_connection.file_no, ip, port, conn_obj) if conn_obj.CONNECTION_TYPE == ConnectionType.SDN: # pyre-fixme[16]: `AbstractNode` has no attribute `sdn_connection`. self.sdn_connection = conn_obj else: logger.warning(log_messages.UNABLE_TO_DETERMINE_CONNECTION_TYPE, ip, port) socket_connection.mark_for_close(should_retry=False) return conn_obj
def on_connection_added( self, socket_connection: AbstractSocketConnectionProtocol ) -> Optional[AbstractConnection]: """ Notifies the node that a connection is coming in. """ # If we're already connected to the remote peer, log the event and request disconnect. self.pending_connection_attempts.discard( ConnectionPeerInfo(socket_connection.endpoint, AbstractConnection.CONNECTION_TYPE)) ip, port = socket_connection.endpoint peer_info: Optional[AuthenticatedPeerInfo] = None if socket_connection.is_ssl: try: peer_info = self._get_socket_peer_info(socket_connection) except ConnectionAuthenticationError as e: logger.warning(log_messages.FAILED_TO_AUTHENTICATE_CONNECTION, ip, port, e) socket_connection.mark_for_close(should_retry=False) return None if self.connection_exists(ip, port, peer_info.peer_id)\ and peer_info.connection_type != ConnectionType.RELAY_PROXY: logger.debug( "Duplicate connection attempted to: {}:{} (peer id: {}). " "Dropping.", ip, port, peer_info.peer_id) socket_connection.mark_for_close(should_retry=False) return None elif self.connection_exists(ip, port): logger.debug( "Duplicate connection attempt to {}:{}. Dropping.", ip, port, ) socket_connection.mark_for_close(should_retry=False) return None connection = self._initialize_connection(socket_connection) if connection is None: return None if peer_info is not None: connection.on_connection_authenticated(peer_info) self.connection_pool.index_conn_node_id(peer_info.peer_id, connection) connection.state |= ConnectionState.INITIALIZED logger.debug("Successfully initialized connection: {}", connection) return connection