2024-06-27 16:51:13 +03:00
|
|
|
import sys
|
2024-06-27 19:18:48 +03:00
|
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
2024-06-27 16:51:13 +03:00
|
|
|
import json
|
2024-06-27 21:26:04 +03:00
|
|
|
import urllib.parse
|
2024-07-02 02:23:39 +03:00
|
|
|
import psutil
|
2024-06-28 14:08:33 +03:00
|
|
|
from downloader import Downloader
|
2024-06-27 16:51:13 +03:00
|
|
|
|
|
|
|
torrent_port = sys.argv[1]
|
2024-06-27 19:18:48 +03:00
|
|
|
http_port = sys.argv[2]
|
2024-06-28 14:03:01 +03:00
|
|
|
rpc_password = sys.argv[3]
|
2024-07-03 22:10:23 +03:00
|
|
|
start_download_payload = sys.argv[4]
|
2024-06-27 19:18:48 +03:00
|
|
|
|
2024-07-03 17:23:50 +03:00
|
|
|
downloader = None
|
2024-06-27 16:51:13 +03:00
|
|
|
|
2024-07-03 22:10:23 +03:00
|
|
|
if start_download_payload:
|
|
|
|
initial_download = json.loads(urllib.parse.unquote(start_download_payload))
|
2024-07-03 17:23:50 +03:00
|
|
|
downloader = Downloader(torrent_port)
|
|
|
|
downloader.start_download(initial_download['game_id'], initial_download['magnet'], initial_download['save_path'])
|
2024-06-27 21:26:04 +03:00
|
|
|
|
2024-06-27 19:18:48 +03:00
|
|
|
class Handler(BaseHTTPRequestHandler):
|
2024-06-28 14:03:01 +03:00
|
|
|
rpc_password_header = 'x-hydra-rpc-password'
|
|
|
|
|
2024-06-27 19:18:48 +03:00
|
|
|
def do_GET(self):
|
|
|
|
if self.path == "/status":
|
2024-06-28 14:03:01 +03:00
|
|
|
if self.headers.get(self.rpc_password_header) != rpc_password:
|
|
|
|
self.send_response(401)
|
|
|
|
self.end_headers()
|
|
|
|
return
|
|
|
|
|
2024-06-27 19:18:48 +03:00
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "application/json")
|
|
|
|
self.end_headers()
|
|
|
|
|
2024-06-27 20:10:30 +03:00
|
|
|
status = downloader.get_download_status()
|
|
|
|
|
|
|
|
self.wfile.write(json.dumps(status).encode('utf-8'))
|
2024-07-03 18:06:26 +03:00
|
|
|
|
|
|
|
elif self.path == "/healthcheck":
|
2024-06-27 20:46:59 +03:00
|
|
|
self.send_response(200)
|
|
|
|
self.end_headers()
|
2024-07-02 02:23:39 +03:00
|
|
|
|
2024-07-03 18:06:26 +03:00
|
|
|
elif self.path == "/process-list":
|
2024-07-03 17:23:50 +03:00
|
|
|
if self.headers.get(self.rpc_password_header) != rpc_password:
|
|
|
|
self.send_response(401)
|
|
|
|
self.end_headers()
|
|
|
|
return
|
|
|
|
|
2024-07-03 21:29:59 +03:00
|
|
|
process_list = [proc.info for proc in psutil.process_iter(['exe', 'pid', 'username'])]
|
2024-07-03 18:06:26 +03:00
|
|
|
|
2024-07-02 02:23:39 +03:00
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "application/json")
|
|
|
|
self.end_headers()
|
2024-07-03 18:06:26 +03:00
|
|
|
|
2024-07-03 21:29:59 +03:00
|
|
|
self.wfile.write(json.dumps(process_list).encode('utf-8'))
|
2024-06-27 19:18:48 +03:00
|
|
|
|
|
|
|
def do_POST(self):
|
2024-07-03 18:06:26 +03:00
|
|
|
global downloader
|
2024-07-03 21:29:59 +03:00
|
|
|
|
2024-06-27 19:18:48 +03:00
|
|
|
if self.path == "/action":
|
2024-06-28 14:03:01 +03:00
|
|
|
if self.headers.get(self.rpc_password_header) != rpc_password:
|
|
|
|
self.send_response(401)
|
|
|
|
self.end_headers()
|
|
|
|
return
|
|
|
|
|
2024-06-27 19:18:48 +03:00
|
|
|
content_length = int(self.headers['Content-Length'])
|
|
|
|
post_data = self.rfile.read(content_length)
|
|
|
|
data = json.loads(post_data.decode('utf-8'))
|
|
|
|
|
2024-07-03 17:23:50 +03:00
|
|
|
if downloader is None:
|
|
|
|
downloader = Downloader(torrent_port)
|
|
|
|
|
2024-06-27 19:18:48 +03:00
|
|
|
if data['action'] == 'start':
|
2024-06-27 20:10:30 +03:00
|
|
|
downloader.start_download(data['game_id'], data['magnet'], data['save_path'])
|
2024-06-27 19:18:48 +03:00
|
|
|
elif data['action'] == 'pause':
|
2024-06-27 20:10:30 +03:00
|
|
|
downloader.pause_download(data['game_id'])
|
2024-06-27 19:18:48 +03:00
|
|
|
elif data['action'] == 'cancel':
|
2024-06-27 20:10:30 +03:00
|
|
|
downloader.cancel_download(data['game_id'])
|
2024-07-03 18:06:26 +03:00
|
|
|
elif data['action'] == 'kill-torrent':
|
2024-07-03 22:10:23 +03:00
|
|
|
downloader.abort_session()
|
2024-07-03 18:06:26 +03:00
|
|
|
downloader = None
|
|
|
|
|
2024-06-27 19:18:48 +03:00
|
|
|
self.send_response(200)
|
|
|
|
self.end_headers()
|
|
|
|
|
2024-06-27 16:51:13 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-06-27 19:18:48 +03:00
|
|
|
httpd = HTTPServer(("", int(http_port)), Handler)
|
2024-06-27 20:10:30 +03:00
|
|
|
httpd.serve_forever()
|