def drop_connection(self, protocol: HathorProtocol) -> None: """ Drop a connection """ assert protocol.peer is not None self.log.debug('dropping connection', peer_id=protocol.peer.id, protocol=type(protocol).__name__) protocol.send_error_and_close_connection('Connection droped')
def drop_duplicate_connection(self, protocol: HathorProtocol) -> None: """ Drop a connection """ assert protocol.peer is not None self.log.debug('dropping connection {peer_id} {protocol}', peer_id=protocol.peer.id, protocol=protocol) protocol.send_error_and_close_connection( 'Connection already established')
def on_peer_connect(self, protocol: HathorProtocol) -> None: """Called when a new connection is established.""" if len(self.connections) >= self.max_connections: self.log.warn('reached maximum number of connections', max_connections=self.max_connections) protocol.disconnect(force=True) return self.connections.add(protocol) self.handshaking_peers.add(protocol)
def test_validate_certificate(self): peer = PeerId('testnet') protocol = HathorProtocol('testnet', peer, None, node=None, use_ssl=True) class FakeTransport: def getPeerCertificate(self): from OpenSSL import crypto # we use a new peer here just to save the trouble of manually creating a certificate random_peer = PeerId('testnet') return crypto.X509.from_cryptography( random_peer.get_certificate()) protocol.transport = FakeTransport() result = peer.validate_certificate(protocol) self.assertFalse(result)
def test_validate_entrypoint(self): manager = self.create_peer('testnet', unlock_wallet=False) peer_id = manager.my_peer peer_id.entrypoints = ['tcp://127.0.0.1:40403'] # we consider that we are starting the connection to the peer protocol = HathorProtocol('testnet', peer_id, None, node=manager, use_ssl=True, inbound=False) protocol.connection_string = 'tcp://127.0.0.1:40403' result = yield peer_id.validate_entrypoint(protocol) self.assertTrue(result) # if entrypoint is an URI peer_id.entrypoints = ['uri_name'] result = yield peer_id.validate_entrypoint(protocol) self.assertTrue(result) # test invalid. DNS in test mode will resolve to '127.0.0.1:40403' protocol.connection_string = 'tcp://45.45.45.45:40403' result = yield peer_id.validate_entrypoint(protocol) self.assertFalse(result) # now test when receiving the connection - i.e. the peer starts it protocol.connection_string = None peer_id.entrypoints = ['tcp://127.0.0.1:40403'] class FakeTransport: def getPeer(self): from collections import namedtuple Peer = namedtuple('Peer', 'host') return Peer(host='127.0.0.1') protocol.transport = FakeTransport() result = yield peer_id.validate_entrypoint(protocol) self.assertTrue(result) # if entrypoint is an URI peer_id.entrypoints = ['uri_name'] result = yield peer_id.validate_entrypoint(protocol) self.assertTrue(result)