From 6f76b9717cd33b156fa16a11955c3b771c184cd9 Mon Sep 17 00:00:00 2001 From: Sarina Date: Sun, 21 Jul 2024 20:36:31 +0330 Subject: [PATCH] Fix change_port.sh --- core/scripts/hysteria2/change_port.sh | 30 ++++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/core/scripts/hysteria2/change_port.sh b/core/scripts/hysteria2/change_port.sh index 76bcb56..be7bd31 100644 --- a/core/scripts/hysteria2/change_port.sh +++ b/core/scripts/hysteria2/change_port.sh @@ -1,16 +1,22 @@ -while true; do - read -p "Enter the new port number you want to use: " port +# Function to update port number in configuration +update_port() { + local port=$1 + + # Validate the port number if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then echo "Invalid port number. Please enter a number between 1 and 65535." - else - break + return 1 fi -done -if [ -f "/etc/hysteria/config.json" ]; then - jq --arg port "$port" '.listen = ":" + $port' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json - python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1 - echo "Port changed successfully to $port." -else - echo "${red}Error:${NC} Config file /etc/hysteria/config.json not found." -fi \ No newline at end of file + # Check if the config file exists and update the port number + if [ -f "/etc/hysteria/config.json" ]; then + jq --arg port "$port" '.listen = ":" + $port' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json + python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1 + echo "Port changed successfully to $port." + else + echo "Error: Config file /etc/hysteria/config.json not found." + return 1 + fi +} + +update_port "$1" \ No newline at end of file