def connect(self, options):
     """
     """
     if _Debug:
         lg.out(4, 'proxy_interface.connect %s' % str(options))
     from transport.proxy import proxy_receiver
     from transport.proxy import proxy_sender
     if settings.enablePROXYreceiving():
         proxy_receiver.A('start', options)
     else:
         lg.warn('proxy transport receiving is disabled')
         interface_receiving_failed()
         return False
     if settings.enablePROXYsending():
         proxy_sender.A('start', options)
     return succeed(True)
Example #2
0
 def init(self, xml_rpc_url_or_object):
     """
     """
     global _GateProxy
     if _Debug:
         lg.out(4, 'proxy_interface.init')
     from transport.proxy import proxy_receiver
     from transport.proxy import proxy_sender
     if isinstance(xml_rpc_url_or_object, six.string_types):
         _GateProxy = xmlrpc.Proxy(xml_rpc_url_or_object, allowNone=True)
     else:
         _GateProxy = xml_rpc_url_or_object
     proxy_receiver.A('init')
     proxy_sender.A('init')
     proxy().callRemote('transport_initialized', 'proxy')
     return True
Example #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
Example #4
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,
     )
Example #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
Example #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
Example #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