From 5a253258e8d6c2a8ade17f96c906aa99073f4ce7 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Fri, 2 May 2025 12:35:44 +0330 Subject: [PATCH] Refactor: Implement Hysteria server restart in Python --- core/cli_api.py | 4 ++-- core/scripts/hysteria2/restart.py | 34 +++++++++++++++++++++++++++++++ core/scripts/hysteria2/restart.sh | 8 -------- 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 core/scripts/hysteria2/restart.py delete mode 100644 core/scripts/hysteria2/restart.sh diff --git a/core/cli_api.py b/core/cli_api.py index beb2dee..ea611db 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -19,7 +19,7 @@ class Command(Enum): INSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'install.sh') UNINSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'uninstall.sh') UPDATE_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'update.sh') - RESTART_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'restart.sh') + RESTART_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'restart.py') CHANGE_PORT_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_port.py') CHANGE_SNI_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_sni.sh') GET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'get_user.py') @@ -139,7 +139,7 @@ def update_hysteria2(): def restart_hysteria2(): '''Restarts Hysteria2.''' - run_cmd(['bash', Command.RESTART_HYSTERIA2.value]) + run_cmd(['python3', Command.RESTART_HYSTERIA2.value]) def get_hysteria2_port() -> int | None: diff --git a/core/scripts/hysteria2/restart.py b/core/scripts/hysteria2/restart.py new file mode 100644 index 0000000..0bd16de --- /dev/null +++ b/core/scripts/hysteria2/restart.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import subprocess +import sys +import os +from init_paths import * +from paths import * + +def restart_hysteria_server(): + """ + Restarts the Hysteria server service. + + Returns: + int: 0 on success, 1 on failure. + """ + try: + subprocess.run([sys.executable, CLI_PATH, "traffic-status"], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + subprocess.run(["systemctl", "restart", "hysteria-server.service"], check=True) + print("Hysteria server restarted successfully.") + return 0 + except subprocess.CalledProcessError as e: + print(f"Error: Failed to restart the Hysteria server.") + return 1 + except FileNotFoundError: + print(f"Error: CLI script not found at {CLI_PATH}.") + return 1 + except Exception as e: + print(f"An unexpected error occurred: {e}") + return 1 + +if __name__ == "__main__": + exit_code = restart_hysteria_server() + sys.exit(exit_code) \ No newline at end of file diff --git a/core/scripts/hysteria2/restart.sh b/core/scripts/hysteria2/restart.sh deleted file mode 100644 index d3c5d8d..0000000 --- a/core/scripts/hysteria2/restart.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -python3 /etc/hysteria/core/cli.py traffic-status > /dev/null 2>&1 -if systemctl restart hysteria-server.service; then - echo "Hysteria server restarted successfully." -else - echo "Error: Failed to restart the Hysteria server." -fi \ No newline at end of file