from flask import Flask, request, jsonify import subprocess #from win10toast import ToastNotifier import os import logging logging.basicConfig(filename='osiris_listener.log', level=logging.DEBUG, format='%(asctime)s %(message)s') app = Flask(__name__) #toaster = ToastNotifier() @app.route('/osiris_execute', methods=['POST']) def execute_script(): logging.info("Commando zur Ausführung erhalten.") data = request.get_json() command = data.get('command') if command: logging.info(f"Commando: {command}") # Hier neue Commands zur Prüfung einpflegen clean_command = command.strip() match clean_command: case 'testcommand': try: #toaster.show_toast("TestCommand", "Verbindung wurde getestet", duration=10) return jsonify({'message': 'Test Command abgesetzt und Notification auf bei Host erstellt.'}) except Exception as exc: logging.error(f"Error executing test command: {exc}") return jsonify({'error': str(exc)}), 500 case 'shutdown': try: #toaster.show_toast("Shutdown", "Shutdown Command erhalten. Host wird heruntergefahren.", duration=10) os.system("shutdown /s /t 1") return jsonify({'message': 'Shutdown command wurde ausgeführt. System fährt herunter.'}) except Exception as e: logging.error(f"Error executing shutdown command: {exc}") return jsonify({'error': str(e)}), 500 case 'start_gameservers_minecraft': try: #toaster.show_toast("start_gameservers_minecraft", "Starte Minecraft-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'Minecraft-Server werden gestartet.'}) except Exception as exc: logging.error(f"Error executing start_gameservers_dontstarve command: {exc}") return jsonify({'error': str(exc)}), 500 case 'stop_gameservers_minecraft': try: #toaster.show_toast("stop_gameservers_minecraft", "Stoppe Minecraft-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'Minecraft-Server wurden angehalten.'}) except Exception as exc: logging.error(f"Error executing stop_gameservers_minecraft command: {exc}") return jsonify({'error': str(exc)}), 500 case 'start_gameservers_dontstarve': try: #toaster.show_toast("start_gameservers_dontstarve", "Starte Dont Starve Together-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'Dont Starve Together-Server werden gestartet.'}) except Exception as exc: logging.error(f"Error executing start_gameservers_dontstarve command: {exc}") return jsonify({'error': str(exc)}), 500 case 'stop_gameservers_dontstarve': try: #toaster.show_toast("stop_gameservers_dontstarve", "Stoppe Dont Starve Together-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'Dont Starve Together-Server wurden angehalten.'}) except Exception as exc: logging.error(f"Error executing stop_gameservers_dontstarve command: {exc}") return jsonify({'error': str(exc)}), 500 case 'start_gameservers_ragnarokonline': try: #toaster.show_toast("start_gameservers_ragnarokonline", "Starte RO-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'RO-Server werden gestartet.'}) except Exception as exc: logging.error(f"Error executing start_gameservers_ragnarokonline command: {exc}") return jsonify({'error': str(exc)}), 500 case 'stop_gameservers_ragnarokonline': try: #toaster.show_toast("stop_gameservers_ragnarokonline", "Stoppe RO-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'RO-Server wurden angehalten.'}) except Exception as exc: logging.error(f"Error executing stop_gameservers_ragnarokonline command: {exc}") return jsonify({'error': str(exc)}), 500 case 'start_gameservers_wow': try: #toaster.show_toast("start_gameservers_wow", "Starte WoW-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'WoW-Server werden gestartet.'}) except Exception as exc: logging.error(f"Error executing start_gameservers_wow command: {exc}") return jsonify({'error': str(exc)}), 500 case 'stop_gameservers_wow': try: #toaster.show_toast("stop_gameservers_wow", "Stoppe WoW-Server", duration=10) # hier pgm aufrufen return jsonify({'message': 'WoW-Server wurden angehalten.'}) except Exception as exc: logging.error(f"Error executing stop_gameservers_wow command: {exc}") return jsonify({'error': str(exc)}), 500 case _: try: result = subprocess.run(['python','-c', command], capture_output=True, text=True, shell=True) return jsonify({'output': result.stdout, 'error': result.stderr, 'returncode': result.returncode}) except Exception as exc: logging.error(f"Error executing command: {exc}") return jsonify({'error': str(exc)}), 500 @app.route('/status', methods=['GET']) def status(): logging.info("Status Check received.") return jsonify({'status': 'online'}) if __name__ == '__main__': #toaster.show_toast("Starte Osiris", "Osiris Listener wurde gestartet. Warte auf Befehle...", duration=30) logging.info("Starte Server.") app.run(host='0.0.0.0', port=9713)