fix: correct JSON array parsing

This commit is contained in:
ReturnFI
2025-09-24 17:35:01 +00:00
parent e5f481ac85
commit d828ca6c3f

41
menu.sh
View File

@ -232,30 +232,37 @@ hysteria2_get_user_handler() {
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)
if [ $? -ne 0 ] || [ -z "$users_json" ]; then if [ $? -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
# Extract keys (usernames) from JSON user_count=$(echo "$users_json" | jq 'length')
users_keys=$(echo "$users_json" | jq -r 'keys[]')
if [ "$user_count" -eq 0 ]; then
if [ -z "$users_keys" ]; then
echo -e "${red}Error:${NC} No users found." echo -e "${red}Error:${NC} No users found."
return 1 return 1
fi fi
# Print headers printf "%-20s %-20s %-15s %-20s %-30s %-10s %-15s %-15s %-15s %-10s\n" \
printf "%-20s %-20s %-15s %-20s %-30s %-10s\n" "Username" "Traffic Limit (GB)" "Expiration (Days)" "Creation Date" "Password" "Blocked" "Username" "Traffic(GB)" "Expiry(Days)" "Created" "Password" "Blocked" "Status" "Down(MB)" "Up(MB)"
# Print user details echo "$users_json" | jq -r '.[] |
for key in $users_keys; do [.username,
echo "$users_json" | jq -r --arg key "$key" ' (if .max_download_bytes == 0 then "Unlimited" else (.max_download_bytes / 1073741824 | tostring) end),
"\($key) \(.[$key].max_download_bytes / 1073741824) \(.[$key].expiration_days) \(.[$key].account_creation_date) \(.[$key].password) \(.[$key].blocked)"' | \ (if .expiration_days == 0 then "Never" else (.expiration_days | tostring) end),
while IFS= read -r line; do (.account_creation_date // "N/A"),
IFS=' ' read -r username traffic_limit expiration_date creation_date password blocked <<< "$line" .password,
printf "%-20s %-20s %-15s %-20s %-30s %-10s\n" "$username" "$traffic_limit" "$expiration_date" "$creation_date" "$password" "$blocked" .blocked,
done .status,
((.download_bytes // 0) / 1048576 | floor),
((.upload_bytes // 0) / 1048576 | floor),
.online_count] |
@tsv' | \
while IFS=$'\t' read -r username traffic expiry created password blocked status down up online; do
printf "%-20s %-20s %-15s %-20s %-30s %-10s %-15s %-15s %-15s %-10s\n" \
"$username" "$traffic" "$expiry" "$created" "$password" "$blocked" "$status" "$down" "$up"
done done
} }