def myhandler(rec, c): try: if rec["y"] == b"q": if rec["q"] == b"get_peers": print(";".join( [ str(time.time()), binascii.hexlify(rec["a"].get("id") ).decode(), binascii.hexlify(rec["a"].get("info_hash")).decode(), repr(c), ]), file=outf) outf.flush() finally: # always ALWAYS pass it off to the real handler dht.default_handler(rec,c)
def get_peers(self, info_hash, attempts=10): """ Recursively call the get_peers function to fidn peers for the given info_hash """ if isinstance(info_hash, bytes): info_hash_hex = binascii.hexlify(info_hash).decode() else: info_hash_hex = info_hash logger.debug("Finding peers for {0}".format(info_hash_hex)) return self._recurse(info_hash, self._server.get_peers, result_key="values", max_attempts=attempts)
def find_node(self, target, attempts=10): """ Recursively call the find_node function to get as close as possible to the target node """ if isinstance(target, bytes): target_hex = binascii.hexlify(target).decode() else: target_hex = target logger.debug("Tracing to {0}".format(target_hex)) self._recurse(target, self._server.find_node, max_attempts=attempts)
def _recurse(self, target, function, max_attempts=10, result_key=None): """ Recursively query the DHT, following "nodes" replies until we hit the desired key This is the workhorse function used by all recursive queries. """ #print("In _recurse.") if isinstance(target, bytes): target_hex = binascii.hexlify(target).decode() else: target_hex = target logger.debug("Recursing to target {0}".format(target)) attempts = 0 while attempts < max_attempts: close_nodes = self._rt.get_close_nodes(target) if not close_nodes: raise NotFoundError("No close nodes found with self.rt.get_close_nodes for "+str(target)+\ " Current routing table: "+str(self._rt._nodes)) for id_, node in close_nodes: try: #print("Calling function", function, "in _recurse.") r = function(self._get_id(id_), node, target) #print("Finished calling function in _recurse.") logger.debug("Recursion results from %r ", node.c) attempts += 1 if result_key and result_key in r: return r[result_key] if "nodes" in r: self._process_incoming_nodes(r["nodes"]) except KRPCTimeout: # The node did not reply. # Blacklist it. if self._rt.node_count() > 8: logger.error("Node timed out: blacklisting {0}".format(node.c)) self._rt.bad_node(id_, node) else: logger.error("Node timed out: Would blacklist, but only 8 nodes known. Node: {0}".format(node.c)) continue except KRPCError: # Sometimes we just flake out due to UDP being unreliable # Don't sweat it, just log and carry on. logger.error("KRPC Error:\n\n" + traceback.format_exc()) if result_key: # We were expecting a result, but we did not find it! # Raise the NotFoundError exception instead of returning None raise NotFoundError
def handle(self): self._logger.debug('handle') request = self.request.recv(MAX_REQUEST_SIZE) if request: self._logger.debug('request of size %d (%s)'%(len(request), b2a.hexlify(request[:8]))) args = re_split(self.string_separator, request[1:]) #TODO-3 ??? figure out way to use self.string_separator(should work now) command = unpack('>b', request[0])[0] method = self.server._command_set.get(command) if method: response = self.server._command_set[command](*args) else: self._logger.error('no such command word %d!'%command) response = pack('>b', -1) else: self._logger.error('null packet received!') response = pack('>b', -2) self.request.send(response)
def handle(self): """ inst.handle() -> None Handles a specific request by finding the appropropriate member function of the instantiating BasicTCPServer using that instance's _command_set member with the first byte of the request as the command word, and the rest of the request is passed as the arguments. The return value of the method is then sent back over TCP.""" self.logger.debug('handle') request = self.request.recv(MAX_REQUEST_SIZE)#.rstrip('\n') if request: self.logger.debug('request of size %d (%s)'%(len(request), b2a.hexlify(request[:8]))) args = request[1:] command = BYTE.unpack(request[0])[0] method = self.server._command_set.get(command) if method: with RLock(): response = self.server._command_set[command](args) else: self.logger.error('no such command word %d!'%command) response = SBYTE.pack(-1) else: self.logger.error('null packet received!') response = SBYTE.pack(-2) self.request.send(response)
req_handler.setFormatter(formatter) stdout_handler = logging.StreamHandler() stdout_handler.setFormatter(formatter) logging.getLogger("krpcserver").setLevel(loglevel) logging.getLogger("krpcserver").addHandler(req_handler) logging.getLogger("krpcserver").addHandler(stdout_handler) logging.getLogger("lightdht").setLevel(loglevel) logging.getLogger("lightdht").addHandler(req_handler) logging.getLogger("lightdht").addHandler(stdout_handler) # Create a DHT node. id_ = os.urandom(20) dht = lightdht.DHT(port=54768, id_=id_, version="XN\x00\x00") # where to put our product outf = open("get-peers.{}.log".format(binascii.hexlify(id_).decode()), "a") # handler def myhandler(rec, c): try: if rec["y"] == b"q": if rec["q"] == b"get_peers": print(";".join( [ str(time.time()), binascii.hexlify(rec["a"].get("id") ).decode(), binascii.hexlify(rec["a"].get("info_hash")).decode(), repr(c), ]), file=outf) outf.flush() finally: