Refactor: Implement Hysteria server restart in Python

This commit is contained in:
Whispering Wind
2025-05-02 12:35:44 +03:30
committed by GitHub
parent cf4726e5d9
commit 5a253258e8
3 changed files with 36 additions and 10 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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