def send(customer_idurl, packet_id, format_type): customer_name = nameurl.GetName(customer_idurl) MyID = my_id.getLocalID() RemoteID = customer_idurl PacketID = packet_id if _Debug: lg.out( _DebugLevel, "list_files.send to %s, format is '%s'" % (customer_name, format_type)) custdir = settings.getCustomersFilesDir() ownerdir = os.path.join(custdir, nameurl.UrlFilename(customer_idurl)) if not os.path.isdir(ownerdir): if _Debug: lg.out(_DebugLevel, "list_files.send did not found customer dir: " + ownerdir) src = PackListFiles('', format_type) result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID) gateway.outbox(result) return result plaintext = TreeSummary(ownerdir) if _Debug: lg.out(_DebugLevel + 8, '\n%s' % (plaintext)) src = PackListFiles(plaintext, format_type) result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID) gateway.outbox(result) return result
def _on_inbox_packet_received(self, newpacket, info, status, error_message): from logs import lg from contacts import contactsdb from userid import my_id from userid import global_id from storage import backup_control from p2p import commands from p2p import p2p_service if newpacket.Command == commands.Files(): list_files_global_id = global_id.ParseGlobalID(newpacket.PacketID) if not list_files_global_id['idurl']: lg.warn('invalid PacketID: %s' % newpacket.PacketID) return False if list_files_global_id['idurl'] != my_id.getLocalIDURL(): # lg.warn('skip %s which is from another customer' % newpacket) return False if not contactsdb.is_supplier(newpacket.OwnerID): lg.warn('%s came, but %s is not my supplier' % (newpacket, newpacket.OwnerID, )) # skip Files() if this is not my supplier return False lg.out(self.debug_level, "service_backups._on_inbox_packet_received: %r for us from %s at %s" % ( newpacket, newpacket.CreatorID, info)) if backup_control.IncomingSupplierListFiles(newpacket, list_files_global_id): # send ack packet back p2p_service.SendAck(newpacket) else: p2p_service.SendFail(newpacket) return True return False
def SendFiles( idurl, raw_list_files_info, packet_id, callbacks={}, timeout=10, ): """ Sending information about known files stored locally for given customer (if you are supplier). You can also send a list of your files to another user if you wish to grand access. This will not send any personal data : only file names, ids, versions, etc. So pass list of files in encrypted form in the `payload` or leave it empty. """ MyID = my_id.getLocalID() if _Debug: lg.out( _DebugLevel, 'p2p_service.SendFiles %d bytes in packetID=%s' % (len(raw_list_files_info), packet_id)) lg.out(_DebugLevel, ' to remoteID=%s' % idurl) newpacket = signed.Packet( Command=commands.Files(), OwnerID=MyID, CreatorID=MyID, PacketID=packet_id, Payload=raw_list_files_info, RemoteID=idurl, ) result = gateway.outbox(newpacket, callbacks=callbacks, response_timeout=timeout) return result
def _on_inbox_packet_received(self, newpacket, info, status, error_message): from p2p import commands from access import key_ring if newpacket.Command == commands.Files(): return key_ring.on_files_received(newpacket, info) return False
def _on_my_list_files_refreshed(self, evt): from access import shared_access_coordinator from customer import supplier_connector from p2p import p2p_service from p2p import commands for key_id in shared_access_coordinator.list_active_shares(): cur_share = shared_access_coordinator.get_active_share(key_id) if not cur_share: continue if cur_share.state != 'CONNECTED': continue for supplier_idurl in cur_share.known_suppliers_list: sc = supplier_connector.by_idurl( supplier_idurl, customer_idurl=cur_share.customer_idurl, ) if sc is not None and sc.state == 'CONNECTED': p2p_service.SendListFiles( target_supplier=supplier_idurl, customer_idurl=cur_share.customer_idurl, key_id=cur_share.key_id, callbacks={ commands.Files(): lambda r, i: self._on_list_files_response( r, i, cur_share.customer_idurl, supplier_idurl, cur_share.key_id), commands.Fail(): lambda r, i: self._on_list_files_failed( r, i, cur_share.customer_idurl, supplier_idurl, cur_share.key_id), })
def _on_inbox_packet_received(self, newpacket, info, status, error_message): from storage import backup_control from p2p import commands if newpacket.Command == commands.Files(): return backup_control.on_files_received(newpacket, info) return False
def _on_key_transfer_success(self, customer_idurl, supplier_idurl, key_id): from logs import lg from p2p import commands from p2p import p2p_service lg.info('public key %r shared to supplier %r of customer %r, now will send ListFiles()' % (key_id, supplier_idurl, customer_idurl)) p2p_service.SendListFiles( target_supplier=supplier_idurl, customer_idurl=customer_idurl, key_id=key_id, timeout=30, callbacks={ commands.Files(): lambda r, i: self._on_list_files_response(r, i, customer_idurl, supplier_idurl, key_id), commands.Fail(): lambda r, i: self._on_list_files_failed(r, i, customer_idurl, supplier_idurl, key_id), } )
def doRequestSupplierFiles(self, *args, **kwargs): """ Action method. """ supplier_idurl = args[0] p2p_service.SendListFiles( target_supplier=supplier_idurl, customer_idurl=self.customer_idurl, key_id=self.key_id, callbacks={ commands.Files(): lambda r, i: self._on_list_files_response( r, i, self.customer_idurl, supplier_idurl, self.key_id), commands.Fail(): lambda r, i: self._on_list_files_failed( r, i, self.customer_idurl, supplier_idurl, self.key_id), })
def _on_inbox_packet_received(self, newpacket, info, status, error_message): from logs import lg from main import settings from contacts import contactsdb from userid import my_id from userid import global_id from storage import backup_control from p2p import commands if newpacket.Command == commands.Data(): if newpacket.OwnerID != my_id.getLocalID(): # only catch data belongs to me return False lg.out( self.debug_level, "service_backups._on_inbox_packet_received: %r for us from %s" % ( newpacket, newpacket.RemoteID, )) if newpacket.PacketID == global_id.MakeGlobalID( idurl=my_id.getLocalID(), path=settings.BackupIndexFileName(), ): # TODO: move to service_backup_db backup_control.IncomingSupplierBackupIndex(newpacket) return True if newpacket.Command == commands.Files(): if not newpacket.PacketID.startswith(my_id.getGlobalID() + ':'): # skip Files() which are from another customer return False if not contactsdb.is_supplier(newpacket.OwnerID): # skip Files() if this is not my supplier return False lg.out( self.debug_level, "service_backups._on_inbox_packet_received: %r for us from %s" % ( newpacket, newpacket.RemoteID, )) return backup_control.IncomingSupplierListFiles(newpacket) return False
def doRequestSupplierFiles(self, *args, **kwargs): """ Action method. """ supplier_idurl = args[0] pkt_out = p2p_service.SendListFiles( target_supplier=supplier_idurl, customer_idurl=self.customer_idurl, key_id=self.key_id, callbacks={ commands.Files(): lambda r, i: self._on_list_files_response( r, i, self.customer_idurl, supplier_idurl, self.key_id), commands.Fail(): lambda r, i: self._on_list_files_failed( r, i, self.customer_idurl, supplier_idurl, self.key_id), }, timeout=20, ) self.outgoing_list_files_packets_ids.append(pkt_out.PacketID)
def _do_request_supplier_list_files(self, supplier_idurl, customer_idurl, key_id): pkt_out = p2p_service.SendListFiles( target_supplier=supplier_idurl, customer_idurl=customer_idurl, key_id=key_id, callbacks={ commands.Files(): lambda r, i: self._on_list_files_response( r, i, customer_idurl, supplier_idurl, key_id), commands.Fail(): lambda r, i: self._on_list_files_failed( r, i, customer_idurl, supplier_idurl, key_id), }, timeout=20, ) self.outgoing_list_files_packets_ids.append(pkt_out.PacketID) if _Debug: lg.args( _DebugLevel, supplier=supplier_idurl, pid=pkt_out.PacketID, outgoing=len(self.outgoing_list_files_packets_ids), )
def inbox(newpacket, info, status, error_message): """ """ if newpacket.CreatorID != my_id.getLocalID( ) and newpacket.RemoteID != my_id.getLocalID(): # packet is NOT for us, skip return False commandhandled = False if newpacket.Command == commands.Ack(): # a response from remote node, typically handled in other places Ack(newpacket, info) commandhandled = False elif newpacket.Command == commands.Fail(): # some operation was failed on other side Fail(newpacket) commandhandled = False elif newpacket.Command == commands.Retrieve(): # retrieve some packet customer stored with us # handled by service_supplier() Retrieve(newpacket) commandhandled = False elif newpacket.Command == commands.RequestService(): # other node send us a request to get some service # handled by service_p2p_hookups() RequestService(newpacket, info) commandhandled = False elif newpacket.Command == commands.CancelService(): # other node wants to stop the service we gave him # handled by service_p2p_hookups() CancelService(newpacket, info) commandhandled = False elif newpacket.Command == commands.Data(): # new packet to store for customer, or data coming back from supplier # handled by service_backups() and service_supplier() Data(newpacket) commandhandled = False elif newpacket.Command == commands.ListFiles(): # customer wants list of their files # handled by service_supplier() ListFiles(newpacket, info) commandhandled = False elif newpacket.Command == commands.Files(): # supplier sent us list of files # handled by service_backups() Files(newpacket, info) commandhandled = False elif newpacket.Command == commands.DeleteFile(): # handled by service_supplier() DeleteFile(newpacket) commandhandled = False elif newpacket.Command == commands.DeleteBackup(): # handled by service_supplier() DeleteBackup(newpacket) commandhandled = False elif newpacket.Command == commands.Correspondent(): # TODO: contact asking for our current identity, not implemented yet Correspondent(newpacket) commandhandled = False elif newpacket.Command == commands.Broadcast(): # handled by service_broadcasting() Broadcast(newpacket, info) commandhandled = False elif newpacket.Command == commands.Coin(): # handled by service_accountant() Coin(newpacket, info) commandhandled = False elif newpacket.Command == commands.RetrieveCoin(): # handled by service_accountant() RetrieveCoin(newpacket, info) commandhandled = False elif newpacket.Command == commands.Key(): # handled by service_keys_registry() Key(newpacket, info) commandhandled = False elif newpacket.Command == commands.Event(): # handled by service_p2p_hookups() Event(newpacket, info) commandhandled = False elif newpacket.Command == commands.Message(): # handled by service_private_messages() Message(newpacket, info) commandhandled = False return commandhandled
def inbox(newpacket, info, status, error_message): """ """ if newpacket.CreatorID != my_id.getLocalID( ) and newpacket.RemoteID != my_id.getLocalID(): # packet is NOT for us, skip return False commandhandled = False if newpacket.Command == commands.Ack(): # a response from remote node, typically handled in other places Ack(newpacket, info) commandhandled = False elif newpacket.Command == commands.Fail(): # some operation was failed on other side Fail(newpacket) commandhandled = False elif newpacket.Command == commands.Retrieve(): # retrieve some packet customer stored with us Retrieve(newpacket) commandhandled = True elif newpacket.Command == commands.RequestService(): # other node send us a request to get some service RequestService(newpacket, info) commandhandled = True elif newpacket.Command == commands.CancelService(): # other node wants to stop the service we gave him CancelService(newpacket, info) commandhandled = True elif newpacket.Command == commands.Data(): # new packet to store for customer commandhandled = Data(newpacket) elif newpacket.Command == commands.ListFiles(): # customer wants list of their files ListFiles(newpacket) commandhandled = True elif newpacket.Command == commands.Files(): # supplier sent us list of files Files(newpacket, info) commandhandled = True elif newpacket.Command == commands.DeleteFile(): # will Delete a customer file for them DeleteFile(newpacket) commandhandled = True elif newpacket.Command == commands.DeleteBackup(): # will Delete all files starting in a backup DeleteBackup(newpacket) commandhandled = True elif newpacket.Command == commands.Message(): # will be handled in message.py commandhandled = False elif newpacket.Command == commands.Correspondent(): # contact asking for our current identity Correspondent(newpacket) commandhandled = True elif newpacket.Command == commands.Broadcast(): # handled by service_broadcasting() Broadcast(newpacket, info) commandhandled = False elif newpacket.Command == commands.Coin(): # handled by service_accountant() Coin(newpacket, info) commandhandled = False elif newpacket.Command == commands.RetrieveCoin(): # handled by service_accountant() RetrieveCoin(newpacket, info) commandhandled = False return commandhandled