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

BICyan='\033[1;96m'       # Cyan
CYAN='\033[1;96m'       # Cyan
BIWhite='\033[1;97m'      # White
UWhite='\033[4;37m'       # White
BIYellow='\033[1;93m'     # Yellow
NC='\033[0m'
white='\033[0;37m'
RED="\033[31m"
NC='\e[0m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
YELL='\033[0;33m'
BGREEN='\033[92;1m'

clear
CITY=$(cat /etc/xray/city)
IPVPS=$(cat /etc/xray/ipvps)
domain=$(cat /etc/xray/domain)

RAM=$(free -m | awk 'NR==2 {print $2}')
USAGERAM=$(free -m | awk 'NR==2 {print $3}')
MEMOFREE=$(printf '%-1s' "$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')")
LOADCPU=$(printf '%-0.00001s' "$(top -bn1 | awk '/Cpu/ { cpu = "" 100 - $8 "%" }; END { print cpu }')")
MODEL=$(cat /etc/os-release | grep -w PRETTY_NAME | head -n1 | sed 's/=//g' | sed 's/"//g' | sed 's/PRETTY_NAME//g')
CORE=$(printf '%-1s' "$(grep -c cpu[0-9] /proc/stat)")
DATEVPS=$(date +'%d-%m-%Y')
TIMEZONE=$(printf '%(%H-%M-%S)T')
SERONLINE=$(uptime -p | cut -d " " -f 2-10)

# IZIN AOUTOSCRIPT
izinsc="https://autoscript.interflazz.my.id/tunnel/register"
# USERNAME
rm -f /usr/bin/user
username=$(curl ${izinsc} | grep $IPVPS | awk '{print $2}')
echo "$username" >/usr/bin/user
# validity
rm -f /usr/bin/e
valid=$(curl ${izinsc} | grep $IPVPS | awk '{print $3}')
echo "$valid" >/usr/bin/e
# DETAIL ORDER
username=$(cat /usr/bin/user)
exp=$(cat /usr/bin/e)

# CERTIFICATE STATUS
d1=$(date -d "$valid" +%s)
d2=$(date -d "$today" +%s)
certifacate=$(((d1 - d2) / 86400))

#Download/Upload today
dtoday="$(vnstat -i eth0 | grep "today" | awk '{print $2" "substr ($3, 1, 1)}')"
utoday="$(vnstat -i eth0 | grep "today" | awk '{print $5" "substr ($6, 1, 1)}')"
ttoday="$(vnstat -i eth0 | grep "today" | awk '{print $8" "substr ($9, 1, 1)}')"
#Download/Upload yesterday
dyest="$(vnstat -i eth0 | grep "yesterday" | awk '{print $2" "substr ($3, 1, 1)}')"
uyest="$(vnstat -i eth0 | grep "yesterday" | awk '{print $5" "substr ($6, 1, 1)}')"
tyest="$(vnstat -i eth0 | grep "yesterday" | awk '{print $8" "substr ($9, 1, 1)}')"
#Download/Upload current month
dmon="$(vnstat -i eth0 -m | grep "`date +"%b '%y"`" | awk '{print $3" "substr ($4, 1, 1)}')"
umon="$(vnstat -i eth0 -m | grep "`date +"%b '%y"`" | awk '{print $6" "substr ($7, 1, 1)}')"
tmon="$(vnstat -i eth0 -m | grep "`date +"%b '%y"`" | awk '{print $9" "substr ($10, 1, 1)}')"
# Jika tmon pertama kosong, ambil nilai tmon kedua
if [[ -z "$tmon" ]]; then
tmon="$(vnstat -i eth0 -m | grep "`date +"%Y-%m"`" | awk -F '|' '{print $3}' | awk '{print $1, $2}')"
fi

# Status Expired Or Active
Info="${BGREEN}ONLINE${NC}"
Error="${RED}EXPIRED${NC}"
today=`date -d "0 days" +"%Y-%m-%d"`
Exp1=$(curl ${izinsc} | grep $IPVPS | awk '{print $3}')
if [[ $today < $Exp1 ]]; then
sts="${Info}"
else
sts="${Error}"
fi
clear

# Getting CPU Information
cpu_usage1="$(ps aux | awk 'BEGIN {sum=0} {sum+=$3}; END {print sum}')"
cpu_usage="$((${cpu_usage1/\.*} / ${coREDiilik:-1}))"
cpu_usage+=" %"
DAY=$(date +%A)
DATE=$(date +%m/%d/%Y)
DATE2=$(date -R | cut -d " " -f -5)

cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo )
cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
freq=$( awk -F: ' /cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo )
tram=$( free -m | awk 'NR==2 {print $2}' )
uram=$( free -m | awk 'NR==2 {print $3}' )
fram=$( free -m | awk 'NR==2 {print $4}' )

ssh_service=$(/etc/init.d/ssh status | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
dropbear_service=$(/etc/init.d/dropbear status | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
haproxy_service=$(systemctl status haproxy | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
xray_service=$(systemctl status xray | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
nginx_service=$(systemctl status nginx | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)

# STATUS SERVICE  SSH 
if [[ $ssh_service == "running" ]]; then 
   status_ssh="${BGREEN}ON${NC}"
else
   status_ssh="${RED}OFF${NC}"
fi

# SSH Websocket Proxy
ssh_ws=$( systemctl status ws | grep Active | awk '{print $3}' | sed 's/(//g' | sed 's/)//g' )
if [[ $ssh_ws == "running" ]]; then
    status_ws_epro="${BGREEN}ON${NC}"
else
    status_ws_epro="${RED}OFF${NC}"
fi

# STATUS SERVICE HAPROXY
if [[ $haproxy_service == "running" ]]; then 
   status_haproxy="${BGREEN}ON${NC}"
else
   status_haproxy="${RED}OFF${NC}"
fi

# STATUS SERVICE XRAY
if [[ $xray_service == "running" ]]; then 
   status_xray="${BGREEN}ON${NC}"
else
   status_xray="${RED}OFF${NC}"
fi

# STATUS SERVICE NGINX
if [[ $nginx_service == "running" ]]; then 
   status_nginx="${BGREEN}ON${NC}"
else
   status_nginx="${RED}OFF${NC}"
fi

# STATUS SERVICE Dropbear
if [[ $dropbear_service == "running" ]]; then 
   status_dropbear="${BGREEN}GOOD${NC}"
else
   status_dropbear="${RED}CRAZY${NC}"
fi

# LOG CREATE ACCOUNT
vlx=$(grep -c -E "^#& " "/etc/xray/config.json")
let vla=$vlx/2
vmc=$(grep -c -E "^### " "/etc/xray/config.json")
let vma=$vmc/2
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
ssx=$(grep -c -E "^#ss# " "/etc/xray/config.json")
let ssa=$ssx/2

clear
echo -e "${BICyan}┌─────────────────────────────────────────────────┐${NC}"
echo -e "${BICyan}│${NC} \033[44;1;97;1m             GEMILANG KINASIH STORE            ${NC} ${BICyan}│${NC}"
echo -e "${BICyan}└─────────────────────────────────────────────────┘${NC}"
echo -e "${BICyan}┌─────────────────────────────────────────────────┐${NC}"
echo -e "${BICyan}│${white} SYSTEM   : $MODEL ${NC}"
echo -e "${BICyan}│${white} RAM      : $uram / $tram MB ${NC}"
echo -e "${BICyan}│${white} UPTIME   : $(uptime -p | cut -d " " -f 2-10)${NC}"
echo -e "${BICyan}│${white} IP VPS   : $(cat /etc/xray/ipvps)${NC}"
echo -e "${BICyan}│${white} ISP      : $(cat /etc/xray/isp)${NC}"
echo -e "${BICyan}│${white} DOMAIN   : $(cat /etc/xray/domain)${NC}"
echo -e "${BICyan}└─────────────────────────────────────────────────┘${NC}"
echo -e "${BICyan}┌─────────────────────────────────────────────────┐${NC}"
echo -e "${BICyan}│${BIYellow} PROXY :${NC} $status_haproxy │ ${BIYellow} NGINX :${NC} $status_nginx ${NC} │ ${BIYellow}SSHWS :${NC} $status_ws_epro ${NC}│  $status_dropbear  \e[96;1m│ ${NC}"
echo -e "${BICyan}└─────────────────────────────────────────────────┘${NC}"
echo -e "${BICyan}       ┌───────────────────────────────────┐${NC}"
echo -e "${white}               SSHWS OVPN    :    $ssh1 ${NC}" 
echo -e "${white}               XRAY VMESS    :    $vma  ${NC}" 
echo -e "${white}               XRAY VLESS    :    $vla  ${NC}" 
echo -e "${white}               XRAY TROJN    :    $trb  ${NC}"    
echo -e "${BICyan}       └───────────────────────────────────┘${NC}"
echo -e "${BICyan}   ┌───────────────────────────────────────────┐${NC}"
echo -e "${BICyan}   │   ${NC}${CYAN}VERSION SC : ${BIYellow} 2.4 ${CYAN}[${white}LAST UPDATE${CYAN}]${NC}"
echo -e "${BICyan}   │   ${NC}${CYAN}USERNAME   : ${BIYellow} $username${NC} ${BIYellow}${BICyan}[$sts${BICyan}]${NC}"
echo -e "${BICyan}   │   ${NC}${CYAN}EXPIRED SC : ${BIYellow} $exp ${CYAN}[${white}$certifacate DAYS${CYAN}]${NC}"
echo -e "${BICyan}   └───────────────────────────────────────────┘${NC}"
echo -e "${BICyan}┌─────────────────────────────────────────────────┐${NC}"
echo -e "${BICyan}│${NC}${CYAN} TODAY${BGREEN} $ttoday${NC} ${CYAN}YESTERDAY${BGREEN} $tyest${NC} ${CYAN}MONTH${BGREEN} $tmon${NC}"
echo -e "${BICyan}└─────────────────────────────────────────────────┘${NC}"
echo -e "${BICyan} ————————————————————————————————————————————————— ${NC}"
echo -e "${BICyan}        ${BIYellow} access all features\033[92;1m menu ${white}command ${NC}" 
echo -e "${BICyan} ————————————————————————————————————————————————— ${NC}"
echo -e ""