From 22670e70718f0488457bbe44f2f6f2eb69d90360 Mon Sep 17 00:00:00 2001 From: mid-kid Date: Sat, 26 Oct 2024 16:40:21 +0200 Subject: [PATCH] Allow configuring the auth server with a config file --- auth/server.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/auth/server.py b/auth/server.py index d128bdf..ea5aa84 100755 --- a/auth/server.py +++ b/auth/server.py @@ -1,20 +1,18 @@ #!/usr/bin/env python3 -from http.server import * from http import HTTPStatus +from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs +from configparser import ConfigParser from base64 import b64encode from os.path import getmtime import json -server_domain = "127.0.0.1" -server_url = "http://%s:25564" % server_domain - userdata = { "9a10d47934294527b3a0a32ab41d207e": { - "name": "Sneppe" + "name": "test1" }, "4ad1f41992193cb986741243f14d81e8": { - "name": "mid-kid" + "name": "test2" } } connections = {} @@ -22,6 +20,10 @@ connections = {} class HTTPRequestHandler(BaseHTTPRequestHandler): protocol_version = "HTTP/1.1" + def setup(self, *args, **kwargs): + super().setup(*args, **kwargs) + self.config = self.server.config["server"] + def send_ok(self, data): self.send_response(HTTPStatus.OK) self.send_header("Content-Length", len(data)) @@ -203,14 +205,15 @@ class HTTPRequestHandler(BaseHTTPRequestHandler): check_skin = self.check_skin(user["name"]) check_cape = self.check_cape(user["name"]) + url = self.config["server_url"] textures = {} textures_stamp = 0 if check_skin is not None: textures_stamp = max(textures_stamp, check_skin) - textures["SKIN"] = {"url": server_url + "/skin/%s.png" % user["name"]} + textures["SKIN"] = {"url": url + "/skin/%s.png" % user["name"]} if check_cape is not None: textures_stamp = max(textures_stamp, check_cape) - textures["CAPE"] = {"url": server_url + "/cape/%s.cape.png" % user["name"]} + textures["CAPE"] = {"url": url + "/cape/%s.cape.png" % user["name"]} if textures: prop = { @@ -249,7 +252,7 @@ class HTTPRequestHandler(BaseHTTPRequestHandler): "feature.no_mojang_namespace": True, "feature.legacy_skin_api": True }, - "skinDomains": [server_domain] + "skinDomains": [self.config["server_dom"]] } body = json.dumps(settings).encode() self.send_ok(body) @@ -259,11 +262,13 @@ class HTTPRequestHandler(BaseHTTPRequestHandler): self.send_header("X-Authlib-Injector-API-Location", server_url + "/ygg") self.end_headers() -def run(server_class=ThreadingHTTPServer, handler_class=HTTPRequestHandler): - server_address = ('', 25564) - httpd = server_class(server_address, handler_class) - httpd.serve_forever() -try: - run() -except KeyboardInterrupt: - pass +if __name__ == "__main__": + server_address = ("", 25564) + config = ConfigParser() + config.read("config.ini") + httpd = ThreadingHTTPServer(server_address, HTTPRequestHandler) + httpd.config = config + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass