Fix list_users_handler

This commit is contained in:
Sarina
2024-07-23 17:51:25 +03:30
parent 69a205a487
commit 6c2d6c0aaa

20
menu.sh
View File

@ -148,25 +148,31 @@ hysteria2_list_users_handler() {
echo -e "\033[0;31mError:\033[0m Failed to list users." echo -e "\033[0;31mError:\033[0m Failed to list users."
return 1 return 1
fi fi
users_array=($(echo "$users_json" | jq -r '.[] | .username'))
if [[ ${#users_array[@]} -eq 0 ]]; then # Extract keys (usernames) from JSON
users_keys=$(echo "$users_json" | jq -r 'keys[]')
if [ -z "$users_keys" ]; then
echo -e "\033[0;31mError:\033[0m No users found." echo -e "\033[0;31mError:\033[0m No users found."
return 1 return 1
fi fi
# Print headers # Print headers
echo -e "\033[0;32mUsers:\033[0m" echo -e "\033[0;32mUsers:\033[0m"
printf "%-20s %-15s %-15s %-20s %-30s %-10s\n" "Username" "Traffic Limit (GB)" "Expiration (Days)" "Creation Date" "Password" "Blocked" printf "%-20s %-20s %-15s %-20s %-30s %-10s\n" "Username" "Traffic Limit (GB)" "Expiration (Days)" "Creation Date" "Password" "Blocked"
# Print user details # Print user details
echo "$users_json" | jq -r '.[] | "\(.username) \(.traffic_limit_GB) \(.expiration_days) \(.creation_date) \(.password) \(.blocked)"' | \ for key in $users_keys; do
while IFS= read -r line; do echo "$users_json" | jq -r --arg key "$key" '
IFS=' ' read -r username traffic_limit expiration_date creation_date password blocked <<< "$line" "\($key) \(.[$key].max_download_bytes / 1073741824) \(.[$key].expiration_days) \(.[$key].account_creation_date) \(.[$key].password) \(.[$key].blocked)"' | \
printf "%-20s %-15s %-15s %-20s %-30s %-10s\n" "$username" "$traffic_limit" "$expiration_date" "$creation_date" "$password" "$blocked" while IFS= read -r line; do
IFS=' ' read -r username traffic_limit expiration_date creation_date password blocked <<< "$line"
printf "%-20s %-20s %-15s %-20s %-30s %-10s\n" "$username" "$traffic_limit" "$expiration_date" "$creation_date" "$password" "$blocked"
done
done done
} }
hysteria2_show_user_uri_handler() { hysteria2_show_user_uri_handler() {
while true; do while true; do
read -p "Enter the username: " username read -p "Enter the username: " username