diff --git a/core/cli.py b/core/cli.py index b30251f..0110b47 100644 --- a/core/cli.py +++ b/core/cli.py @@ -22,6 +22,7 @@ class Command(Enum): 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_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') EDIT_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'edit_user.sh') @@ -103,6 +104,11 @@ def restart_hysteria2(): def change_hysteria2_port(port: int): run_cmd(['bash', Command.CHANGE_PORT_HYSTERIA2.value, str(port)]) +@cli.command('change-hysteria2-sni') +@click.option('--sni', '-s', required=True, help='New SNI for Hysteria2', type=str) +def change_hysteria2_sni(sni: str): + run_cmd(['bash', Command.CHANGE_SNI_HYSTERIA2.value, sni]) + @cli.command('get-user') @click.option('--username', '-u', required=True, help='Username for the user to get', type=str) def get_user(username: str): diff --git a/core/scripts/hysteria2/change_sni.sh b/core/scripts/hysteria2/change_sni.sh new file mode 100644 index 0000000..e4c6a9e --- /dev/null +++ b/core/scripts/hysteria2/change_sni.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# Source the necessary paths +source /etc/hysteria/core/scripts/path.sh + +update_sni() { + local sni=$1 + + if [ -z "$sni" ]; then + echo "Invalid SNI. Please provide a valid SNI." + return 1 + fi + + cd /etc/hysteria/ || exit + rm -f ca.key ca.crt + + echo "Generating CA key and certificate for SNI: $sni ..." + openssl ecparam -genkey -name prime256v1 -out ca.key >/dev/null 2>&1 + openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/CN=$sni" >/dev/null 2>&1 + chown hysteria:hysteria /etc/hysteria/ca.key /etc/hysteria/ca.crt + chmod 640 /etc/hysteria/ca.key /etc/hysteria/ca.crt + fingerprint=$(openssl x509 -noout -fingerprint -sha256 -inform pem -in ca.crt | sed 's/.*=//;s/://g') + + sha256=$(python3 - < "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" + echo "SHA-256 updated successfully in $CONFIG_FILE" + else + echo "Error: Config file $CONFIG_FILE not found." + return 1 + fi + + if [ -f "$CONFIG_ENV" ]; then + sed -i "s/^SNI=.*/SNI=$sni/" "$CONFIG_ENV" + echo "SNI updated successfully in .config.env" + else + echo "SNI=$sni" > "$CONFIG_ENV" + echo "Created .config.env with new SNI." + fi + + python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1 + echo "Hysteria2 restarted successfully with new SNI: $sni." +} + +update_sni "$1" diff --git a/core/scripts/path.sh b/core/scripts/path.sh index 6218d00..95c09fb 100644 --- a/core/scripts/path.sh +++ b/core/scripts/path.sh @@ -2,6 +2,7 @@ CLI_PATH="/etc/hysteria/core/cli.py" USERS_FILE="/etc/hysteria/users.json" TRAFFIC_FILE="/etc/hysteria/traffic_data.json" CONFIG_FILE="/etc/hysteria/config.json" +CONFIG_ENV="/etc/hysteria/.config.env" TELEGRAM_ENV="/etc/hysteria/core/scripts/telegrambot/.env" SINGBOX_ENV="/etc/hysteria/core/scripts/singbox/.env" ONLINE_API_URL="http://127.0.0.1:25413/online" diff --git a/menu.sh b/menu.sh index 8e6d25a..6df4af7 100644 --- a/menu.sh +++ b/menu.sh @@ -235,6 +235,21 @@ hysteria2_change_port_handler() { python3 $CLI_PATH change-hysteria2-port --port "$port" } +hysteria2_change_sni_handler() { + while true; do + read -p "Enter the new SNI (e.g., example.com): " sni + + if [[ "$sni" =~ ^[a-zA-Z0-9.]+$ ]]; then + break + else + echo -e "${red}Error:${NC} SNI can only contain letters, numbers, and dots." + fi + done + + python3 $CLI_PATH change-hysteria2-sni --sni "$sni" +} + + hysteria_upgrade(){ bash <(curl https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/upgrade.sh) } @@ -505,8 +520,9 @@ display_advance_menu() { echo -e "${green}[5] ${NC}↝ Telegram Bot" echo -e "${green}[6] ${NC}↝ SingBox SubLink" echo -e "${cyan}[7] ${NC}↝ Change Port Hysteria2" - echo -e "${cyan}[8] ${NC}↝ Update Core Hysteria2" - echo -e "${red}[9] ${NC}↝ Uninstall Hysteria2" + echo -e "${cyan}[8] ${NC}↝ Change SNI Hysteria2" + echo -e "${cyan}[9] ${NC}↝ Update Core Hysteria2" + echo -e "${red}[10] ${NC}↝ Uninstall Hysteria2" echo -e "${red}[0] ${NC}↝ Back to Main Menu" echo -e "${LPurple}◇──────────────────────────────────────────────────────────────────────◇${NC}" echo -ne "${yellow}➜ Enter your option: ${NC}" @@ -527,8 +543,9 @@ advance_menu() { 5) telegram_bot_handler ;; 6) singbox_handler ;; 7) hysteria2_change_port_handler ;; - 8) python3 $CLI_PATH update-hysteria2 ;; - 9) python3 $CLI_PATH uninstall-hysteria2 ;; + 8) hysteria2_change_sni_handler ;; + 9) python3 $CLI_PATH update-hysteria2 ;; + 10) python3 $CLI_PATH uninstall-hysteria2 ;; 0) return ;; *) echo "Invalid option. Please try again." ;; esac