From e16c2faf90570e90ea37de92e98f97b4c96af36e Mon Sep 17 00:00:00 2001 From: Lino Schmidt Date: Sun, 27 Oct 2024 15:41:07 +0100 Subject: [PATCH] added abilty to toggle shutdown --- README.md | 1 + example.config.json | 3 ++- synology-webhook.py | 23 ++++++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6defc15..980f8b2 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,4 @@ To shutdown the NAS, send a POST request to `http://:48080/shutdown` w "message": "" } ``` +This can be turned on and off in the `config.json` file or with the webinterface `http://:48080`. diff --git a/example.config.json b/example.config.json index 21fa796..be3133f 100644 --- a/example.config.json +++ b/example.config.json @@ -1,5 +1,6 @@ { "shutdown_keywords": [ "Sicherungsaufgabe Backup auf RS815p wurde abgeschlossen" - ] + ], + "shutdown_active": true } \ No newline at end of file diff --git a/synology-webhook.py b/synology-webhook.py index 8178460..8851dc5 100644 --- a/synology-webhook.py +++ b/synology-webhook.py @@ -11,6 +11,9 @@ except FileNotFoundError: config = json.load(config_file) def do_shutdown(): + if config['shutdown_active'] == False: + return + import subprocess import shlex command = shlex.split('sudo shutdown -h now') @@ -19,7 +22,10 @@ def do_shutdown(): class MyHandler(http.server.SimpleHTTPRequestHandler): def do_POST(self): self.data_string = self.rfile.read(int(self.headers['Content-Length'])) - data = json.loads(self.data_string) + try: + data = json.loads(self.data_string) + except: + data = "" if self.path == '/shutdown': match = False @@ -39,5 +45,20 @@ class MyHandler(http.server.SimpleHTTPRequestHandler): self.wfile.write(b'Not shutting down') print('Not shutting down') + elif self.path == '/toggle_shutdown': + config['shutdown_active'] = not config['shutdown_active'] + with open('config.json', 'w') as config_file: + json.dump(config, config_file) + self.send_response(303) + self.send_header('Location', '/') + self.end_headers() + print('Shutdown toggled') + + def do_GET(self): + if self.path == '/': + self.send_response(200) + self.end_headers() + self.wfile.write(b'Synology Webhook

Shutdown active: ' + str(config['shutdown_active']).encode() + b'

') + httpd = socketserver.TCPServer(('', 48080), MyHandler) httpd.serve_forever() \ No newline at end of file