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

BICyan='\033[1;96m'       # Cyan
BIWhite='\033[1;97m'      # White
UWhite='\033[4;37m'       # White
BIYellow='\033[1;93m'     # Yellow
BGWHITE='\E[40;1;37m'     # Bgwhite

RED="\033[31m"
NC='\e[0m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
YELL='\033[0;33m'

CHATID=$(grep -E "^#bot# " "/etc/bot/.bot.db" | cut -d ' ' -f 3)
KEY=$(grep -E "^#bot# " "/etc/bot/.bot.db" | cut -d ' ' -f 2)
TIME="10"
URL="https://api.telegram.org/bot$KEY/sendMessage"
ISP=$(cat /etc/xray/isp)
CITY=$(cat /etc/xray/city)
IP=$(cat /etc/xray/ipvps)
MYIP=$(cat /etc/xray/ipvps)
domain=$(cat /etc/xray/domain)

clear
user=Trial-VM`</dev/urandom tr -dc 0-9 | head -c3`
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${BGWHITE}      CREATE TRIAL V2RAY TROJN     ${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
read -p "Set Menit : " pup
echo -e "${BIYellow}───────────────────────────────────${NC}"

generateRandomString() {
    # Generate string acak menggunakan /dev/urandom
    string=$(LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w "$1" | head -n 1)
    # Return string acak
    echo "xraytrial$string"
}

# Panggil fungsi generateRandomString dengan panjang 15 karakter
uuid=$(generateRandomString 15)
masaaktif=1
Quota=1000
iplimit=1000

exp=$(date -d "$masaaktif days" +"%Y-%m-%d")
tgl=$(date -d "$masaaktif days" +"%d")
bln=$(date -d "$masaaktif days" +"%b")
thn=$(date -d "$masaaktif days" +"%Y")
expe="$tgl $bln, $thn"
tgl2=$(date +"%d")
bln2=$(date +"%b")
thn2=$(date +"%Y")
tnggl="$tgl2 $bln2, $thn2"
sed -i '/#trojanws$/a\#! '"$user $exp"'\
},{"password": "'""$uuid""'","email": "'""$user""'"' /etc/xray/config.json
sed -i '/#trojangrpc$/a\#! '"$user $exp"'\
},{"password": "'""$uuid""'","email": "'""$user""'"' /etc/xray/config.json
trojanlink1="trojan://${uuid}@${domain}:443?path=/trojan-ws&security=tls&host=${domain}&type=ws&sni=${domain}#${user}"
trojanlink2="trojan://${uuid}@${domain}:443?mode=gun&security=tls&authority=${domain}&type=grpc&serviceName=trojan-grpc&sni=${domain}#${user}"

if [ ! -e /etc/trojan ]; then
  mkdir -p /etc/trojan
fi

if [[ $iplimit -gt 0 ]]; then
mkdir -p /etc/kyt/limit/trojan/ip
echo -e "$iplimit" > /etc/kyt/limit/trojan/ip/$user
else
echo > /dev/null
fi

if [ -z ${Quota} ]; then
  Quota="0"
fi

c=$(echo "${Quota}" | sed 's/[^0-9]*//g')
d=$((${c} * 1024 * 1024 * 1024))

if [[ ${c} != "0" ]]; then
  echo "${d}" >/etc/trojan/${user}
fi

DATADB=$(cat /etc/trojan/.trojan.db | grep "^#&#" | grep -w "${user}" | awk '{print $2}')
if [[ "${DATADB}" != '' ]]; then
  sed -i "/\b${user}\b/d" /etc/trojan/.trojan.db
fi
echo "#&# ${user} ${exp} ${uuid}" >> /etc/trojan/.trojan.db

cat >/var/www/html/trojan-$user.txt <<-END
----------------------
Status Trial trojan Success
----------------------
ISP   : $ISP
City  : $CITY
----------------------
Host  : $domain
User  : $user
UUID  : $uuid
Max   : $iplimit IP
Quota : $Quota GB
Port  : 443, 80
Path  : /trojan
----------------------
WS    : $trojanlink1
----------------------
Grpc  : $trojanlink2
----------------------
Exp   : $pup Menit
----------------------
END

clear

TEXT="
──────────────────────
Status Trial TROJAN Success
──────────────────────
<code>✧ ISP   :</code> <code>$ISP</code>
<code>✧ City  :</code> <code>$CITY</code>
──────────────────────
<code>✧ Host  :</code> <code>$domain</code>
<code>✧ IP    :</code> <code>$MYIP</code>
<code>✧ User  :</code> <code>$user</code>
<code>✧ UUID  :</code> <code>$uuid</code>
<code>✧ Max   :</code> <code>Unlimited</code>
<code>✧ Quota :</code> <code>Unlimited</code>
<code>✧ Port  :</code> <code>443, 80</code>
<code>✧ Path  :</code> <code>/trojan</code>
──────────────────────
<code>WS, GRPC Check Link ↴</code>
──────────────────────
<code>✧ Doc   :</code> https://${domain}:81/trojan-$user.txt
──────────────────────
<code>✧ Exp   :</code> <code>$pup Menit</code>
──────────────────────  "

curl -s --max-time $TIME -d "chat_id=$CHATID&disable_web_page_preview=1&text=$TEXT&parse_mode=html" $URL >/dev/null

echo "tunnel trojan ${user}" | at now + $pup minutes &> /dev/null
systemctl restart xray > /dev/null 2>&1

clear
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${NC}Status Trial TROJAN Success${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${NC}✧ ISP   : $ISP${NC}"
echo -e "${NC}✧ City  : $CITY${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${NC}✧ Host  : $domain${NC}"
echo -e "${NC}✧ IP    : $MYIP${NC}"
echo -e "${NC}✧ User  : $user${NC}"
echo -e "${NC}✧ UUID  : $uuid${NC}"
echo -e "${NC}✧ Max   : $iplimit IP${NC}"
echo -e "${NC}✧ Quota : $Quota GB${NC}"
echo -e "${NC}✧ Port  : 443, 80${NC}"
echo -e "${NC}✧ Path  : /trojan${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${NC}✧ WS    : $trojanlink1${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${NC}✧ Grpc  : $trojanlink2${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${NC}✧ Doc   : https://${domain}:81/trojan-$user.txt${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e "${NC}✧ Exp   : $pup Menit${NC}"
echo -e "${BIYellow}───────────────────────────────────${NC}"
echo -e ""
read -n 1 -s -r -p "Press any key to back"
m-trojan