#!/bin/bash

# URL data akun
data_acc="https://raw.githubusercontent.com/RozTun/permission/main/ip"
data_key=$(cat /usr/bin/user)

# Mengambil data dari URL
data=$(curl -s $data_acc)

# Mendapatkan baris yang sesuai dengan key dari data di GitHub
expiry_info=$(echo "$data" | grep "^### $data_key ")

if [ -n "$expiry_info" ]; then
    # Mengambil tanggal kedaluwarsa
    expiry_date=$(echo $expiry_info | awk '{print $3}')
    # Konversi ke timestamp
    expiry_date=$(date -d "$expiry_date" +%s)
    current_date=$(date +%s)
    remaining_days=$(( (expiry_date - current_date) / 86400 ))

    if [ $expiry_date -gt $current_date ]; then
        # Colors
        green="\e[38;5;82m"
        red="\e[38;5;196m"
        neutral="\e[0m"
        orange="\e[38;5;130m"
        blue="\e[38;5;39m"
        yellow="\e[38;5;226m"
        purple="\e[38;5;141m"
        bold_white="\e[1;37m"
        reset="\e[0m"
        pink="\e[38;5;205m"

        print_rainbow() {
            local text="$1"
            local length=${#text}
            local start_color=(0 5 0)
            local mid_color=(0 200 0)
            local end_color=(0 5 0)

            for ((i = 0; i < length; i++)); do
                local progress=$((i * 100 / (length - 1)))

                if [ $progress -lt 50 ]; then
                    local factor=$((progress * 2))
                    r=$(( (start_color[0] * (100 - factor) + mid_color[0] * factor) / 100 ))
                    g=$(( (start_color[1] * (100 - factor) + mid_color[1] * factor) / 100 ))
                    b=$(( (start_color[2] * (100 - factor) + mid_color[2] * factor) / 100 ))
                else
                    local factor=$(((progress - 50) * 2))
                    r=$(( (mid_color[0] * (100 - factor) + end_color[0] * factor) / 100 ))
                    g=$(( (mid_color[1] * (100 - factor) + end_color[1] * factor) / 100 ))
                    b=$(( (mid_color[2] * (100 - factor) + end_color[2] * factor) / 100 ))
                fi

                printf "\e[38;2;%d;%d;%dm%s" "$r" "$g" "$b" "${text:$i:1}"
            done
            echo -e "$reset" # Reset color at the end
        }

        # Function to count accounts
        count_accounts() {
            if [ ! -e "/etc/xray/$1/.$1.db" ]; then
                mkdir -p "/etc/xray/$1"
                touch "/etc/xray/$1/.$1.db"
            fi

            accounts=$(cat "/etc/xray/$1/.$1.db")
            if [[ $accounts = "" ]]; then
                echo "0"
            else
                cat "/etc/xray/$1/.$1.db" | grep "###" | wc -l
            fi
        }
# \\ Vless account //
vlx=$(grep -c -E "^#& " "/etc/xray/config.json")
let vla=$vlx/2
# \\ Vmess account //
vmc=$(grep -c -E "^### " "/etc/xray/config.json")
let vma=$vmc/2
# \\ Trojan account //
ssh1="$(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd | wc -l)"
trx=$(grep -c -E "^#! " "/etc/xray/config.json")
let trb=$trx/2
# \\ shadowsocks account //
ssx=$(grep -c -E "^#!# " "/etc/xray/config.json")
let ssa=$ssx/2

kemarin=$(date -d "-1 days" +"%m-%d")
hariini=$(date -d "0 days" +"%m-%d")
kemarin1=$(date -d "-1 days" +"%m/%d")
hariini1=$(date -d "0 days" +"%m/%d")
bulan=$(date +"%b '%y")
bulan1=$(date +"%Y-%m")
kemarin2=$(vnstat -d | grep -w "${kemarin}" | awk '{print $8" "substr ($9 ,1 ,1) substr ($9 ,3 ,1)}') 
kemarin3=$(vnstat -d | grep -w "${kemarin1}" | awk '{print $8" "substr ($9 ,1 ,1) substr ($9 ,3 ,1)}') 
if [[ $kemarin2 == "" ]]; then
bwkmrn=${kemarin3}
else
bwkmrn=${kemarin2}
fi
if [[ $bwkmrn == "" ]]; then
bwkmrn1="NEWVPS "
else
bwkmrn1=${bwkmrn}
fi
hariini2=$(vnstat -d | grep -w "${hariini}" | awk '{print $8" "substr ($9 ,1 ,1) substr ($9 ,3 ,1)}') 
hariini3=$(vnstat -d | grep -w "${hariini1}" | awk '{print $8" "substr ($9 ,1 ,1) substr ($9 ,3 ,1)}') 
if [[ $hariini2 == "" ]]; then
bwhari=${hariini3}
else
bwhari=${hariini2}
fi
bulan2=$(vnstat -m | grep -w "${bulan}" | awk '{print $9" "substr ($10 ,1 ,1) substr ($10 ,3 ,1)}') 
bulan3=$(vnstat -m | grep -w "${bulan1}" | awk '{print $8" "substr ($9 ,1 ,1) substr ($9 ,3 ,1)}') 
if [[ $bulan2 == "" ]]; then
bwbln=${bulan3}
else
bwbln=${bulan2}
fi


        # Check service status
        cek_status() {
            status=$(systemctl is-active --quiet $1 && echo "aktif" || echo "nonaktif")
            if [ "$status" = "aktif" ]; then
                echo -e "${green}GOOD${neutral}"
            else
                echo -e "${red}BAD${neutral}"
            fi
        }
        # Function to display menu
        display_menu() {
            # Display menu
            clear
            echo -e "   ${orange}─────────────────────────────────────────${neutral}"
            echo -e "        ${green}.::::.   HunterTunnel   .::::.${neutral}"
            echo -e "   ${orange}─────────────────────────────────────────${neutral}"
            echo -e "      \e[38;5;196m• \e[0mSYSTEM    : $(cat /etc/os-release | grep -w PRETTY_NAME | head -n1 | sed 's/=//g' | sed 's/"//g' | sed 's/PRETTY_NAME//g' | cut -d '.' -f 1-3 2>/dev/null || echo "Unable to detect system")"
            echo -e "      \e[38;5;196m• \e[0mRAM       : $(free -m | awk '{print $7}' | tr -d '\n') MB   \033[0m "
            echo -e "      \e[38;5;196m• \e[0mSWAP      : $(free -m | awk 'NR==3 {print $4}') MB   \033[0m "
            echo -e "      \e[38;5;196m• \e[0mISP       : $(cat /etc/xray/isp 2>/dev/null | cut -d ' ' -f 1-2 || echo "Unable to detect ISP")"
            echo -e "      \e[38;5;196m• \e[0mCITY      : $(cat /etc/xray/city 2>/dev/null || echo "Unable to detect city")"
            echo -e "      \e[38;5;196m• \e[0mUPTIME    : $(uptime -p 2>/dev/null | cut -d " " -f 2-10000 || echo "Unable to detect uptime")"
            print_rainbow "   ───────────────────────────────────────── "
            echo -e "      \e[38;5;196m• \e[0mYESTERDAY : ${bwkmrn1}"
            echo -e "      \e[38;5;196m• \e[0mTODAY     : ${bwhari}"
            echo -e "      \e[38;5;196m• \e[0mMONTHLY   : ${bwbln}"
            echo -e "   ${orange}─────────────────────────────────────────${neutral}"
            echo -e "   ${neutral}XRAY : "$(cek_status xray)" / PROXY : "$(cek_status haproxy)" / NGINX : "$(cek_status nginx)" ${neutral}"
            echo -e "   ${orange}─────────────────────────────────────────${neutral}"
            echo -e "${orange}      ┌───────────────────────────────┐${neutral}"
            printf "           %-10s : %-3s ACCOUNT\n" "SSH/OVPN" "$ssh1"
            printf "           %-10s : %-3s ACCOUNT\n" "VMESS" "$vma"
            printf "           %-10s : %-3s ACCOUNT\n" "VLESS" "$vla"
            printf "           %-10s : %-3s ACCOUNT\n" "TROJAN" "$trb"
            echo -e "${orange}      └───────────────────────────────┘ ${neutral}"
            print_rainbow "   ───────────────────────────────────────── "
            echo -e "      ${green}• ${neutral}SCRIPT KEY : $(cat /usr/bin/user || echo "Unable to retrieve Script ID")"
            echo -e "      ${green}• ${neutral}SCRIPT EXP : $remaining_days DAYS REMAINING"
            print_rainbow "   ───────────────────────────────────────── "
            echo -e "   ${orange}─────────────────────────────────────────${neutral}"
            echo -e "           type ${red}menu${neutral} to continue"
            echo -e "   ${orange}─────────────────────────────────────────${neutral}"
        }

        case "$1" in
        "dashboard")
            display_menu
            ;;
        *)
            echo "Unknown command. Use 'dashboard'."
            ;;
        esac
    else
        echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
        echo -e "\e[38;5;82m         HUNTERTUNNEL AUTOSCRIPT          \033[0m"
        echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
        echo -e ""
        echo -e "            \033[31mPERMISSION DENIED !\033[0m"
        echo -e "   \033[0;33mYour VPS\033[0m $(wget -qO- ipv4.icanhazip.com) \033[0;33mHas been Banned\033[0m"
        echo -e "     \033[0;33mBuy access permissions for scripts\033[0m"
        echo -e "             \033[0;33mContact Admin :\033[0m"
        echo -e "      \033[0;32mWhatsApp\033[0m wa.me/6285759375937"
        echo -e "         \033[0;96mTelegram\033[0m t.me/RozTun"
        echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
        exit
    fi
else
    echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
    echo -e "\e[38;5;82m         HUNTERTUNNEL AUTOSCRIPT          \033[0m"
    echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
    echo -e ""
    echo -e "            \033[31mPERMISSION DENIED !\033[0m"
    echo -e "   \033[0;33mYour VPS\033[0m $(wget -qO- ipv4.icanhazip.com) \033[0;33mHas been Banned\033[0m"
    echo -e "     \033[0;33mBuy access permissions for scripts\033[0m"
    echo -e "             \033[0;33mContact Admin :\033[0m"
    echo -e "      \033[0;32mWhatsApp\033[0m wa.me/6285759375937"
    echo -e "         \033[0;96mTelegram\033[0m t.me/RozTun"
    echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
    exit
fi
