def local_address(): local_ip = socket.gethostbyname('localhost') local_port = 56789 yield PeerAddress( network=Network.IPV4, host=local_ip, port=local_port, )
def gen_squeak_peer(peer_name): host = "random_host_{}".format(uuid.uuid1()) port = random.randint(1, 10000) peer_address = PeerAddress( network=Network.IPV4, host=host, port=port, ) return create_saved_peer( peer_name, peer_address, )
def handle_get_offer(self, squeak_hash_str, client_host, client_port) -> Offer: squeak_hash = bytes.fromhex(squeak_hash_str) client_addr = PeerAddress( network=Network.IPV4, host=client_host, port=client_port, ) offer = self.squeak_controller.get_packaged_offer( squeak_hash, client_addr, ) if not offer: raise NotFoundError() return offer
def create_saved_peer( peer_name: str, peer_address: PeerAddress, default_peer_port: int, ) -> SqueakPeer: validate_saved_peer_name(peer_name) if peer_address.port == 0: peer_address = peer_address._replace(port=default_peer_port, ) return SqueakPeer( peer_id=None, peer_name=peer_name, address=peer_address, autoconnect=True, share_for_free=False, )
def accept_connections(self): try: self.listen_socket.bind(('', self.port)) self.listen_socket.listen() while True: peer_socket, address = self.listen_socket.accept() host, port = address peer_address = PeerAddress( network=Network.IPV4, host=host, port=port, ) peer_socket.setblocking(True) self.handle_connection( peer_socket, peer_address, ) except Exception: logger.exception("Accept peer connections failed.")
def peer_address_with_zero(): return PeerAddress( network=Network.IPV4, host="fake_host", port=0, )
def peer_address(): return PeerAddress( network=Network.IPV4, host="fake_host", port=5678, )
def peer_address_with_no_port(): yield PeerAddress( network=Network.IPV4, host="fake_host", port=0, )
def peer_address_with_tor(): yield PeerAddress( network=Network.TORV3, host="fake_host", port=1234, )
def peer_address(): yield PeerAddress( network=Network.IPV4, host="fake_host", port=8765, )
def message_to_peer_address(msg: squeak_admin_pb2.PeerAddress) -> PeerAddress: return PeerAddress( network=Network[msg.network], host=msg.host, port=msg.port, )
def get_external_address(self) -> PeerAddress: return PeerAddress( network=Network.IPV4, host=self.config.server.external_address or '', port=self.config.server.external_port or 0, )
def outbound_local_address(): yield PeerAddress( network=Network.IPV4, host='outbound.com', port=4321, )
def inbound_local_address(): yield PeerAddress( network=Network.IPV4, host='inbound.com', port=56789, )
def external_address(self) -> PeerAddress: return PeerAddress( network=Network.IPV4, host=self.external_host or self.local_ip, port=self.local_port, )