|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to install and configure Hysteria2 |
| 4 | +install_and_configure() { |
| 5 | + |
| 6 | + bash <(curl https://raw.githubusercontent.com/H-Return/Hysteria2/main/install.sh) |
| 7 | + |
| 8 | +} |
| 9 | + |
| 10 | +# Function to change port |
| 11 | +change_port() { |
| 12 | + read -p "Enter the new port number you want to use: " port |
| 13 | + # Check if the config.yaml file exists |
| 14 | + if [ -f "/etc/hysteria/config.yaml" ]; then |
| 15 | + # Update the port in the config.yaml file |
| 16 | + sed -i "s/listen: :[0-9]*/listen: :$port/" /etc/hysteria/config.yaml |
| 17 | + # Restart the Hysteria2 service |
| 18 | + systemctl restart hysteria-server.service >/dev/null 2>&1 |
| 19 | + echo "Port changed successfully to $port" |
| 20 | + else |
| 21 | + echo "Error: Config file /etc/hysteria/config.yaml not found." |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +# Function to show URI if Hysteria2 is installed and active |
| 26 | +show_uri() { |
| 27 | + # Check if the config.yaml file exists |
| 28 | + if [ -f "/etc/hysteria/config.yaml" ]; then |
| 29 | + # Retrieve values from config.yaml |
| 30 | + port=$(grep -oP '(?<=listen: :)\d+' /etc/hysteria/config.yaml) |
| 31 | + sha256=$(grep -oP '(?<=pinSHA256: ").*(?=")' /etc/hysteria/config.yaml) |
| 32 | + obfspassword=$(grep -oP '(?<=password: ).*' /etc/hysteria/config.yaml | head -1) |
| 33 | + authpassword=$(grep -oP '(?<=password: ).*' /etc/hysteria/config.yaml | tail -1) |
| 34 | + |
| 35 | + if systemctl is-active --quiet hysteria-server.service; then |
| 36 | + # Generate URI Scheme |
| 37 | + echo "Generating URI Scheme..." |
| 38 | + IP=$(curl -4 ip.sb) |
| 39 | + URI="hy2://$authpassword@$IP:$port?obfs=salamander&obfs-password=$obfspassword&pinSHA256=$sha256&insecure=1&sni=bing.com#Hysteria2" |
| 40 | + |
| 41 | + # Generate and display QR Code in the center of the terminal |
| 42 | + cols=$(tput cols) |
| 43 | + rows=$(tput lines) |
| 44 | + qr=$(echo -n "$URI" | qrencode -t UTF8 -s 3 -m 2) |
| 45 | + |
| 46 | + echo -e "\n\n\n" |
| 47 | + echo "$qr" | while IFS= read -r line; do |
| 48 | + printf "%*s\n" $(( (${#line} + cols) / 2)) "$line" |
| 49 | + done |
| 50 | + echo -e "\n" |
| 51 | + |
| 52 | + # Output the URI scheme |
| 53 | + echo "$URI" |
| 54 | + else |
| 55 | + echo "Error: Hysteria2 is not active." |
| 56 | + fi |
| 57 | + else |
| 58 | + echo "Error: Config file /etc/hysteria/config.yaml not found." |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +# Function to Check Traffic Status |
| 63 | +traffic_status() { |
| 64 | + |
| 65 | + green='\033[0;32m' |
| 66 | + cyan='\033[0;36m' |
| 67 | + NC='\033[0m' |
| 68 | + |
| 69 | + # Extract secret from config.yaml |
| 70 | + secret=$(grep -Po '(?<=secret: ).*' /etc/hysteria/config.yaml | awk '{$1=$1};1') |
| 71 | + |
| 72 | + # If secret is empty, exit with error |
| 73 | + if [ -z "$secret" ]; then |
| 74 | + echo "Error: Secret not found in config.yaml" |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + |
| 78 | + response=$(curl -s -H "Authorization: $secret" http://127.0.0.1:25413/traffic) |
| 79 | + if [ -z "$response" ] || [ "$response" = "{}" ]; then |
| 80 | + echo -e "Upload (TX): ${green}0B${NC}" |
| 81 | + echo -e "Download (RX): ${cyan}0B${NC}" |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | + |
| 85 | + tx_bytes=$(echo "$response" | jq -r '.user.tx // 0') |
| 86 | + rx_bytes=$(echo "$response" | jq -r '.user.rx // 0') |
| 87 | + |
| 88 | + format_bytes() { |
| 89 | + bytes=$1 |
| 90 | + |
| 91 | + if [ "$bytes" -lt 1024 ]; then |
| 92 | + echo "${bytes}B" |
| 93 | + elif [ "$bytes" -lt 1048576 ]; then |
| 94 | + echo "$(bc <<< "scale=2; $bytes / 1024")KB" |
| 95 | + elif [ "$bytes" -lt 1073741824 ]; then |
| 96 | + echo "$(bc <<< "scale=2; $bytes / 1048576")MB" |
| 97 | + elif [ "$bytes" -lt 1099511627776 ]; then |
| 98 | + echo "$(bc <<< "scale=2; $bytes / 1073741824")GB" |
| 99 | + else |
| 100 | + echo "$(bc <<< "scale=2; $bytes / 1099511627776")TB" |
| 101 | + fi |
| 102 | + } |
| 103 | + |
| 104 | + echo -e "Upload (TX): ${green}$(format_bytes "$tx_bytes")${NC}" |
| 105 | + echo -e "Download (RX): ${cyan}$(format_bytes "$rx_bytes")${NC}" |
| 106 | + |
| 107 | +} |
| 108 | +# Main menu |
| 109 | +main_menu() { |
| 110 | + echo "===== Hysteria2 Setup Menu =====" |
| 111 | + echo "1. Install and Configure" |
| 112 | + echo "2. Change Port" |
| 113 | + echo "3. Show URI" |
| 114 | + echo "4. Check Traffic Status" |
| 115 | + echo "5. Exit" |
| 116 | + |
| 117 | + read -p "Enter your choice: " choice |
| 118 | + case $choice in |
| 119 | + 1) install_and_configure ;; |
| 120 | + 2) change_port ;; |
| 121 | + 3) show_uri ;; |
| 122 | + 4) traffic_status ;; |
| 123 | + 5) exit ;; |
| 124 | + *) echo "Invalid option. Please try again." ;; |
| 125 | + esac |
| 126 | +} |
| 127 | + |
| 128 | +# Loop to display the menu repeatedly |
| 129 | +while true; do |
| 130 | + main_menu |
| 131 | +done |
0 commit comments