def doSendHisFiles(self, *args, **kwargs): """ Action method. """ customer_key_id = my_keys.make_key_id( alias='customer', creator_idurl=self.customer_idurl) if my_keys.is_key_registered(customer_key_id): list_files.send( customer_idurl=self.customer_idurl, packet_id='%s:%s' % ( customer_key_id, packetid.UniqueID(), ), format_type=settings.ListFilesFormat(), key_id=customer_key_id, remote_idurl=self.customer_idurl, # send to the customer ) else: # if "customer" key is not delivered to me yet, use his "master" key list_files.send( customer_idurl=self.customer_idurl, packet_id='%s:%s' % ( customer_key_id, packetid.UniqueID(), ), format_type=settings.ListFilesFormat(), key_id=my_keys.make_key_id(alias='master', creator_idurl=self.customer_idurl), remote_idurl=self.customer_idurl, # send to the customer ) lg.err('key %s is not registered, not able to send his files' % customer_key_id)
def SendMessage(remote_idurl, packet_id=None, payload=None, wide=True, callbacks={}, response_timeout=None): """ """ if packet_id is None: packet_id = packetid.UniqueID() if _Debug: lg.out( _DebugLevel, "p2p_service.SendMessage to %s with packet_id=%s" % ( nameurl.GetName(remote_idurl), packet_id, )) outpacket = signed.Packet( Command=commands.Message(), OwnerID=my_id.getIDURL(), CreatorID=my_id.getIDURL(), PacketID=packet_id, Payload=payload, RemoteID=remote_idurl, ) result = gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=response_timeout) return result, outpacket
def SendListFiles(target_supplier, customer_idurl=None, key_id=None, wide=False, callbacks={}): """ 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 _Debug: lg.out(_DebugLevel, "p2p_service.SendListFiles to %s" % nameurl.GetName(RemoteID)) if not key_id: key_id = global_id.MakeGlobalID(idurl=customer_idurl, key_alias='customer') PacketID = "%s:%s" % (key_id, packetid.UniqueID(), ) Payload = settings.ListFilesFormat() result = signed.Packet( Command=commands.ListFiles(), OwnerID=MyID, CreatorID=MyID, PacketID=PacketID, Payload=Payload, RemoteID=RemoteID, ) gateway.outbox(result, wide=wide, callbacks=callbacks) return result
def doSendMyListFiles(self, *args, **kwargs): """ Action method. """ json_list_files = backup_fs.Serialize( to_json=True, filter_cb=lambda path_id, path, info: True if strng.to_text(info.key_id) == strng.to_text(self.key_id) else False, ) # raw_list_files = json.dumps(json_list_files, indent=2, encoding='utf-8') raw_list_files = serialization.DictToBytes(json_list_files, keys_to_text=True, values_to_text=True) if _Debug: lg.out(_DebugLevel, 'shared_access_donor.doSendMyListFiles prepared list of files for %s :\n%s' % ( self.remote_idurl, raw_list_files)) block = encrypted.Block( CreatorID=my_id.getLocalID(), BackupID=self.key_id, Data=raw_list_files, SessionKey=key.NewSessionKey(), EncryptKey=self.key_id, ) encrypted_list_files = block.Serialize() packet_id = "%s:%s" % (self.key_id, packetid.UniqueID(), ) p2p_service.SendFiles( idurl=self.remote_idurl, raw_list_files_info=encrypted_list_files, packet_id=packet_id, callbacks={ commands.Ack(): lambda response, _: self.automat('list-files-ok', response), commands.Fail(): lambda response, _: self.automat('fail', Exception(str(response))), None: lambda pkt_out: self.automat('fail', Exception('timeout')), }, )
def doSendRequestService(self, arg): """ Action method. """ if len(self.request_service_packet_id) >= 3: if _Debug: lg.warn('too many service requests to %s' % self.router_idurl) self.automat('service-refused', arg) return service_info = 'service_proxy_server \n' orig_identity = config.conf().getData( 'services/proxy-transport/my-original-identity').strip() if not orig_identity: orig_identity = my_id.getLocalIdentity().serialize() service_info += orig_identity # for t in gateway.transports().values(): # service_info += '%s://%s' % (t.proto, t.host) # service_info += ' ' newpacket = signed.Packet( commands.RequestService(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), service_info, self.router_idurl, ) packet_out.create( newpacket, wide=False, callbacks={ commands.Ack(): self._on_request_service_ack, commands.Fail(): self._on_request_service_fail }, ) self.request_service_packet_id.append(newpacket.PacketID)
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 _do_send_identity_to_router(self, identity_source, failed_event): try: identity_obj = identity.identity(xmlsrc=identity_source) except: lg.exc() return if _Debug: lg.out(_DebugLevel, 'proxy_receiver._do_send_identity_to_router to %s' % self.router_idurl) lg.out(_DebugLevel, ' contacts=%r, sources=%r' % ( identity_obj.contacts, identity_obj.getSources(as_originals=True))) newpacket = signed.Packet( Command=commands.Identity(), OwnerID=my_id.getIDURL(), CreatorID=my_id.getIDURL(), PacketID=('proxy_receiver:%s' % packetid.UniqueID()), Payload=identity_obj.serialize(), RemoteID=self.router_idurl, ) packet_out.create( newpacket, wide=True, callbacks={ commands.Ack(): lambda response, info: self.automat('ack-received', (response, info)), commands.Fail(): lambda x: self.automat(failed_event), None: lambda pkt_out: self.automat('ack-timeout', pkt_out), 'failed': lambda pkt_out, error_message: self.automat('sending-failed', (pkt_out, error_message)), }, keep_alive=True, response_timeout=30, )
def SendContacts(remote_idurl, json_payload={}, wide=False, callbacks={}): """ """ MyID = my_id.getLocalID() if _Debug: lg.out( _DebugLevel, "p2p_service.SendContacts to %s" % nameurl.GetName(remote_idurl)) PacketID = packetid.UniqueID() try: json_payload['type'] json_payload['space'] except: lg.err() return None Payload = serialization.DictToBytes(json_payload) result = signed.Packet( Command=commands.Contacts(), OwnerID=MyID, CreatorID=MyID, PacketID=PacketID, Payload=Payload, RemoteID=remote_idurl, ) gateway.outbox(result, wide=wide, callbacks=callbacks) return result
def _do_send_request_service(self, *args, **kwargs): if len(self.request_service_packet_id) >= 10: if _Debug: lg.warn('too many service requests to %r' % self.router_idurl) self.automat('service-refused', *args, **kwargs) return orig_identity = config.conf().getData('services/proxy-transport/my-original-identity').strip() if not orig_identity: orig_identity = my_id.getLocalIdentity().serialize(as_text=True) service_info = { 'name': 'service_proxy_server', 'payload': { 'identity': orig_identity, }, } newpacket = signed.Packet( commands.RequestService(), my_id.getIDURL(), my_id.getIDURL(), packetid.UniqueID(), serialization.DictToBytes(service_info, values_to_text=True), self.router_idurl, ) packet_out.create( newpacket, wide=False, callbacks={ commands.Ack(): self._on_request_service_ack, commands.Fail(): self._on_request_service_fail, None: lambda pkt_out: self.automat('request-timeout', pkt_out), }, response_timeout=30, ) self.request_service_packet_id.append(newpacket.PacketID)
def SendEvent(remote_idurl, event_id, payload=None, producer_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 and message_id: e_json['producer_id'] = producer_id e_json['message_id'] = message_id if created: 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 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 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 _do_send_request_service(self, *args, **kwargs): if len(self.request_service_packet_id) >= 3: if _Debug: lg.warn('too many service requests to %r' % self.router_idurl) self.automat('service-refused', *args, **kwargs) return orig_identity = config.conf().getData('services/proxy-transport/my-original-identity').strip() if not orig_identity: orig_identity = my_id.getLocalIdentity().serialize() service_info = { 'name': 'service_proxy_server', 'payload': { 'identity': strng.to_text(orig_identity), }, } service_info_raw = json.dumps(service_info) newpacket = signed.Packet( commands.RequestService(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), service_info_raw, self.router_idurl, ) packet_out.create(newpacket, wide=False, callbacks={ commands.Ack(): self._on_request_service_ack, commands.Fail(): self._on_request_service_fail, },) self.request_service_packet_id.append(newpacket.PacketID)
def SendCancelService(remote_idurl, service_name, json_payload={}, wide=False, callbacks={}): service_info = { 'name': service_name, 'payload': json_payload, } service_info_raw = json.dumps(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 doReadQueue(self, *args, **kwargs): """ Action method. """ result = message.send_message( json_data={ 'created': utime.get_sec1970(), 'payload': 'queue-read', 'last_sequence_id': self.last_sequence_id, 'queue_id': self.active_queue_id, 'consumer_id': self.member_id, }, recipient_global_id=self.active_broker_id, packet_id='queue_%s_%s' % (self.active_queue_id, packetid.UniqueID()), message_ack_timeout=config.conf().getInt( 'services/private-groups/message-ack-timeout'), skip_handshake=True, fire_callbacks=False, ) if _Debug: result.addErrback(lg.errback, debug=_Debug, debug_level=_DebugLevel, method='group_member.doReadQueue') result.addErrback(lambda err: self.automat('queue-read-failed', err))
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.getIDURL(), CreatorID=my_id.getIDURL(), 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 SendFiles( idurl, raw_list_files_info, packet_id=None, 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 not packet_id: packet_id = "%s:%s" % (global_id.UrlToGlobalID(idurl), packetid.UniqueID()) 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 doSendRequestService(self, *args, **kwargs): """ Action method. """ self.target_idurl.refresh() packet_id = packetid.UniqueID() if _Debug: lg.args(_DebugLevel, idurl=self.target_idurl, service=self.target_service, packet_id=packet_id) service_request_payload = self.request_service_params if callable(service_request_payload): service_request_payload = service_request_payload( self.target_idurl) out_packet = p2p_service.SendRequestService( remote_idurl=self.target_idurl, service_name=self.target_service, json_payload=service_request_payload, timeout=self.request_service_timeout, callbacks={ commands.Ack(): self._node_acked, commands.Fail(): self._node_failed, None: self._node_timed_out, }, packet_id=packet_id, ) self.requested_packet_id = out_packet.PacketID
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( Command=commands.CancelService(), 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) return result
def SendRequestService(remote_idurl, service_name, json_payload={}, wide=False, callbacks={}, timeout=10): 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( commands.RequestService(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), service_info_raw, remote_idurl, ) gateway.outbox(result, wide=wide, callbacks=callbacks, response_timeout=timeout) return result
def SendToIDs(idlist, wide=False, ack_handler=None, timeout_handler=None, response_timeout=20): """ Same, but send to many IDs and also check previous packets to not re-send. """ global _PropagateCounter if _Debug: lg.out(_DebugLevel, "propagate.SendToIDs to %d users, wide=%s" % (len(idlist), wide)) if ack_handler is None: ack_handler = HandleAck if timeout_handler is None: timeout_handler = HandleTimeOut LocalIdentity = my_id.getLocalIdentity() Payload = strng.to_bin(LocalIdentity.serialize()) alreadysent = set() totalsent = 0 inqueue = {} found_previous_packets = 0 for pkt_out in packet_out.queue(): if id_url.is_in(pkt_out.remote_idurl, idlist, as_field=False): if pkt_out.description.count('Identity'): if pkt_out.remote_idurl not in inqueue: inqueue[pkt_out.remote_idurl] = 0 inqueue[pkt_out.remote_idurl] += 1 found_previous_packets += 1 for contact in idlist: if not contact: continue if contact in alreadysent: # just want to send once even if both customer and supplier continue if contact in inqueue and inqueue[contact] > 2: # now only 2 protocols is working: tcp and udp if _Debug: lg.out(_DebugLevel, ' skip sending [Identity] to %s, packet already in the queue' % contact) continue p = signed.Packet( Command=commands.Identity(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=('propagate:%d:%s' % (_PropagateCounter, packetid.UniqueID())), Payload=Payload, RemoteID=contact, ) _PropagateCounter += 1 if _Debug: lg.out(_DebugLevel, " sending [Identity] to %s" % nameurl.GetName(contact)) 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(contact) p2p_stats.EraseMyProtosStates(contact) alreadysent.add(contact) totalsent += 1 del alreadysent return totalsent
def doSendRejectService(self, arg): """ Action method. """ space_dict, spent_bytes, current_customers, removed_customers = arg for customer_idurl in removed_customers: p2p_service.SendFailNoRequest(customer_idurl, packetid.UniqueID(), 'service rejected') events.send('existing-customer-terminated', dict(idurl=customer_idurl)) self.automat('packets-sent')
def SendRetrieveCoin(remote_idurl, query, wide=False, callbacks={}): if _Debug: lg.out(_DebugLevel, "p2p_service.SendRetrieveCoin to %s" % remote_idurl) outpacket = signed.Packet(commands.RetrieveCoin(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), json.dumps(query), remote_idurl) gateway.outbox(outpacket, wide=wide, callbacks=callbacks) return outpacket
def send_message(json_data, recipient_global_id, packet_id=None, timeout=None): """ Send command.Message() packet to remote peer. Returns Deferred (if remote_idurl was not cached yet) or outbox packet object. """ global _LastUserPingTime global _PingTrustIntervalSeconds if not packet_id: packet_id = packetid.UniqueID() lg.out( 4, "message.send_message to %s with PackteID=%s" % (recipient_global_id, packet_id)) remote_idurl = global_id.GlobalUserToIDURL(recipient_global_id) if not remote_idurl: return fail(Exception('invalid recipient')) ret = Deferred() if remote_idurl not in _LastUserPingTime: is_expired = True else: is_expired = time.time( ) - _LastUserPingTime[remote_idurl] > _PingTrustIntervalSeconds remote_identity = identitycache.FromCache(remote_idurl) if is_expired or remote_identity is None or not online_status.isOnline( remote_idurl): d = propagate.PingContact(remote_idurl, timeout=timeout or 5) d.addCallback(lambda response_tuple: on_ping_success( response_tuple, remote_idurl)) d.addCallback( lambda response_tuple: do_send_message(json_data, recipient_global_id, packet_id, timeout, result_defer=ret)) d.addErrback(lambda err: on_message_failed(remote_idurl, json_data, recipient_global_id, packet_id, None, None, result_defer=ret)) return ret try: do_send_message(json_data, recipient_global_id, packet_id, timeout, ret) except Exception as exc: lg.warn(str(exc)) on_message_failed( remote_idurl, json_data, recipient_global_id, packet_id, None, None, ) ret.errback(exc) return ret
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(commands.DeleteBackup(), MyID, MyID, PacketID, Payload, RemoteID) gateway.outbox(result) return result
def SendCancelService(remote_idurl, service_info, callbacks={}): if _Debug: lg.out( _DebugLevel, "p2p_service.SendCancelService [%s]" % service_info.replace('\n', ' ')[:40]) result = signed.Packet(commands.CancelService(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), service_info, remote_idurl) gateway.outbox(result, callbacks=callbacks) return result
def doSendHisFiles(self, arg): """ Action method. """ packet_id = '%s:%s' % ( global_id.UrlToGlobalID(self.customer_idurl), packetid.UniqueID(), ) list_files.send(self.customer_idurl, packet_id, settings.ListFilesFormat())
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), remote_idurl) gateway.outbox(outpacket, wide=wide, callbacks=callbacks) return outpacket
def SendRequestService(remote_idurl, service_info, wide=False, callbacks={}): if _Debug: lg.out( _DebugLevel, "p2p_service.SendRequestService to %s [%s]" % (nameurl.GetName(remote_idurl), service_info.replace('\n', ' ')[:40])) result = signed.Packet(commands.RequestService(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), service_info, remote_idurl) gateway.outbox(result, wide=wide, callbacks=callbacks) return result
def doSendMessageToBroker(self, *args, **kwargs): """ Action method. """ data = kwargs['json_payload'] data['msg_type'] = 'personal_message' data['action'] = 'read' self._do_send_message_to_broker( json_payload=data, outgoing_counter=None, packet_id='personal_%s' % packetid.UniqueID(), )