def doCancelServiceQueue(self, *args, **kwargs): """ Action method. """ service_info = { 'items': [{ 'scope': 'consumer', 'action': 'unsubscribe', 'consumer_id': strng.to_text(my_id.getGlobalID()), 'queue_id': global_id.MakeGlobalQueueID( queue_alias='supplier-file-modified', owner_id=my_id.getGlobalID(), supplier_id=global_id.MakeGlobalID(idurl=self.supplier_idurl), ), }, { 'scope': 'consumer', 'action': 'remove_callback', 'consumer_id': strng.to_text(my_id.getGlobalID()), 'method': strng.to_text(my_id.getLocalID()), }, { 'scope': 'consumer', 'action': 'stop', 'consumer_id': strng.to_text(my_id.getGlobalID()), }, ], } p2p_service.SendCancelService( remote_idurl=self.supplier_idurl, service_name='service_p2p_notifications', json_payload=service_info, callbacks={ commands.Ack(): self._supplier_acked, commands.Fail(): self._supplier_failed, }, )
def _on_list_files_failed(self, response, info, customer_idurl, supplier_idurl, key_id): from logs import lg from lib import strng from access import key_ring if strng.to_text( response.Payload) == 'key not registered' or strng.to_text( response.Payload) == '': lg.warn( 'supplier %r of customer %r do not possess public key %r yet, sending it now' % ( supplier_idurl, customer_idurl, key_id, )) result = key_ring.transfer_key(key_id, supplier_idurl, include_private=False) result.addCallback(lambda r: self._on_key_transfer_success( customer_idurl, supplier_idurl, key_id)) result.addErrback(lambda err: lg.err('failed sending key %r : %r' % ( key_id, err, ))) else: lg.err( 'failed requesting ListFiles() with %r for customer %r from supplier %r' % ( key_id, customer_idurl, supplier_idurl, )) return None
def Unserialize(data): """ We expect here a string containing a whole packet object in text form. Will return a real object in the memory from given string. All class fields are loaded, signature can be verified to be sure - it was truly original string. """ if data is None: return None dct = serialization.BytesToDict(data, keys_to_text=True) if _Debug: lg.out(_DebugLevel, 'signed.Unserialize %r' % dct) try: newobject = Packet( Command=strng.to_text(dct['m']), OwnerID=dct['o'], CreatorID=dct['c'], PacketID=strng.to_text(dct['i']), Date=strng.to_text(dct['d']), Payload=dct['p'], RemoteID=dct['r'], KeyID=strng.to_text(dct['k']), Signature=dct['s'], ) except: lg.exc() newobject = None if newobject is None: lg.warn("result is None") return None return newobject
def __init__(self, idurl): self.current = b'' self.current_as_string = '' self.current_id = '' self.latest = b'' self.latest_as_string = '' self.latest_id = '' self.latest_revision = -1 if isinstance(idurl, ID_URL_FIELD): self.current = idurl.current else: if idurl in [None, 'None', '', b'None', b'', False, ]: self.current = b'' else: self.current = strng.to_bin(idurl.strip()) self.current_as_string = strng.to_text(self.current) self.current_id = idurl_to_id(self.current) self.latest, self.latest_revision = get_latest_revision(self.current) if not self.latest: self.latest = self.current self.latest_revision = -1 self.latest_as_string = strng.to_text(self.latest) self.latest_id = idurl_to_id(self.latest) if _Debug: lg.out(_DebugLevel * 2, 'NEW ID_URL_FIELD(%r) with id=%r latest=%r' % (self.current, id(self), self.latest))
def __init__(self, Command, OwnerID, CreatorID, PacketID, Payload, RemoteID, KeyID=None, Date=None, Signature=None, ): """ Init all fields and sign the packet. """ # Legal Commands are in commands.py self.Command = strng.to_text(Command) # who owns this data and pays bills - http://cate.com/id1.xml self.OwnerID = strng.to_bin(OwnerID) # signer - http://cate.com/id1.xml - might be an authorized scrubber self.CreatorID = strng.to_bin(CreatorID) # string of the above 4 "Number"s with "-" separator to uniquely identify a packet # on the local machine. Can be used for filenames, and to prevent duplicates. self.PacketID = strng.to_text(PacketID) # create a string to remember current world time self.Date = strng.to_text(Date or utime.sec1970_to_datetime_utc().strftime("%Y/%m/%d %I:%M:%S %p")) # datetime.datetime.now().strftime("%Y/%m/%d %I:%M:%S %p") # main body of binary data self.Payload = strng.to_bin(Payload) # want full IDURL for other party so troublemaker could not # use his packets to mess up other nodes by sending it to them self.RemoteID = strng.to_bin(RemoteID) # which private key to use to generate signature self.KeyID = strng.to_text(KeyID or my_id.getGlobalID(key_alias='master')) if Signature: self.Signature = Signature else: # signature on Hash is always by CreatorID self.Signature = None # must be signed to be valid self.Sign() # stores list of related objects packet_in() or packet_out() self.Packets = []
def Unserialize(data, decrypt_key=None): """ A method to create a ``encrypted.Block`` instance from input string. """ dct = serialization.BytesToDict(data, keys_to_text=True, encoding='utf-8') if _Debug: lg.out(_DebugLevel, 'encrypted.Unserialize %s' % repr(dct)[:100]) try: newobject = Block( CreatorID=id_url.field(dct['c']), BackupID=strng.to_text(dct['b']), BlockNumber=dct['n'], LastBlock=dct['e'], EncryptedSessionKey=base64.b64decode(strng.to_bin(dct['k'])), SessionKeyType=strng.to_text(dct['t']), Length=dct['l'], EncryptedData=dct['p'], Signature=dct['s'], DecryptKey=decrypt_key, ) except: lg.exc() if _Debug: lg.out(_DebugLevel, repr(dct)) return None return newobject
def toDict(self, include_private=False, output_format_private='PEM', output_format_public='OpenSSH'): if not self.keyObject: raise ValueError('key object is not exist') if include_private and not self.keyObject.has_private(): raise ValueError('this key contains only public component') if include_private: key_body = strng.to_text( self.keyObject.exportKey(format=output_format_private)) else: key_body = strng.to_text(self.keyObject.publickey().exportKey( format=output_format_public)) key_dict = { 'body': key_body, 'local_key_id': self.local_key_id, 'label': self.label, } if self.isSigned(): key_dict.update({ 'signature': self.signed[0], 'signature_pubkey': self.signed[1], }) return key_dict
def doSetUp(self, arg): """ Action method. """ self.hostname = settings.getIdServerHost() if self.hostname == '': self.hostname = strng.to_bin(misc.readExternalIP()) # bpio.ReadTextFile(settings.ExternalIPFilename()) if self.hostname == '': self.hostname = net_misc.getLocalIp() lg.out(4, 'id_server.doSetUp hostname=%s' % strng.to_text(self.hostname)) if not os.path.isdir(settings.IdentityServerDir()): os.makedirs(settings.IdentityServerDir()) lg.out(4, ' created a folder %s' % settings.IdentityServerDir()) root = WebRoot() root.putChild(b'', WebMainPage()) try: self.tcp_listener = reactor.listenTCP(self.tcp_port, IdServerFactory()) lg.out(4, " identity server listen on TCP port %d started" % (self.tcp_port)) except: lg.out(4, "id_server.set_up ERROR exception trying to listen on port " + str(self.tcp_port)) lg.exc() try: self.web_listener = reactor.listenTCP(self.web_port, server.Site(root)) lg.out(4, " have started web server at http://%s:%d " % (strng.to_text(self.hostname), self.web_port)) except: lg.out(4, "id_server.set_up ERROR exception trying to listen on port " + str(self.web_port)) lg.exc()
def osinfofull(): """ Return detailed system info. """ import pprint o = '' o += '=====================================================\n' o += '=====================================================\n' o += '=====================================================\n' o += 'platform.uname(): ' + strng.to_text(platform.uname()) + '\n' try: o += '__file__: ' + strng.to_text(__file__) + '\n' except: o += 'variable __file__ is not defined\n' o += 'sys.executable: ' + sys.executable + '\n' o += 'os.path.abspath("."): ' + os.path.abspath('.') + '\n' o += 'os.path.abspath(sys.argv[0]): ' + os.path.abspath(sys.argv[0]) + '\n' o += 'os.path.expanduser("~"): ' + os.path.expanduser('~') + '\n' o += 'sys.argv: ' + pprint.pformat(sys.argv) + '\n' o += 'sys.path:\n' + pprint.pformat(sys.path) + '\n' o += 'os.environ:\n' + pprint.pformat(list(os.environ.items())) + '\n' o += '=====================================================\n' o += '=====================================================\n' o += '=====================================================\n' return o
def render(self, request): src = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Identities on %(hostname)s</title> <style> body{margin: 0 auto; padding: 0;} #content {margin: 0 auto; padding: 0; text-align: justify; line-height: 1.7; min-height: 500px; width: 960px; font-size: 18px; text-decoration: none; font-family: "Tw Cen MT", "Century Gothic", Futura, Arial, sans-serif;} </style> </head> <body> <div id="content"> <h1 align=center>Identities on %(hostname)s</h1> ''' % { 'hostname': strng.to_text(A().hostname) } src += '<table cellspacing=0 width=100% border=0><tr valign=top>\n' src += '<td width=152px nowrap>\n' HTDOCS_DIR = settings.IdentityServerDir() files = [] if os.path.isdir(HTDOCS_DIR): for filename in os.listdir(HTDOCS_DIR): filepath = os.path.join(HTDOCS_DIR, filename) if os.path.isdir(filepath): continue if not filename.endswith('.xml'): continue files.append(filename) files.sort() currentChar = '' charIndex = 0 for filename in files: if filename[0] != currentChar: currentChar = filename[0] if charIndex % 4 == 3: src += '\n</td>\n<td width=152px nowrap>\n' charIndex += 1 src += '\n<br>\n<h3>%s</h3>\n' % str(currentChar).upper() url = '/' + filename name = filename[:-4] src += '<p><a href="%s"><nobr>%s</nobr></a></p>\n' % (url, name) src += '</td>\n</tr>\n</table>\n</td>\n</tr>\n<tr><td align=left>' src += '<br><br><p>Total identities on "%s": %d</p><br><br>\n' % ( strng.to_text(A().hostname), len(files)) src += '<p>Other known identity servers:\n' for idhost in sorted(known_servers.by_host().keys()): idport = known_servers.by_host()[idhost][0] if idport != 80: idhost += ':%d' % idport src += '<a href="http://%s/"><nobr>%s</nobr></a> \n' % ( idhost, idhost) src += '</p>' src += '</body>\n</html>' del files return strng.to_bin(src)
def MakeGlobalQueueID(queue_alias, owner_id, supplier_id): """ """ global _FORMAT_GLOBAL_ID_QUEUE_ID return _FORMAT_GLOBAL_ID_QUEUE_ID.format( queue_alias=strng.to_text(queue_alias), owner_id=strng.to_text(owner_id), supplier_id=strng.to_text(supplier_id), )
def doStartListening(self, *args, **kwargs): """ Action method. """ try: _, info = args[0] self.router_proto_host = (info.proto, info.host) except: try: s = config.conf().getString('services/proxy-transport/current-router').strip() _, router_proto, router_host = s.split(' ') self.router_proto_host = (router_proto, strng.to_bin(router_host), ) except: lg.exc() self.router_identity = identitycache.FromCache(self.router_idurl) config.conf().setString('services/proxy-transport/current-router', '%s %s %s' % ( strng.to_text(self.router_idurl), strng.to_text(self.router_proto_host[0]), strng.to_text(self.router_proto_host[1]), )) current_identity = my_id.getLocalIdentity().serialize() previous_identity = ReadMyOriginalIdentitySource() if previous_identity: lg.warn('my original identity is not empty, SKIP overwriting') lg.out(2, '\nPREVIOUS ORIGINAL IDENTITY:\n%s\n' % current_identity) else: WriteMyOriginalIdentitySource(current_identity) lg.warn('current identity was stored as my-original-identity') self.request_service_packet_id = [] callback.insert_inbox_callback(0, self._on_inbox_packet_received) if contact_status.isKnown(self.router_idurl): contact_status.A(self.router_idurl).addStateChangedCallback( self._on_router_contact_status_connected, newstate='CONNECTED') contact_status.A(self.router_idurl).addStateChangedCallback( self._on_router_contact_status_offline, newstate='OFFLINE') active_router_sessions = gateway.find_active_session(info.proto, info.host) if active_router_sessions: self.router_connection_info = { 'id': active_router_sessions[0].id, 'index': active_router_sessions[0].index, 'proto': info.proto, 'host': info.host, 'idurl': self.router_idurl, 'global_id': global_id.UrlToGlobalID(self.router_idurl), } active_router_session_machine = automat.objects().get(self.router_connection_info['index'], None) if active_router_session_machine: active_router_session_machine.addStateChangedCallback( self._on_router_session_disconnected, oldstate='CONNECTED') lg.info('connected to proxy router and set active session: %s' % self.router_connection_info) else: lg.err('not found proxy router session state machine: %s' % self.router_connection_info['index']) else: lg.err('active connection with proxy router at %s:%s was not found' % (info.proto, info.host, )) if _Debug: lg.out(2, 'proxy_receiver.doStartListening !!!!!!! router: %s at %s://%s' % ( self.router_idurl, self.router_proto_host[0], self.router_proto_host[1]))
def save_correspondents(path=None): """ Write current correspondents list on the disk, ``path`` is a file path to save. """ if path is None: path = settings.CorrespondentIDsFilename() lst = ["%s %s" % (strng.to_text(t[0]), strng.to_text(t[1]),) for t in correspondents()] bpio._write_list(path, lst)
def __init__( self, CreatorID=None, BackupID='', BlockNumber=0, SessionKey='', SessionKeyType=None, LastBlock=True, Data=b'', EncryptKey=None, DecryptKey=None, EncryptedSessionKey=None, EncryptedData=None, Length=None, Signature=None, ): self.CreatorID = CreatorID if not self.CreatorID: self.CreatorID = my_id.getLocalID() if not isinstance(self.CreatorID, id_url.ID_URL_FIELD): self.CreatorID = id_url.field(self.CreatorID) self.BackupID = strng.to_text(BackupID) self.BlockNumber = BlockNumber self.LastBlock = bool(LastBlock) self.SessionKeyType = SessionKeyType or key.SessionKeyType() if EncryptedSessionKey: # this block to be decrypted after receiving self.EncryptedSessionKey = EncryptedSessionKey else: # this block to be encrypted before sending if callable(EncryptKey): self.EncryptedSessionKey = EncryptKey(SessionKey) elif strng.is_text(EncryptKey): self.EncryptedSessionKey = my_keys.encrypt( EncryptKey, SessionKey) elif strng.is_bin(EncryptKey): self.EncryptedSessionKey = my_keys.encrypt( strng.to_text(EncryptKey), SessionKey) else: self.EncryptedSessionKey = key.EncryptLocalPublicKey( SessionKey) if EncryptedData and Length is not None: self.Length = Length self.EncryptedData = EncryptedData else: self.Length = len(Data) self.EncryptedData = key.EncryptWithSessionKey( SessionKey, Data, session_key_type=self.SessionKeyType) if Signature: self.Signature = Signature else: self.Signature = None self.Sign(signing_key=EncryptKey) self.DecryptKey = DecryptKey if _Debug: lg.out(_DebugLevel, 'new data in %s' % self)
def __init__(self, recipient, sender=None, encrypted_session=None, encrypted_body=None): self.sender = strng.to_text(sender or my_id.getGlobalID(key_alias='master')) self.recipient = strng.to_text(recipient) self.encrypted_session = encrypted_session self.encrypted_body = encrypted_body
def sign_key_info(key_info): key_info['signature_pubkey'] = key.MyPublicKey() sorted_fields = sorted(key_info.keys()) hash_items = [] for field in sorted_fields: if field not in ['include_private', 'signature', 'private', ]: hash_items.append(strng.to_text(key_info[field])) hash_text = '-'.join(hash_items) hash_bin = key.Hash(strng.to_bin(hash_text)) key_info['signature'] = strng.to_text(key.Sign(hash_bin)) return key_info
def getContactsAsTuples(self, as_text=False): """ """ result = [] for c in self.contacts: proto, host = c.split(b'://') if as_text: result.append((strng.to_text(proto), strng.to_text(host))) else: result.append((proto, host)) return result
def osinfo(): """ Return full OS info, like: "Linux-2.6.32.9-rscloud-x86_64-with- Ubuntu-12.04-precise" or "Windows-XP-5.1.2600-SP3". """ if Android(): try: u = platform.uname() return '%s %s %s' % (strng.to_text(u[0]), strng.to_text(u[2]), strng.to_text(u[3])) except: return 'Android' return strng.to_text(platform.platform()).strip()
def __init__(self, recipient_global_id, sender=None, encrypted_session=None, encrypted_body=None): self.sender = strng.to_text(sender or my_id.getGlobalID(key_alias='master')) self.recipient = strng.to_text(recipient_global_id) self.encrypted_session = encrypted_session self.encrypted_body = encrypted_body if _Debug: lg.out(_DebugLevel, 'message.%s created' % self)
def UrlMake(protocol='', machine='', port='', filename='', parts=None): """ Reverse method, create a URL from 4 pieces. """ if parts is not None: protocol, machine, port, filename = parts url = protocol + '://' + strng.to_text(machine) if port: url += ':' + strng.to_text(port) if filename: url += '/' + filename return strng.to_bin(url)
def make_key_info(key_object, key_id=None, key_alias=None, creator_idurl=None, include_private=False, generate_signature=False, include_signature=False, include_local_id=False, include_label=True, event=None): if key_id: key_id = latest_key_id(key_id) key_alias, creator_idurl = split_key_id(key_id) else: key_id = make_key_id(alias=key_alias, creator_idurl=id_url.field(creator_idurl)) r = { 'key_id': key_id, 'alias': key_alias, 'creator': creator_idurl, 'public': strng.to_text(key_object.toPublicString()) if key_object else None, 'private': None, 'include_private': include_private, } if event: r['event'] = event if include_label: r['label'] = key_object.label if key_object else '' if key_object and key_object.isPublic(): r['is_public'] = True if include_private: raise Exception('this key contains only public component') else: r['is_public'] = not include_private if include_private: r['private'] = strng.to_text( key_object.toPrivateString()) if key_object else None if key_object and hasattr(key_object, 'size'): r['size'] = strng.to_text(key_object.size()) else: r['size'] = '0' if include_local_id: r['local_key_id'] = getattr(key_object, 'local_key_id', None) if key_object else None if key_object and generate_signature: r = sign_key_info(r) else: if include_signature and key_object and key_object.isSigned(): r['signature'] = key_object.signed[0] r['signature_pubkey'] = key_object.signed[1] return r
def set_identity(idurl, raw_xml_data): if _Debug: lg.args(_DebugLevel, idurl) return dht_service.set_valid_data( key=idurl, json_data={ 'type': 'identity', 'timestamp': utime.get_sec1970(), 'idurl': strng.to_text(idurl), 'identity': strng.to_text(raw_xml_data), }, rules=get_rules('identity'), )
def deserialize(cls, input_string): try: dct = serialization.BytesToDict(input_string, keys_to_text=True, encoding='utf-8') message_obj = cls( recipient=strng.to_text(dct['r']), sender=strng.to_text(dct['s']), encrypted_session=base64.b64decode(strng.to_bin(dct['k'])), encrypted_body=dct['p'], ) except: lg.exc() return None return message_obj
def msg(self, msgid, *args, **kwargs): msg = self.MESSAGES.get(msgid, ['', 'black']) text = msg[0] % { 'login': strng.to_text(bpio.ReadTextFile(settings.UserNameFilename())), 'externalip': strng.to_text(misc.readExternalIP()), 'localip': strng.to_text(bpio.ReadTextFile(settings.LocalIPFilename())), } color = 'black' if len(msg) == 2: color = msg[1] return text, color
def ParseGlobalQueueID(inp): global _REGEX_GLOBAL_ID_QUEUE_ID ret = { 'queue_alias': '', 'owner_id': '', 'supplier_id': '', } result = re.match(_REGEX_GLOBAL_ID_QUEUE_ID, inp) if not result: return ret ret['queue_alias'] = strng.to_text(result.group('queue_alias')) ret['owner_id'] = strng.to_text(result.group('owner_id')) ret['supplier_id'] = strng.to_text(result.group('supplier_id')) return ret
def _pack_dict(dictionary, sort=False): """ The core method, convert dictionary to the string. Every line in resulted string will contain a key, value pair, separated with single space. So keys and must not contain spaces. Values must not contain new lines. If ``sort`` is True the resulted string will be sorted by keys. """ if sort: seq = sorted(dictionary.keys()) else: seq = list(dictionary.keys()) return u'\n'.join([u'%s %s' % (k, strng.to_text(strng.to_text(dictionary[k]))) for k in seq])
def _request_arg(request, key, default='', mandatory=False): """ Simplify extracting arguments from url query in request. """ args = request.args or {} if key in args: values = args.get(key, [default, ]) return strng.to_text(values[0]) if values else default if strng.to_bin(key) in args: values = args.get(strng.to_bin(key), [default, ]) return strng.to_text(values[0]) if values else default if mandatory: raise Exception('mandatory url query argument missed: %s' % key) return default
def doSaveRouteProtoHost(self, *args, **kwargs): """ Action method. """ idurl, _, item, _, _, _ = args[0] self.routes[idurl]['address'].append(( strng.to_text(item.proto), strng.to_text(item.host), )) self._write_route(idurl) if _Debug: lg.out( _DebugLevel, 'proxy_router.doSaveRouteProtoHost : active address %s://%s added for %s' % (item.proto, item.host, nameurl.GetName(idurl)))
def sign_key_info(key_info): key_info['signature_pubkey'] = key.MyPublicKey() hash_items = [] for field in [ 'alias', 'public', 'signature_pubkey', ]: hash_items.append(strng.to_text(key_info[field])) hash_text = '-'.join(hash_items) if _Debug: lg.dbg(_DebugLevel, hash_text) hash_bin = key.Hash(strng.to_bin(hash_text)) key_info['signature'] = strng.to_text(key.Sign(hash_bin)) return key_info
def doRequestQueueService(self, *args, **kwargs): """ Action method. """ if not self.queue_subscribe: reactor.callLater(0, self.automat, 'queue-skip') # @UndefinedVariable return service_info = { 'items': [ { 'scope': 'consumer', 'action': 'start', 'consumer_id': strng.to_text(my_id.getGlobalID()), }, { 'scope': 'consumer', 'action': 'add_callback', 'consumer_id': strng.to_text(my_id.getGlobalID()), 'method': strng.to_text(my_id.getLocalID()), }, { 'scope': 'consumer', 'action': 'subscribe', 'consumer_id': strng.to_text(my_id.getGlobalID()), 'queue_id': global_id.MakeGlobalQueueID( queue_alias='supplier-file-modified', owner_id=my_id.getGlobalID(), supplier_id=global_id.MakeGlobalID( idurl=self.supplier_idurl), ), }, ], } request = p2p_service.SendRequestService( remote_idurl=self.supplier_idurl, service_name='service_p2p_notifications', json_payload=service_info, callbacks={ commands.Ack(): self._supplier_queue_acked, commands.Fail(): self._supplier_queue_failed, }, ) self.request_queue_packet_id = request.PacketID