diff --git a/menu.sh b/menu.sh index 98eaa33..00f0a9a 100644 --- a/menu.sh +++ b/menu.sh @@ -185,12 +185,12 @@ traffic_status() { python3 /etc/hysteria/traffic.py >/dev/null 2>&1 else echo "Error: /etc/hysteria/traffic.py not found." - exit 1 + return 1 fi if [ ! -f "/etc/hysteria/traffic_data.json" ]; then echo "Error: /etc/hysteria/traffic_data.json not found." - exit 1 + return 1 fi data=$(cat /etc/hysteria/traffic_data.json) @@ -199,8 +199,24 @@ traffic_status() { echo -e "User Upload (TX) Download (RX) Status" echo "---------------------------------------------------------------------------" - echo "$data" | jq -r 'to_entries[] | [.key, (.value.upload_bytes / 1024 / 1024), (.value.download_bytes / 1024 / 1024), .value.status] | @tsv' | while IFS=$'\t' read -r user upload download status; do - printf "${yellow}%-15s ${cyan}%-15s ${green}%-15s ${NC}%-10s\n" "$user" "$(printf "%.2fMB" "$upload")" "$(printf "%.2fMB" "$download")" "$status" + echo "$data" | jq -r 'to_entries[] | [.key, .value.upload_bytes, .value.download_bytes, .value.status] | @tsv' | while IFS=$'\t' read -r user upload_bytes download_bytes status; do + if [ $(echo "$upload_bytes < 1073741824" | bc -l) -eq 1 ]; then + upload=$(echo "scale=2; $upload_bytes / 1024 / 1024" | bc) + upload_unit="MB" + else + upload=$(echo "scale=2; $upload_bytes / 1024 / 1024 / 1024" | bc) + upload_unit="GB" + fi + + if [ $(echo "$download_bytes < 1073741824" | bc -l) -eq 1 ]; then + download=$(echo "scale=2; $download_bytes / 1024 / 1024" | bc) + download_unit="MB" + else + download=$(echo "scale=2; $download_bytes / 1024 / 1024 / 1024" | bc) + download_unit="GB" + fi + + printf "${yellow}%-15s ${cyan}%-15s ${green}%-15s ${NC}%-10s\n" "$user" "$(printf "%.2f%s" "$upload" "$upload_unit")" "$(printf "%.2f%s" "$download" "$download_unit")" "$status" echo "---------------------------------------------------------------------------" done }