Ejemplo n.º 1
0
 def doSaveMyIdentity(self, arg):
     """
     Action method.
     """
     lg.out(4, 'id_registrator.doSaveMyIdentity %s' % self.new_identity)
     my_id.setLocalIdentity(self.new_identity)
     my_id.saveLocalIdentity()
Ejemplo n.º 2
0
 def doSaveMyIdentity(self, *args, **kwargs):
     """
     Action method.
     """
     lg.out(4, 'id_registrator.doSaveMyIdentity %s' % self.new_identity)
     my_id.setLocalIdentity(self.new_identity)
     my_id.saveLocalIdentity()
Ejemplo n.º 3
0
def test1():
    """
    Some tests.
    """
    from userid import my_id
    myidentity = my_id.getLocalIdentity()
    print 'getIP =', myidentity.getIP()
    if myidentity.Valid():
        print "myidentity is Valid!!!!"
    else:
        print "myidentity is not Valid"
        my_id.saveLocalIdentity()  # sign and save
        raise Exception("myidentity is not Valid")
    print "myidentity.contacts"
    print myidentity.contacts
    print "len myidentity.contacts "
    print len(myidentity.contacts)
    print "len myidentity.contacts[0] "
    print myidentity.contacts[0]
    con = myidentity.getContact()
    print "con:", con, type(con)
    protocol, machine, port, filename = nameurl.UrlParse(con)
    print protocol, machine, port, filename
    print "identity.main serialize:\n", myidentity.serialize()
    for index in range(myidentity.getContactsNumber()):
        proto, host, port, filename = myidentity.getContactParts(index)
        print '[%s] [%s] [%s] [%s]' % (proto, host, port, filename)
Ejemplo n.º 4
0
def test1():
    """
    Some tests.
    """
    from userid import my_id
    myidentity = my_id.getLocalIdentity()
    print 'getIP =', myidentity.getIP()
    if myidentity.Valid():
        print "myidentity is Valid!!!!"
    else:
        print "myidentity is not Valid"
        my_id.saveLocalIdentity()            # sign and save
        raise Exception("myidentity is not Valid")
    print "myidentity.contacts"
    print myidentity.contacts
    print "len myidentity.contacts "
    print len(myidentity.contacts)
    print "len myidentity.contacts[0] "
    print myidentity.contacts[0]
    con = myidentity.getContact()
    print "con:", con, type(con)
    protocol, machine, port, filename = nameurl.UrlParse(con)
    print protocol, machine, port, filename
    print "identity.main serialize:\n", myidentity.serialize()
    for index in range(myidentity.getContactsNumber()):
        proto, host, port, filename = myidentity.getContactParts(index)
        print '[%s] [%s] [%s] [%s]' % (proto, host, port, filename)
Ejemplo n.º 5
0
def main():
    """
    This should print a current identity or create a new one.
    """
    settings.init(base_dir='/tmp/fake_id')
    from userid import my_id
    my_id.loadLocalIdentity()
    if my_id.isLocalIdentityReady():
        my_id.getLocalIdentity().sign()
        print(my_id.getLocalIdentity().serialize())
        print('Valid is: ', my_id.getLocalIdentity().Valid())
    else:
        if not key.InitMyKey():
            key.GenerateNewKey()
        ipaddr = '127.0.0.1'
        if len(sys.argv) > 2:
            ipaddr = sys.argv[2]
        rev = 0
        if len(sys.argv) > 3:
            rev = int(sys.argv[3])
        idurls = []
        if len(sys.argv) > 4:
            idurls = sys.argv[4:]
        my_id.setLocalIdentity(
            my_id.buildDefaultIdentity(name=sys.argv[1],
                                       ip=ipaddr,
                                       idurls=idurls,
                                       revision=rev))
        my_id.saveLocalIdentity()
        print(my_id.getLocalIdentity().serialize())
        print('Valid is: ', my_id.getLocalIdentity().Valid())
        my_id._LocalIdentity = None
        my_id.loadLocalIdentity()
    settings.shutdown()
Ejemplo n.º 6
0
def update():
    """
    A good way to check all things - load and sign again.
    """
    from userid import my_id
    bpio.init()
    settings.init()
    src = bpio.ReadTextFile(settings.LocalIdentityFilename())
    my_id.setLocalIdentity(identity(xmlsrc=src))
    my_id.getLocalIdentity().sign()
    my_id.saveLocalIdentity()
    print my_id.getLocalIdentity().serialize()
Ejemplo n.º 7
0
def update():
    """
    A good way to check all things - load and sign again.
    """
    from userid import my_id
    bpio.init()
    settings.init()
    src = bpio.ReadTextFile(settings.LocalIdentityFilename())
    my_id.setLocalIdentity(identity(xmlsrc=src))
    my_id.getLocalIdentity().sign()
    my_id.saveLocalIdentity()
    print my_id.getLocalIdentity().serialize()
Ejemplo n.º 8
0
def update():
    """
    A good way to check all things - load and sign again.
    Also will test rebuilding of the identity
    """
    from userid import my_id
    bpio.init()
    settings.init()
    src = bpio.ReadTextFile(settings.LocalIdentityFilename())
    print(src)
    my_id.setLocalIdentity(identity(xmlsrc=src))
    my_id.getLocalIdentity().sign()
    my_id.saveLocalIdentity()
    print(my_id.getLocalIdentity().serialize())
    print(my_id.rebuildLocalIdentity(revision_up=True))
Ejemplo n.º 9
0
def main():
    """
    This should print a current identity or create a new one.
    """
    from userid import my_id
    my_id.loadLocalIdentity()
    if my_id.isLocalIdentityReady():
        my_id.getLocalIdentity().sign()
        print my_id.getLocalIdentity().serialize()
        print 'Valid is: ', my_id.getLocalIdentity().Valid()
    else:
        my_id.setLocalIdentity(my_id.buildDefaultIdentity(sys.argv[1]))
        my_id.saveLocalIdentity()
        print my_id.getLocalIdentity().serialize()
        print 'Valid is: ', my_id.getLocalIdentity().Valid()
        my_id._LocalIdentity = None
        my_id.loadLocalIdentity()
Ejemplo n.º 10
0
def main():
    """
    This should print a current identity or create a new one.
    """
    from userid import my_id
    my_id.loadLocalIdentity()
    if my_id.isLocalIdentityReady():
        my_id.getLocalIdentity().sign()
        print my_id.getLocalIdentity().serialize()
        print 'Valid is: ', my_id.getLocalIdentity().Valid()
    else:
        my_id.setLocalIdentity(my_id.buildDefaultIdentity(sys.argv[1]))
        my_id.saveLocalIdentity()
        print my_id.getLocalIdentity().serialize()
        print 'Valid is: ', my_id.getLocalIdentity().Valid()
        my_id._LocalIdentity = None
        my_id.loadLocalIdentity()
Ejemplo n.º 11
0
 def _pop_active_proto(self):
     if len(active_protos()) == 0:
         return
     lid = my_id.getLocalIdentity()
     order = lid.getProtoOrder()
     first = order[0]
     wantedproto = ''
     # if first contact in local identity is not working yet
     # but there is another working methods - switch first method
     if first not in active_protos():
         # take (but not remove) any item from the set
         wantedproto = active_protos().pop()
         active_protos().add(wantedproto)
     # if proxy method is not the first but it works - switch to proxy
     if first != 'proxy' and 'proxy' in active_protos():
         wantedproto = 'proxy'
     # if http method is not the first but it works - switch to http
     if first != 'http' and 'http' in active_protos():
         wantedproto = 'http'
     # if udp method is not the first but it works - switch to udp
     if first != 'udp' and 'udp' in active_protos():
         wantedproto = 'udp'
     # if tcp method is not the first but it works - switch to tcp
     if first != 'tcp' and 'tcp' in active_protos():
         wantedproto = 'tcp'
     if _Debug:
         lg.out(
             _DebugLevel - 6,
             'p2p_connector.PopWorkingProto will pop %s contact order=%s active_protos()=%s'
             % (wantedproto, str(order), str(active_protos())))
     # now move best proto on the top
     # other users will use this method to send to us
     lid.popProtoContact(wantedproto)
     # save local id
     # also need to propagate our identity
     # other users must know our new contacts
     my_id.setLocalIdentity(lid)
     my_id.saveLocalIdentity()
Ejemplo n.º 12
0
    def doVerifyAndRestore(self, arg):
        global _WorkingKey
        lg.out(4, 'identity_restorer.doVerifyAndRestore')

        remote_identity_src = arg

        if os.path.isfile(settings.KeyFileName()):
            lg.out(
                4,
                'identity_restorer.doVerifyAndRestore will backup and remove '
                + settings.KeyFileName())
            bpio.backup_and_remove(settings.KeyFileName())

        if os.path.isfile(settings.LocalIdentityFilename()):
            lg.out(
                4,
                'identity_restorer.doVerifyAndRestore will backup and remove '
                + settings.LocalIdentityFilename())
            bpio.backup_and_remove(settings.LocalIdentityFilename())

        try:
            remote_ident = identity.identity(xmlsrc=remote_identity_src)
            local_ident = identity.identity(xmlsrc=remote_identity_src)
        except:
            # lg.exc()
            reactor.callLater(0.1, self.automat, 'restore-failed',
                              ('remote identity have incorrect format', 'red'))
            return

        lg.out(
            4, 'identity_restorer.doVerifyAndRestore checking remote identity')
        try:
            res = remote_ident.isCorrect()
        except:
            lg.exc()
            res = False
        if not res:
            lg.out(
                4,
                'identity_restorer.doVerifyAndRestore remote identity is not correct FAILED!!!!'
            )
            reactor.callLater(0.1, self.automat, 'restore-failed',
                              ('remote identity format is not correct', 'red'))
            return

        lg.out(
            4, 'identity_restorer.doVerifyAndRestore validate remote identity')
        try:
            res = remote_ident.Valid()
        except:
            lg.exc()
            res = False
        if not res:
            lg.out(
                4,
                'identity_restorer.doVerifyAndRestore validate remote identity FAILED!!!!'
            )
            reactor.callLater(0.1, self.automat, 'restore-failed',
                              ('remote identity is not valid', 'red'))
            return

        key.ForgetMyKey()
        bpio.WriteFile(settings.KeyFileName(), _WorkingKey)
        try:
            key.InitMyKey()
        except:
            key.ForgetMyKey()
            # lg.exc()
            try:
                os.remove(settings.KeyFileName())
            except:
                pass
            reactor.callLater(0.1, self.automat, 'restore-failed',
                              ('private key is not valid', 'red'))
            return

        try:
            local_ident.sign()
        except:
            # lg.exc()
            reactor.callLater(0.1, self.automat, 'restore-failed',
                              ('error while signing identity', 'red'))
            return

        if remote_ident.signature != local_ident.signature:
            reactor.callLater(
                0.1, self.automat, 'restore-failed',
                ('signature did not match, key verification failed!', 'red'))
            return

        my_id.setLocalIdentity(local_ident)
        my_id.saveLocalIdentity()

        bpio.WriteFile(settings.UserNameFilename(), my_id.getIDName())

        if os.path.isfile(settings.KeyFileName() + '.backup'):
            lg.out(
                4,
                'identity_restorer.doVerifyAndRestore will remove backup file for '
                + settings.KeyFileName())
            bpio.remove_backuped_file(settings.KeyFileName())

        if os.path.isfile(settings.LocalIdentityFilename() + '.backup'):
            lg.out(
                4,
                'identity_restorer.doVerifyAndRestore will remove backup file for '
                + settings.LocalIdentityFilename())
            bpio.remove_backuped_file(settings.LocalIdentityFilename())

        reactor.callLater(0.1, self.automat, 'restore-success')
Ejemplo n.º 13
0
    def doVerifyAndRestore(self, arg):
        global _WorkingKey
        lg.out(4, 'identity_restorer.doVerifyAndRestore')

        remote_identity_src = arg

        if os.path.isfile(settings.KeyFileName()):
            lg.out(4, 'identity_restorer.doVerifyAndRestore will backup and remove ' + settings.KeyFileName())
            bpio.backup_and_remove(settings.KeyFileName())

        if os.path.isfile(settings.LocalIdentityFilename()):
            lg.out(4, 'identity_restorer.doVerifyAndRestore will backup and remove ' + settings.LocalIdentityFilename())
            bpio.backup_and_remove(settings.LocalIdentityFilename())

        try:
            remote_ident = identity.identity(xmlsrc=remote_identity_src)
            local_ident = identity.identity(xmlsrc=remote_identity_src)
        except:
            # lg.exc()
            reactor.callLater(0.1, self.automat, 'restore-failed', ('remote identity have incorrect format', 'red'))
            return

        lg.out(4, 'identity_restorer.doVerifyAndRestore checking remote identity')
        try:
            res = remote_ident.isCorrect()
        except:
            lg.exc()
            res = False
        if not res:
            lg.out(4, 'identity_restorer.doVerifyAndRestore remote identity is not correct FAILED!!!!')
            reactor.callLater(0.1, self.automat, 'restore-failed', ('remote identity format is not correct', 'red'))
            return

        lg.out(4, 'identity_restorer.doVerifyAndRestore validate remote identity')
        try:
            res = remote_ident.Valid()
        except:
            lg.exc()
            res = False
        if not res:
            lg.out(4, 'identity_restorer.doVerifyAndRestore validate remote identity FAILED!!!!')
            reactor.callLater(0.1, self.automat, 'restore-failed', ('remote identity is not valid', 'red'))
            return

        key.ForgetMyKey()
        bpio.WriteFile(settings.KeyFileName(), _WorkingKey)
        try:
            key.InitMyKey()
        except:
            key.ForgetMyKey()
            # lg.exc()
            try:
                os.remove(settings.KeyFileName())
            except:
                pass
            reactor.callLater(0.1, self.automat, 'restore-failed', ('private key is not valid', 'red'))
            return

        try:
            local_ident.sign()
        except:
            # lg.exc()
            reactor.callLater(0.1, self.automat, 'restore-failed', ('error while signing identity', 'red'))
            return

        if remote_ident.signature != local_ident.signature:
            reactor.callLater(0.1, self.automat, 'restore-failed', ('signature did not match, key verification failed!', 'red'))
            return

        my_id.setLocalIdentity(local_ident)
        my_id.saveLocalIdentity()

        bpio.WriteFile(settings.UserNameFilename(), my_id.getIDName())

        if os.path.isfile(settings.KeyFileName() + '.backup'):
            lg.out(4, 'identity_restorer.doVerifyAndRestore will remove backup file for ' + settings.KeyFileName())
            bpio.remove_backuped_file(settings.KeyFileName())

        if os.path.isfile(settings.LocalIdentityFilename() + '.backup'):
            lg.out(4, 'identity_restorer.doVerifyAndRestore will remove backup file for ' + settings.LocalIdentityFilename())
            bpio.remove_backuped_file(settings.LocalIdentityFilename())

        reactor.callLater(0.1, self.automat, 'restore-success')