From 19192bd02d6de68e31fbe64550bf3ef35f22fbdf Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Fri, 2 May 2025 12:08:19 +0330 Subject: [PATCH] Refactor: Implement port update functionality in Python --- core/cli_api.py | 4 +- core/scripts/hysteria2/change_port.py | 54 +++++++++++++++++++++++++++ core/scripts/hysteria2/change_port.sh | 23 ------------ 3 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 core/scripts/hysteria2/change_port.py delete mode 100644 core/scripts/hysteria2/change_port.sh diff --git a/core/cli_api.py b/core/cli_api.py index be7e508..4629e01 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -20,7 +20,7 @@ class Command(Enum): 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') - CHANGE_PORT_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_port.sh') + 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.sh') ADD_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'add_user.sh') @@ -158,7 +158,7 @@ def change_hysteria2_port(port: int): ''' Changes the port for Hysteria2. ''' - run_cmd(['bash', Command.CHANGE_PORT_HYSTERIA2.value, str(port)]) + run_cmd(['python3', Command.CHANGE_PORT_HYSTERIA2.value, str(port)]) def get_hysteria2_sni() -> str | None: diff --git a/core/scripts/hysteria2/change_port.py b/core/scripts/hysteria2/change_port.py new file mode 100644 index 0000000..bdb037a --- /dev/null +++ b/core/scripts/hysteria2/change_port.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +import json +import sys +import re +import subprocess +from init_paths import * +from paths import * + +def update_port(port): + """ + Update the port in the configuration file and restart the service. + + Args: + port (str): The port number to set + + Returns: + bool: True if successful, False otherwise + """ + try: + if not re.match(r'^[0-9]+$', port) or int(port) < 1 or int(port) > 65535: + print("Invalid port number. Please enter a number between 1 and 65535.") + return False + + try: + with open(CONFIG_FILE, 'r') as f: + config = json.load(f) + except FileNotFoundError: + print(f"Error: Config file {CONFIG_FILE} not found.") + return False + + config['listen'] = f":{port}" + + with open(CONFIG_FILE, 'w') as f: + json.dump(config, f, indent=2) + + subprocess.run([CLI_PATH, "restart-hysteria2"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + + print(f"Port changed successfully to {port}.") + return True + + except Exception as e: + print(f"Error updating port: {str(e)}") + return False + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python update_port.py ") + sys.exit(1) + + success = update_port(sys.argv[1]) + sys.exit(0 if success else 1) \ No newline at end of file diff --git a/core/scripts/hysteria2/change_port.sh b/core/scripts/hysteria2/change_port.sh deleted file mode 100644 index e89da31..0000000 --- a/core/scripts/hysteria2/change_port.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -source /etc/hysteria/core/scripts/path.sh - -update_port() { - local port=$1 - - if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then - echo "Invalid port number. Please enter a number between 1 and 65535." - return 1 - fi - - if [ -f "$CONFIG_FILE" ]; then - jq --arg port "$port" '.listen = ":" + $port' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" - python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1 - echo "Port changed successfully to $port." - else - echo "Error: Config file $CONFIG_FILE not found." - return 1 - fi -} - -update_port "$1"