def _on_inbox_packet_received(self, newpacket, info, status, error_message): if newpacket.Command == commands.Identity() and \ newpacket.CreatorID == self.router_idurl and \ newpacket.RemoteID == my_id.getLocalID(): self.automat('router-id-received', (newpacket, info)) self.latest_packet_received = time.time() return True if newpacket.Command == commands.Fail() and \ newpacket.CreatorID == self.router_idurl and \ newpacket.RemoteID == my_id.getLocalID(): self.automat('service-refused', (newpacket, info)) return True if newpacket.CreatorID == self.router_idurl: self.latest_packet_received = time.time() if newpacket.Command != commands.Relay(): return False # if not newpacket.PacketID.startswith('routed_in_'): # return False # if newpacket.RemoteID != my_id.getLocalID(): # return False # if newpacket.CreatorID != self.router_idurl: # return False self.automat('inbox-packet', (newpacket, info, status, error_message)) return True
def _on_inbox_packet_received(self, newpacket, info, status, error_message): if newpacket.Command == commands.Identity() and \ newpacket.CreatorID == self.router_idurl and \ newpacket.RemoteID == my_id.getLocalID(): self.automat('router-id-received', (newpacket, info)) self.latest_packet_received = time.time() return True if newpacket.CreatorID == self.router_idurl: self.latest_packet_received = time.time() if newpacket.Command in [ commands.Relay(), commands.RelayIn(), commands.RelayAck(), commands.RelayFail(), ]: if driver.is_enabled('service_proxy_server'): # TODO: # in case this node already running proxy router service this will not work # actually you can not use proxy transport for receiving and running proxy router at same time # need to change one of the services to solve that dependency and prevent this return False self.automat('inbox-packet', (newpacket, info, status, error_message)) return True return False
def _on_inbox_packet_received(self, newpacket, info, status, error_message): if newpacket.Command == commands.Identity() and \ newpacket.CreatorID == self.router_idurl and \ newpacket.RemoteID == my_id.getLocalID(): self.automat('router-id-received', (newpacket, info)) self.latest_packet_received = time.time() return True # TODO: if this is a response from supplier - this must be skipped here # if newpacket.Command == commands.Fail() and \ # newpacket.CreatorID == self.router_idurl and \ # newpacket.RemoteID == my_id.getLocalID(): # self.automat('service-refused', (newpacket, info)) # return True if newpacket.CreatorID == self.router_idurl: self.latest_packet_received = time.time() if newpacket.Command == commands.Relay(): self.automat('inbox-packet', (newpacket, info, status, error_message)) return True return False
def _on_inbox_packet_received(self, newpacket, info, status, error_message): if _Debug: lg.out( _DebugLevel, 'proxy_router._on_inbox_packet_received %s from %s for %s' % (newpacket, newpacket.CreatorID, newpacket.RemoteID)) if newpacket.RemoteID == my_id.getLocalID(): # this packet was addressed directly to me ... if newpacket.Command == commands.Relay(): # but this is a routed packet addressed to someone else if newpacket.CreatorID in self.routes.keys(): # sent by proxy_sender() from node A : a man behind proxy_router() # addressed to some third node B in outside world - need to route self.automat('routed-outbox-packet-received', (newpacket, info)) return True lg.warn( 'packet %s from %s SKIPPED, because no routes with %s' % (newpacket, newpacket.CreatorID, newpacket.CreatorID)) return False # and this is not a Relay packet if newpacket.Command == commands.Identity( ) and newpacket.CreatorID == newpacket.OwnerID: if newpacket.CreatorID in self.routes.keys(): # this is a "propagate" packet from node A addressed to this proxy # mark that packet as handled and send Ack # otherwise it will be wrongly handled in p2p_service self.automat('known-identity-received', newpacket) return True else: # this node is not yet in routers list, # but seems like it tries to contact me # mark this packet as handled and try to process it self.automat('unknown-identity-received', newpacket) return True # so this packet may be of any kind, but addressed to me # for example if I am a supplier for node A he will send me packets in usual way # need to skip this packet here and process it as a normal inbox packet if _Debug: lg.out( _DebugLevel, 'proxy_router._on_inbox_packet_received %s from %s SKIPEED, addressed to me' % (newpacket, newpacket.CreatorID)) return False # this packet was addressed to someone else receiver_idurl = None if newpacket.Command == commands.Data(): # Data packets may have two cases: a new Data or response with existing Data if info.sender_idurl == newpacket.CreatorID: # incoming new Data created by node B addressed to node A if newpacket.RemoteID in self.routes.keys(): receiver_idurl = newpacket.RemoteID elif info.sender_idurl == newpacket.RemoteID: # response from node B addressed to node A, by request from A who own this Data if newpacket.CreatorID in self.routes.keys(): # a Data packet sent by node B : a man from outside world # addressed to a man behind this proxy_router() - need to route to node A receiver_idurl = newpacket.CreatorID else: # some wired packet received lg.warn('unidentified Data packet received: %s from %s' % (newpacket, info.sender_idurl)) return False else: # other packets (not Data) always should be routed to node A by RemoteID if newpacket.RemoteID in self.routes.keys(): # sent by node B : a man from outside world # addressed to a man behind this proxy - need to route to node A receiver_idurl = newpacket.RemoteID if receiver_idurl is not None: self.automat('routed-inbox-packet-received', (receiver_idurl, newpacket, info)) return True if _Debug: lg.out( _DebugLevel, 'proxy_router._on_inbox_packet_received SKIPPED %s' % newpacket) return False
def doForwardInboxPacket(self, arg): """ Action method. """ # encrypt with proxy_receiver()'s key and sent to man behind my proxy receiver_idurl, newpacket, info = arg route_info = self.routes.get(receiver_idurl, None) if not route_info: lg.warn('route with %s not found for inbox packet: %s' % (receiver_idurl, newpacket)) return hosts = route_info['address'] if len(hosts) == 0: lg.warn( 'route with %s do not have actual info about the host, use identity contacts instead' % receiver_idurl) hosts = route_info['contacts'] if len(hosts) == 0: lg.warn('has no known contacts for route with %s' % receiver_idurl) return receiver_proto, receiver_host = hosts[0] publickey = route_info['publickey'] src = '' src += newpacket.Serialize() block = encrypted.Block( my_id.getLocalID(), 'routed incoming data', 0, key.NewSessionKey(), key.SessionKeyType(), True, src, EncryptKey=lambda inp: key.EncryptOpenSSHPublicKey(publickey, inp)) routed_packet = signed.Packet(commands.Relay(), newpacket.OwnerID, my_id.getLocalID(), newpacket.PacketID, block.Serialize(), receiver_idurl) pout = packet_out.create(newpacket, wide=False, callbacks={}, route={ 'packet': routed_packet, 'proto': receiver_proto, 'host': receiver_host, 'remoteid': receiver_idurl, 'description': ('Relay_%s[%s]_%s' % (newpacket.Command, newpacket.PacketID, nameurl.GetName(receiver_idurl))) }) if _Debug: lg.out( _DebugLevel, '<<<Relay-IN %s %s:%s' % ( str(newpacket), info.proto, info.host, )) lg.out( _DebugLevel, ' sent to %s://%s with %d bytes in %s' % (receiver_proto, receiver_host, len(src), pout)) del src del block del newpacket del routed_packet
def _on_inbox_packet_received(self, newpacket, info, status, error_message): if _Debug: lg.out( _DebugLevel, 'proxy_router._on_inbox_packet_received %s from %s://%s' % ( newpacket, info.proto, info.host, )) lg.out( _DebugLevel, ' creator=%s owner=%s' % ( newpacket.CreatorID, newpacket.OwnerID, )) lg.out( _DebugLevel, ' sender=%s remote_id=%s' % ( info.sender_idurl, newpacket.RemoteID, )) for k, v in self.routes.items(): lg.out( _DebugLevel, ' route with %s : address=%s contacts=%s' % ( k, v.get('address'), v.get('contacts'), )) # first filter all traffic addressed to me if newpacket.RemoteID == my_id.getLocalID(): # check command type, filter Routed traffic first if newpacket.Command == commands.Relay(): # look like this is a routed packet addressed to someone else if newpacket.CreatorID in list(self.routes.keys()): # sent by proxy_sender() from node A : a man behind proxy_router() # addressed to some third node B in outside world - need to route # A is my consumer and B is a recipient which A wants to contant if _Debug: lg.out( _DebugLevel, ' sending "routed-outbox-packet-received" event' ) self.automat('routed-outbox-packet-received', (newpacket, info)) return True # looke like we do not know this guy, so why he is sending us routed traffic? lg.warn( 'unknown %s from %s received, no known routes with %s' % (newpacket, newpacket.CreatorID, newpacket.CreatorID)) self.automat('unknown-packet-received', (newpacket, info)) return True # and this is not a Relay packet, Identity elif newpacket.Command == commands.Identity(): # this is a "propagate" packet from node A addressed to this proxy router if newpacket.CreatorID in list(self.routes.keys()): # also we need to "reset" overriden identity # return False so that other services also can process that Identity() if _Debug: lg.out( _DebugLevel, ' sending "known-identity-received" event') self.automat('known-identity-received', newpacket) return False # this node is not yet in routers list, # but seems like it tries to contact me # return False so that other services also can process that Identity() if _Debug: lg.out( _DebugLevel, ' sending "unknown-identity-received" event') self.automat('unknown-identity-received', newpacket) return False # it can be a RequestService or CancelService packets... # elif newpacket.Command == commands.RequestService(): # self.automat(event_string, *args, **kwargs) # 'request-route-received'.... # so this packet may be of any kind, but addressed to me # for example if I am a supplier for node A he will send me packets in usual way # need to skip this packet here and process it as a normal inbox packet if _Debug: lg.out( _DebugLevel, ' proxy_router() SKIP packet %s from %s addressed to me' % (newpacket, newpacket.CreatorID)) return False # this packet was addressed to someone else # it can be different scenarios, if can not found valid scenario - must skip the packet receiver_idurl = None known_remote_id = newpacket.RemoteID in list(self.routes.keys()) known_creator_id = newpacket.CreatorID in list(self.routes.keys()) known_owner_id = newpacket.OwnerID in list(self.routes.keys()) if known_remote_id: # incoming packet from node B addressed to node A behind that proxy, capture it! receiver_idurl = newpacket.RemoteID if _Debug: lg.out( _DebugLevel, ' proxy_router() ROUTED packet %s from %s to %s' % (newpacket, info.sender_idurl, receiver_idurl)) self.automat('routed-inbox-packet-received', (receiver_idurl, newpacket, info)) return True # uknown RemoteID... # Data() packets may have two cases: a new Data or response with existing Data # in that case RemoteID of the Data packet is not pointing to the real recipient # need to filter this scenario here and do workaround if known_creator_id or known_owner_id: # response from node B addressed to node A, after Retrieve() from A who owns this Data() # a Data packet sent by node B : a man from outside world # addressed to a man behind this proxy_router() - need to route to node A # but who is node A? Creator or Owner? based_on = '' if known_creator_id: receiver_idurl = newpacket.CreatorID based_on = 'creator' else: receiver_idurl = newpacket.OwnerID based_on = 'owner' if _Debug: lg.out( _DebugLevel, ' proxy_router() based on %s ROUTED packet %s from %s to %s' % (based_on, newpacket, info.sender_idurl, receiver_idurl)) self.automat('routed-inbox-packet-received', (receiver_idurl, newpacket, info)) return True # this packet is not related to any of the routes if _Debug: lg.out( _DebugLevel, ' proxy_router() SKIP packet %s from %s : no relations found' % (newpacket, newpacket.CreatorID)) return False
def _do_forward_inbox_packet(self, *args, **kwargs): # encrypt with proxy_receiver()'s key and sent to man behind my proxy receiver_idurl, newpacket, info = args[0] route_info = self.routes.get(receiver_idurl, None) if not route_info: lg.warn('route with %s not found for inbox packet: %s' % (receiver_idurl, newpacket)) return hosts = route_info['address'] if len(hosts) == 0: lg.warn( 'route with %s do not have actual info about the host, use identity contacts instead' % receiver_idurl) hosts = route_info['contacts'] if len(hosts) == 0: lg.warn('has no known contacts for route with %s' % receiver_idurl) return if len(hosts) > 1: lg.warn('found more then one channel with receiver %s : %r' % ( receiver_idurl, hosts, )) receiver_proto, receiver_host = strng.to_bin( hosts[0][0]), strng.to_bin(hosts[0][1]) publickey = route_info['publickey'] block = encrypted.Block( CreatorID=my_id.getLocalID(), BackupID='routed incoming data', BlockNumber=0, SessionKey=key.NewSessionKey(), SessionKeyType=key.SessionKeyType(), LastBlock=True, Data=newpacket.Serialize(), EncryptKey=lambda inp: key.EncryptOpenSSHPublicKey(publickey, inp), ) raw_data = block.Serialize() routed_packet = signed.Packet( commands.Relay(), newpacket.OwnerID, my_id.getLocalID(), newpacket.PacketID, raw_data, receiver_idurl, ) pout = packet_out.create( newpacket, wide=False, callbacks={}, route={ 'packet': routed_packet, 'proto': receiver_proto, 'host': receiver_host, 'remoteid': receiver_idurl, 'description': ('Relay_%s[%s]_%s' % (newpacket.Command, newpacket.PacketID, nameurl.GetName(receiver_idurl))), }, ) if _Debug: lg.out( _DebugLevel, '<<<Relay-IN-OUT %s %s:%s' % ( str(newpacket), info.proto, info.host, )) lg.out( _DebugLevel, ' sent to %s://%s with %d bytes in %s' % (receiver_proto, receiver_host, len(raw_data), pout)) del raw_data del block del newpacket del routed_packet
def _on_first_outbox_packet(self, outpacket, wide, callbacks, target=None, route=None, response_timeout=None, keep_alive=True): """ Will be called first for every outgoing packet. Must to return None if that packet should be send normal way. Otherwise will create another "routerd" packet instead and return it. """ if not driver.is_on('service_proxy_transport'): if _Debug: lg.out( _DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP because service_proxy_transport is not started' ) return None if proxy_receiver.A() and proxy_receiver.A().state != 'LISTEN': if _Debug: lg.out( _DebugLevel, 'proxy_sender._on_first_outbox_packet DELLAYED because proxy_receiver state is not LISTEN yet' ) return self._add_pending_packet(outpacket, wide, callbacks) router_idurl = proxy_receiver.GetRouterIDURL() router_identity_obj = proxy_receiver.GetRouterIdentity() router_proto_host = proxy_receiver.GetRouterProtoHost() router_proto, router_host = router_proto_host publickey = router_identity_obj.publickey my_original_identity_src = proxy_receiver.ReadMyOriginalIdentitySource( ) if not router_idurl or not router_identity_obj or not router_proto_host or not my_original_identity_src: if _Debug: lg.out( _DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP because remote router not ready' ) return self._add_pending_packet(outpacket, wide, callbacks) if outpacket.RemoteID == router_idurl: if _Debug: lg.out( _DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP, packet addressed to router and must be sent in a usual way' ) return None try: raw_data = outpacket.Serialize() except: lg.exc('failed to Serialize %s' % outpacket) return None # see proxy_router.ProxyRouter : doForwardOutboxPacket() for receiving part json_payload = { 'f': my_id.getLocalID(), # from 't': outpacket.RemoteID, # to 'w': wide, # wide 'p': raw_data, # payload } raw_bytes = serialization.DictToBytes(json_payload) block = encrypted.Block( CreatorID=my_id.getLocalID(), BackupID='routed outgoing data', BlockNumber=0, SessionKey=key.NewSessionKey(), SessionKeyType=key.SessionKeyType(), LastBlock=True, Data=raw_bytes, EncryptKey=lambda inp: key.EncryptOpenSSHPublicKey(publickey, inp), ) block_encrypted = block.Serialize() newpacket = signed.Packet( commands.Relay(), outpacket.OwnerID, my_id.getLocalID(), outpacket.PacketID, block_encrypted, router_idurl, ) routed_packet = packet_out.create( outpacket, wide=wide, callbacks=callbacks, route={ 'packet': newpacket, # pointing "newpacket" to another node 'proto': router_proto, 'host': router_host, 'remoteid': router_idurl, 'description': 'Relay_%s[%s]_%s' % (outpacket.Command, outpacket.PacketID, nameurl.GetName(router_idurl)), }, response_timeout=response_timeout, keep_alive=keep_alive, ) self.event('outbox-packet-sent', (outpacket, newpacket, routed_packet)) if _Debug: lg.out(_DebugLevel, '>>>Relay-OUT %s' % str(outpacket)) lg.out( _DebugLevel, ' sent to %s://%s with %d bytes' % (router_proto, router_host, len(block_encrypted))) del raw_bytes del block del newpacket del outpacket del router_identity_obj del router_idurl del router_proto_host return routed_packet
def _do_process_inbox_packet(self, *args, **kwargs): newpacket, info, _, _ = args[0] block = encrypted.Unserialize(newpacket.Payload) if block is None: lg.err('reading data from %s' % newpacket.CreatorID) return try: session_key = key.DecryptLocalPrivateKey(block.EncryptedSessionKey) padded_data = key.DecryptWithSessionKey(session_key, block.EncryptedData, session_key_type=block.SessionKeyType) inpt = BytesIO(padded_data[:int(block.Length)]) data = inpt.read() except: lg.err('reading data from %s' % newpacket.CreatorID) lg.exc() try: inpt.close() except: pass return inpt.close() if newpacket.Command == commands.RelayAck(): try: ack_info = serialization.BytesToDict(data, keys_to_text=True, values_to_text=True) except: lg.exc() return if _Debug: lg.out(_DebugLevel, '<<<Relay-ACK %s:%s from %s://%s with %d bytes %s' % ( ack_info['command'], ack_info['packet_id'], info.proto, info.host, len(data), ack_info['error'], )) if _PacketLogFileEnabled: lg.out(0, ' \033[0;49;33mRELAY ACK %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % ( ack_info['command'], ack_info['packet_id'], info.bytes_received, global_id.UrlToGlobalID(ack_info['from']), global_id.UrlToGlobalID(ack_info['to']), info.transfer_id), log_name='packet', showtime=True) from transport.proxy import proxy_sender if proxy_sender.A(): proxy_sender.A('relay-ack', ack_info, info) return True if newpacket.Command == commands.RelayFail(): try: fail_info = serialization.BytesToDict(data, keys_to_text=True, values_to_text=True) except: lg.exc() return if _Debug: lg.out(_DebugLevel, '<<<Relay-FAIL %s:%s from %s://%s with %d bytes %s' % ( fail_info['command'], fail_info['packet_id'], info.proto, info.host, len(data), fail_info['error'], )) if _PacketLogFileEnabled: lg.out(0, ' \033[0;49;33mRELAY FAIL %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % ( fail_info['command'], fail_info['packet_id'], info.bytes_received, global_id.UrlToGlobalID(fail_info['from']), global_id.UrlToGlobalID(fail_info['to']), info.transfer_id), log_name='packet', showtime=True) from transport.proxy import proxy_sender if proxy_sender.A(): proxy_sender.A('relay-failed', fail_info, info) return True routed_packet = signed.Unserialize(data) if not routed_packet: lg.err('unserialize packet failed from %s' % newpacket.CreatorID) return if _Debug: lg.out(_DebugLevel, '<<<Relay-IN %s from %s://%s with %d bytes' % ( str(routed_packet), info.proto, info.host, len(data))) if _PacketLogFileEnabled: lg.out(0, ' \033[0;49;33mRELAY IN %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % ( routed_packet.Command, routed_packet.PacketID, info.bytes_received, global_id.UrlToGlobalID(info.sender_idurl), global_id.UrlToGlobalID(routed_packet.RemoteID), info.transfer_id), log_name='packet', showtime=True) if routed_packet.Command == commands.Identity(): if _Debug: lg.out(_DebugLevel, ' found identity in relay packet %s' % routed_packet) newidentity = identity.identity(xmlsrc=routed_packet.Payload) idurl = newidentity.getIDURL() if not identitycache.HasKey(idurl): lg.info('received new identity %s rev %r' % (idurl.original(), newidentity.getRevisionValue(), )) if not identitycache.UpdateAfterChecking(idurl, routed_packet.Payload): lg.warn("ERROR has non-Valid identity") return if routed_packet.Command in [commands.Relay(), commands.RelayIn(), ] and routed_packet.PacketID.lower().startswith('identity:'): if _Debug: lg.out(_DebugLevel, ' found routed identity in relay packet %s' % routed_packet) try: routed_identity = signed.Unserialize(routed_packet.Payload) newidentity = identity.identity(xmlsrc=routed_identity.Payload) idurl = newidentity.getIDURL() if not identitycache.HasKey(idurl): lg.warn('received new "routed" identity: %s' % idurl) if not identitycache.UpdateAfterChecking(idurl, routed_identity.Payload): lg.warn("ERROR has non-Valid identity") return except: lg.exc() if newpacket.Command == commands.RelayIn() and routed_packet.Command == commands.Fail(): if routed_packet.Payload == b'route not exist' or routed_packet.Payload == b'route already closed': for pout in packet_out.search_by_packet_id(routed_packet.PacketID): lg.warn('received %r from %r, outgoing packet is failed: %r' % (routed_packet.Payload, newpacket.CreatorID, pout, )) pout.automat('request-failed') return self.traffic_in += len(data) packet_in.process(routed_packet, info) del block del data del padded_data del inpt del session_key del routed_packet
def _on_outbox_packet(self, outpacket, wide, callbacks, target=None, route=None, response_timeout=None, keep_alive=True): """ """ if not driver.is_on('service_proxy_transport'): if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_outbox_packet SKIP because service_proxy_transport is not started') return None if proxy_receiver.A() and proxy_receiver.A().state != 'LISTEN': if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_outbox_packet SKIP because proxy_receiver state is not LISTEN') return self._add_pending_packet(outpacket, wide, callbacks) router_idurl = proxy_receiver.GetRouterIDURL() router_identity_obj = proxy_receiver.GetRouterIdentity() router_proto_host = proxy_receiver.GetRouterProtoHost() router_proto, router_host = router_proto_host publickey = router_identity_obj.publickey my_original_identity_src = proxy_receiver.ReadMyOriginalIdentitySource() if not router_idurl or not router_identity_obj or not router_proto_host or not my_original_identity_src: if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_outbox_packet SKIP because remote router not ready') return self._add_pending_packet(outpacket, wide, callbacks) if outpacket.RemoteID == router_idurl: if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_outbox_packet SKIP, packet addressed to router and must be sent in a usual way') return None try: raw_data = outpacket.Serialize() except: lg.exc('failed to Serialize %s' % outpacket) return None src = '' src += my_id.getLocalID() + '\n' src += outpacket.RemoteID + '\n' src += 'wide\n' if wide else '\n' src += raw_data block = encrypted.Block( my_id.getLocalID(), 'routed outgoing data', 0, key.NewSessionKey(), key.SessionKeyType(), True, src, EncryptKey=lambda inp: key.EncryptOpenSSHPublicKey(publickey, inp)) block_encrypted = block.Serialize() newpacket = signed.Packet( commands.Relay(), outpacket.OwnerID, my_id.getLocalID(), outpacket.PacketID, block_encrypted, router_idurl, ) result_packet = packet_out.create( outpacket, wide=wide, callbacks=callbacks, route={ 'packet': newpacket, 'proto': router_proto, 'host': router_host, 'remoteid': router_idurl, 'description': 'Relay_%s[%s]_%s' % (outpacket.Command, outpacket.PacketID, nameurl.GetName(router_idurl)), }, response_timeout=response_timeout, keep_alive=keep_alive, ) self.event('outbox-packet-sent', (outpacket, newpacket, result_packet)) if _Debug: lg.out(_DebugLevel, '>>>Relay-OUT %s' % str(outpacket)) lg.out(_DebugLevel, ' sent to %s://%s with %d bytes' % ( router_proto, router_host, len(block_encrypted))) del src del block del newpacket del outpacket del router_identity_obj del router_idurl del router_proto_host return result_packet
def _do_process_inbox_packet(self, *args, **kwargs): newpacket, info, _, _ = args[0] block = encrypted.Unserialize(newpacket.Payload) if block is None: lg.err('reading data from %s' % newpacket.CreatorID) return try: session_key = key.DecryptLocalPrivateKey(block.EncryptedSessionKey) padded_data = key.DecryptWithSessionKey(session_key, block.EncryptedData) inpt = BytesIO(padded_data[:int(block.Length)]) data = inpt.read() except: lg.err('reading data from %s' % newpacket.CreatorID) lg.exc() try: inpt.close() except: pass return inpt.close() routed_packet = signed.Unserialize(data) if not routed_packet: lg.err('unserialize packet failed from %s' % newpacket.CreatorID) return if _Debug: lg.out(_DebugLevel, '<<<Relay-IN %s from %s://%s with %d bytes' % ( str(routed_packet), info.proto, info.host, len(data))) if routed_packet.Command == commands.Identity(): if _Debug: lg.out(_DebugLevel, ' found identity in relay packet %s' % routed_packet) newidentity = identity.identity(xmlsrc=routed_packet.Payload) idurl = newidentity.getIDURL() if not identitycache.HasKey(idurl): lg.info('received new identity: %s' % idurl) if not identitycache.UpdateAfterChecking(idurl, routed_packet.Payload): lg.warn("ERROR has non-Valid identity") return if routed_packet.Command == commands.Relay() and routed_packet.PacketID.lower() == 'identity': if _Debug: lg.out(_DebugLevel, ' found routed identity in relay packet %s' % routed_packet) try: routed_identity = signed.Unserialize(routed_packet.Payload) newidentity = identity.identity(xmlsrc=routed_identity.Payload) idurl = newidentity.getIDURL() if not identitycache.HasKey(idurl): lg.warn('received new "routed" identity: %s' % idurl) if not identitycache.UpdateAfterChecking(idurl, routed_identity.Payload): lg.warn("ERROR has non-Valid identity") return except: lg.exc() # if not routed_packet.Valid(): # lg.err('invalid packet %s from %s' % ( # routed_packet, newpacket.CreatorID, )) # return self.traffic_in += len(data) packet_in.process(routed_packet, info) del block del data del padded_data del inpt del session_key del routed_packet
def _on_first_outbox_packet(self, outpacket, wide, callbacks, target=None, route=None, response_timeout=None, keep_alive=True): """ Will be called first for every outgoing packet. Must return `None` if that packet should be send normal way. Otherwise will create another "routed" packet instead and return it. """ if not driver.is_on('service_proxy_transport'): if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP sending %r because service_proxy_transport is not started yet' % outpacket) return None if not proxy_receiver.A(): if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP sending %r because proxy_receiver() not exist' % outpacket) return None if outpacket.Command == commands.Identity() and outpacket.CreatorID == my_id.getLocalID(): if proxy_receiver.GetPossibleRouterIDURL() and proxy_receiver.GetPossibleRouterIDURL().to_bin() == outpacket.RemoteID.to_bin(): if network_connector.A().state is 'DISCONNECTED': if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP sending %r because network_connector() is DISCONNECTED' % outpacket) return None if network_connector.A().state is 'CONNECTED': lg.warn('sending %r to "possible" proxy router %r' % (outpacket, proxy_receiver.GetPossibleRouterIDURL())) pkt_out = packet_out.create(outpacket, wide, callbacks, target, route, response_timeout, keep_alive) return pkt_out if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP sending %r, network_connector() have transition state' % outpacket) return None if proxy_receiver.A().state != 'LISTEN': if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_first_outbox_packet DELLAYED %r because proxy_receiver state is not LISTEN yet' % outpacket) return self._add_pending_packet(outpacket, wide, callbacks) router_idurl = proxy_receiver.GetRouterIDURL() router_identity_obj = proxy_receiver.GetRouterIdentity() router_proto_host = proxy_receiver.GetRouterProtoHost() router_proto, router_host = router_proto_host publickey = router_identity_obj.publickey my_original_identity_src = proxy_receiver.ReadMyOriginalIdentitySource() if not router_idurl or not router_identity_obj or not router_proto_host or not my_original_identity_src: if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP because remote router not ready') return self._add_pending_packet(outpacket, wide, callbacks) if outpacket.RemoteID.to_bin() == router_idurl.to_bin(): if _Debug: lg.out(_DebugLevel, 'proxy_sender._on_first_outbox_packet SKIP, packet addressed to router and must be sent in a usual way') return None try: raw_data = outpacket.Serialize() except: lg.exc('failed to Serialize %s' % outpacket) return None # see proxy_router.ProxyRouter : doForwardOutboxPacket() for receiving part json_payload = { 'f': my_id.getLocalID().to_bin(), # from 't': outpacket.RemoteID.to_bin(), # to 'w': wide, # wide 'p': raw_data, # payload } if not json_payload['t']: raise ValueError('receiver idurl was not set') raw_bytes = serialization.DictToBytes(json_payload) block = encrypted.Block( CreatorID=my_id.getLocalID(), BackupID='routed outgoing data', BlockNumber=0, SessionKey=key.NewSessionKey(session_key_type=key.SessionKeyType()), SessionKeyType=key.SessionKeyType(), LastBlock=True, Data=raw_bytes, EncryptKey=lambda inp: key.EncryptOpenSSHPublicKey(publickey, inp), ) block_encrypted = block.Serialize() newpacket = signed.Packet( Command=commands.Relay(), OwnerID=outpacket.OwnerID, CreatorID=my_id.getLocalID(), PacketID=outpacket.PacketID, Payload=block_encrypted, RemoteID=router_idurl, ) routed_packet = packet_out.create( outpacket, wide=wide, callbacks=callbacks, route={ 'packet': newpacket, # pointing "newpacket" to another node 'proto': router_proto, 'host': router_host, 'remoteid': router_idurl, 'description': 'Relay_%s[%s]_%s' % (outpacket.Command, outpacket.PacketID, nameurl.GetName(router_idurl)), }, response_timeout=response_timeout, keep_alive=keep_alive, ) self.event('outbox-packet-sent', (outpacket, newpacket, routed_packet)) if _Debug: lg.out(_DebugLevel, '>>>Relay-OUT %s' % str(outpacket)) lg.out(_DebugLevel, ' sent to %s://%s with %d bytes' % ( router_proto, router_host, len(block_encrypted))) if _PacketLogFileEnabled: lg.out(0, '\033[0;49;36mRELAY OUT %s(%s) with %s bytes from %s to %s via %s\033[0m' % ( outpacket.Command, outpacket.PacketID, len(raw_bytes), global_id.UrlToGlobalID(outpacket.CreatorID), global_id.UrlToGlobalID(outpacket.RemoteID), global_id.UrlToGlobalID(router_idurl), ), log_name='packet', showtime=True,) del raw_bytes del block del newpacket del outpacket del router_identity_obj del router_idurl del router_proto_host return routed_packet
def _do_process_inbox_packet(self, *args, **kwargs): newpacket, info, _, _ = args[0] block = encrypted.Unserialize(newpacket.Payload) if block is None: lg.err('reading data from %s' % newpacket.CreatorID) return try: session_key = key.DecryptLocalPrivateKey(block.EncryptedSessionKey) padded_data = key.DecryptWithSessionKey( session_key, block.EncryptedData, session_key_type=block.SessionKeyType) inpt = BytesIO(padded_data[:int(block.Length)]) data = inpt.read() except: lg.err('reading data from %s' % newpacket.CreatorID) lg.exc() try: inpt.close() except: pass return inpt.close() routed_packet = signed.Unserialize(data) if not routed_packet: lg.err('unserialize packet failed from %s' % newpacket.CreatorID) return if _Debug: lg.out( _DebugLevel, '<<<Relay-IN %s from %s://%s with %d bytes' % (str(routed_packet), info.proto, info.host, len(data))) if _PacketLogFileEnabled: lg.out( 0, ' \033[0;49;33mRELAY IN %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % (routed_packet.Command, routed_packet.PacketID, info.bytes_received, global_id.UrlToGlobalID(info.sender_idurl), global_id.UrlToGlobalID( routed_packet.RemoteID), info.transfer_id), log_name='packet', showtime=True) if routed_packet.Command == commands.Identity(): if _Debug: lg.out(_DebugLevel, ' found identity in relay packet %s' % routed_packet) newidentity = identity.identity(xmlsrc=routed_packet.Payload) idurl = newidentity.getIDURL() if not identitycache.HasKey(idurl): lg.info('received new identity %s rev %r' % ( idurl.original(), newidentity.getRevisionValue(), )) if not identitycache.UpdateAfterChecking(idurl, routed_packet.Payload): lg.warn("ERROR has non-Valid identity") return if routed_packet.Command == commands.Relay( ) and routed_packet.PacketID.lower().startswith('identity:'): if _Debug: lg.out( _DebugLevel, ' found routed identity in relay packet %s' % routed_packet) try: routed_identity = signed.Unserialize(routed_packet.Payload) newidentity = identity.identity(xmlsrc=routed_identity.Payload) idurl = newidentity.getIDURL() if not identitycache.HasKey(idurl): lg.warn('received new "routed" identity: %s' % idurl) if not identitycache.UpdateAfterChecking( idurl, routed_identity.Payload): lg.warn("ERROR has non-Valid identity") return except: lg.exc() # if not routed_packet.Valid(): # lg.err('invalid packet %s from %s' % ( # routed_packet, newpacket.CreatorID, )) # return self.traffic_in += len(data) packet_in.process(routed_packet, info) del block del data del padded_data del inpt del session_key del routed_packet