Exemple #1
0
    def _find(self, rpcmethod):
        """
        Get either a value or list of nodes.

        Args:
            rpcmethod: The protocol's callfindValue or callFindNode.

        The process:
          1. calls find_* to current ALPHA nearest not already queried nodes,
             adding results to current nearest list of k nodes.
          2. current nearest list needs to keep track of who has been queried already
             sort by nearest, keep KSIZE
          3. if list is same as last time, next call should be to everyone not
             yet queried
          4. repeat, unless nearest list has all been queried, then ur done
        """
        self.log.info("crawling with nearest: %s" % str(tuple(self.nearest)))
        count = self.alpha
        if self.nearest.getIDs() == self.lastIDsCrawled:
            self.log.info("last iteration same as current - checking all in list now")
            count = len(self.nearest)
        self.lastIDsCrawled = self.nearest.getIDs()

        ds = {}
        for peer in self.nearest.getUncontacted()[:count]:
            ds[peer.id] = rpcmethod(peer, self.node)
            self.nearest.markContacted(peer)
        d = deferredDict(ds)
        d.addCallback(self._nodesFound)
        d.addErrback(self.onError)
        return d
Exemple #2
0
    def _find(self, rpcmethod):
        """
        Get either a value or list of nodes.

        Args:
            rpcmethod: The protocol's callfindValue or callFindNode.

        The process:
          1. calls find_* to current ALPHA nearest not already queried nodes,
             adding results to current nearest list of k nodes.
          2. current nearest list needs to keep track of who has been queried already
             sort by nearest, keep KSIZE
          3. if list is same as last time, next call should be to everyone not
             yet queried
          4. repeat, unless nearest list has all been queried, then ur done
        """
        self.log.info("crawling with nearest: %s" % str(tuple(self.nearest)))
        count = self.alpha
        if self.nearest.getIDs() == self.lastIDsCrawled:
            self.log.info(
                "last iteration same as current - checking all in list now")
            count = len(self.nearest)
        self.lastIDsCrawled = self.nearest.getIDs()

        ds = {}
        for peer in self.nearest.getUncontacted()[:count]:
            ds[peer.id] = rpcmethod(peer, self.node)
            self.nearest.markContacted(peer)
        return deferredDict(ds).addCallback(self._nodesFound)
Exemple #3
0
    def bootstrap(self, addrs):
        """
        Bootstrap the server by connecting to other known nodes in the network.

        Args:
            addrs: A `list` of (ip, port) `tuple` pairs.  Note that only IP addresses
                   are acceptable - hostnames will cause an error.
        """
        # if the transport hasn't been initialized yet, wait a second
        if self.protocol.transport is None:
            return task.deferLater(reactor, 1, self.bootstrap, addrs)

        def initTable(results):
            nodes = []
            for addr, result in results.items():
                if result[0]:
                    nodes.append(Node(result[1], addr[0], addr[1]))
            spider = NodeSpiderCrawl(self.protocol, self.node, nodes,
                                     self.ksize, self.alpha)
            return spider.find()

        ds = {}
        for addr in addrs:
            ds[addr] = self.protocol.ping(addr, self.node.id)
        return deferredDict(ds).addCallback(initTable)
Exemple #4
0
    def bootstrap(self, addrs):
        """
        Bootstrap the server by connecting to other known nodes in the network.

        Args:
            addrs: A `list` of (ip, port) `tuple` pairs.  Note that only IP addresses
                   are acceptable - hostnames will cause an error.
        """
        # if the transport hasn't been initialized yet, wait a second
        if self.protocol.transport is None:
            return task.deferLater(reactor, 1, self.bootstrap, addrs)

        def initTable(results):
            nodes = []
            for addr, result in results.items():
                if result[0]:
                    nodes.append(Node(result[1], addr[0], addr[1]))
            spider = NodeSpiderCrawl(self.protocol, self.node, nodes, self.ksize, self.alpha)
            return spider.find()

        ds = {}
        for addr in addrs:
            ds[addr] = self.protocol.ping(addr, self.node.id)
        d = deferredDict(ds)
        d.addCallback(initTable)
        d.addErrback(self.onError)
        return d
Exemple #5
0
 def find(self):
     """
     Find either the closest nodes or the value requested.
     """
     if self.get_more_nodes:
         return self._find(self.protocol.callFindNode)
     else:
         self.log.info("crawling with nearest: %s" % str(tuple(self.nodesToQuery)))
         count = self.alpha
         if self.nodesToQuery.getIDs() == self.lastNodesQueried:
             self.log.info("last iteration same as current - checking all in list now")
             count = len(self.nearest)
         self.lastNodesQueried = self.nodesToQuery.getIDs()
         ds = {}
         for peer in self.nodesToQuery.getUncontacted()[:count]:
             ds[peer.id] = self.protocol.callFindRange(peer, self.prefix)
             self.nodesToQuery.markContacted(peer)
     return deferredDict(ds).addCallback(self._nodesFound)
    def bootstrap(self, addrs):
        """
        Bootstrap the server by connecting to other known nodes in the network.
        Args:
            addrs: A `list` of (ip, port) `tuple` pairs.  Note that only IP addresses
                   are acceptable - hostnames will cause an error.
        """
        # if the transport hasn't been initialized yet, wait a second
        # _log.debug("bootstrap"
        if self.protocol.transport is None:
            return append_server.task.deferLater(
                service_discovery_ssdp.reactor, 1, self.bootstrap, addrs)

        def initTable(results, challenge, id):
            nodes = []
            for addr, result in results.items():
                ip = addr[0]
                port = addr[1]
                if result[0]:
                    resultId = result[1]['id']
                    resultIdHex = resultId.encode('hex').upper()
                    resultSign = result[1]['signature']
                    data = self.protocol.certificateExists(resultIdHex)
                    if not data:
                        identifier = "{}cert".format(resultIdHex)
                        self.protocol.callCertFindValue(
                            Node(resultId, ip, port), Node(identifier))
                    else:
                        cert_stored = self.protocol.searchForCertificate(
                            resultIdHex)
                        try:
                            self.runtime_credentials.verify_signed_data_from_certstring(
                                cert_stored, resultSign, challenge,
                                certificate.TRUSTSTORE_TRANSPORT)
                        except:
                            traceback.print_exc()
                        nodes.append(Node(resultId, ip, port))
            spider = NodeSpiderCrawl(self.protocol, self.node, nodes,
                                     self.ksize, self.alpha)
            return spider.find()

        ds = {}
        challenge = generate_challenge()
        id = None
        if addrs:
            data = addrs[0]
            addr = (data[0], data[1])
            cert = data[2]
            try:
                id = certificate.id_from_cert_string(cert)
                signature = self.runtime_credentials.sign_data("{}{}".format(
                    id, challenge))
                ds[addr] = self.protocol.ping(addr, self.node.id, challenge,
                                              signature,
                                              self.protocol.getOwnCert())
                self.protocol.storeCert(data[2], id)
            except:
                logger(self.protocol.sourceNode, "Certificate creation failed")
            if not id:
                return deferredDict(ds)
            node = Node(id.decode("hex"), data[0], data[1])
            if self.protocol.router.isNewNode(node):
                return deferredDict(ds).addCallback(initTable, challenge, id)
        return deferredDict(ds)
    def bootstrap(self, addrs):
        """
        Bootstrap the server by connecting to other known nodes in the network.
        Args:
            addrs: A `list` of (ip, port) `tuple` pairs.  Note that only IP addresses
                   are acceptable - hostnames will cause an error.
        """
        # if the transport hasn't been initialized yet, wait a second
        # _log.debug("bootstrap"
        if self.protocol.transport is None:
            return append_server.task.deferLater(service_discovery_ssdp.reactor,
                                                1,
                                                self.bootstrap,
                                                addrs)

        def initTable(results, challenge, id):
            nodes = []
            for addr, result in results.items():
                ip = addr[0]
                port = addr[1]
                if result[0]:
                    resultId = result[1]['id']
                    resultIdHex = resultId.encode('hex').upper()
                    resultSign = result[1]['signature']
                    data = self.protocol.certificateExists(resultIdHex)
                    if not data:
                        identifier = "{}cert".format(resultIdHex)
                        self.protocol.callCertFindValue(Node(resultId,
                                                            ip,
                                                            port),
                                                       Node(identifier))
                    else:
                        cert_stored = self.protocol.searchForCertificate(resultIdHex)
                        try:
                            OpenSSL.crypto.verify(cert_stored,
                                                 resultSign,
                                                 challenge,
                                                 "sha256")
                        except:
                            traceback.print_exc()
                        nodes.append(Node(resultId, ip, port))
            spider = NodeSpiderCrawl(self.protocol,
                                    self.node,
                                    nodes,
                                    self.ksize,
                                    self.alpha)
            return spider.find()

        ds = {}
        challenge = generate_challenge()
        id = None
        if addrs:
            data = addrs[0]
            addr = (data[0], data[1])
            try:
                cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                      data[2])
                fingerprint = cert.digest("sha256")
                id = fingerprint.replace(":", "")[-40:]
                private = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM,
                                                      self.protocol.priv_key,
                                                      '')
                signature = OpenSSL.crypto.sign(private,
                                                "{}{}".format(id, challenge),
                                                "sha256")
                ds[addr] = self.protocol.ping(addr,
                                             self.node.id,
                                             challenge,
                                             signature,
                                             self.protocol.getOwnCert())
                self.protocol.storeCert(data[2], id)
            except:
                logger(self.protocol.sourceNode, "Certificate creation failed")
            if not id:
                return deferredDict(ds)
            node = Node(id.decode("hex"), data[0], data[1])
            if self.protocol.router.isNewNode(node):
                return deferredDict(ds).addCallback(initTable, challenge, id)
        return deferredDict(ds)