mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-01-23 13:34:54 +03:00
fix: using python http server
This commit is contained in:
parent
8a30e946e3
commit
954037b826
@ -1,24 +1,24 @@
|
|||||||
from flask import Flask, request, jsonify
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
import sys, json, urllib.parse, psutil
|
import json
|
||||||
|
import urllib.parse
|
||||||
|
import sys
|
||||||
|
import psutil
|
||||||
from torrent_downloader import TorrentDownloader
|
from torrent_downloader import TorrentDownloader
|
||||||
from http_downloader import HttpDownloader
|
from http_downloader import HttpDownloader
|
||||||
from profile_image_processor import ProfileImageProcessor
|
from profile_image_processor import ProfileImageProcessor
|
||||||
import libtorrent as lt
|
import libtorrent as lt
|
||||||
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
# Retrieve command line arguments
|
# Retrieve command line arguments
|
||||||
torrent_port = sys.argv[1]
|
torrent_port = sys.argv[1]
|
||||||
http_port = sys.argv[2]
|
http_port = int(sys.argv[2])
|
||||||
rpc_password = sys.argv[3]
|
rpc_password = sys.argv[3]
|
||||||
start_download_payload = sys.argv[4]
|
start_download_payload = sys.argv[4]
|
||||||
start_seeding_payload = sys.argv[5]
|
start_seeding_payload = sys.argv[5]
|
||||||
|
|
||||||
downloads = {}
|
downloads = {}
|
||||||
# This can be streamed down from Node
|
|
||||||
downloading_game_id = -1
|
downloading_game_id = -1
|
||||||
|
|
||||||
torrent_session = lt.session({'listen_interfaces': '0.0.0.0:{port}'.format(port=torrent_port)})
|
torrent_session = lt.session({'listen_interfaces': f'0.0.0.0:{torrent_port}'})
|
||||||
|
|
||||||
if start_download_payload:
|
if start_download_payload:
|
||||||
initial_download = json.loads(urllib.parse.unquote(start_download_payload))
|
initial_download = json.loads(urllib.parse.unquote(start_download_payload))
|
||||||
@ -49,33 +49,37 @@ if start_seeding_payload:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error starting seeding", e)
|
print("Error starting seeding", e)
|
||||||
|
|
||||||
def validate_rpc_password():
|
class RequestHandler(BaseHTTPRequestHandler):
|
||||||
"""Middleware to validate RPC password."""
|
def validate_rpc_password(self):
|
||||||
header_password = request.headers.get('x-hydra-rpc-password')
|
header_password = self.headers.get('x-hydra-rpc-password')
|
||||||
if header_password != rpc_password:
|
if header_password != rpc_password:
|
||||||
return jsonify({"error": "Unauthorized"}), 401
|
self.send_response(401)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps({"error": "Unauthorized"}).encode('utf-8'))
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
@app.route("/status", methods=["GET"])
|
def do_GET(self):
|
||||||
def status():
|
if self.path == "/status":
|
||||||
auth_error = validate_rpc_password()
|
if not self.validate_rpc_password():
|
||||||
if auth_error:
|
return
|
||||||
return auth_error
|
|
||||||
|
|
||||||
downloader = downloads.get(downloading_game_id)
|
downloader = downloads.get(downloading_game_id)
|
||||||
if downloader:
|
if downloader:
|
||||||
status = downloads.get(downloading_game_id).get_download_status()
|
status = downloader.get_download_status()
|
||||||
return jsonify(status), 200
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps(status).encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
return jsonify(None)
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps(None).encode('utf-8'))
|
||||||
|
|
||||||
@app.route("/seed-status", methods=["GET"])
|
elif self.path == "/seed-status":
|
||||||
def seed_status():
|
if not self.validate_rpc_password():
|
||||||
auth_error = validate_rpc_password()
|
return
|
||||||
if auth_error:
|
|
||||||
return auth_error
|
|
||||||
|
|
||||||
seed_status = []
|
seed_status = []
|
||||||
|
|
||||||
for game_id, downloader in downloads.items():
|
for game_id, downloader in downloads.items():
|
||||||
if not downloader:
|
if not downloader:
|
||||||
continue
|
continue
|
||||||
@ -90,46 +94,45 @@ def seed_status():
|
|||||||
**response,
|
**response,
|
||||||
})
|
})
|
||||||
|
|
||||||
return jsonify(seed_status), 200
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps(seed_status).encode('utf-8'))
|
||||||
|
|
||||||
@app.route("/healthcheck", methods=["GET"])
|
elif self.path == "/healthcheck":
|
||||||
def healthcheck():
|
self.send_response(200)
|
||||||
return "", 200
|
self.end_headers()
|
||||||
|
|
||||||
@app.route("/process-list", methods=["GET"])
|
elif self.path == "/process-list":
|
||||||
def process_list():
|
if not self.validate_rpc_password():
|
||||||
auth_error = validate_rpc_password()
|
return
|
||||||
if auth_error:
|
|
||||||
return auth_error
|
|
||||||
|
|
||||||
process_list = [proc.info for proc in psutil.process_iter(['exe', 'pid', 'name'])]
|
process_list = [proc.info for proc in psutil.process_iter(['exe', 'pid', 'name'])]
|
||||||
return jsonify(process_list), 200
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps(process_list).encode('utf-8'))
|
||||||
|
|
||||||
@app.route("/profile-image", methods=["POST"])
|
def do_POST(self):
|
||||||
def profile_image():
|
if not self.validate_rpc_password():
|
||||||
auth_error = validate_rpc_password()
|
return
|
||||||
if auth_error:
|
|
||||||
return auth_error
|
|
||||||
|
|
||||||
data = request.get_json()
|
content_length = int(self.headers['Content-Length'])
|
||||||
|
post_data = self.rfile.read(content_length)
|
||||||
|
data = json.loads(post_data.decode('utf-8'))
|
||||||
|
|
||||||
|
if self.path == "/profile-image":
|
||||||
image_path = data.get('image_path')
|
image_path = data.get('image_path')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
processed_image_path, mime_type = ProfileImageProcessor.process_image(image_path)
|
processed_image_path, mime_type = ProfileImageProcessor.process_image(image_path)
|
||||||
return jsonify({'imagePath': processed_image_path, 'mimeType': mime_type}), 200
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps({'imagePath': processed_image_path, 'mimeType': mime_type}).encode('utf-8'))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 400
|
self.send_response(400)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps({"error": str(e)}).encode('utf-8'))
|
||||||
|
|
||||||
@app.route("/action", methods=["POST"])
|
elif self.path == "/action":
|
||||||
def action():
|
|
||||||
global torrent_session
|
|
||||||
global downloading_game_id
|
global downloading_game_id
|
||||||
|
|
||||||
auth_error = validate_rpc_password()
|
|
||||||
if auth_error:
|
|
||||||
return auth_error
|
|
||||||
|
|
||||||
data = request.get_json()
|
|
||||||
action = data.get('action')
|
action = data.get('action')
|
||||||
game_id = data.get('game_id')
|
game_id = data.get('game_id')
|
||||||
|
|
||||||
@ -172,12 +175,16 @@ def action():
|
|||||||
downloader = downloads.get(game_id)
|
downloader = downloads.get(game_id)
|
||||||
if downloader:
|
if downloader:
|
||||||
downloader.cancel_download()
|
downloader.cancel_download()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return jsonify({"error": "Invalid action"}), 400
|
self.send_response(400)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps({"error": "Invalid action"}).encode('utf-8'))
|
||||||
|
return
|
||||||
|
|
||||||
return "", 200
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=int(http_port))
|
server = HTTPServer(('0.0.0.0', http_port), RequestHandler)
|
||||||
|
print(f"Server running on port {http_port}")
|
||||||
|
server.serve_forever()
|
||||||
|
@ -4,5 +4,4 @@ cx_Logging; sys_platform == 'win32'
|
|||||||
pywin32; sys_platform == 'win32'
|
pywin32; sys_platform == 'win32'
|
||||||
psutil
|
psutil
|
||||||
Pillow
|
Pillow
|
||||||
flask
|
|
||||||
aria2p
|
aria2p
|
||||||
|
Loading…
Reference in New Issue
Block a user