From 1e4f6439d388cf7b631d0e19f6da06fad90963e7 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Fri, 2 May 2025 14:14:43 +0330 Subject: [PATCH] Refactor: Implement Hysteria server update in Python --- core/cli_api.py | 4 +- core/scripts/hysteria2/update.py | 105 +++++++++++++++++++++++++++++++ core/scripts/hysteria2/update.sh | 47 -------------- 3 files changed, 107 insertions(+), 49 deletions(-) create mode 100644 core/scripts/hysteria2/update.py delete mode 100644 core/scripts/hysteria2/update.sh diff --git a/core/cli_api.py b/core/cli_api.py index 61c4966..f644380 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -18,7 +18,7 @@ class Command(Enum): '''Contains path to command's script''' 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') + UPDATE_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'update.py') 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') @@ -137,7 +137,7 @@ def uninstall_hysteria2(): def update_hysteria2(): '''Updates Hysteria2.''' - run_cmd(['bash', Command.UPDATE_HYSTERIA2.value]) + run_cmd(['python3', Command.UPDATE_HYSTERIA2.value]) def restart_hysteria2(): diff --git a/core/scripts/hysteria2/update.py b/core/scripts/hysteria2/update.py new file mode 100644 index 0000000..7e41ead --- /dev/null +++ b/core/scripts/hysteria2/update.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 + +import subprocess +import shutil +import os +import sys +from init_paths import * +from paths import * + +CONFIG_BACKUP = "/etc/hysteria/config_backup.json" +SERVICE_FILE = "/etc/systemd/system/hysteria-server.service" +OLD_CONFIG_PATH = "/etc/hysteria/config.yaml" + +def backup_config(): + print("šŸ“¦ Backing up the current configuration...") + try: + shutil.copy(CONFIG_FILE, CONFIG_BACKUP) + return True + except Exception as e: + print(f"āŒ Error: Failed to back up configuration: {e}") + return False + +def restore_config(): + print("ā™»ļø Restoring configuration from backup...") + try: + shutil.move(CONFIG_BACKUP, CONFIG_FILE) + return True + except Exception as e: + print(f"āŒ Error: Failed to restore configuration: {e}") + return False + +def install_latest_hysteria(): + print("ā¬‡ļø Downloading and installing the latest version of Hysteria2...") + try: + cmd = 'bash -c "$(curl -fsSL https://get.hy2.sh/)"' + result = subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + return result.returncode == 0 + except Exception as e: + print(f"āŒ Error during installation: {e}") + return False + +def modify_systemd_service(): + print("āš™ļø Modifying systemd service to use config.json...") + try: + with open(SERVICE_FILE, 'r') as f: + service_data = f.read() + + new_data = service_data.replace( + "Description=Hysteria Server Service (config.yaml)", + "Description=Hysteria Server Service (Blitz Panel)" + ) + + new_data = new_data.replace(str(OLD_CONFIG_PATH), str(CONFIG_FILE)) + + with open(SERVICE_FILE, 'w') as f: + f.write(new_data) + + return True + except Exception as e: + print(f"āŒ Error: Failed to modify systemd service: {e}") + return False + +def restart_hysteria(): + print("šŸ”„ Restarting Hysteria2 service...") + try: + subprocess.run(["systemctl", "daemon-reload"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["python3", CLI_PATH, "restart-hysteria2"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + return True + except Exception as e: + print(f"āŒ Error: Failed to restart Hysteria2: {e}") + return False + +def main(): + print("šŸš€ Starting the update process for Hysteria2...") + + if not backup_config(): + print("āŒ Aborting update due to failed backup.") + sys.exit(1) + + if not install_latest_hysteria(): + print("āŒ Installation failed. Restoring previous config...") + restore_config() + restart_hysteria() + sys.exit(1) + + if not restore_config(): + sys.exit(1) + + if not modify_systemd_service(): + sys.exit(1) + + try: + if os.path.exists(OLD_CONFIG_PATH): + os.remove(OLD_CONFIG_PATH) + except Exception as e: + print(f"āš ļø Failed to remove old YAML config: {e}") + + if not restart_hysteria(): + sys.exit(1) + + print("\nāœ… Hysteria2 has been successfully updated.") + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/core/scripts/hysteria2/update.sh b/core/scripts/hysteria2/update.sh deleted file mode 100644 index ec371bc..0000000 --- a/core/scripts/hysteria2/update.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash - -# Source the path.sh script to load the CONFIG_FILE variable -source /etc/hysteria/core/scripts/path.sh - -echo "Starting the update process for Hysteria2..." -echo "Backing up the current configuration..." -cp "$CONFIG_FILE" /etc/hysteria/config_backup.json -if [ $? -ne 0 ]; then - echo "Error: Failed to back up configuration. Aborting update." - exit 1 -fi - -echo "Downloading and installing the latest version of Hysteria2..." -bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1 -if [ $? -ne 0 ]; then - echo "Error: Failed to download or install the latest version. Restoring backup configuration." - mv /etc/hysteria/config_backup.json "$CONFIG_FILE" - python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1 - exit 1 -fi - -echo "Restoring configuration from backup..." -mv /etc/hysteria/config_backup.json "$CONFIG_FILE" -if [ $? -ne 0 ]; then - echo "Error: Failed to restore configuration from backup." - exit 1 -fi - -echo "Modifying systemd service to use config.json..." -sed -i "s|/etc/hysteria/config.yaml|$CONFIG_FILE|" /etc/systemd/system/hysteria-server.service -if [ $? -ne 0 ]; then - echo "Error: Failed to modify systemd service." - exit 1 -fi - -rm /etc/hysteria/config.yaml -systemctl daemon-reload >/dev/null 2>&1 -python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1 -if [ $? -ne 0 ]; then - echo "Error: Failed to restart Hysteria2 service." - exit 1 -fi - -echo "Hysteria2 has been successfully updated." -echo "" -exit 0