예제 #1
0
 def bytes2String(self, data : bytes, showAsHex : bool, encoding="utf-8"):
     isHexString = False
     dataColored = None
     if showAsHex:
         return True, utils.hexlify(data, ' ').decode(encoding=encoding), dataColored
     try:
         dataPlain, dataColore, remain = self.getColoredText(data, self.configGlobal["encoding"])
         if remain:
             dataPlain += remain.decode(encoding=self.configGlobal["encoding"], errors="ignore")
     except Exception:
         dataPlain = utils.hexlify(data, ' ').decode(encoding=encoding)
         isHexString = True
     return isHexString, dataPlain, dataColored
예제 #2
0
def decode(raw: bytes):
    reqeust, cmd, data, body, raw = _decode(raw)
    if cmd is None:
        return b''
    if type(data) == bytes:
        data = bytes([cmd]) + data
        data = '<= {}\n  [HEX]: {}\n  [BYTES]:  {}'.format(
            "REQUEST" if reqeust else "RESPONSE",
            utils.hexlify(data, " ").decode(),
            str(data)[2:-1])
        return data
    cmd_bytes = bytes([cmd if reqeust else cmd | CMD_RESPONSE_MASK])
    data = '<= {}\n  [HEX]: {}\n  [BYTES]: {}{}\n  [DECODED]: {}\n'.format(
        "REQUEST" if reqeust else "RESPONSE",
        utils.hexlify(cmd_bytes + body, " ").decode(),
        str(cmd_bytes)[2:-1],
        str(body)[2:-1], str(data))
    return data
예제 #3
0
    async def connect_with_peers(self, info_hash, peers):
        for i in range(0, len(peers), 20):
            try:
                torrent = await self.wait_for_torrent(info_hash, peers[i: i + 20])
            except:
                torrent = None

            if torrent:
                logging.debug("Got torrent metadata\r\n\tinfo_hash: {}".format(hexlify(info_hash)))
                await self.save_torrent(info_hash, torrent)
                break

        if info_hash in self.torrent_in_progress:
            self.torrent_in_progress.remove(info_hash)
예제 #4
0
    async def save_torrent(self, info_hash, torrent):
        if "files" in torrent:
            files = torrent["files"]
        else:
            files = [{"length": torrent["length"], "path": [torrent["name"]]}]

        metadata = {
            "info_hash": hexlify(info_hash),
            "files": decode_bytes(files),
            "name": decode_bytes(torrent["name"]),
            "timestamp": datetime.now()
        }

        await self.db.torrents.insert_one(metadata)
예제 #5
0
def encode(data: bytes):
    cmd = data[0]
    body = data[1:]
    header = b'\xAA\xCA\xAC\xBB'
    data_len = len(body) + 1
    data_len = pack("I", data_len)
    frame = header + bytes([version]) + data_len + bytes([cmd]) + body
    _crc = crc16(frame)
    _crc = pack("H", _crc)
    frame += _crc
    msg = '  [HEX] {}\n  [BYTES]: {}\n'.format(
        utils.hexlify(data, " ").decode(),
        str(data)[2:-1])
    print(msg, start="=> REQUEST\n")
    return frame
예제 #6
0
    async def save_torrent_metadata(self, info_hash, metadata):
        torrent = bdecode(metadata, decoder=decode_bkeys)

        if "files" in torrent:
            files = torrent["files"]
        else:
            files = [{"length": torrent["length"], "path": [torrent["name"]]}]

        item = {
            "info_hash": hexlify(info_hash),
            "files": decode_bytes(files),
            "name": decode_bytes(torrent["name"]),
            "timestamp": datetime.now()
        }

        await self.db.torrents.insert_one(item)
예제 #7
0
 async def enqueue_torrent(self, info_hash):
     if await self.db.torrents.count(
             filter={"info_hash": hexlify(info_hash)}) == 0:
         await super(TorrentCrawlerMongo, self).enqueue_torrent(info_hash)
예제 #8
0
 async def is_torrent_exists(self, info_hash):
     result = await self.db.torrents.count(filter={"info_hash": hexlify(info_hash)}) > 0
     return result
예제 #9
0
 def get_path_for_torrent(self, info_hash):
     return os.path.join(self.folder_path, hexlify(info_hash) + ".torrent")