feat(cli): Add unlimited IP options to menu script

This commit is contained in:
Whispering Wind
2025-08-13 23:19:46 +03:30
committed by GitHub
parent be5fe69f0b
commit 75d417084f

58
menu.sh
View File

@ -52,7 +52,7 @@ hysteria2_add_user_handler() {
read -p "Enter the username: " username read -p "Enter the username: " username
if [[ "$username" =~ ^[a-zA-Z0-9]+$ ]]; then if [[ "$username" =~ ^[a-zA-Z0-9]+$ ]]; then
if [[ -n $(python3 $CLI_PATH get-user -u "$username") ]]; then if [[ -n $(python3 $CLI_PATH get-user -u "$username" 2>/dev/null) ]]; then
echo -e "${red}Error:${NC} Username already exists. Please choose another username." echo -e "${red}Error:${NC} Username already exists. Please choose another username."
else else
break break
@ -65,14 +65,24 @@ hysteria2_add_user_handler() {
read -p "Enter the traffic limit (in GB): " traffic_limit_GB read -p "Enter the traffic limit (in GB): " traffic_limit_GB
read -p "Enter the expiration days: " expiration_days read -p "Enter the expiration days: " expiration_days
local unlimited_arg=""
while true; do
read -p "Exempt user from IP limit checks (unlimited IP)? (y/n) [n]: " unlimited_choice
case "$unlimited_choice" in
y|Y) unlimited_arg="--unlimited"; break ;;
n|N|"") break ;;
*) echo -e "${red}Error:${NC} Please answer 'y' or 'n'." ;;
esac
done
password=$(pwgen -s 32 1) password=$(pwgen -s 32 1)
creation_date=$(date +%Y-%m-%d) creation_date=$(date +%Y-%m-%d)
python3 $CLI_PATH add-user --username "$username" --traffic-limit "$traffic_limit_GB" --expiration-days "$expiration_days" --password "$password" --creation-date "$creation_date" python3 $CLI_PATH add-user --username "$username" --traffic-limit "$traffic_limit_GB" --expiration-days "$expiration_days" --password "$password" --creation-date "$creation_date" $unlimited_arg
} }
hysteria2_edit_user_handler() { hysteria2_edit_user_handler() {
# Function to prompt for user input with validation
prompt_for_input() { prompt_for_input() {
local prompt_message="$1" local prompt_message="$1"
local validation_regex="$2" local validation_regex="$2"
@ -93,65 +103,69 @@ hysteria2_edit_user_handler() {
done done
} }
# Prompt for username
prompt_for_input "Enter the username you want to edit: " '^[a-zA-Z0-9]+$' '' username prompt_for_input "Enter the username you want to edit: " '^[a-zA-Z0-9]+$' '' username
# Check if user exists
user_exists_output=$(python3 $CLI_PATH get-user -u "$username" 2>&1) user_exists_output=$(python3 $CLI_PATH get-user -u "$username" 2>&1)
if [[ -z "$user_exists_output" ]]; then if [[ -z "$user_exists_output" ]]; then
echo -e "${red}Error:${NC} User '$username' not found or an error occurred." echo -e "${red}Error:${NC} User '$username' not found or an error occurred."
return 1 return 1
fi fi
# Prompt for new username
prompt_for_input "Enter the new username (leave empty to keep the current username): " '^[a-zA-Z0-9]*$' '' new_username prompt_for_input "Enter the new username (leave empty to keep the current username): " '^[a-zA-Z0-9]*$' '' new_username
# Prompt for new traffic limit
prompt_for_input "Enter the new traffic limit (in GB) (leave empty to keep the current limit): " '^[0-9]*$' '' new_traffic_limit_GB prompt_for_input "Enter the new traffic limit (in GB) (leave empty to keep the current limit): " '^[0-9]*$' '' new_traffic_limit_GB
# Prompt for new expiration days
prompt_for_input "Enter the new expiration days (leave empty to keep the current expiration days): " '^[0-9]*$' '' new_expiration_days prompt_for_input "Enter the new expiration days (leave empty to keep the current expiration days): " '^[0-9]*$' '' new_expiration_days
# Determine if we need to renew password
while true; do while true; do
read -p "Do you want to generate a new password? (y/n): " renew_password read -p "Do you want to generate a new password? (y/n) [n]: " renew_password
case "$renew_password" in case "$renew_password" in
y|Y) renew_password=true; break ;; y|Y) renew_password=true; break ;;
n|N) renew_password=false; break ;; n|N|"") renew_password=false; break ;;
*) echo -e "${red}Error:${NC} Please answer 'y' or 'n'." ;; *) echo -e "${red}Error:${NC} Please answer 'y' or 'n'." ;;
esac esac
done done
# Determine if we need to renew creation date
while true; do while true; do
read -p "Do you want to generate a new creation date? (y/n): " renew_creation_date read -p "Do you want to generate a new creation date? (y/n) [n]: " renew_creation_date
case "$renew_creation_date" in case "$renew_creation_date" in
y|Y) renew_creation_date=true; break ;; y|Y) renew_creation_date=true; break ;;
n|N) renew_creation_date=false; break ;; n|N|"") renew_creation_date=false; break ;;
*) echo -e "${red}Error:${NC} Please answer 'y' or 'n'." ;; *) echo -e "${red}Error:${NC} Please answer 'y' or 'n'." ;;
esac esac
done done
# Determine if user should be blocked local blocked_arg=""
while true; do while true; do
read -p "Do you want to block the user? (y/n): " block_user read -p "Change user block status? ([b]lock/[u]nblock/[s]kip) [s]: " block_user
case "$block_user" in case "$block_user" in
y|Y) blocked=true; break ;; b|B) blocked_arg="--blocked"; break ;;
n|N) blocked=false; break ;; u|U) blocked_arg="--unblocked"; break ;;
*) echo -e "${red}Error:${NC} Please answer 'y' or 'n'." ;; s|S|"") break ;;
*) echo -e "${red}Error:${NC} Please answer 'b', 'u', or 's'." ;;
esac
done
local ip_limit_arg=""
while true; do
read -p "Change IP limit status? ([u]nlimited/[l]imited/[s]kip) [s]: " ip_limit_status
case "$ip_limit_status" in
u|U) ip_limit_arg="--unlimited-ip"; break ;;
l|L) ip_limit_arg="--limited-ip"; break ;;
s|S|"") break ;;
*) echo -e "${red}Error:${NC} Please answer 'u', 'l', or 's'." ;;
esac esac
done done
# Construct the arguments for the edit-user command
args=() args=()
if [[ -n "$new_username" ]]; then args+=("--new-username" "$new_username"); fi if [[ -n "$new_username" ]]; then args+=("--new-username" "$new_username"); fi
if [[ -n "$new_traffic_limit_GB" ]]; then args+=("--new-traffic-limit" "$new_traffic_limit_GB"); fi if [[ -n "$new_traffic_limit_GB" ]]; then args+=("--new-traffic-limit" "$new_traffic_limit_GB"); fi
if [[ -n "$new_expiration_days" ]]; then args+=("--new-expiration-days" "$new_expiration_days"); fi if [[ -n "$new_expiration_days" ]]; then args+=("--new-expiration-days" "$new_expiration_days"); fi
if [[ "$renew_password" == "true" ]]; then args+=("--renew-password"); fi if [[ "$renew_password" == "true" ]]; then args+=("--renew-password"); fi
if [[ "$renew_creation_date" == "true" ]]; then args+=("--renew-creation-date"); fi if [[ "$renew_creation_date" == "true" ]]; then args+=("--renew-creation-date"); fi
if [[ "$blocked" == "true" ]]; then args+=("--blocked"); fi if [[ -n "$blocked_arg" ]]; then args+=("$blocked_arg"); fi
if [[ -n "$ip_limit_arg" ]]; then args+=("$ip_limit_arg"); fi
# Call the edit-user script with the constructed arguments
python3 $CLI_PATH edit-user --username "$username" "${args[@]}" python3 $CLI_PATH edit-user --username "$username" "${args[@]}"
} }