示例#1
0
 def doStart(self, *args, **kwargs):
     """
     Action method.
     """
     options = {'idurl': my_id.getLocalID(), }
     id_contact = ''
     default_host = ''
     if self.proto == 'tcp':
         if not id_contact:
             default_host = strng.to_bin(misc.readExternalIP()) + b':' + strng.to_bin(str(settings.getTCPPort()))
         options['host'] = id_contact or default_host
         options['tcp_port'] = settings.getTCPPort()
     elif self.proto == 'udp':
         if not id_contact:
             default_host = strng.to_bin(nameurl.GetName(my_id.getLocalID())) + b'@' + strng.to_bin(platform.node())
         options['host'] = id_contact or default_host
         options['dht_port'] = settings.getDHTPort()
         options['udp_port'] = settings.getUDPPort()
     elif self.proto == 'proxy':
         pass
     elif self.proto == 'http':
         if not id_contact:
             default_host = strng.to_bin(misc.readExternalIP()) + b':' + strng.to_bin(str(settings.getHTTPPort()))
         options['host'] = id_contact or default_host
         options['http_port'] = settings.getHTTPPort()
     if _Debug:
         lg.out(8, 'network_transport.doStart connecting %s transport : %s' % (self.proto.upper(), options))
     self.interface.connect(options)
示例#2
0
 def build_contacts(self, id_obj):
     """
     """
     result = []
     nowip = strng.to_bin(misc.readExternalIP())
     result.append(b'http://%s:%d' % (nowip, settings.getHTTPPort()))
     if _Debug:
         lg.out(4, 'http_interface.build_contacts : %s' % result)
     return result
 def doStart(self, arg):
     """
     Action method.
     """
     options = {
         'idurl': my_id.getLocalID(),
     }
     id_contact = ''
     default_host = ''
     #         ident = my_id.getLocalIdentity()
     #         if ident:
     #             id_contact = ident.getContactsByProto().get(self.proto, '')
     #         if id_contact:
     #             assert id_contact.startswith(self.proto + '://')
     #             id_contact = id_contact.lstrip(self.proto + '://')
     if self.proto == 'tcp':
         if not id_contact:
             default_host = strng.to_bin(
                 misc.readExternalIP()) + b':' + strng.to_bin(
                     str(settings.getTCPPort()))
         options['host'] = id_contact or default_host
         options['tcp_port'] = settings.getTCPPort()
     elif self.proto == 'udp':
         if not id_contact:
             default_host = strng.to_bin(nameurl.GetName(
                 my_id.getLocalID())) + b'@' + strng.to_bin(platform.node())
         options['host'] = id_contact or default_host
         options['dht_port'] = settings.getDHTPort()
         options['udp_port'] = settings.getUDPPort()
     elif self.proto == 'proxy':
         pass
     elif self.proto == 'http':
         if not id_contact:
             default_host = strng.to_bin(
                 misc.readExternalIP()) + b':' + strng.to_bin(
                     str(settings.getHTTPPort()))
         options['host'] = id_contact or default_host
         options['http_port'] = settings.getHTTPPort()
     if _Debug:
         lg.out(
             8, 'network_transport.doStart connecting %s transport : %s' %
             (self.proto.upper(), options))
     self.interface.connect(options)
示例#4
0
    def verify_contacts(self, id_obj):
        """
        """
        nowip = misc.readExternalIP()
        http_contact = 'http://%s:%s' % (nowip, str(settings.getHTTPPort()))
        if id_obj.getContactIndex(contact=http_contact) < 0:
            if _Debug:
                lg.out(4, 'http_interface.verify_contacts returning False: http contact not found or changed')
            return False
#         if http_node.get_internal_port() != settings.getHTTPPort():
#             if _Debug:
#                 lg.out(4, 'http_interface.verify_contacts returning False: http port has been changed')
#             return False
        if _Debug:
            lg.out(4, 'http_interface.verify_contacts returning True')
        return True
示例#5
0
def buildDefaultIdentity(name='', ip='', idurls=[]):
    """
    Use some local settings and config files to create some new identity.

    Nice to provide a user name or it will have a form like: [ip_address]_[date].
    """
    if not ip:
        ip = misc.readExternalIP()
    if not name:
        name = ip.replace('.', '-') + '_' + time.strftime('%M%S')
    lg.out(4, 'my_id.buildDefaultIdentity: %s %s' % (name, ip))
    # create a new identity object
    # it is stored in memory and another copy on disk drive
    ident = identity.identity(xmlsrc=identity.default_identity_src)
    # this is my IDURL address
    # you can have many IDURL locations for same identity
    # just need to keep all them synchronized
    # this is identity propagate procedure, see p2p/propagate.py
    if len(idurls) == 0:
        idurls.append(b'http://127.0.0.1/%s.xml' % strng.to_bin(name.lower()))
    for idurl in idurls:
        ident.sources.append(strng.to_bin(idurl.strip()))
    # create a full list of needed transport methods
    # to be able to accept incoming traffic from other nodes
    new_contacts, new_order = buildProtoContacts(ident)
    if len(new_contacts) == 0:
        if settings.enableTCP() and settings.enableTCPreceiving():
            new_contacts['tcp'] = b'tcp://' + strng.to_bin(
                ip) + b':' + strng.to_bin(str(settings.getTCPPort()))
            new_order.append('tcp')
        if settings.enableUDP() and settings.enableUDPreceiving():
            _, servername, _, _ = nameurl.UrlParse(ident.sources[0])
            new_contacts['udp'] = b'udp://' + strng.to_bin(
                name.lower()) + b'@' + strng.to_bin(servername)
            new_order.append('udp')
        if settings.enableHTTP() and settings.enableHTTPreceiving():
            new_contacts['http'] = b'http://' + strng.to_bin(
                ip) + b':' + strng.to_bin(str(settings.getHTTPPort()))
            new_order.append('http')
    # erase current contacts from my identity
    ident.clearContacts()
    # add contacts data to the local identity
    for proto in new_order:
        contact = new_contacts.get(proto, None)
        if contact is None:
            lg.warn('proto %s was not found in contacts' % proto)
            continue
        ident.setProtoContact(proto, contact)
    # set other info
    # ident.certificates = []
    ident.setDate(time.strftime('%b %d, %Y'))
    ident.setPostage(1)
    ident.setRevision(0)
    ident.setVersion('')  # TODO: put latest git commit hash here
    # update software version number
    # version_number = bpio.ReadTextFile(settings.VersionNumberFile()).strip()
    # repo, location = misc.ReadRepoLocation()
    # ident.version = (version_number.strip() + ' ' + repo.strip() + ' ' + bpio.osinfo().strip()).strip()
    # build a version info
    # vernum = bpio.ReadTextFile(settings.VersionNumberFile())
    # repo, location = misc.ReadRepoLocation()
    # ident.version = (vernum.strip() + ' ' + repo.strip() + ' ' + bpio.osinfo().strip()).strip()
    # put my public key in my identity
    ident.setPublicKey(key.MyPublicKey())
    # generate signature
    ident.sign()
    # validate new identity
    if not ident.Valid():
        lg.warn('generated identity is not valid !!!')
    return ident