diff --git a/proxy.py b/proxy.py index 477dcc9..f90e68e 100644 --- a/proxy.py +++ b/proxy.py @@ -1,4 +1,4 @@ -import http.client, re, socket, sys, time, _thread +import http.client, re, socket, sys, time, _thread, threading, os RECBUF = 1024 @@ -108,6 +108,13 @@ def handle(client): client.close() +def run_watchdog(service_name, timeout): + while True: + if time.time() > last_request + timeout: + os.system("systemctl restart {}".format(service_name)) + last_request = time.time() + time.sleep(60) + if __name__ == "__main__": if "--help" in sys.argv or "-h" in sys.argv: print("""MineTest Matrix Bridge mod proxy @@ -122,6 +129,10 @@ Options: (with defaults) -A matrix.txmn.tk Matrix address (without protocol and port) -P 8448 Matrix port -s Use HTTP instead of HTTPS (default: no) + -w Enable watchdog + Restarts the given systemd service when no request from + the minetest server after a given time. Needs root. + --wtime 600 Watchdog timeout (seconds) """) exit() @@ -130,6 +141,14 @@ Options: (with defaults) matrix_addr = getargv("-A", "matrix.txmn.tk") matrix_port = int(getargv("-P", 8448)) matrix_https = not "-s" in sys.argv + watchdog = getargv("-w", None) + watchdog_timeout = int(getargv("--wtime", 600)) + + last_request = time.time() + + if watchdog: + t = threading.Thread(target=run_watchdog, args=(watchdog, watchdog_timeout,)) + t.start() if matrix_https: import ssl @@ -146,8 +165,11 @@ Options: (with defaults) try: client, addr = sock.accept() except socket.timeout: + last_request = time.time() continue + last_request = time.time() + _thread.start_new_thread(handle, (client,)) sock.close() except KeyboardInterrupt: