Example #1
0
    def getEntries(self):
        # Build and return a dict of entries which should be sent to the
        # dynamic config store.

        def b64(arg):
            return binascii.b2a_base64(arg).rstrip()

        # Dictionary of key=value pairs to return.
        # Start out with the static entries provided in the config.
        entries = cfg.dconfig_fixed_entries.copy()

        osm = self.main.osm

        # Generate public key hash
        if cfg.private_key:
            pubkey = long_to_bytes(RSA.construct(cfg.private_key).n)
            entries['pkhash'] = b64(md5(pubkey).digest())

        # Collect IPPs for the ipcache string
        GOAL = 10
        ipps = set()

        # Initially add all the exempt IPs, without a port
        for ip in self.main.state.exempt_ips:
            ad = Ad()
            ad.ip = ip
            ad.port = 0
            ipps.add(ad.getRawIPPort())

        # Helper function to add an IPP, overriding any portless entries.
        def add_ipp(ipp):
            ipps.discard(ipp[:4] + '\0\0')
            ipps.add(ipp)

        syncd = (osm and osm.syncd)

        # Add my own IP.
        # If I'm a hidden node, then only add it if I'm not online yet.
        if (not self.main.hide_node) or (not syncd):
            if osm:
                add_ipp(osm.me.ipp)
            else:
                try:
                    add_ipp(self.main.selectMyIP())
                except ValueError:
                    pass

        # Add the IPPs of online nodes
        if syncd:
            sec = seconds()

            def n_uptime(n):
                uptime = max(0, sec - n.uptime)
                if n.persist:
                    uptime *= 1.5
                return -uptime

            # Sort nodes by uptime, highest first
            nodes = osm.nodes[:]
            nodes.sort(key=n_uptime)

            # Chop list down to the top eighth or so.
            del nodes[min(GOAL, len(nodes) // 8):]

            # Select a random sample from the best nodes.
            try:
                nodes = random.sample(nodes, GOAL)
            except ValueError:
                pass

            for n in nodes:
                add_ipp(n.ipp)
                if len(ipps) >= GOAL:
                    break

        # Add the IPPs of offline nodes (if necessary)
        if len(ipps) < GOAL:
            for when,ipp in self.main.state.getYoungestPeers(GOAL):
                add_ipp(ipp)

                if len(ipps) >= GOAL:
                    break

        ipcache = list(ipps)
        random.shuffle(ipcache)

        ipcache = '\xFF\xFF\xFF\xFF' + ''.join(ipcache)
        ipcache = b64(self.main.pk_enc.encrypt(ipcache))

        entries['ipcache'] = ipcache

        return entries
Example #2
0
    def getEntries(self):
        # Build and return a dict of entries which should be sent to the
        # dynamic config store.

        def b64(arg):
            return binascii.b2a_base64(arg).rstrip()

        # Dictionary of key=value pairs to return.
        # Start out with the static entries provided in the config.
        entries = cfg.dconfig_fixed_entries.copy()

        osm = self.main.osm

        # Generate public key hash
        if cfg.private_key:
            pubkey = long_to_bytes(RSA.construct(cfg.private_key).n)
            entries['pkhash'] = b64(md5(pubkey).digest())

        # Collect IPPs for the ipcache string
        GOAL = 10
        ipps = set()

        # Initially add all the exempt IPs, without a port
        for ip in self.main.state.exempt_ips:
            ad = Ad()
            ad.ip = ip
            ad.port = 0
            ipps.add(ad.getRawIPPort())

        # Helper function to add an IPP, overriding any portless entries.
        def add_ipp(ipp):
            ipps.discard(ipp[:4] + '\0\0')
            ipps.add(ipp)

        syncd = (osm and osm.syncd)

        # Add my own IP.
        # If I'm a hidden node, then only add it if I'm not online yet.
        if (not self.main.hide_node) or (not syncd):
            if osm:
                add_ipp(osm.me.ipp)
            else:
                try:
                    add_ipp(self.main.selectMyIP().getRawIPPort())
                except ValueError:
                    pass

        # Add the IPPs of online nodes
        if syncd:
            sec = seconds()

            def n_uptime(n):
                uptime = max(0, sec - n.uptime)
                if n.persist:
                    uptime *= 1.5
                return -uptime

            # Sort nodes by uptime, highest first
            nodes = osm.nodes[:]
            nodes.sort(key=n_uptime)

            # Chop list down to the top eighth or so.
            del nodes[min(GOAL, len(nodes) // 8):]

            # Select a random sample from the best nodes.
            try:
                nodes = random.sample(nodes, GOAL)
            except ValueError:
                pass

            for n in nodes:
                add_ipp(n.ipp)
                if len(ipps) >= GOAL:
                    break

        # Add the IPPs of offline nodes (if necessary)
        if len(ipps) < GOAL:
            for when,ipp in self.main.state.getYoungestPeers(GOAL):
                add_ipp(ipp)

                if len(ipps) >= GOAL:
                    break

        ipcache = list(ipps)
        random.shuffle(ipcache)

        ipcache = '\xFF\xFF\xFF\xFF' + ''.join(ipcache)
        ipcache = b64(self.main.pk_enc.encrypt(ipcache))

        entries['ipcache'] = ipcache

        return entries