#!/usr/bin/python3 import os, sys import subprocess import xmlrpc.client as xc import ssl import argparse parser = argparse.ArgumentParser(description='Upload a file to the bittorrent seeder.') parser.add_argument('--rpc-server', required=True, help='the RPC server IPaddress:port') parser.add_argument('--rpc-secret', required=True, help='the RPC secret') parser.add_argument('--dht-port', required=True, help='the DHT port the RPC server is listening on') parser.add_argument('--no-cert', action='store_true', help='do not use SSL certificate') parser.add_argument('--cert', help='the certificate to use for verification') parser.add_argument('file', help='the file to upload') args = parser.parse_args() rpcseeder = 'https://' + args.rpc_server + '/rpc' secret = 'token:' + args.rpc_secret dhtentry = args.rpc_server.split(':')[0] + ':' + args.dht_port file2send = args.file torrent = '/tmp/' + os.path.basename(file2send) + '.torrent' ssl_ctx = ssl.create_default_context() if args.no_cert: ssl_ctx.check_hostname = False ssl_ctx.verify_mode = ssl.CERT_NONE print("Certificate verification disabled.") elif args.cert is not None: ssl_ctx.load_verify_locations(args.cert) s = xc.ServerProxy(rpcseeder, context = ssl_ctx) def make_torrent(): if os.path.isfile(torrent): print("Torrent file", torrent, "exists already, please (re)move it.") sys.exit(1) subprocess.run(["/usr/bin/mktorrent", "-l 24", "-v", "-o", torrent, file2send], check=True) h = subprocess.check_output(["/usr/bin/aria2c", "-S ", torrent]) for line in h.decode().splitlines(): if "Info Hash" in line: return line.split(': ')[1] def check_seeds(bthash): active_seeds = s.aria2.tellActive(secret) for seed in active_seeds: f = seed['bittorrent']['info']['name'] gid = seed['gid'] ihash = seed['infoHash'] if f == os.path.basename(file2send): print(file2send, "is already seeded with GID:", gid) print("Info Hash is:", ihash) if bthash == ihash: print("The torrent file has not changed, exiting.") return False else: print("The torrent file has changed, replacing torrent.") s.aria2.remove(secret, gid) return True print("="*19, " Uploading new torrent with aria2 now. ", "="*19) return True def upload_torrent(): s.aria2.addTorrent(secret, xc.Binary(open(torrent, mode='rb').read())) subprocess.run(["/usr/bin/aria2c", "--dht-entry-point=" + dhtentry, "--check-integrity", "--dir=" + os.path.dirname(file2send), torrent]) ############################ if __name__ == '__main__': infoHash = make_torrent() if check_seeds(infoHash): upload_torrent() print("Upload finished.")