From ef3224ecf312504e90c0dab8e598487489e452fc Mon Sep 17 00:00:00 2001 From: Stefan Rupp Date: Fri, 29 Dec 2017 06:30:58 +0100 Subject: [PATCH] reject torrents that are not announced by our tracker --- indexer.py | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/indexer.py b/indexer.py index 6c5cf2e..3347a94 100644 --- a/indexer.py +++ b/indexer.py @@ -131,10 +131,33 @@ def createNewTorrent(reuqest): bcoded = bencoder.decode(content) info_hash = sha1(bencoder.encode(bcoded[b'info'])).hexdigest() + #TODO: Validate the input serverside before writing it to the database + name = request.form["name"] + category = request.form["category"] + subcategory = request.form["subcategory"] + description = request.form["description"] + audioquality_description = request.form["audioquality_description"] + videoquality_description = request.form["videoquality_description"] + newTFile = TorrentFile(info_hash, name, category, subcategory, description, audioquality_description, videoquality_description) + try: + announce = bcoded[b'announce-list'] + except KeyError: + announce = (bcoded[b'announce'], ) + + is_ours = False + for a in announce: + if a in [b'udp://tracker.lootbox.cf:6969/announce', b'udp://tracker.lootbox.cf:6969/announce/',b'udp://tracker.lootbox.cf/announce',b'udp://tracker.lootbox.cf/announce/']: + is_ours = True + break + + if not is_ours: + newTFile.errors = ["Rejecting torrent {}, as it does not use our tracker".format(info_hash, info_hash[:-20])] + return newTFile + + with open("torrentFiles/" + info_hash, "wb") as torrent_file: torrent_file.write(content) - bcoded = bencoder.decode(content) size = ((len(bcoded[b'info'][b'pieces']) / 20) * bcoded[b'info'][b'piece length']) / 1024 / 1024 print("=== CREATE NEW TORRENT FILE ===") @@ -144,14 +167,6 @@ def createNewTorrent(reuqest): print( "Subcategory: " + request.form["subcategory"] ) print( "Description: " + request.form["description"] ) - #TODO: Validate the input serverside before writing it to the database - name = request.form["name"] - category = request.form["category"] - subcategory = request.form["subcategory"] - description = request.form["description"] - audioquality_description = request.form["audioquality_description"] - videoquality_description = request.form["videoquality_description"] - newTFile = TorrentFile(info_hash, name, category, subcategory, description, audioquality_description, videoquality_description) try: connection = sqlite3.connect("torrentdb.sqlite") newTFile.writeToDb(connection.cursor()) @@ -162,14 +177,18 @@ def createNewTorrent(reuqest): print(e) newTFile.errors = ["Torrent {} already exists".format(info_hash, info_hash[:-20])] except Exception as e: + print(e) newTFile.errors = ["Unknown error in creation"] return newTFile class Metadata(): def __init__(self, fileid): - with open("torrentFiles/" + fileid, "rb") as f: - torrent = f.read() + try: + with open("torrentFiles/" + fileid, "rb") as f: + torrent = f.read() + except FileNotFoundError: + return self.fileid = fileid self.bcoded = bencoder.decode(torrent) self.created_by = self.bcoded.get(b'created by', b"")