Ejemplo n.º 1
0
def CreateNewIdentity():
    global _NewIdentity
    
    dhncrypto.InitMyKey()
    misc.loadLocalIdentity()
    if misc.isLocalIdentityReady():
        try:
            lid = misc.getLocalIdentity()
            lid.sign()
            # misc.setLocalIdentity(lid)
            # misc.saveLocalIdentity()
            valid = lid.Valid()
        except:
            valid = False
            dhnio.DprintException()
        if valid:
            _NewIdentity = lid
            return
        dhnio.Dprint(2, 'identity_registrator.CreateNewIdentity existing local identity is not VALID')

    login = dhnio.ReadTextFile(settings.UserNameFilename())
    externalIP = dhnio.ReadTextFile(settings.ExternalIPFilename())
    localIP = dhnio.ReadTextFile(settings.LocalIPFilename())

    dhnio.Dprint(4, 'identity_registrator.CreateNewIdentity %s %s ' % (login, externalIP))
    
    idurl = 'http://'+settings.IdentityServerName()+'/'+login.lower()+'.xml'
    ident = identity.identity( )
    ident.sources.append(idurl)

    cdict = {}
    if settings.enableTCP():
        cdict['tcp'] = 'tcp://'+externalIP+':'+settings.getTCPPort()
    if settings.enableCSpace() and transport_control._TransportCSpaceEnable:
        cdict['cspace'] = 'cspace://'
        if settings.getCSpaceKeyID() != '':
            cdict['cspace'] += settings.getCSpaceKeyID()
    if settings.enableUDP() and transport_control._TransportUDPEnable:
        if stun.getUDPClient() is not None:
            if stun.getUDPClient().externalAddress is not None: # _altStunAddress
                cdict['udp'] = 'udp://'+stun.getUDPClient().externalAddress[0]+':'+str(stun.getUDPClient().externalAddress[1])
        
    for c in misc.validTransports:
        if cdict.has_key(c):
            ident.contacts.append(cdict[c])

    ident.publickey = dhncrypto.MyPublicKey()
    ident.date = time.ctime() #time.strftime('%b %d, %Y')

    revnum = dhnio.ReadTextFile(settings.RevisionNumberFile()).strip()
    repo, location = misc.ReadRepoLocation()
    ident.version = (revnum.strip() + ' ' + repo.strip() + ' ' + dhnio.osinfo().strip()).strip()

    ident.sign()
    
    dhnio.WriteFile(settings.LocalIdentityFilename()+'.new', ident.serialize())
    
    _NewIdentity = ident
Ejemplo n.º 2
0
 def _start_transports():
     startlist = []
     for contact in misc.getLocalIdentity().getContacts():
         proto, host, port, filename = nameurl.UrlParse(contact)
         opts = transport_control.ProtocolOptions(proto)
         if not transport_control.ProtocolIsSupported(proto):
             if settings.transportIsEnabled(proto) and settings.transportIsInstalled(proto) and settings.transportReceivingIsEnabled(proto):
                 #---tcp---
                 if proto == 'tcp':
                     def _tcp_started(l, opts):
                         dhnio.Dprint(4, 'p2p_connector.UpdateTransports._tcp_started')
                         if l is not None:
                             transport_control.StartProtocol('tcp', l, opts[0], opts[1], opts[2])
                         return l
                     def _tcp_failed(x):
                         dhnio.Dprint(4, 'p2p_connector.UpdateTransports._tcp_failed WARNING: '+str(x))
                         return x
                     def _start_tcp(options):
                         dhnio.Dprint(4, 'p2p_connector.UpdateTransports._start_tcp on port %d' % int(options[1]))
                         d = transport_tcp.receive(int(options[1]))
                         d.addCallback(_tcp_started, options)
                         d.addErrback(_tcp_failed)
                         startlist.append(d)
                     def _upnp_result(result, options):
                         dhnio.Dprint(4, 'p2p_connector.UpdateTransports._upnp_result %s' % str(result))
                         if result is not None:
                             options[1] = str(result[1])
                         _start_tcp(options)
                     def _run_upnpc(port):
                         shutdowner.A('block')
                         run_upnpc.update(port)
                         shutdowner.A('unblock')
                     externalIP = misc.readExternalIP() 
                     if opts is None:
                         opts = (externalIP, '', '')
                     opts = list(opts)
                     opts[0] = externalIP
                     opts[1] = settings.getTCPPort()
                     if settings.enableUPNP():
                         d = maybeDeferred(_run_upnpc, int(opts[1]))
                         d.addCallback(_upnp_result, opts)
                     else:
                         _start_tcp(opts)
                 #---cspace---
                 elif proto == 'cspace' and transport_control._TransportCSpaceEnable:
                     def _cspace_started(x, opts):
                         if not transport_cspace.registered():
                             dhnio.Dprint(4, 'p2p_connector.UpdateTransports._cspace_started WARNING not registered: ' + str(x))
                             return x
                         dhnio.Dprint(4, 'p2p_connector.UpdateTransports._cspace_started')
                         keyID = transport_cspace.keyID()
                         settings.setCSpaceKeyID(keyID)
                         opts[0] = keyID
                         l = transport_cspace.getListener()
                         transport_control.StartProtocol('cspace', l, opts[0], opts[1], opts[2])
                         return x
                     if opts is None:
                         opts = (settings.getCSpaceKeyID(), '', '')
                     opts = list(opts)
                     d = transport_cspace.init().addBoth(_cspace_started, opts)
                     startlist.append(d)
                 #---udp---
                 elif proto == 'udp' and transport_control._TransportUDPEnable:
                     def _udp_start(x, opts):
                         dhnio.Dprint(4, 'p2p_connector.UpdateTransports._udp_start')
                         if opts is None:
                             opts = (stun.getUDPClient().externalAddress[0], str(stun.getUDPClient().externalAddress[1]), '')
                         opts = list(opts)
                         opts[0] = stun.getUDPClient().externalAddress[0]
                         opts[1] = str(stun.getUDPClient().externalAddress[1])
                         transport_udp.init(stun.getUDPClient())
                         l = transport_udp.getListener()
                         if l is not None:
                             transport_control.StartProtocol('udp', l, opts[0], opts[1], opts[2])
                         return x
                     def _udp_failed(x):
                         dhnio.Dprint(4, 'p2p_connector.UpdateTransports._udp_failed')
                         return x
                     if stun.getUDPClient() is None or stun.getUDPClient().externalAddress is None:
                         d = stun.stunExternalIP(
                             close_listener=False, 
                             internal_port=settings.getUDPPort(), 
                             block_marker=shutdowner.A)
                     else:
                         d = succeed('')
                     d.addCallback(_udp_start, opts)
                     d.addErrback(_udp_failed)
                     startlist.append(d)
                     
     #need to wait before all listeners will be started
     return DeferredList(startlist)
Ejemplo n.º 3
0
def UpdateIdentity():
    global _RevisionNumber
    global _WorkingProtocols

    #getting local identity
    lid = misc.getLocalIdentity()
    nowip = misc.readExternalIP()
    order = lid.getProtoOrder()
    lid.clearContacts()

    #prepare contacts data
    cdict = {}
    cdict['tcp'] = 'tcp://'+nowip+':'+settings.getTCPPort()
    if transport_control._TransportSSHEnable:
        cdict['ssh'] = 'ssh://'+nowip+':'+settings.getSSHPort()
    if transport_control._TransportHTTPEnable:
        cdict['http'] = 'http://'+nowip+':'+settings.getHTTPPort()
    if transport_control._TransportQ2QEnable:
        cdict['q2q'] = 'q2q://'+settings.getQ2Quserathost()
    if transport_control._TransportEmailEnable:
        cdict['email'] = 'email://'+settings.getEmailAddress()
    if transport_control._TransportCSpaceEnable:
        cdict['cspace'] = 'cspace://'+settings.getCSpaceKeyID()
    if transport_control._TransportUDPEnable:
        if stun.getUDPClient() is None or stun.getUDPClient().externalAddress is None:
            cdict['udp'] = 'udp://'+nowip+':'+settings.getUDPPort()
        else:
            cdict['udp'] = 'udp://'+stun.getUDPClient().externalAddress[0]+':'+str(stun.getUDPClient().externalAddress[1])

    #making full order list
    for proto in cdict.keys():
        if proto not in order:
            order.append(proto)

    #add contacts data to the local identity
    #check if some transport is not installed
    for proto in order:
        if settings.transportIsEnabled(proto) and settings.transportReceivingIsEnabled(proto):
            contact = cdict.get(proto, None)
            if contact is not None:
                lid.setProtoContact(proto, contact)
        else:
            # if protocol is disabled - mark this
            # because we may want to turn it on in the future
            _WorkingProtocols.discard(proto)
            
    #misc.setLocalIdentity(lid)

    del order

#    #if IP is not external and upnp configuration was failed for some reasons
#    #we want to use another contact methods, NOT tcp or ssh
#    if IPisLocal() and run_upnpc.last_result('tcp') != 'upnp-done':
#        dhnio.Dprint(4, 'p2p_connector.update_identity want to push tcp contact: local IP, no upnp ...')
#        lid.pushProtoContact('tcp')
#        misc.setLocalIdentity(lid)

    #update software version number
    revnum = _RevisionNumber.strip()
    repo, location = misc.ReadRepoLocation()
    lid.version = (revnum.strip() + ' ' + repo.strip() + ' ' + dhnio.osinfo().strip()).strip()
    
    #generate signature with changed content
    lid.sign()
    
    #remember the identity
    misc.setLocalIdentity(lid)

    #finally saving local identity
    misc.saveLocalIdentity()
    dhnio.Dprint(4, 'p2p_connector.UpdateIdentity')
    dhnio.Dprint(4, '    version: %s' % str(lid.version))
    dhnio.Dprint(4, '    contacts: %s' % str(lid.contacts))