Esempio n. 1
0
 def _do_send_identity_to_router(self, identity_source, failed_event):
     try:
         identity_obj = identity.identity(xmlsrc=identity_source)
     except:
         lg.exc()
         return
     if _Debug:
         lg.out(_DebugLevel, 'proxy_receiver._do_send_identity_to_router to %s' % self.router_idurl)
         lg.out(_DebugLevel, '        contacts=%r, sources=%r' % (
             identity_obj.contacts, identity_obj.getSources(as_originals=True)))
     newpacket = signed.Packet(
         Command=commands.Identity(),
         OwnerID=my_id.getIDURL(),
         CreatorID=my_id.getIDURL(),
         PacketID=('proxy_receiver:%s' % packetid.UniqueID()),
         Payload=identity_obj.serialize(),
         RemoteID=self.router_idurl,
     )
     packet_out.create(
         newpacket,
         wide=True,
         callbacks={
             commands.Ack(): lambda response, info: self.automat('ack-received', (response, info)),
             commands.Fail(): lambda x: self.automat(failed_event),
             None: lambda pkt_out: self.automat('ack-timeout', pkt_out),
             'failed': lambda pkt_out, error_message: self.automat('sending-failed', (pkt_out, error_message)),
         },
         keep_alive=True,
         response_timeout=30,
     )
Esempio n. 2
0
 def _do_send_identity_to_router(self, identity_source, failed_event):
     try:
         identity_obj = identity.identity(xmlsrc=identity_source)
     except:
         lg.exc()
         return
     if _Debug:
         lg.out(_DebugLevel, 'proxy_receiver.doSendMyIdentity to %s' % self.router_idurl)
         lg.out(_DebugLevel, '        contacts=%s, sources=%s' % (identity_obj.contacts, identity_obj.sources))
     newpacket = signed.Packet(
         commands.Identity(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         commands.Identity(),
         identity_source,
         self.router_idurl,
     )
     packet_out.create(
         newpacket,
         wide=True,
         callbacks={
             commands.Ack(): lambda response, info: self.automat('ack-received', (response, info)),
             commands.Fail(): lambda x: self.automat(failed_event),
         },
         keep_alive=True,
     )
Esempio n. 3
0
 def _do_send_request_service(self, *args, **kwargs):
     if len(self.request_service_packet_id) >= 10:
         if _Debug:
             lg.warn('too many service requests to %r' % self.router_idurl)
         self.automat('service-refused', *args, **kwargs)
         return
     orig_identity = config.conf().getData('services/proxy-transport/my-original-identity').strip()
     if not orig_identity:
         orig_identity = my_id.getLocalIdentity().serialize(as_text=True)
     service_info = {
         'name': 'service_proxy_server',
         'payload': {
             'identity': orig_identity,
         },
     }
     newpacket = signed.Packet(
         commands.RequestService(),
         my_id.getIDURL(),
         my_id.getIDURL(),
         packetid.UniqueID(),
         serialization.DictToBytes(service_info, values_to_text=True),
         self.router_idurl,
     )
     packet_out.create(
         newpacket,
         wide=False,
         callbacks={
             commands.Ack(): self._on_request_service_ack,
             commands.Fail(): self._on_request_service_fail,
             None: lambda pkt_out: self.automat('request-timeout', pkt_out),
         },
         response_timeout=30,
     )
     self.request_service_packet_id.append(newpacket.PacketID)
Esempio n. 4
0
 def _do_send_request_service(self, *args, **kwargs):
     if len(self.request_service_packet_id) >= 3:
         if _Debug:
             lg.warn('too many service requests to %r' % self.router_idurl)
         self.automat('service-refused', *args, **kwargs)
         return
     orig_identity = config.conf().getData('services/proxy-transport/my-original-identity').strip()
     if not orig_identity:
         orig_identity = my_id.getLocalIdentity().serialize()
     service_info = {
         'name': 'service_proxy_server',
         'payload': {
             'identity': strng.to_text(orig_identity),
         },
     }
     service_info_raw = json.dumps(service_info)
     newpacket = signed.Packet(
         commands.RequestService(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         packetid.UniqueID(),
         service_info_raw,
         self.router_idurl,
     )
     packet_out.create(newpacket, wide=False, callbacks={
         commands.Ack(): self._on_request_service_ack,
         commands.Fail(): self._on_request_service_fail,
     },)
     self.request_service_packet_id.append(newpacket.PacketID)
Esempio n. 5
0
 def doSendRequestService(self, arg):
     """
     Action method.
     """
     if len(self.request_service_packet_id) >= 3:
         if _Debug:
             lg.warn('too many service requests to %s' % self.router_idurl)
         self.automat('service-refused', arg)
         return
     service_info = 'service_proxy_server \n'
     orig_identity = config.conf().getData('services/proxy-transport/my-original-identity').strip()
     if not orig_identity:
         orig_identity = my_id.getLocalIdentity().serialize()
     service_info += orig_identity
     # for t in gateway.transports().values():
     #     service_info += '%s://%s' % (t.proto, t.host)
     # service_info += ' '
     newpacket = signed.Packet(
         commands.RequestService(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         packetid.UniqueID(),
         service_info,
         self.router_idurl,)
     packet_out.create(newpacket, wide=False, callbacks={
         commands.Ack(): self._on_request_service_ack,
         commands.Fail(): self._on_request_service_fail},)
     self.request_service_packet_id.append(newpacket.PacketID)
Esempio n. 6
0
 def doSendRequestService(self, arg):
     """
     Action method.
     """
     if len(self.request_service_packet_id) >= 3:
         if _Debug:
             lg.warn('too many service requests to %s' % self.router_idurl)
         self.automat('service-refused', arg)
         return
     service_info = 'service_proxy_server \n'
     orig_identity = config.conf().getData(
         'services/proxy-transport/my-original-identity').strip()
     if not orig_identity:
         orig_identity = my_id.getLocalIdentity().serialize()
     service_info += orig_identity
     # for t in gateway.transports().values():
     #     service_info += '%s://%s' % (t.proto, t.host)
     # service_info += ' '
     newpacket = signed.Packet(
         commands.RequestService(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         packetid.UniqueID(),
         service_info,
         self.router_idurl,
     )
     packet_out.create(
         newpacket,
         wide=False,
         callbacks={
             commands.Ack(): self._on_request_service_ack,
             commands.Fail(): self._on_request_service_fail
         },
     )
     self.request_service_packet_id.append(newpacket.PacketID)
Esempio n. 7
0
 def doSendMyIdentity(self, *args, **kwargs):
     """
     Action method.
     """
     global _KnownChannels
     self.ping_attempts += 1
     if self.fake_identity:
         identity_object = self.fake_identity
     else:
         identity_object = my_id.getLocalIdentity()
     if not identity_object.Valid():
         raise Exception('can not use invalid identity for ping')
     if self.channel_counter:
         packet_id = '%s:%d:%d:%s' % (self.channel, _KnownChannels[self.channel], self.ping_attempts, packetid.UniqueID())
     else:
         packet_id = '%s:%d:%s' % (self.channel, self.ping_attempts, packetid.UniqueID())
     ping_packet = signed.Packet(
         Command=commands.Identity(),
         OwnerID=my_id.getIDURL(),
         CreatorID=my_id.getIDURL(),
         PacketID=packet_id,
         Payload=strng.to_bin(identity_object.serialize()),
         RemoteID=self.remote_idurl,
     )
     if self.skip_outbox:
         packet_out.create(
             outpacket=ping_packet,
             wide=True,
             response_timeout=self.ack_timeout,
             callbacks={
                 commands.Ack(): lambda response, info: self.automat('ack-received', response=response, info=info),
                 commands.Fail(): lambda response, info: self.automat('fail-received', response=response, info=info),
                 None: lambda pkt_out: self.automat('ack-timeout', pkt_out),
             },
             keep_alive=self.keep_alive,
         )
     else:
         gateway.outbox(
             outpacket=ping_packet,
             wide=True,
             response_timeout=self.ack_timeout,
             callbacks={
                 commands.Ack(): lambda response, info: self.automat('ack-received', response=response, info=info),
                 commands.Fail(): lambda response, info: self.automat('fail-received', response=response, info=info),
                 None: lambda pkt_out: self.automat('ack-timeout', pkt_out),
             },
             keep_alive=self.keep_alive,
         )
     if _Debug:
         lg.args(_DebugLevel, packet_id=packet_id, remote_idurl=self.remote_idurl, ping_attempts=self.ping_attempts)
Esempio n. 8
0
 def doSendCancelService(self, arg):
     """
     Action method.
     """
     newpacket = signed.Packet(
         commands.CancelService(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         packetid.UniqueID(),
         'service_proxy_server',
         self.router_idurl,)
     packet_out.create(newpacket, wide=True, callbacks={
         commands.Ack(): self._on_request_service_ack,
         commands.Fail(): self._on_request_service_fail},)
Esempio n. 9
0
 def doSendCancelService(self, *args, **kwargs):
     """
     Action method.
     """
     newpacket = signed.Packet(
         commands.CancelService(),
         my_id.getIDURL(),
         my_id.getIDURL(),
         packetid.UniqueID(),
         serialization.DictToBytes({'name': 'service_proxy_server', }),
         self.router_idurl,
     )
     packet_out.create(newpacket, wide=True, callbacks={
         commands.Ack(): self._on_request_service_ack,
         commands.Fail(): self._on_request_service_fail,
     })
Esempio n. 10
0
def on_outbox_packet(outpacket,
                     wide,
                     callbacks,
                     target=None,
                     route=None,
                     response_timeout=None,
                     keep_alive=True):
    """
    """
    started_packets = packet_out.search_similar_packets(outpacket)
    if started_packets:
        for active_packet, active_item in started_packets:
            if callbacks:
                for command, cb in callbacks.items():
                    active_packet.set_callback(command, cb)
            lg.warn(
                'skip creating new outbox packet because found similar packet: %r'
                % active_packet)
            return active_packet
    pkt_out = packet_out.create(outpacket, wide, callbacks, target, route,
                                response_timeout, keep_alive)
    # if _Debug and lg.is_debug(_DebugLevel):
    #     monitoring()
    control.request_update([('packet', outpacket.PacketID)])
    return pkt_out
Esempio n. 11
0
 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,
         EncryptFunc=lambda inp: key.EncryptStringPK(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
Esempio n. 12
0
 def doSendCancelService(self, arg):
     """
     Action method.
     """
     service_info = {
         'name': 'service_proxy_server',
     }
     service_info_raw = json.dumps(service_info)
     newpacket = signed.Packet(
         commands.CancelService(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         packetid.UniqueID(),
         service_info_raw,
         self.router_idurl, )
     packet_out.create(newpacket, wide=True, callbacks={
         commands.Ack(): self._on_request_service_ack,
         commands.Fail(): self._on_request_service_fail,
     },)
Esempio n. 13
0
 def doForwardOutboxPacket(self, arg):
     """
     Action method.
     """
     # decrypt with my key and send to outside world
     newpacket, info = arg
     block = encrypted.Unserialize(newpacket.Payload)
     if block is None:
         lg.out(2, 'proxy_router.doForwardOutboxPacket ERROR reading data from %s' % newpacket.RemoteID)
         return
     try:
         session_key = key.DecryptLocalPK(block.EncryptedSessionKey)
         padded_data = key.DecryptWithSessionKey(session_key, block.EncryptedData)
         inpt = cStringIO.StringIO(padded_data[:int(block.Length)])
         sender_idurl = inpt.readline().rstrip('\n')
         receiver_idurl = inpt.readline().rstrip('\n')
         wide = inpt.readline().rstrip('\n')
         wide = wide == 'wide'
     except:
         lg.out(2, 'proxy_router.doForwardOutboxPacket ERROR reading data from %s' % newpacket.RemoteID)
         lg.exc()
         try:
             inpt.close()
         except:
             pass
         return
     route = self.routes.get(sender_idurl, None)
     if not route:
         inpt.close()
         lg.warn('route with %s not found' % (sender_idurl))
         p2p_service.SendFail(newpacket, 'route not exist', remote_idurl=sender_idurl)
         return
     data = inpt.read()
     inpt.close()
     routed_packet = signed.Unserialize(data)
     if not routed_packet:
         lg.out(2, 'proxy_router.doForwardOutboxPacket ERROR unserialize packet from %s' % newpacket.RemoteID)
         return
     # send the packet directly to target
     pout = packet_out.create(routed_packet, wide=wide, callbacks={}, target=receiver_idurl,)
     # gateway.outbox(routed_packet, wide=wide)
     if _Debug:
         lg.out(_DebugLevel, '>>>Relay-OUT %d bytes from %s at %s://%s :' % (
             len(data), nameurl.GetName(sender_idurl), info.proto, info.host,))
         lg.out(_DebugLevel, '    routed to %s : %s' % (nameurl.GetName(receiver_idurl), pout))
     del block
     del data
     del padded_data
     del route
     del inpt
     del session_key
     del routed_packet
Esempio n. 14
0
 def doSendMyIdentity(self, arg):
     """
     Action method.
     """
     identity_source = config.conf().getData('services/proxy-transport/my-original-identity').strip()
     if identity_source:
         if _Debug:
             lg.out(_DebugLevel, 'proxy_receiver.doSendMyIdentity use my previously stored identity')
     else:
         identity_source = my_id.getLocalIdentity().serialize()
         cur_contacts = my_id.getLocalIdentity().getContacts()
         if _Debug:
             lg.out(_DebugLevel, 'proxy_receiver.doSendMyIdentity using my current identity, contacts=%s' % cur_contacts)
     newpacket = signed.Packet(
         commands.Identity(), my_id.getLocalID(),
         my_id.getLocalID(), 'identity',
         identity_source, self.router_idurl,
     )
     packet_out.create(newpacket, wide=True, callbacks={
         commands.Ack(): lambda response, info: self.automat('ack-received', (response, info)),
         commands.Fail(): lambda x: self.automat('nodes-not-found')
     })
Esempio n. 15
0
def on_outbox_packet(outpacket, wide, callbacks, target=None, route=None):
    """
    """
    started_packets = packet_out.search_similar_packets(outpacket)
    if started_packets:
        for active_packet, active_item in started_packets:
            if callbacks:
                for command, cb in callbacks.items():
                    active_packet.set_callback(command, cb)
            return active_packet
    pkt_out = packet_out.create(outpacket, wide, callbacks, target, route)
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
    control.request_update([('packet', outpacket.PacketID)])
    return pkt_out
Esempio n. 16
0
def on_outbox_packet(outpacket, wide, callbacks, target=None, route=None):
    """
    
    """
    started_packets = packet_out.search_similar_packets(outpacket)
    if started_packets:
        for active_packet, active_item in started_packets:
            if callbacks:
                for command, cb in callbacks.items():
                    active_packet.set_callback(command, cb)
            return active_packet
    pkt_out = packet_out.create(outpacket, wide, callbacks, target, route)
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
    control.request_update([("packet", outpacket.PacketID)])
    return pkt_out
Esempio n. 17
0
 def _do_forward_outbox_packet(self, outpacket_info_tuple):
     """
     This packet addressed to me but contain routed data to be transferred to another node.
     I will decrypt with my private key and send to outside world further.
     """
     newpacket, info = outpacket_info_tuple
     block = encrypted.Unserialize(newpacket.Payload)
     if block is None:
         lg.out(
             2,
             'proxy_router.doForwardOutboxPacket ERROR reading data from %s'
             % newpacket.RemoteID)
         return
     try:
         session_key = key.DecryptLocalPrivateKey(block.EncryptedSessionKey)
         padded_data = key.DecryptWithSessionKey(session_key,
                                                 block.EncryptedData)
         inpt = BytesIO(padded_data[:int(block.Length)])
         # see proxy_sender.ProxySender : _on_first_outbox_packet() for sending part
         json_payload = serialization.BytesToDict(inpt.read(),
                                                  keys_to_text=True)
         inpt.close()
         sender_idurl = json_payload['f']  # from
         receiver_idurl = json_payload['t']  # to
         wide = json_payload['w']  # wide
         routed_data = json_payload['p']  # payload
     except:
         lg.out(
             2,
             'proxy_router.doForwardOutboxPacket ERROR reading data from %s'
             % newpacket.RemoteID)
         lg.exc()
         try:
             inpt.close()
         except:
             pass
         return
     route = self.routes.get(sender_idurl, None)
     if not route:
         inpt.close()
         lg.warn('route with %s not found' % (sender_idurl))
         p2p_service.SendFail(newpacket,
                              'route not exist',
                              remote_idurl=sender_idurl)
         return
     routed_packet = signed.Unserialize(routed_data)
     if not routed_packet or not routed_packet.Valid():
         lg.out(
             2,
             'proxy_router.doForwardOutboxPacket ERROR unserialize packet from %s'
             % newpacket.RemoteID)
         return
     # send the packet directly to target user_id
     # we pass not callbacks because all response packets from this call will be also re-routed
     pout = packet_out.create(
         routed_packet,
         wide=wide,
         callbacks={},
         target=receiver_idurl,
     )
     if _Debug:
         lg.out(
             _DebugLevel,
             '>>>Relay-IN-OUT %d bytes from %s at %s://%s :' % (
                 len(routed_data),
                 nameurl.GetName(sender_idurl),
                 info.proto,
                 info.host,
             ))
         lg.out(
             _DebugLevel, '    routed to %s : %s' %
             (nameurl.GetName(receiver_idurl), pout))
     del block
     del routed_data
     del padded_data
     del route
     del inpt
     del session_key
     del routed_packet
Esempio n. 18
0
 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
Esempio n. 19
0
 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
Esempio n. 20
0
 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
Esempio n. 21
0
 def _on_outbox_packet(self, outpacket, wide, callbacks, target=None, route=None):
     """
     
     """
     if not driver.is_started('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':
         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:
         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
     src = ''
     src += my_id.getLocalID() + '\n'
     src += outpacket.RemoteID + '\n'
     src += 'wide\n' if wide else '\n'
     src += outpacket.Serialize()
     block = encrypted.Block(
         my_id.getLocalID(),
         'routed outgoing data',
         0,
         key.NewSessionKey(),
         key.SessionKeyType(),
         True,
         src,
         EncryptFunc=lambda inp: key.EncryptStringPK(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)),
         })
     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
Esempio n. 22
0
 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
Esempio n. 23
0
 def doForwardOutboxPacket(self, arg):
     """
     Action method.
     """
     # decrypt with my key and send to outside world
     newpacket, info = arg
     block = encrypted.Unserialize(newpacket.Payload)
     if block is None:
         lg.out(
             2,
             'proxy_router.doForwardOutboxPacket ERROR reading data from %s'
             % newpacket.RemoteID)
         return
     try:
         session_key = key.DecryptLocalPrivateKey(block.EncryptedSessionKey)
         padded_data = key.DecryptWithSessionKey(session_key,
                                                 block.EncryptedData)
         inpt = cStringIO.StringIO(padded_data[:int(block.Length)])
         sender_idurl = inpt.readline().rstrip('\n')
         receiver_idurl = inpt.readline().rstrip('\n')
         wide = inpt.readline().rstrip('\n')
         wide = wide == 'wide'
     except:
         lg.out(
             2,
             'proxy_router.doForwardOutboxPacket ERROR reading data from %s'
             % newpacket.RemoteID)
         lg.exc()
         try:
             inpt.close()
         except:
             pass
         return
     route = self.routes.get(sender_idurl, None)
     if not route:
         inpt.close()
         lg.warn('route with %s not found' % (sender_idurl))
         p2p_service.SendFail(newpacket,
                              'route not exist',
                              remote_idurl=sender_idurl)
         return
     data = inpt.read()
     inpt.close()
     routed_packet = signed.Unserialize(data)
     if not routed_packet:
         lg.out(
             2,
             'proxy_router.doForwardOutboxPacket ERROR unserialize packet from %s'
             % newpacket.RemoteID)
         return
     # send the packet directly to target user_id
     # we pass not callbacks because all response packets from this call will be also re-routed
     pout = packet_out.create(
         routed_packet,
         wide=wide,
         callbacks={},
         target=receiver_idurl,
     )
     # gateway.outbox(routed_packet, wide=wide)
     if _Debug:
         lg.out(
             _DebugLevel, '>>>Relay-OUT %d bytes from %s at %s://%s :' % (
                 len(data),
                 nameurl.GetName(sender_idurl),
                 info.proto,
                 info.host,
             ))
         lg.out(
             _DebugLevel, '    routed to %s : %s' %
             (nameurl.GetName(receiver_idurl), pout))
     del block
     del data
     del padded_data
     del route
     del inpt
     del session_key
     del routed_packet
Esempio n. 24
0
 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 sent in a 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.getIDURL():
         if proxy_receiver.GetPossibleRouterIDURL(
         ) and proxy_receiver.GetPossibleRouterIDURL().to_bin(
         ) == outpacket.RemoteID.to_bin():
             if network_connector.A().state == '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 == '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._do_add_pending_packet(outpacket, callbacks, wide,
                                            response_timeout, keep_alive)
     return self._do_send_packet_to_router(
         outpacket=outpacket,
         callbacks=callbacks,
         wide=wide,
         keep_alive=keep_alive,
         response_timeout=response_timeout,
     )
Esempio n. 25
0
 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
Esempio n. 26
0
 def _do_send_packet_to_router(self,
                               outpacket,
                               callbacks,
                               wide,
                               response_timeout,
                               keep_alive,
                               is_retry=False):
     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._do_send_packet_to_router SKIP because router not ready yet'
             )
         return self._do_add_pending_packet(outpacket, callbacks, wide,
                                            response_timeout, keep_alive)
     if outpacket.RemoteID.to_bin() == router_idurl.to_bin():
         if _Debug:
             lg.out(
                 _DebugLevel,
                 'proxy_sender._do_send_packet_to_router 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.getIDURL().to_bin(),  # from
         't': outpacket.RemoteID.to_bin(),  # to
         'p': raw_data,  # payload
         'w': wide,  # wide
         'i': response_timeout,
         'a': keep_alive,
         'r': is_retry,
     }
     if not json_payload['t']:
         raise ValueError('receiver idurl was not set')
     raw_bytes = serialization.DictToBytes(json_payload)
     block = encrypted.Block(
         CreatorID=my_id.getIDURL(),
         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.RelayOut(),
         OwnerID=outpacket.OwnerID,
         CreatorID=my_id.getIDURL(),
         PacketID=outpacket.PacketID,
         Payload=block_encrypted,
         RemoteID=router_idurl,
     )
     if response_timeout is not None:
         # must give some extra time for the proxy re-routing
         response_timeout += 10.0
     routed_packet = packet_out.create(
         outpacket=outpacket,
         wide=False,
         callbacks={},
         route={
             'packet':
             newpacket,
             # pointing "newpacket" to router node
             'proto':
             router_proto,
             'host':
             router_host,
             'remoteid':
             router_idurl,
             'description':
             'RelayOut_%s[%s]_%s' % (outpacket.Command, outpacket.PacketID,
                                     nameurl.GetName(router_idurl)),
         },
         response_timeout=response_timeout,
         keep_alive=True,
     )
     for command, cb_list in callbacks.items():
         if isinstance(cb_list, list):
             for cb in cb_list:
                 routed_packet.set_callback(command, cb)
         else:
             routed_packet.set_callback(command, cb_list)
     if not is_retry:
         _key = (
             outpacket.Command,
             outpacket.PacketID,
             outpacket.RemoteID.to_bin(),
         )
         self.sent_packets[_key] = (
             routed_packet,
             outpacket,
         )
     self.event('relay-out', (outpacket, newpacket, routed_packet))
     if _Debug:
         lg.out(
             _DebugLevel,
             '>>>Relay-OUT %s sent to %s://%s with %d bytes, timeout=%r' % (
                 str(outpacket),
                 router_proto,
                 router_host,
                 len(block_encrypted),
                 response_timeout,
             ))
     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