48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
import os
|
|
import json
|
|
import posixpath
|
|
import urllib.parse
|
|
from http.server import SimpleHTTPRequestHandler, HTTPServer
|
|
|
|
CONFIG_FILENAME = "config.json"
|
|
|
|
|
|
def load_config():
|
|
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), CONFIG_FILENAME)
|
|
with open(config_path, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
|
|
|
|
CONFIG = load_config()
|
|
HOST = CONFIG.get("host", "0.0.0.0")
|
|
PORT = CONFIG.get("port", 3232)
|
|
|
|
# Maps a URL access key (long hex string, see generate_key.py) -> physical
|
|
# folder on the server holding that client's files. Using a random hex key
|
|
# instead of a plain name like "main" just makes the files harder to stumble
|
|
# upon by guessing; it does not stop someone who deliberately extracts the
|
|
# key from the updater executable. If a key ever leaks, generate a new one
|
|
# and update it here plus in the updater's CLIENT_KEYS - no need to touch
|
|
# folder names or anything else.
|
|
CLIENTS = CONFIG.get("clients", {})
|
|
|
|
|
|
class MultiClientHandler(SimpleHTTPRequestHandler):
|
|
def translate_path(self, path):
|
|
path = urllib.parse.urlparse(path).path
|
|
parts = [p for p in path.split('/') if p]
|
|
if len(parts) >= 1 and parts[0] in CLIENTS:
|
|
client_key = parts[0]
|
|
base_dir = CLIENTS[client_key]
|
|
rel_path = posixpath.join(*parts[1:]) if len(parts) > 1 else ""
|
|
return os.path.join(base_dir, rel_path)
|
|
return super().translate_path(path)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
server_address = (HOST, PORT)
|
|
httpd = HTTPServer(server_address, MultiClientHandler)
|
|
print(f"Server running on {HOST}:{PORT}...")
|
|
print(f"Available client keys: {', '.join(CLIENTS.keys()) if CLIENTS else '(none configured)'}")
|
|
httpd.serve_forever()
|