-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.py
More file actions
73 lines (53 loc) · 2.09 KB
/
serve.py
File metadata and controls
73 lines (53 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import logging
import click
from pathlib import Path
from flask import Flask, send_from_directory, jsonify, request, redirect
app = Flask(__name__, static_folder=None)
current_files_dir = Path(os.getcwd())
psfree_files_dir = current_files_dir / "PSFree"
static_files_dir = current_files_dir / "static"
is_debug = False
redirect_url = None
@app.route("/api/<path:path>", methods=["POST"])
def read_api_routes(path: str):
def disable_etho0():
logging.error("Disable eth0 interface...")
if not is_debug:
os.system("ifconfig eth0 down")
exit(0)
def shutdown():
logging.error("Shutting down the system...")
if not is_debug:
os.system("halt")
exit(0)
if path == "disable-eth0":
disable_etho0()
elif path == "shutdown":
shutdown()
return jsonify({"path": path})
@app.route("/", methods=["GET"])
@app.route("/static/<path:path>", methods=["GET"])
def catch_static_routes(path: str = "index.html"):
file_path = static_files_dir / path
if os.path.isdir(file_path):
file_path = file_path / "index.html"
return send_from_directory(file_path.parent, file_path.name)
@app.route("/<path:path>", methods=["GET"])
def catch_all_routes(path: str):
if redirect_url and "playstation.net" in request.host:
return redirect(redirect_url)
file_path = psfree_files_dir / path
return send_from_directory(file_path.parent, file_path.name)
@click.command()
@click.option("--host", default="0.0.0.0", help="Host to run the server on")
@click.option("--port", default=9191, help="Port to run the server on")
@click.option("--debug", is_flag=True, help="Enable debug mode")
@click.option("--redirect-playstation-domain", default=None, type=str, help="Redirect *.playstation.net domain to other URL")
def main(host: str, port: int, debug: bool, redirect_playstation_domain: str):
global is_debug, redirect_url
is_debug = debug
redirect_url = redirect_playstation_domain
app.run(host=host, port=port, debug=debug, threaded=True)
if __name__ == "__main__":
main()