added abilty to toggle shutdown

This commit is contained in:
2024-10-27 15:41:07 +01:00
parent b8712ef607
commit e16c2faf90
3 changed files with 25 additions and 2 deletions

View File

@@ -37,3 +37,4 @@ To shutdown the NAS, send a POST request to `http://<your-nas>:48080/shutdown` w
"message": "<shutdown_keywords set in config.json>" "message": "<shutdown_keywords set in config.json>"
} }
``` ```
This can be turned on and off in the `config.json` file or with the webinterface `http://<your-nas>:48080`.

View File

@@ -1,5 +1,6 @@
{ {
"shutdown_keywords": [ "shutdown_keywords": [
"Sicherungsaufgabe Backup auf RS815p wurde abgeschlossen" "Sicherungsaufgabe Backup auf RS815p wurde abgeschlossen"
] ],
"shutdown_active": true
} }

View File

@@ -11,6 +11,9 @@ except FileNotFoundError:
config = json.load(config_file) config = json.load(config_file)
def do_shutdown(): def do_shutdown():
if config['shutdown_active'] == False:
return
import subprocess import subprocess
import shlex import shlex
command = shlex.split('sudo shutdown -h now') command = shlex.split('sudo shutdown -h now')
@@ -19,7 +22,10 @@ def do_shutdown():
class MyHandler(http.server.SimpleHTTPRequestHandler): class MyHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self): def do_POST(self):
self.data_string = self.rfile.read(int(self.headers['Content-Length'])) 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': if self.path == '/shutdown':
match = False match = False
@@ -39,5 +45,20 @@ class MyHandler(http.server.SimpleHTTPRequestHandler):
self.wfile.write(b'Not shutting down') self.wfile.write(b'Not shutting down')
print('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'<html><head><title>Synology Webhook</title></head><body><p>Shutdown active: ' + str(config['shutdown_active']).encode() + b'</p><form method="post" action="/toggle_shutdown"><input type="submit" value="Toggle shutdown"></form></body></html>')
httpd = socketserver.TCPServer(('', 48080), MyHandler) httpd = socketserver.TCPServer(('', 48080), MyHandler)
httpd.serve_forever() httpd.serve_forever()