fix traffic displayed

This commit is contained in:
Whispering Wind
2024-07-05 11:21:53 +03:30
committed by GitHub
parent e0667ee575
commit ff8099b3c1

24
menu.sh
View File

@ -185,12 +185,12 @@ traffic_status() {
python3 /etc/hysteria/traffic.py >/dev/null 2>&1 python3 /etc/hysteria/traffic.py >/dev/null 2>&1
else else
echo "Error: /etc/hysteria/traffic.py not found." echo "Error: /etc/hysteria/traffic.py not found."
exit 1 return 1
fi fi
if [ ! -f "/etc/hysteria/traffic_data.json" ]; then if [ ! -f "/etc/hysteria/traffic_data.json" ]; then
echo "Error: /etc/hysteria/traffic_data.json not found." echo "Error: /etc/hysteria/traffic_data.json not found."
exit 1 return 1
fi fi
data=$(cat /etc/hysteria/traffic_data.json) data=$(cat /etc/hysteria/traffic_data.json)
@ -199,8 +199,24 @@ traffic_status() {
echo -e "User Upload (TX) Download (RX) Status" echo -e "User Upload (TX) Download (RX) Status"
echo "---------------------------------------------------------------------------" 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 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
printf "${yellow}%-15s ${cyan}%-15s ${green}%-15s ${NC}%-10s\n" "$user" "$(printf "%.2fMB" "$upload")" "$(printf "%.2fMB" "$download")" "$status" 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 "---------------------------------------------------------------------------" echo "---------------------------------------------------------------------------"
done done
} }