コード例 #1
0
 def to_json(self):
     return {
         'name':
         self.name,
         'state':
         self.state,
         'host': ('%s://%s' % proxy_receiver.GetRouterProtoHost())
         if proxy_receiver.GetRouterProtoHost() else '',
         'idurl':
         proxy_receiver.GetRouterIDURL(),
         'bytes_received':
         0,
         'bytes_sent':
         self.traffic_out,
     }
コード例 #2
0
 def to_json(self):
     j = super().to_json()
     j.update({
         'proto':
         proxy_receiver.GetRouterProtoHost()[0]
         if proxy_receiver.GetRouterProtoHost() else '',
         'host':
         net_misc.pack_address_text(proxy_receiver.GetRouterProtoHost()[1])
         if proxy_receiver.GetRouterProtoHost() else '',
         'idurl':
         proxy_receiver.GetRouterIDURL(),
         'bytes_received':
         0,
         'bytes_sent':
         self.traffic_out,
     })
     return j
コード例 #3
0
ファイル: proxy_sender.py プロジェクト: StasEvseev/devel
 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
コード例 #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
コード例 #5
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
コード例 #6
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