def SendFailNoRequest(remoteID, packetID, response=''): result = signed.Packet( Command=commands.Fail(), OwnerID=my_id.getIDURL(), CreatorID=my_id.getIDURL(), PacketID=packetID, Payload=response, RemoteID=remoteID, ) if _Debug: lg.out( _DebugLevel, "p2p_service.SendFailNoRequest packetID=%s to %s" % (result.PacketID, result.RemoteID)) gateway.outbox(result) return result
def SendFailNoRequest(remoteID, packetID, response=''): result = signed.Packet( commands.Fail(), my_id.getLocalID(), my_id.getLocalID(), packetID, response, remoteID, ) if _Debug: lg.out( _DebugLevel, "p2p_service.SendFailNoRequest packetID=%s to %s" % (result.PacketID, result.RemoteID)) gateway.outbox(result) return result
def push_signed_message(producer_id, queue_id, data, creation_time=None): try: signed_data = signed.Packet( Command=commands.Event(), OwnerID=producer_id, CreatorID=producer_id, PacketID=packetid.UniqueID(), Payload=json.dumps(data), RemoteID=queue_id, KeyID=producer_id, ) except: lg.exc() raise Exception('sign message failed') return push_message(producer_id, queue_id, data=signed_data.Serialize(), creation_time=creation_time)
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, })
def SendFail(request, response='', remote_idurl=None, wide=False): if remote_idurl is None: remote_idurl = request.OwnerID result = signed.Packet( Command=commands.Fail(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=request.PacketID, # This is needed to identify Fail on remote side Payload=response, RemoteID=remote_idurl, ) if _Debug: lg.out(_DebugLevel, "p2p_service.SendFail packetID=%s to %s with %d bytes" % ( result.PacketID, result.RemoteID, len(response))) gateway.outbox(result, wide=wide) return result
def SendAck(packettoack, response='', wide=False, callbacks={}, remote_idurl=None): if remote_idurl is None: remote_idurl = packettoack.OwnerID result = signed.Packet( Command=commands.Ack(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=packettoack.PacketID, Payload=response, RemoteID=remote_idurl, ) if _Debug: lg.out(_DebugLevel, "p2p_service.SendAck %s to %s with %d bytes" % ( result.PacketID, result.RemoteID, len(response))) gateway.outbox(result, wide=wide, callbacks=callbacks) return result
def SendKey(remote_idurl, encrypted_key_data, packet_id=None, wide=False, callbacks={}, timeout=10, ): if packet_id is None: packet_id = packetid.UniqueID() if _Debug: lg.out(_DebugLevel, "p2p_service.SendKey [%s] to %s with %d bytes encrypted key data" % ( packet_id, remote_idurl, len(encrypted_key_data))) outpacket = signed.Packet( Command=commands.Key(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=packet_id, Payload=encrypted_key_data, RemoteID=remote_idurl, ) gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=timeout) return outpacket
def SendDeleteBackup(SupplierID, BackupID): if _Debug: lg.out(_DebugLevel, "p2p_service.SendDeleteBackup SupplierID=%s BackupID=%s" % (SupplierID, BackupID)) MyID = my_id.getLocalID() PacketID = packetid.RemotePath(BackupID) RemoteID = SupplierID result = signed.Packet( Command=commands.DeleteBackup(), OwnerID=MyID, CreatorID=MyID, PacketID=PacketID, Payload="", RemoteID=RemoteID, ) gateway.outbox(result) return result
def SendRetreive(ownerID, creatorID, packetID, remoteID, payload='', callbacks={}): """ """ newpacket = signed.Packet( commands.Retrieve(), ownerID, creatorID, packetID, payload, remoteID, ) result = gateway.outbox(newpacket, callbacks=callbacks) if _Debug: lg.out(_DebugLevel, 'p2p_service.SendRetreive packetID=%s' % packetID) lg.out(_DebugLevel, ' remoteID=%s ownerID=%s creatorID=%s' % (remoteID, ownerID, creatorID)) return result
def SendRetreive(ownerID, creatorID, packetID, remoteID, payload='', response_timeout=None, callbacks={}): """ """ newpacket = signed.Packet( Command=commands.Retrieve(), OwnerID=ownerID, CreatorID=creatorID, PacketID=packetID, Payload=payload, RemoteID=remoteID, ) result = gateway.outbox(newpacket, callbacks=callbacks, response_timeout=response_timeout) if _Debug: lg.out(_DebugLevel, 'p2p_service.SendRetreive packetID=%s' % packetID) lg.out(_DebugLevel, ' remoteID=%s ownerID=%s creatorID=%s' % (remoteID, ownerID, creatorID)) return result
def SendAuditKey(remote_idurl, encrypted_payload, packet_id=None, timeout=10, wide=False, callbacks={}): if _Debug: lg.out(_DebugLevel, "p2p_service.SendAuditKey to %s with %d bytes in json payload data" % ( remote_idurl, len(encrypted_payload))) if packet_id is None: packet_id = packetid.UniqueID() outpacket = signed.Packet( Command=commands.AuditKey(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=packet_id, Payload=encrypted_payload, RemoteID=remote_idurl, ) gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=timeout) return outpacket
def SendEvent(remote_idurl, event_id, payload=None, producer_id=None, consumer_id=None, queue_id=None, message_id=None, created=None, packet_id=None, wide=False, callbacks={}, response_timeout=5): if packet_id is None: packet_id = packetid.UniqueID() e_json = { 'event_id': event_id, 'payload': payload, } if producer_id is not None: e_json['producer_id'] = producer_id if consumer_id is not None: e_json['consumer_id'] = consumer_id if queue_id is not None: e_json['queue_id'] = queue_id if message_id is not None: e_json['message_id'] = message_id if created is not None: e_json['created'] = created e_json_src = serialization.DictToBytes(e_json) if _Debug: lg.out( _DebugLevel, "p2p_service.SendEvent to %s with %d bytes message json data" % (remote_idurl, len(e_json_src))) outpacket = signed.Packet( Command=commands.Event(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=packet_id, Payload=e_json_src, RemoteID=remote_idurl, ) gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=response_timeout) return outpacket
def SendToID( idurl, Payload=None, wide=False, ack_handler=None, timeout_handler=None, response_timeout=20, ): """ Create ``packet`` with my Identity file and calls ``transport.gateway.outbox()`` to send it. """ global _PropagateCounter if _Debug: lg.out( _DebugLevel, "propagate.SendToID [%s] wide=%s" % (nameurl.GetName(idurl), str(wide))) if ack_handler is None: ack_handler = HandleAck if timeout_handler is None: timeout_handler = HandleTimeOut thePayload = Payload if thePayload is None: thePayload = strng.to_bin(my_id.getLocalIdentity().serialize()) p = signed.Packet( Command=commands.Identity(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=('propagate:%d:%s' % (_PropagateCounter, packetid.UniqueID())), Payload=thePayload, RemoteID=idurl, ) _PropagateCounter += 1 result = gateway.outbox(p, wide, response_timeout=response_timeout, callbacks={ commands.Ack(): ack_handler, commands.Fail(): ack_handler, None: timeout_handler, }) if wide: # this is a ping packet - need to clear old info p2p_stats.ErasePeerProtosStates(idurl) p2p_stats.EraseMyProtosStates(idurl) return result
def SendCoin(remote_idurl, coins, packet_id=None, wide=False, callbacks={}): if _Debug: lg.out( _DebugLevel, "p2p_service.SendCoin to %s with %d records" % (remote_idurl, len(coins))) if packet_id is None: packet_id = packetid.UniqueID() outpacket = signed.Packet( commands.Coin(), my_id.getLocalID(), my_id.getLocalID(), packet_id, serialization.DictToBytes(coins, keys_to_text=True), remote_idurl, ) gateway.outbox(outpacket, wide=wide, callbacks=callbacks) return outpacket
def SendDeleteListBackups(SupplierID, ListBackupIDs): if _Debug: lg.out(_DebugLevel, "p2p_service.SendDeleteListBackups SupplierID=%s BackupIDs: %s" % (SupplierID, ListBackupIDs)) MyID = my_id.getLocalID() PacketID = packetid.UniqueID() RemoteID = SupplierID Payload = '\n'.join(ListBackupIDs) result = signed.Packet( Command=commands.DeleteBackup(), OwnerID=MyID, CreatorID=MyID, PacketID=PacketID, Payload=Payload, RemoteID=RemoteID, ) gateway.outbox(result) return result
def SendData(raw_data, ownerID, creatorID, remoteID, packetID, callbacks={}): """ """ newpacket = signed.Packet( Command=commands.Data(), OwnerID=ownerID, CreatorID=creatorID, PacketID=packetID, Payload=raw_data, RemoteID=remoteID, ) result = gateway.outbox(newpacket, callbacks=callbacks) if _Debug: lg.out(_DebugLevel, 'p2p_service.SendData %d bytes in packetID=%s' % ( len(raw_data), packetID)) lg.out(_DebugLevel, ' to remoteID=%s ownerID=%s creatorID=%s' % (remoteID, ownerID, creatorID)) return newpacket, result
def SendIdentity(remote_idurl, wide=False, timeout=10, callbacks={}): """ """ packet_id = 'identity:%s' % packetid.UniqueID() if _Debug: lg.out(_DebugLevel, "p2p_service.SendIdentity to %s wide=%s packet_id=%r" % ( nameurl.GetName(remote_idurl), wide, packet_id, )) result = signed.Packet( Command=commands.Identity(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=packet_id, Payload=my_id.getLocalIdentity().serialize(), RemoteID=remote_idurl, ) gateway.outbox(result, wide=wide, callbacks=callbacks, response_timeout=timeout) return result
def SendFail(request, response='', remote_idurl=None): if remote_idurl is None: remote_idurl = request.OwnerID result = signed.Packet( commands.Fail(), my_id.getLocalID(), my_id.getLocalID(), request.PacketID, # This is needed to identify Fail on remote side response, remote_idurl, ) if _Debug: lg.out( _DebugLevel, "p2p_service.SendFail %s to %s response: %s ..." % (result.PacketID, result.RemoteID, str(response)[:40])) gateway.outbox(result) return result
def doSuppliersSendIndexFile(self, arg): """ Action method. """ if _Debug: lg.out(_DebugLevel, 'index_synchronizer.doSuppliersSendIndexFile') packetID = global_id.MakeGlobalID( customer=my_id.getGlobalID(key_alias='master'), path=settings.BackupIndexFileName(), ) self.sending_suppliers.clear() self.sent_suppliers_number = 0 src = bpio.ReadBinaryFile(settings.BackupIndexFilePath()) localID = my_id.getLocalID() b = encrypted.Block( localID, packetID, 0, key.NewSessionKey(), key.SessionKeyType(), True, src, ) Payload = b.Serialize() for supplierId in contactsdb.suppliers(): if not supplierId: continue if not contact_status.isOnline(supplierId): continue newpacket = signed.Packet(commands.Data(), localID, localID, packetID, Payload, supplierId) pkt_out = gateway.outbox(newpacket, callbacks={ commands.Ack(): self._on_supplier_acked, commands.Fail(): self._on_supplier_acked, }) if pkt_out: self.sending_suppliers.add(supplierId) self.sent_suppliers_number += 1 if _Debug: lg.out( _DebugLevel, ' %s sending to %s' % (newpacket, nameurl.GetName(supplierId)))
def SendDeleteFile(SupplierID, pathID): if _Debug: lg.out( _DebugLevel, "p2p_service.SendDeleteFile SupplierID=%s PathID=%s" % (SupplierID, pathID)) MyID = my_id.getIDURL() PacketID = pathID RemoteID = SupplierID result = signed.Packet( Command=commands.DeleteFile(), OwnerID=MyID, CreatorID=MyID, PacketID=PacketID, Payload="", RemoteID=RemoteID, ) gateway.outbox(result) return result
def test_signed_packet(self): key.InitMyKey() payload_size = 1024 attempts = 10 for i in range(attempts): data1 = os.urandom(payload_size) p1 = signed.Packet( 'Data', my_id.getIDURL(), my_id.getIDURL(), 'SomeID', data1, self.bob_ident.getIDURL(), ) self.assertTrue(p1.Valid()) raw1 = p1.Serialize() p2 = signed.Unserialize(raw1) self.assertTrue(p2.Valid())
def SendCancelService(remote_idurl, service_name, json_payload={}, wide=False, callbacks={}): service_info = { 'name': service_name, 'payload': json_payload, } service_info_raw = serialization.DictToBytes(service_info) if _Debug: lg.out(_DebugLevel, 'p2p_service.SendCancelService "%s" to %s with %d bytes payload' % ( service_name, remote_idurl, len(service_info_raw))) result = signed.Packet( commands.CancelService(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), service_info_raw, remote_idurl, ) gateway.outbox(result, wide=wide, callbacks=callbacks) return result
def SendToID( idurl, Payload=None, wide=False, ack_handler=None, timeout_handler=None, response_timeout=20, ): """ Create ``packet`` with my Identity file and calls ``transport.gateway.outbox()`` to send it. """ lg.out( 8, "propagate.SendToID [%s] wide=%s" % (nameurl.GetName(idurl), str(wide))) if ack_handler is None: ack_handler = HandleAck if timeout_handler is None: timeout_handler = HandleTimeOut thePayload = Payload if thePayload is None: thePayload = strng.to_bin(my_id.getLocalIdentity().serialize()) p = signed.Packet( commands.Identity(), my_id.getLocalID(), # MyID, my_id.getLocalID(), # MyID, commands.Identity(), # 'Identity', # my_id.getLocalID(), #PacketID, thePayload, idurl, ) # callback.register_interest(AckHandler, p.RemoteID, p.PacketID) result = gateway.outbox(p, wide, response_timeout=response_timeout, callbacks={ commands.Ack(): ack_handler, commands.Fail(): ack_handler, None: timeout_handler, }) if wide: # this is a ping packet - need to clear old info p2p_stats.ErasePeerProtosStates(idurl) p2p_stats.EraseMyProtosStates(idurl) return result
def SendAckNoRequest(remoteID, packetid, response='', wide=False, callbacks={}): result = signed.Packet( commands.Ack(), my_id.getLocalID(), my_id.getLocalID(), packetid, response, remoteID, ) if _Debug: lg.out( _DebugLevel, "p2p_service.SendAckNoRequest packetID=%s to %s with %d bytes" % (result.PacketID, result.RemoteID, len(response))) gateway.outbox(result, wide=wide, callbacks=callbacks)
def backup_done(bid, result): from crypt import signed try: os.mkdir(os.path.join(settings.getLocalBackupsDir(), bid + '.out')) except: pass for filename in os.listdir(os.path.join(settings.getLocalBackupsDir(), bid)): filepath = os.path.join(settings.getLocalBackupsDir(), bid, filename) payld = str(bpio.ReadBinaryFile(filepath)) outpacket = signed.Packet('Data', my_id.getLocalID(), my_id.getLocalID(), filename, payld, 'http://megafaq.ru/cvps1010.xml') newfilepath = os.path.join(settings.getLocalBackupsDir(), bid + '.out', filename) bpio.WriteBinaryFile(newfilepath, outpacket.Serialize()) # Assume we delivered all pieces from ".out" to suppliers and lost original data # Then we requested the data back and got it into ".inp" try: os.mkdir(os.path.join(settings.getLocalBackupsDir(), bid + '.inp')) except: pass for filename in os.listdir( os.path.join(settings.getLocalBackupsDir(), bid + '.out')): filepath = os.path.join(settings.getLocalBackupsDir(), bid + '.out', filename) data = bpio.ReadBinaryFile(filepath) inppacket = signed.Unserialize(data) assert inppacket assert inppacket.Valid() newfilepath = os.path.join(settings.getLocalBackupsDir(), bid + '.inp', filename) bpio.WriteBinaryFile(newfilepath, inppacket.Payload) # Now do restore from input data backupID = bid + '.inp' outfd, tarfilename = tmpfile.make( 'restore', extension='.tar.gz', prefix=backupID.replace('/', '_') + '_', ) r = restore_worker.RestoreWorker(backupID, outfd) r.MyDeferred.addBoth(restore_done, tarfilename) reactor.callLater(1, r.automat, 'init')
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, },)
def push_signed_message(producer_id, queue_id, data, creation_time=None): # TODO: to be continue try: signed_data = signed.Packet( Command=commands.Event(), OwnerID=producer_id, CreatorID=my_id.getLocalID(), PacketID=packetid.UniqueID(), Payload=serialization.DictToBytes(data, keys_to_text=True), RemoteID=queue_id, KeyID=producer_id, ) except: lg.exc() raise Exception('sign message failed') return write_message(producer_id, queue_id, data=signed_data.Serialize(), creation_time=creation_time)
def SendData(raw_data, ownerID, creatorID, remoteID, packetID, callbacks={}): """ """ # TODO: newpacket = signed.Packet( commands.Data(), ownerID, creatorID, packetID, raw_data, remoteID, ) result = gateway.outbox(newpacket, callbacks=callbacks) if _Debug: lg.out( _DebugLevel, 'p2p_service.SendData %d bytes in [%s] to %s, by %s | %s' % (len(raw_data), packetID, remoteID, ownerID, creatorID)) return result
def SendRequestService(remote_idurl, service_name, json_payload={}, wide=False, callbacks={}, timeout=60): service_info = { 'name': service_name, 'payload': json_payload, } service_info_raw = serialization.DictToBytes(service_info) if _Debug: lg.out(_DebugLevel, 'p2p_service.SendRequestService "%s" to %s with %r' % ( service_name, remote_idurl, service_info)) result = signed.Packet( Command=commands.RequestService(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=packetid.UniqueID(), Payload=service_info_raw, RemoteID=remote_idurl, ) gateway.outbox(result, wide=wide, callbacks=callbacks, response_timeout=timeout) return result
def SendListFiles(target_supplier, customer_idurl=None, key_id=None, query_items=[], wide=False, callbacks={}, timeout=None): """ This is used as a request method from your supplier : if you send him a ListFiles() packet he will reply you with a list of stored files in a Files() packet. """ MyID = my_id.getLocalID() if not customer_idurl: customer_idurl = MyID if not str(target_supplier).isdigit(): RemoteID = target_supplier else: RemoteID = contactsdb.supplier(target_supplier, customer_idurl=customer_idurl) if not RemoteID: lg.warn("RemoteID is empty target_supplier=%s" % str(target_supplier)) return None if not key_id: # key_id = global_id.MakeGlobalID(idurl=customer_idurl, key_alias='customer') # TODO: due to issue with "customer" key backup/restore decided to always use my "master" key # to retrieve my list files info from supplier # expect remote user always poses my master public key from my identity. # probably require more work to build more reliable solution without using my master key at all # when my identity rotated supplier first needs to receive my new identity and then sending ListFiles() key_id = my_id.getGlobalID(key_alias='master') if not my_keys.is_key_registered(key_id) or not my_keys.is_key_private(key_id): lg.warn('key %r not exist or public, my "master" key to be used with ListFiles() packet' % key_id) key_id = my_id.getGlobalID(key_alias='master') PacketID = "%s:%s" % (key_id, packetid.UniqueID(), ) if not query_items: query_items = ['*', ] Payload = serialization.DictToBytes({'items': query_items, }) if _Debug: lg.out(_DebugLevel, "p2p_service.SendListFiles %r to %s with query : %r" % ( PacketID, nameurl.GetName(RemoteID), query_items, )) result = signed.Packet( Command=commands.ListFiles(), OwnerID=MyID, CreatorID=MyID, PacketID=PacketID, Payload=Payload, RemoteID=RemoteID, ) gateway.outbox(result, wide=wide, callbacks=callbacks, response_timeout=timeout) return result