Example #1
0
    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' % (
                strng.to_text(url), strng.to_text(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 += b':%d' % idport
            src += '<a href="http://%s/"><nobr>%s</nobr></a>&nbsp;&nbsp;\n' % (
                strng.to_text(idhost), strng.to_text(idhost))
        src += '</p>'
        src += '<!--CLIENT_HOST=%s:%s-->\n' % (request.client.host,
                                               request.client.port)
        src += '</body>\n</html>'
        del files
        return strng.to_bin(src)
Example #2
0
 def _send_new_identity(self):
     """
     Send created identity to the identity servers to register it.
     """
     if _Debug:
         lg.out(_DebugLevel, 'id_registrator._send_new_identity')
     from transport.tcp import tcp_node
     sendfilename = settings.LocalIdentityFilename() + '.new'
     dlist = []
     for idurl in self.new_identity.getSources(as_originals=True):
         self.free_idurls.remove(strng.to_bin(idurl))
         _, host, _, _ = nameurl.UrlParse(idurl)
         _, tcpport = known_servers.by_host().get(
             host,
             (settings.IdentityWebPort(), settings.IdentityServerPort()))
         srvhost = net_misc.pack_address((
             host,
             tcpport,
         ))
         if _Debug:
             lg.out(_DebugLevel, '    sending to %r via TCP' % srvhost)
         dlist.append(
             tcp_node.send(
                 sendfilename,
                 srvhost,
                 'Identity',
                 keep_alive=False,
             ))
     return DeferredList(dlist, fireOnOneCallback=True)
Example #3
0
 def _send_new_identity(self):
     """
     Send created identity to the identity server to register it.
     TODO: need to close transport and gateway after that
     """
     lg.out(4, 'id_registrator._send_new_identity ')
     from transport import gateway
     from transport import network_transport
     from transport.tcp import tcp_interface
     gateway.init()
     interface = tcp_interface.GateInterface()
     transport = network_transport.NetworkTransport('tcp', interface)
     transport.automat('init', gateway.listener())
     transport.automat('start')
     gateway.start()
     sendfilename = settings.LocalIdentityFilename() + '.new'
     dlist = []
     for idurl in self.new_identity.sources:
         self.free_idurls.remove(strng.to_bin(idurl))
         _, host, _, _ = nameurl.UrlParse(idurl)
         _, tcpport = known_servers.by_host().get(
             host,
             (settings.IdentityWebPort(), settings.IdentityServerPort()))
         srvhost = net_misc.pack_address((
             host,
             tcpport,
         ))
         dlist.append(
             gateway.send_file_single(idurl, 'tcp', srvhost, sendfilename,
                                      'Identity'))
     # assert len(self.free_idurls) == 0
     return DeferredList(dlist, fireOnOneCallback=True)
Example #4
0
 def doSaveMyName(self, *args, **kwargs):
     """
     Action method.
     """
     login = kwargs['username']
     self.preferred_servers = [
         s.strip() for s in kwargs.get('preferred_servers', [])
     ]
     if not self.known_servers:
         self.known_servers = known_servers.by_host()
     if not self.preferred_servers:
         try:
             for srv in str(config.conf().getData(
                     'services/identity-propagate/preferred-servers')
                            ).split(','):
                 if srv.strip():
                     self.preferred_servers.append(srv.strip())
         except:
             pass
     self.min_servers = max(
         settings.MinimumIdentitySources(),
         config.conf().getInt('services/identity-propagate/min-servers')
         or settings.MinimumIdentitySources())
     self.max_servers = min(
         settings.MaximumIdentitySources(),
         config.conf().getInt('services/identity-propagate/max-servers')
         or settings.MaximumIdentitySources())
     lg.out(4, 'id_registrator.doSaveMyName [%s]' % login)
     lg.out(4, '    known_servers=%s' % self.known_servers)
     lg.out(4, '    preferred_servers=%s' % self.preferred_servers)
     lg.out(4, '    min_servers=%s' % self.min_servers)
     lg.out(4, '    max_servers=%s' % self.max_servers)
     bpio.WriteTextFile(settings.UserNameFilename(), login)
Example #5
0
def SendServers():
    """
    My identity file can be stored in different locations, see the "sources"
    field.

    So I can use different identity servers to store more secure. This
    method will send my identity file to all my identity servers via
    transport_tcp.
    """
    from transport.tcp import tcp_node
    sendfile, sendfilename = tmpfile.make("propagate")
    os.close(sendfile)
    LocalIdentity = my_id.getLocalIdentity()
    bpio.WriteFile(sendfilename, LocalIdentity.serialize())
    dlist = []
    for idurl in LocalIdentity.sources:
        # sources for out identity are servers we need to send to
        protocol, host, port, filename = nameurl.UrlParse(idurl)
        # if host == settings.IdentityServerName():
        #     host = '67.207.147.183'
        webport, tcpport = known_servers.by_host().get(
            host, (settings.IdentityWebPort(), settings.IdentityServerPort()))
        # srvhost = '%s:%d' % (host, int(tcpport))
        dlist.append(
            tcp_node.send(sendfilename, (host, int(tcpport)), 'Identity',
                          True))
        # dlist.append(gateway.send_file_single('tcp', srvhost, sendfilename, 'Identity'))
    dl = DeferredList(dlist, consumeErrors=True)
    return dl
Example #6
0
 def start(self):
     from logs import lg
     from userid import my_id
     from main.config import conf
     my_id.loadLocalIdentity()
     if my_id._LocalIdentity is None:
         lg.warn(
             'Loading local identity failed - need to create an identity first'
         )
         return False
     from main import events
     from contacts import identitycache
     from userid import known_servers
     from p2p import propagate
     from contacts import contactsdb
     identitycache.init()
     d = contactsdb.init()
     propagate.init()
     conf().addConfigNotifier('services/identity-propagate/known-servers',
                              self._on_known_servers_changed)
     lg.info('known ID servers are : %r' % known_servers.by_host())
     events.add_subscriber(self._on_local_identity_modified,
                           'local-identity-modified')
     events.add_subscriber(self._on_my_identity_rotated,
                           'my-identity-rotated')
     return d
Example #7
0
    def doPingServers(self, arg):
        """
        Action method.
        """
        lg.out(4, 'id_registrator.doPingServers    %d in list' % len(self.discovered_servers))

        def _cb(htmlsrc, id_server_host):
            lg.out(4, '            RESPONDED: %s' % id_server_host)
            if self.preferred_servers and id_server_host in self.preferred_servers:
                self.good_servers.insert(0, id_server_host)
            else:
                self.good_servers.append(id_server_host)
            self.discovered_servers.remove(id_server_host)
            self.automat('id-server-response', (id_server_host, htmlsrc))

        def _eb(err, id_server_host):
            lg.out(4, '               FAILED: %s : %s' % (id_server_host, err))
            self.discovered_servers.remove(id_server_host)
            self.automat('id-server-failed', (id_server_host, err))

        for host in self.discovered_servers:
            webport, tcpport = known_servers.by_host().get(
                host,
                (settings.IdentityWebPort(), settings.IdentityServerPort()),
            )
            if webport == 80:
                webport = ''
            server_url = nameurl.UrlMake('http', host, webport, '')
            lg.out(4, '               connecting to %s:%s   known tcp port is %d' % (
                server_url, webport, tcpport, ))
            d = net_misc.getPageTwisted(server_url, timeout=10)
            d.addCallback(_cb, host)
            d.addErrback(_eb, host)
Example #8
0
def SendServers():
    """
    My identity file can be stored in different locations, see the "sources"
    field.

    So I can use different identity servers to store more secure and reliable. This
    method will send my identity file to all my identity servers via
    transport_tcp.
    """
    from transport.tcp import tcp_node
    sendfile, sendfilename = tmpfile.make("propagate")
    os.close(sendfile)
    LocalIdentity = my_id.getLocalIdentity()
    bpio.WriteTextFile(sendfilename, LocalIdentity.serialize(as_text=True))
    dlist = []
    for idurl in LocalIdentity.getSources(as_originals=True):
        # sources for out identity are servers we need to send to
        protocol, host, port, filename = nameurl.UrlParse(idurl)
        # TODO: rebuild identity-server logic to be able to send my identity via HTTP POST instead of TCP and
        # get rid of second TCP port at all 
        webport, tcpport = known_servers.by_host().get(host, (
            # by default use "expected" port numbers
            settings.IdentityWebPort(), settings.IdentityServerPort()))
        normalized_address = net_misc.normalize_address((host, int(tcpport), ))
        dlist.append(tcp_node.send(
            sendfilename, normalized_address, 'Identity', keep_alive=False,
        ))
        if _Debug:
            lg.args(_DebugLevel, normalized_address=normalized_address, filename=filename)
    dl = DeferredList(dlist, consumeErrors=True)
    return dl
Example #9
0
def TestInternetConnection(remote_hosts=None, timeout=10):
    """
    """
    if remote_hosts is None:
        remote_hosts = []
        from userid import known_servers
        for host, ports in known_servers.by_host().items():
            remote_hosts.append('http://%s:%d' % (host, ports[0], ))
    random.shuffle(remote_hosts)
    dl = []
    for host in remote_hosts[:5]:
        dl.append(getPageTwisted(host, timeout=timeout))
    return DeferredList(dl, fireOnOneCallback=True, fireOnOneErrback=False, consumeErrors=True)
Example #10
0
    def doRequestServers(self, *args, **kwargs):
        """
        Action method.
        """
        login = bpio.ReadTextFile(settings.UserNameFilename())

        def _cb(xmlsrc, idurl, host):
            if not xmlsrc:
                if self.preferred_servers and host in self.preferred_servers:
                    if self.preferred_servers[0] == host:
                        self.free_idurls.insert(0, idurl)
                    else:
                        self.free_idurls.insert(1, idurl)
                else:
                    self.free_idurls.append(idurl)
                self.registrations.remove(idurl)
                self.automat('id-not-exist', idurl)
            else:
                lg.out(4, '                EXIST: %s' % idurl)
                self.registrations.remove(idurl)
                self.automat('id-exist', idurl)

        def _eb(err, idurl, host):
            lg.out(4, '            NOT EXIST: %s' % idurl)
            if self.preferred_servers and host in self.preferred_servers:
                if self.preferred_servers[0] == host:
                    self.free_idurls.insert(0, idurl)
                else:
                    self.free_idurls.insert(1, idurl)
            else:
                self.free_idurls.append(idurl)
            self.registrations.remove(idurl)
            self.automat('id-not-exist', idurl)

        for host in self.good_servers:
            webport, tcpport = known_servers.by_host().get(
                host,
                (settings.IdentityWebPort(), settings.IdentityServerPort()))
            if webport == 80:
                webport = ''
            idurl = nameurl.UrlMake('http', strng.to_text(host), webport,
                                    login + '.xml')
            lg.out(4, '    %s' % idurl)
            d = net_misc.getPageTwisted(idurl, timeout=7)
            d.addCallback(_cb, idurl, host)
            d.addErrback(_eb, idurl, host)
            self.registrations.append(idurl)
        lg.out(
            4, 'id_registrator.doRequestServers login=%s registrations=%d' %
            (login, len(self.registrations)))
 def start(self):
     from logs import lg
     from userid import my_id
     my_id.loadLocalIdentity()
     if my_id._LocalIdentity is None:
         lg.warn(
             'Loading local identity failed - need to create an identity first'
         )
         return False
     from contacts import identitycache
     identitycache.init()
     from contacts import contactsdb
     contactsdb.init()
     from p2p import propagate
     propagate.init()
     from userid import known_servers
     lg.info('known ID servers are : %r' % known_servers.by_host())
     return True
Example #12
0
 def doInit(self, event, *args, **kwargs):
     """
     Action method.
     """
     self.result_defer = kwargs.get('result_defer')
     self.check_only = False
     if event == 'check':
         self.check_only = True
     self.alive_idurls = []
     self.old_sources = my_id.getLocalIdentity().getSources(
         as_originals=True)
     self.known_servers = known_servers.by_host()
     self.preferred_servers = kwargs.get('preferred_servers', {})
     self.possible_sources = []
     if _Debug:
         lg.args(_DebugLevel, preferred_servers=self.preferred_servers)
     self.force = kwargs.get('force', False)
     self.new_revision = kwargs.get('new_revision')
     self.rotated = False
     if not self.preferred_servers:
         try:
             for srv in strng.to_text(config.conf().getData(
                     'services/identity-propagate/known-servers')).split(
                         ','):
                 if srv.strip():
                     host, web_port, tcp_port = srv.strip().split(':')
                     self.preferred_servers[host] = (
                         int(web_port),
                         int(tcp_port),
                     )
         except:
             lg.exc()
     self.preferred_servers = {
         strng.to_bin(k): v
         for k, v in self.preferred_servers.items()
     }
     self.current_servers = []
     for idurl in my_id.getLocalIdentity().getSources(as_originals=True):
         self.current_servers.append(strng.to_bin(nameurl.GetHost(idurl)))
     if _Debug:
         lg.args(_DebugLevel,
                 known_servers=self.known_servers,
                 preferred_servers=self.preferred_servers,
                 current_servers=self.current_servers)
Example #13
0
def http_stun(result_defer=None):
    from userid import known_servers
    identity_servers = known_servers.by_host()
    if not identity_servers:
        if _Debug:
            lg.out(
                _DebugLevel,
                'stun_client.http_stun   SKIP, no known identity servers found'
            )
        return False
    one_host = random.choice(list(identity_servers.keys()))
    one_port = identity_servers[one_host][0]
    one_url = nameurl.UrlMake('http', one_host, one_port)
    if _Debug:
        lg.out(_DebugLevel,
               'stun_client.http_stun   GO with one node : %r' % one_url)

    def _check_body(html_response):
        ret = {
            'result': 'stun-failed',
            'type': None,
            'ip': None,
            'details': [
                'unknown client host from response',
            ],
        }
        mo = re.search(b'\<\!\-\-CLIENT_HOST\=([\d\.]+):(\d+)\-\-\>',
                       html_response)
        if not mo:
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'stun_client.http_stun   FAILED : unknown client host from response'
                )
            if result_defer:
                result_defer.callback(ret)
            return None
        ret = {
            'result': 'stun-success',
            'type': 'unknown',
            'ip': strng.to_text(mo.group(1)),
            'details': [],
        }
        if _Debug:
            lg.out(_DebugLevel, 'stun_client.http_stun   SUCCESS : %r' % ret)
        if result_defer:
            result_defer.callback(ret)
        return None

    def _request_failed(err):
        if _Debug:
            lg.out(_DebugLevel, 'stun_client.http_stun   FAILED : %r' % err)
        ret = {
            'result': 'stun-failed',
            'type': None,
            'ip': None,
            'details': [
                err.getErrorMessage(),
            ],
        }
        if result_defer:
            result_defer.callback(ret)
        return None

    d = net_misc.getPageTwisted(one_url)
    d.addCallback(_check_body)
    d.addErrback(_request_failed)
    return True