fix(user-handlers): allow underscores in usernames and improve error handling
This commit is contained in:
58
menu.sh
58
menu.sh
@ -170,7 +170,7 @@ hysteria2_remove_user_handler() {
|
|||||||
while true; do
|
while true; do
|
||||||
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
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
||||||
@ -182,7 +182,7 @@ hysteria2_remove_user_handler() {
|
|||||||
hysteria2_get_user_handler() {
|
hysteria2_get_user_handler() {
|
||||||
while true; do
|
while true; do
|
||||||
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
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
||||||
@ -190,12 +190,18 @@ hysteria2_get_user_handler() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
user_data=$(python3 "$CLI_PATH" get-user --username "$username" 2>/dev/null)
|
user_data=$(python3 "$CLI_PATH" get-user --username "$username" 2>/dev/null)
|
||||||
|
local exit_code=$?
|
||||||
|
|
||||||
if [[ $exit_code -ne 0 || -z "$user_data" ]]; then
|
if [[ $exit_code -ne 0 || -z "$user_data" ]]; then
|
||||||
echo -e "${red}Error:${NC} User '$username' not found or invalid response."
|
echo -e "${red}Error:${NC} User '$username' not found or invalid response."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if ! echo "$user_data" | jq -e . > /dev/null 2>&1; then
|
||||||
|
echo -e "${red}Error:${NC} Received invalid data for user '$username'."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
password=$(echo "$user_data" | jq -r '.password // "N/A"')
|
password=$(echo "$user_data" | jq -r '.password // "N/A"')
|
||||||
max_download_bytes=$(echo "$user_data" | jq -r '.max_download_bytes // 0')
|
max_download_bytes=$(echo "$user_data" | jq -r '.max_download_bytes // 0')
|
||||||
upload_bytes=$(echo "$user_data" | jq -r '.upload_bytes // 0')
|
upload_bytes=$(echo "$user_data" | jq -r '.upload_bytes // 0')
|
||||||
@ -209,12 +215,23 @@ hysteria2_get_user_handler() {
|
|||||||
upload_gb=$(echo "scale=2; $upload_bytes / 1073741824" | bc)
|
upload_gb=$(echo "scale=2; $upload_bytes / 1073741824" | bc)
|
||||||
download_gb=$(echo "scale=2; $download_bytes / 1073741824" | bc)
|
download_gb=$(echo "scale=2; $download_bytes / 1073741824" | bc)
|
||||||
total_usage_gb=$(echo "scale=2; $total_usage / 1073741824" | bc)
|
total_usage_gb=$(echo "scale=2; $total_usage / 1073741824" | bc)
|
||||||
expiration_date=$(date -d "$account_creation_date + $expiration_days days" +"%Y-%m-%d")
|
|
||||||
current_date=$(date +"%Y-%m-%d")
|
|
||||||
used_days=$(( ( $(date -d "$current_date" +%s) - $(date -d "$account_creation_date" +%s) ) / 86400 ))
|
|
||||||
|
|
||||||
if [[ $used_days -gt $expiration_days ]]; then
|
local expiration_date_str="N/A"
|
||||||
used_days=$expiration_days
|
local used_days_str="N/A"
|
||||||
|
|
||||||
|
if [[ "$account_creation_date" != "N/A" ]]; then
|
||||||
|
expiration_date_str=$(date -d "$account_creation_date + $expiration_days days" +"%Y-%m-%d")
|
||||||
|
current_date=$(date +"%Y-%m-%d")
|
||||||
|
used_days=$(( ( $(date -d "$current_date" +%s) - $(date -d "$account_creation_date" +%s) ) / 86400 ))
|
||||||
|
|
||||||
|
if [[ $used_days -lt 0 ]]; then
|
||||||
|
used_days=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $used_days -gt $expiration_days ]]; then
|
||||||
|
used_days=$expiration_days
|
||||||
|
fi
|
||||||
|
used_days_str=$used_days
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "${green}User Details:${NC}"
|
echo -e "${green}User Details:${NC}"
|
||||||
@ -222,27 +239,33 @@ hysteria2_get_user_handler() {
|
|||||||
echo -e "Password: $password"
|
echo -e "Password: $password"
|
||||||
echo -e "Total Traffic: $max_download_gb GB"
|
echo -e "Total Traffic: $max_download_gb GB"
|
||||||
echo -e "Total Usage: $total_usage_gb GB"
|
echo -e "Total Usage: $total_usage_gb GB"
|
||||||
echo -e "Time Expiration: $expiration_date ($used_days/$expiration_days Days)"
|
echo -e "Time Expiration: $expiration_date_str ($used_days_str/$expiration_days Days)"
|
||||||
echo -e "Blocked: $blocked"
|
echo -e "Blocked: $blocked"
|
||||||
echo -e "Status: $status"
|
echo -e "Status: $status"
|
||||||
}
|
}
|
||||||
|
|
||||||
hysteria2_list_users_handler() {
|
hysteria2_list_users_handler() {
|
||||||
users_json=$(python3 $CLI_PATH list-users 2>/dev/null)
|
users_json=$(python3 $CLI_PATH list-users 2>/dev/null)
|
||||||
|
local exit_code=$?
|
||||||
|
|
||||||
if [ $? -ne 0 ] || [ -z "$users_json" ]; then
|
if [ $exit_code -ne 0 ] || [ -z "$users_json" ]; then
|
||||||
echo -e "${red}Error:${NC} Failed to list users."
|
echo -e "${red}Error:${NC} Failed to list users."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if ! echo "$users_json" | jq -e . > /dev/null 2>&1; then
|
||||||
|
echo -e "${red}Error:${NC} Received invalid data while listing users."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
user_count=$(echo "$users_json" | jq 'length')
|
user_count=$(echo "$users_json" | jq 'length')
|
||||||
|
|
||||||
if [ "$user_count" -eq 0 ]; then
|
if [ "$user_count" -eq 0 ]; then
|
||||||
echo -e "${red}Error:${NC} No users found."
|
echo -e "${yellow}No users found.${NC}"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "%-20s %-20s %-15s %-20s %-30s %-10s %-15s %-15s %-15s %-10s\n" \
|
printf "%-20s %-20s %-15s %-20s %-30s %-10s %-15s %-15s %-15s\n" \
|
||||||
"Username" "Traffic(GB)" "Expiry(Days)" "Created" "Password" "Blocked" "Status" "Down(MB)" "Up(MB)"
|
"Username" "Traffic(GB)" "Expiry(Days)" "Created" "Password" "Blocked" "Status" "Down(MB)" "Up(MB)"
|
||||||
|
|
||||||
echo "$users_json" | jq -r '.[] |
|
echo "$users_json" | jq -r '.[] |
|
||||||
@ -254,11 +277,10 @@ hysteria2_list_users_handler() {
|
|||||||
.blocked,
|
.blocked,
|
||||||
.status,
|
.status,
|
||||||
((.download_bytes // 0) / 1048576 | floor),
|
((.download_bytes // 0) / 1048576 | floor),
|
||||||
((.upload_bytes // 0) / 1048576 | floor),
|
((.upload_bytes // 0) / 1048576 | floor)] |
|
||||||
.online_count] |
|
|
||||||
@tsv' | \
|
@tsv' | \
|
||||||
while IFS=$'\t' read -r username traffic expiry created password blocked status down up online; do
|
while IFS=$'\t' read -r username traffic expiry created password blocked status down up; do
|
||||||
printf "%-20s %-20s %-15s %-20s %-30s %-10s %-15s %-15s %-15s %-10s\n" \
|
printf "%-20s %-20s %-15s %-20s %-30s %-10s %-15s %-15s %-15s\n" \
|
||||||
"$username" "$traffic" "$expiry" "$created" "$password" "$blocked" "$status" "$down" "$up"
|
"$username" "$traffic" "$expiry" "$created" "$password" "$blocked" "$status" "$down" "$up"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
@ -267,7 +289,7 @@ hysteria2_reset_user_handler() {
|
|||||||
while true; do
|
while true; do
|
||||||
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
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
||||||
@ -283,7 +305,7 @@ hysteria2_show_user_uri_handler() {
|
|||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
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
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
||||||
@ -1222,4 +1244,4 @@ advance_menu() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
define_colors
|
define_colors
|
||||||
main_menu
|
main_menu
|
||||||
Reference in New Issue
Block a user