# !/bin/bash
# ████████╗░█████╗░██╗░░██╗░█████╗░██╗░░░██╗██████╗░███╗░░██╗
# ╚══██╔══╝██╔══██╗██║░██╔╝██╔══██╗██║░░░██║██╔══██╗████╗░██║
# ░░░██║░░░██║░░██║█████═╝░██║░░██║╚██╗░██╔╝██████╔╝██╔██╗██║
# ░░░██║░░░██║░░██║██╔═██╗░██║░░██║░╚████╔╝░██╔═══╝░██║╚████║
# ░░░██║░░░╚█████╔╝██║░╚██╗╚█████╔╝░░╚██╔╝░░██║░░░░░██║░╚███║
# ░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝░╚════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝░░╚══╝

RED="\033[31m"
YELLOW="\033[33m"
NC='\e[0m'
YELL='\033[0;33m'
BRED='\033[1;31m'
GREEN='\033[0;32m'
ORANGE='\033[33m'
BGWHITE='\e[0;100;37m'

clear

# Function to convert bytes to readable format
function con() {
    local -i bytes=$1
    if [[ $bytes -lt 1024 ]]; then
        echo "${bytes}B"
    elif [[ $bytes -lt 1048576 ]]; then
        echo "$(( (bytes + 1023)/1024 )) KB"
    elif [[ $bytes -lt 1073741824 ]]; then
        echo "$(( (bytes + 1048575)/1048576 )) MB"
    else
        echo "$(( (bytes + 1073741823)/1073741824 )) GB"
    fi
}

data=(`cat /etc/xray/config.json | grep '###' | cut -d ' ' -f 2 | sort | uniq`)

# Read log data once and process it
log_data=$(cat /var/log/xray/access.log | tail -n 500)
ip_data=($(echo "$log_data" | grep email | grep "accepted" | awk -F "from " '{print $2}' | sed 's/tcp://g' | sed 's/\[.*//g' | cut -d: -f1 | sort | uniq))

# Display header
echo -e "\e[33m────────────────────────────────────────────────────────────\033[0m"
echo -e "\E[40;1;37m               CHECK MEMBER LOGIN V2RAY VMESS               \E[0m"
echo -e "\e[33m────────────────────────────────────────────────────────────\033[0m"
echo -e "\E[40;1;37mUSERNAME   | LAST LOGIN |  USAGE  |  QUOTA  | LOGIN |  LIMIT\E[0m"
echo -e "\e[33m────────────────────────────────────────────────────────────\033[0m"

# Loop through each user
for akun in "${data[@]}"; do
    echo -n > /tmp/ipvmess.txt
    for ip in "${ip_data[@]}"; do
        # Extract the first two segments of the IP
        ip_prefiks=$(echo "$ip" | cut -d "." -f1,2)

        # Check if this IP belongs to the current user (only need to search through the preloaded log data)
        jum=$(echo "$log_data" | grep -w "$akun" | grep -w "$ip_prefiks" | grep "accepted" | awk -F "from " '{print $2}' | sed 's/tcp://g' | cut -d ":" -f1 | cut -d "." -f1,2 | sort | uniq)
        if [[ "$jum" == "$ip_prefiks" ]]; then
            echo "$jum" >> /tmp/ipvmess.txt
        fi
    done
    
    jum=$(cat /tmp/ipvmess.txt)
    if [[ -z "$jum" ]]; then
        echo > /dev/null
    else
        iplimit=$(cat /etc/kyt/limit/vmess/ip/${akun})
        jum2=$(cat /tmp/ipvmess.txt | sort | uniq | wc -l)
        byte=$(cat /etc/vmess/${akun})
        lim=$(con ${byte})
        wey=$(cat /etc/limit/vmess/${akun})
        gb=$(con ${wey})
        lastlogin=$(echo "$log_data" | grep -w "$akun" | cut -d " " -f 2 | tail -1)
        
        # Check if the number of logged-in IPs exceeds the limit
        if [ "$jum2" -gt "$iplimit" ]; then
            color="$BRED"  # Set color to red if exceeded
        else
            color="$NC"  # Normal color if within the limit
        fi
        
        # Display the formatted result with aligned columns
        echo -e "\e[33m────────────────────────────────────────────────────────────\033[0m"
        
        # Using printf to ensure proper alignment and adjusting column widths
        printf "${color}%-13s %-11s %-10s %-8s %-8s %-5s${NC}\n" \
            "$akun" \
            "$lastlogin" \
            " $gb" \
            "$lim" \
            "$jum2 IP" \
            "$iplimit IP"
    fi 
    rm -rf /tmp/ipvmess.txt
done

# Display footer
echo -e "\e[33m────────────────────────────────────────────────────────────\033[0m"
echo ""
read -n 1 -s -r -p "Press Any Key To Back"
m-vmess
