Merge pull request #49 from ReturnFI/Dev

Added ip-address CLI
This commit is contained in:
Whispering Wind
2024-12-08 00:05:52 +03:30
committed by GitHub
8 changed files with 191 additions and 19 deletions

View File

@ -1 +1 @@
0.2.9 0.3.0

View File

@ -29,6 +29,7 @@ class Command(Enum):
RESET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'reset_user.sh') RESET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'reset_user.sh')
REMOVE_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'remove_user.sh') REMOVE_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'remove_user.sh')
SHOW_USER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'show_user_uri.sh') SHOW_USER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'show_user_uri.sh')
IP_ADD = os.path.join(SCRIPT_DIR, 'hysteria2', 'ip.sh')
MANAGE_OBFS = os.path.join(SCRIPT_DIR, 'hysteria2', 'manage_obfs.sh') MANAGE_OBFS = os.path.join(SCRIPT_DIR, 'hysteria2', 'manage_obfs.sh')
TRAFFIC_STATUS = 'traffic.py' # won't be call directly (it's a python module) TRAFFIC_STATUS = 'traffic.py' # won't be call directly (it's a python module)
LIST_USERS = os.path.join(SCRIPT_DIR, 'hysteria2', 'list_users.sh') LIST_USERS = os.path.join(SCRIPT_DIR, 'hysteria2', 'list_users.sh')
@ -266,6 +267,26 @@ def manage_obfs(remove, generate):
else: else:
click.echo("Error: Please specify either --remove or --generate.") click.echo("Error: Please specify either --remove or --generate.")
@cli.command('ip-address')
@click.option('--edit', is_flag=True, help="Edit IP addresses manually.")
@click.option('-4', '--ipv4', type=str, help="Specify the new IPv4 address.")
@click.option('-6', '--ipv6', type=str, help="Specify the new IPv6 address.")
def ip_address(edit, ipv4, ipv6):
"""
Manage IP addresses in .configs.env.
- Use without options to add auto-detected IPs.
- Use --edit with -4 or -6 to manually update IPs.
"""
if edit:
if ipv4:
run_cmd(['bash', Command.IP_ADD.value, 'edit', '-4', ipv4])
if ipv6:
run_cmd(['bash', Command.IP_ADD.value, 'edit', '-6', ipv6])
if not ipv4 and not ipv6:
click.echo("Error: --edit requires at least one of --ipv4 or --ipv6.")
else:
run_cmd(['bash', Command.IP_ADD.value, 'add'])
# endregion # endregion
# region advanced menu # region advanced menu

View File

@ -104,6 +104,7 @@ EOF
(crontab -l ; echo "0 3 */3 * * /bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && python3 /etc/hysteria/core/cli.py restart-hysteria2' >/dev/null 2>&1") | crontab - (crontab -l ; echo "0 3 */3 * * /bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && python3 /etc/hysteria/core/cli.py restart-hysteria2' >/dev/null 2>&1") | crontab -
(crontab -l ; echo "0 */6 * * * /bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && python3 /etc/hysteria/core/cli.py backup-hysteria' >/dev/null 2>&1") | crontab - (crontab -l ; echo "0 */6 * * * /bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && python3 /etc/hysteria/core/cli.py backup-hysteria' >/dev/null 2>&1") | crontab -
(crontab -l ; echo "*/1 * * * * /etc/hysteria/core/scripts/hysteria2/kick.sh >/dev/null 2>&1") | crontab - (crontab -l ; echo "*/1 * * * * /etc/hysteria/core/scripts/hysteria2/kick.sh >/dev/null 2>&1") | crontab -
} }
if systemctl is-active --quiet hysteria-server.service; then if systemctl is-active --quiet hysteria-server.service; then

View File

@ -0,0 +1,69 @@
#!/bin/bash
source /etc/hysteria/core/scripts/path.sh
# ensure_config_env() {
# if [ ! -f "$CONFIG_ENV" ]; then
# echo ".configs.env not found. Creating it with default SNI=bts.com."
# echo "SNI=bts.com" > "$CONFIG_ENV"
# fi
# }
add_ips() {
ipv4_address=""
ipv6_address=""
interfaces=$(ip -o link show | awk -F': ' '{print $2}' | grep -vE '^(lo|wgcf|warp)$')
for interface in $interfaces; do
if ip addr show "$interface" > /dev/null 2>&1; then
ipv4=$(ip -o -4 addr show "$interface" | awk '{print $4}' | grep -vE '^(127\.|10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[0-1]))' | head -n 1 | cut -d/ -f1)
if [[ -z $ipv4_address && -n $ipv4 ]]; then
ipv4_address=$ipv4
fi
ipv6=$(ip -o -6 addr show "$interface" | awk '{print $4}' | grep -vE '^(::1|fe80:)' | head -n 1 | cut -d/ -f1)
if [[ -z $ipv6_address && -n $ipv6 ]]; then
ipv6_address=$ipv6
fi
fi
done
sed -i '/^IP4=/d' "$CONFIG_ENV" 2>/dev/null
sed -i '/^IP6=/d' "$CONFIG_ENV" 2>/dev/null
echo -e "\nIP4=${ipv4_address:-}" >> "$CONFIG_ENV"
echo "IP6=${ipv6_address:-}" >> "$CONFIG_ENV"
# echo "IPs have been added to $CONFIG_ENV:"
# echo "IP4=${ipv4_address:-Not Found}"
# echo "IP6=${ipv6_address:-Not Found}"
}
edit_ip() {
local type=$1
local new_ip=$2
if [[ $type == "-4" ]]; then
sed -i '/^IP4=/d' "$CONFIG_ENV" 2>/dev/null
echo "IP4=$new_ip" >> "$CONFIG_ENV"
echo "IP4 has been updated to $new_ip."
elif [[ $type == "-6" ]]; then
sed -i '/^IP6=/d' "$CONFIG_ENV" 2>/dev/null
echo "IP6=$new_ip" >> "$CONFIG_ENV"
echo "IP6 has been updated to $new_ip."
else
echo "Invalid option. Use -4 for IPv4 or -6 for IPv6."
fi
}
# ensure_config_env
case "$1" in
add)
add_ips
;;
edit)
edit_ip "$2" "$3"
;;
*)
echo "Usage: $0 {add|edit -4|-6 <new_ip>}"
exit 1
;;
esac

View File

@ -36,7 +36,8 @@ show_uri() {
local generate_normalsub=false local generate_normalsub=false
load_hysteria2_env load_hysteria2_env
load_hysteria2_ips
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
case $1 in case $1 in
-u|--username) username="$2"; shift ;; -u|--username) username="$2"; shift ;;
@ -80,19 +81,15 @@ show_uri() {
} }
if [ "$show_all" = true ]; then if [ "$show_all" = true ]; then
IP=$(curl -s -4 ip.gs) URI=$(generate_uri 4 "$IP4")
URI=$(generate_uri 4 "$IP")
IP6=$(curl -s -6 ip.gs)
URI6=$(generate_uri 6 "$IP6") URI6=$(generate_uri 6 "$IP6")
echo -e "\nIPv4:\n$URI\n" echo -e "\nIPv4:\n$URI\n"
echo -e "\nIPv6:\n$URI6\n" echo -e "\nIPv6:\n$URI6\n"
else else
if [ "$ip_version" -eq 4 ]; then if [ "$ip_version" -eq 4 ]; then
IP=$(curl -s -4 ip.gs) URI=$(generate_uri 4 "$IP4")
URI=$(generate_uri 4 "$IP")
echo -e "\nIPv4:\n$URI\n" echo -e "\nIPv4:\n$URI\n"
elif [ "$ip_version" -eq 6 ]; then elif [ "$ip_version" -eq 6 ]; then
IP6=$(curl -s -6 ip.gs)
URI6=$(generate_uri 6 "$IP6") URI6=$(generate_uri 6 "$IP6")
echo -e "\nIPv6:\n$URI6\n" echo -e "\nIPv6:\n$URI6\n"
else else

View File

@ -1,3 +1,4 @@
source /etc/hysteria/core/scripts/path.sh
# Function to define colors # Function to define colors
define_colors() { define_colors() {
@ -54,14 +55,31 @@ check_version() {
load_hysteria2_env() { load_hysteria2_env() {
if [ -f /etc/hysteria/.configs.env ]; then if [ -f "$CONFIG_ENV" ]; then
export $(grep -v '^#' /etc/hysteria/.configs.env | xargs) export $(grep -v '^#' "$CONFIG_ENV" | xargs)
else else
echo "Error: configs.env file not found. Using default SNI 'bts.com'." echo "Error: configs.env file not found. Using default SNI 'bts.com'."
SNI="bts.com" SNI="bts.com"
fi fi
} }
load_hysteria2_ips() {
if [ -f "$CONFIG_ENV" ]; then
export $(grep -v '^#' "$CONFIG_ENV" | xargs)
if [[ -z "$IP4" || -z "$IP6" ]]; then
echo "Warning: IP4 or IP6 is not set in configs.env. Fetching from ip.gs..."
IP4=$(curl -s -4 ip.gs)
IP6=$(curl -s -6 ip.gs)
fi
else
echo "Error: configs.env file not found. Fetching IPs from ip.gs..."
IP4=$(curl -s -4 ip.gs)
IP6=$(curl -s -6 ip.gs)
fi
}
check_services() { check_services() {
declare -A service_names=( declare -A service_names=(
["hysteria-server.service"]="Hysteria2" ["hysteria-server.service"]="Hysteria2"

62
menu.sh
View File

@ -29,6 +29,7 @@ hysteria2_install_handler() {
cat <<EOF > /etc/hysteria/.configs.env cat <<EOF > /etc/hysteria/.configs.env
SNI=$sni SNI=$sni
EOF EOF
python3 $CLI_PATH ip-address
} }
hysteria2_add_user_handler() { hysteria2_add_user_handler() {
@ -253,6 +254,53 @@ hysteria2_change_sni_handler() {
fi fi
} }
edit_ips() {
while true; do
echo "======================================"
echo " IP Address Manager "
echo "======================================"
echo "1. Change IP4"
echo "2. Change IP6"
echo "0. Back"
echo "======================================"
read -p "Enter your choice [1-3]: " choice
case $choice in
1)
read -p "Enter the new IPv4 address: " new_ip4
if [[ $new_ip4 =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
if [[ $(echo "$new_ip4" | awk -F. '{for (i=1;i<=NF;i++) if ($i>255) exit 1}') ]]; then
echo "Error: Invalid IPv4 address. Values must be between 0 and 255."
else
python3 "$CLI_PATH" ip-address --edit -4 "$new_ip4"
fi
else
echo "Error: Invalid IPv4 address format."
fi
break
;;
2)
read -p "Enter the new IPv6 address: " new_ip6
if [[ $new_ip6 =~ ^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^(([0-9a-fA-F]{1,4}:){1,7}:)$|^(::([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})$ ]]; then
python3 "$CLI_PATH" ip-address --edit -6 "$new_ip6"
echo "IPv6 address has been updated to $new_ip6."
else
echo "Error: Invalid IPv6 address format."
fi
break
;;
0)
break
;;
*)
echo "Invalid option. Please try again."
break
;;
esac
echo "======================================"
read -p "Press Enter to continue..."
done
}
hysteria_upgrade(){ hysteria_upgrade(){
bash <(curl https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/upgrade.sh) bash <(curl https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/upgrade.sh)
@ -606,9 +654,10 @@ display_advance_menu() {
echo -e "${cyan}[8] ${NC}↝ Change Port Hysteria2" echo -e "${cyan}[8] ${NC}↝ Change Port Hysteria2"
echo -e "${cyan}[9] ${NC}↝ Change SNI Hysteria2" echo -e "${cyan}[9] ${NC}↝ Change SNI Hysteria2"
echo -e "${cyan}[10] ${NC}↝ Manage OBFS" echo -e "${cyan}[10] ${NC}↝ Manage OBFS"
echo -e "${cyan}[11] ${NC}Restart Hysteria2" echo -e "${cyan}[11] ${NC}Change IPs(4-6)"
echo -e "${cyan}[12] ${NC}Update Core Hysteria2" echo -e "${cyan}[12] ${NC}Restart Hysteria2"
echo -e "${red}[13] ${NC}↝ Uninstall Hysteria2" echo -e "${cyan}[13] ${NC}↝ Update Core Hysteria2"
echo -e "${red}[14] ${NC}↝ Uninstall Hysteria2"
echo -e "${red}[0] ${NC}↝ Back to Main Menu" echo -e "${red}[0] ${NC}↝ Back to Main Menu"
echo -e "${LPurple}◇──────────────────────────────────────────────────────────────────────◇${NC}" echo -e "${LPurple}◇──────────────────────────────────────────────────────────────────────◇${NC}"
echo -ne "${yellow}➜ Enter your option: ${NC}" echo -ne "${yellow}➜ Enter your option: ${NC}"
@ -632,9 +681,10 @@ advance_menu() {
8) hysteria2_change_port_handler ;; 8) hysteria2_change_port_handler ;;
9) hysteria2_change_sni_handler ;; 9) hysteria2_change_sni_handler ;;
10) obfs_handler ;; 10) obfs_handler ;;
11) python3 $CLI_PATH restart-hysteria2 ;; 11) edit_ips ;;
12) python3 $CLI_PATH update-hysteria2 ;; 12) python3 $CLI_PATH restart-hysteria2 ;;
13) python3 $CLI_PATH uninstall-hysteria2 ;; 13) python3 $CLI_PATH update-hysteria2 ;;
14) python3 $CLI_PATH uninstall-hysteria2 ;;
0) return ;; 0) return ;;
*) echo "Invalid option. Please try again." ;; *) echo "Invalid option. Please try again." ;;
esac esac

View File

@ -8,6 +8,7 @@ FILES=(
"/etc/hysteria/ca.crt" "/etc/hysteria/ca.crt"
"/etc/hysteria/users.json" "/etc/hysteria/users.json"
"/etc/hysteria/config.json" "/etc/hysteria/config.json"
"/etc/hysteria/.configs.env"
"/etc/hysteria/core/scripts/telegrambot/.env" "/etc/hysteria/core/scripts/telegrambot/.env"
"/etc/hysteria/core/scripts/singbox/.env" "/etc/hysteria/core/scripts/singbox/.env"
"/etc/hysteria/core/scripts/normalsub/.env" "/etc/hysteria/core/scripts/normalsub/.env"
@ -34,13 +35,28 @@ for FILE in "${FILES[@]}"; do
cp "$TEMP_DIR/$FILE" "$FILE" cp "$TEMP_DIR/$FILE" "$FILE"
done done
if [ ! -f /etc/hysteria/.configs.env ]; then CONFIG_ENV="/etc/hysteria/.configs.env"
echo ".configs.env not found, creating it with default SNI=bts.com" if [ ! -f "$CONFIG_ENV" ]; then
echo "SNI=bts.com" > /etc/hysteria/.configs.env echo ".configs.env not found, creating it with default SNI=bts.com and IPs."
echo "SNI=bts.com" > "$CONFIG_ENV"
else else
echo ".configs.env already exists." echo ".configs.env already exists."
fi fi
export $(grep -v '^#' "$CONFIG_ENV" | xargs 2>/dev/null)
if [[ -z "$IP4" ]]; then
echo "IP4 not found, fetching from ip.gs..."
IP4=$(curl -s -4 ip.gs || echo "")
echo "IP4=${IP4:-}" >> "$CONFIG_ENV"
fi
if [[ -z "$IP6" ]]; then
echo "IP6 not found, fetching from ip.gs..."
IP6=$(curl -s -6 ip.gs || echo "")
echo "IP6=${IP6:-}" >> "$CONFIG_ENV"
fi
echo "Setting ownership and permissions" echo "Setting ownership and permissions"
chown hysteria:hysteria /etc/hysteria/ca.key /etc/hysteria/ca.crt chown hysteria:hysteria /etc/hysteria/ca.key /etc/hysteria/ca.crt
chmod 640 /etc/hysteria/ca.key /etc/hysteria/ca.crt chmod 640 /etc/hysteria/ca.key /etc/hysteria/ca.crt