Ejemplo n.º 1
0
 def build_contacts(self, id_obj):
     """
     """
     from transport.proxy import proxy_receiver
     # from userid import my_id
     # current_identity_contacts = my_id.getLocalIdentity().getContacts()
     if not proxy_receiver.GetRouterIdentity():
         # if not yet found one node to route your traffic - do nothing
         if _Debug:
             lg.out(
                 4,
                 'proxy_interface.build_contacts SKIP, router not yet found'
             )
         return []  # current_identity_contacts
     if not proxy_receiver.ReadMyOriginalIdentitySource():
         # if we did not save our original identity we will have troubles contacting remote node
         if _Debug:
             lg.out(
                 4,
                 'proxy_interface.build_contacts SKIP, original identity was not saved'
             )
         return []  # current_identity_contacts
     # switch contacts - use router contacts instead of my
     # he will receive all packets addressed to me and redirect to me
     result = proxy_receiver.GetRouterIdentity().getContacts()
     if _Debug:
         lg.out(
             4, 'proxy_interface.build_contacts %s : %s' %
             (proxy_receiver.GetRouterIdentity().getIDName(), str(result)))
     return result
Ejemplo n.º 2
0
 def _finish_verification(res):
     if _Debug:
         lg.out(4, 'proxy_interface._finish_verification')
     try:
         cached_id = identitycache.FromCache(proxy_receiver.GetRouterIDURL())
         if not cached_id:
             if _Debug:
                 lg.out(4, '    returning False: router identity is not cached')
             res.callback(False)
             return False
         if not proxy_receiver.GetRouterIdentity():
             if _Debug:
                 lg.out(4, '    returning False : router identity is None or router is not ready yet')
             return True
         if cached_id.serialize() != proxy_receiver.GetRouterIdentity().serialize():
             if _Debug:
                 lg.out(4, 'proxy_interface.verify_contacts return False: cached copy is different')
                 lg.out(20, '\n%s\n' % cached_id.serialize())
                 lg.out(20, '\n%s\n' % proxy_receiver.GetRouterIdentity().serialize())
             res.callback(False)
             return
         router_contacts = proxy_receiver.GetRouterIdentity().getContactsByProto()
         if len(router_contacts) != id_obj.getContactsNumber():
             if _Debug:
                 lg.out(4, '    returning False: router contacts is different')
             res.callback(False)
             return False
         for proto, contact in id_obj.getContactsByProto().items():
             if proto not in list(router_contacts.keys()):
                 if _Debug:
                     lg.out(4, '    returning False: [%s] is not present in router contacts' % proto)
                 res.callback(False)
                 return False
             if router_contacts[proto] != contact:
                 if _Debug:
                     lg.out(4, '    returning False: [%s] contact is different in router id' % proto)
                 res.callback(False)
                 return False
         if _Debug:
             lg.out(4, '    returning True : my contacts and router contacts is same')
         res.callback(True)
         return True
     except:
         lg.exc()
         res.callback(True)
         return True
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def verify_contacts(self, id_obj):
        """
        Check if router is ready and his contacts exists in that identity.
        """
        from transport.proxy import proxy_receiver
        if not proxy_receiver.A() or not proxy_receiver.GetRouterIDURL(
        ) or not proxy_receiver.GetRouterIdentity():
            # if not yet found any node to route your traffic - do nothing
            if _Debug:
                lg.out(
                    4,
                    'proxy_interface.verify_contacts returning True : router not yet found'
                )
            return True
        if not proxy_receiver.ReadMyOriginalIdentitySource():
            if _Debug:
                lg.out(
                    4,
                    'proxy_interface.verify_contacts returning False : my original identity is empty'
                )
            return False
        result = Deferred()

        def _finish_verification(res):
            if _Debug:
                lg.out(4, 'proxy_interface._finish_verification')
            try:
                cached_id = identitycache.FromCache(
                    proxy_receiver.GetRouterIDURL())
                if not cached_id:
                    if _Debug:
                        lg.out(
                            4,
                            '    returning False: router identity is not cached'
                        )
                    res.callback(False)
                    return False
                if not proxy_receiver.GetRouterIdentity():
                    if _Debug:
                        lg.out(
                            4,
                            '    returning False : router identity is None or router is not ready yet'
                        )
                    return True
                if cached_id.serialize() != proxy_receiver.GetRouterIdentity(
                ).serialize():
                    if _Debug:
                        lg.out(
                            4,
                            'proxy_interface.verify_contacts return False: cached copy is different'
                        )
                        lg.out(20, '\n%s\n' % cached_id.serialize())
                        lg.out(
                            20, '\n%s\n' %
                            proxy_receiver.GetRouterIdentity().serialize())
                    res.callback(False)
                    return
                router_contacts = proxy_receiver.GetRouterIdentity(
                ).getContactsByProto()
                if len(router_contacts) != id_obj.getContactsNumber():
                    if _Debug:
                        lg.out(
                            4,
                            '    returning False: router contacts is different'
                        )
                    res.callback(False)
                    return False
                for proto, contact in id_obj.getContactsByProto().items():
                    if proto not in router_contacts.keys():
                        if _Debug:
                            lg.out(
                                4,
                                '    returning False: [%s] is not present in router contacts'
                                % proto)
                        res.callback(False)
                        return False
                    if router_contacts[proto] != contact:
                        if _Debug:
                            lg.out(
                                4,
                                '    returning False: [%s] contact is different in router id'
                                % proto)
                        res.callback(False)
                        return False
                if _Debug:
                    lg.out(
                        4,
                        '    returning True : my contacts and router contacts is same'
                    )
                res.callback(True)
                return True
            except:
                lg.exc()
                res.callback(True)
                return True

        d = identitycache.immediatelyCaching(proxy_receiver.GetRouterIDURL())
        d.addCallback(lambda src: _finish_verification(result))
        d.addErrback(lambda err: result.callback(False))
        return result
Ejemplo n.º 6
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
Ejemplo n.º 7
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