Example #1
0
    def remove_from_peers(self, peer):
        address = "{}:{}".format(\
            peer.protocol.address[0], peer.protocol.address[1])

        self.peers.pop(address, None)
        if peer.distance:
            self.peer_buckets[peer.distance - 1].pop(address, None)

        if peer.node_id:
            xorkey = bittrie.XorKey(self.node_id, peer.node_id)
            self.peer_trie.pop(xorkey, None)
Example #2
0
    def __rebuild_peer_trie(self):
        "Recycle self.peer_trie because I didn't fully implement pruning."

        #FIXME: Fix bittrie.py to prune itself properly, this is a temp hack
        # to prevent it from leaking memory.

        node_id = self.node_id

        while True:

            new_trie = bittrie.BitTrie()

            for peer in self.peers.values():
                xorkey = bittrie.XorKey(node_id, peer.node_id)
                new_trie[xorkey] = peer

            self.peer_trie = new_trie
            yield from asyncio.sleep(3600)  # Only do every hour.
Example #3
0
    def is_peer_connection_desirable(self, peer):
        peercnt = len(self.peers)

        if peer.protocol.address[0].startswith("127."):
            if peer.protocol.remote_banner.startswith("SSH-2.0-OpenSSH"):
                return True

        if peercnt >= self.hard_maximum_connections:
            return False

        if peer.protocol.server_mode:
            return True

        bucket = self.peer_buckets[peer.distance - 1]

        if len(bucket) < BUCKET_SIZE:
            return True

        # Otherwise check that the node_id is closer than all others.
        xorkey = bittrie.XorKey(self.node_id, peer.node_id)

        cnt = 0
        none_started = False
        for closer_node in self.peer_trie.find(xorkey, False):
            if closer_node is bittrie.none_found:
                none_started = True
                continue
#            elif not none_started and peer.protocol.server_mode:
#                # Outgoing connections are expected to be in the peer_trie.
#                log.info("Peer already connected, undesirable.")
#                return False

            if closer_node.distance != peer.distance:
                # No more closer ones.
                return True

            cnt += 1
            if cnt == BUCKET_SIZE:
                log.info(\
                    "Peer is further than BUCKET_SIZE connected PeerS,"\
                    " undesirable.")
                return False

        return True
Example #4
0
    def add_to_peers(self, peer):
        if self.node.tormode:
            address = peer.address
        else:
            address = "{}:{}".format(\
                peer.protocol.address[0], peer.protocol.address[1])

        existing = self.peers.setdefault(address, peer)
        if existing is not peer:
            log.warning(
                "Somehow we are trying to connect to an address [{}] already connected!"
                .format(address))
            return False
        self.peer_buckets[peer.distance - 1][address] = peer

        xorkey = bittrie.XorKey(self.node_id, peer.node_id)
        self.peer_trie[xorkey] = peer

        return True