import socketserver import http.server import json import re try: config_file = open('config.json', 'r') except FileNotFoundError: print('Config file not found') exit() 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') subprocess.call(command) class MyHandler(http.server.SimpleHTTPRequestHandler): def do_POST(self): self.data_string = self.rfile.read(int(self.headers['Content-Length'])) try: data = json.loads(self.data_string) except: data = "" if self.path == '/shutdown': match = False for keyword in config['shutdown_keywords']: if re.search(keyword, data['message']): self.send_response(200) self.end_headers() self.wfile.write(b'Shutting down...') print('Shutting down...') do_shutdown() match = True return if not match: self.send_response(200) self.end_headers() 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'

') else: self.send_response(404) self.end_headers() self.wfile.write(b'Not found') httpd = socketserver.TCPServer(('', 48080), MyHandler) httpd.serve_forever()