#!/bin/bash

BASE_URL="https://autoscript.interflazz.my.id/v2"

red="\e[91m"
green="\e[92m"
yellow="\e[93m"
blue="\e[94m"
purple="\e[95m"
cyan="\e[96m"
white="\e[97m"
orange="\e[38;5;130m"
bold_white="\e[1;37m"
reset="\e[0m"

update_box_width() {
    local cols

    cols=$(tput cols 2>/dev/null || echo 80)

    if [ "$cols" -gt 72 ]; then
        BOX_WIDTH=72
    elif [ "$cols" -lt 30 ]; then
        BOX_WIDTH=30
    else
        BOX_WIDTH=$((cols - 2))
    fi

    BOX_INNER=$((BOX_WIDTH - 4))
}

repeat_char() {
    local count="$1"
    local char="$2"
    local line

    printf -v line '%*s' "$count" ""
    printf '%s' "${line// /$char}"
}

box_top() {
    update_box_width

    printf "%b┌" "$orange"
    repeat_char $((BOX_WIDTH - 2)) "─"
    printf "┐%b\n" "$reset"
}

box_middle() {
    update_box_width

    printf "%b├" "$orange"
    repeat_char $((BOX_WIDTH - 2)) "─"
    printf "┤%b\n" "$reset"
}

box_bottom() {
    update_box_width

    printf "%b└" "$orange"
    repeat_char $((BOX_WIDTH - 2)) "─"
    printf "┘%b\n" "$reset"
}

box_empty() {
    update_box_width

    printf "%b│%b" "$orange" "$reset"
    printf "%*s" $((BOX_WIDTH - 2)) ""
    printf "%b│%b\n" "$orange" "$reset"
}

box_center() {
    update_box_width

    local text="$1"
    local color="${2:-$white}"
    local length=${#text}
    local available="$BOX_INNER"

    if [ "$length" -gt "$available" ]; then
        box_left "$text" "$color"
        return
    fi

    local left=$(((available - length) / 2))
    local right=$((available - length - left))

    printf "%b│%b " "$orange" "$reset"
    printf "%*s" "$left" ""
    printf "%b%s%b" "$color" "$text" "$reset"
    printf "%*s" "$right" ""
    printf " %b│%b\n" "$orange" "$reset"
}

box_left() {
    update_box_width

    local text="$1"
    local color="${2:-$white}"
    local available="$BOX_INNER"
    local length=${#text}
    local offset=0
    local part
    local part_length
    local right

    if [ "$length" -eq 0 ]; then
        box_empty
        return
    fi

    while [ "$offset" -lt "$length" ]; do
        part="${text:$offset:$available}"
        part_length=${#part}
        right=$((available - part_length))

        printf "%b│%b " "$orange" "$reset"
        printf "%b%s%b" "$color" "$part" "$reset"
        printf "%*s" "$right" ""
        printf " %b│%b\n" "$orange" "$reset"

        offset=$((offset + available))
    done
}

show_access_denied() {
    local reason="$1"

    clear

    box_top
    box_center "INTERFLAZZ.MY.ID" "$green"
    box_center "AUTOSCRIPT PREMIUM" "$bold_white"
    box_middle
    box_center "AKSES DITOLAK!" "$red"
    box_middle
    box_left "IP VPS : $vps_ip" "$yellow"
    box_left "$reason" "$yellow"
    box_middle
    box_center "HUBUNGI ADMIN" "$bold_white"
    box_left "WhatsApp ~ https://wa.me/6285956131443" "$green"
    box_left "Telegram ~ https://t.me/interflazz" "$cyan"
    box_bottom
}

vps_ip=$(wget -qO- ipv4.icanhazip.com)

data_acc=$(
    curl -s "${BASE_URL}/access" |
        grep "### $vps_ip " |
        awk '{print $3}'
)

if [ -z "$data_acc" ]; then
    show_access_denied "VPS belum memiliki izin akses script."
    exit 1
fi

expiry_date=$(date -d "$data_acc" +%s 2>/dev/null)
current_date=$(date +%s)

if [ -z "$expiry_date" ] || [ "$expiry_date" -le "$current_date" ]; then
    show_access_denied "Masa aktif akses script telah berakhir."
    exit 1
fi

remaining_days=$(((expiry_date - current_date) / 86400))

ip=$(wget -qO- ipv4.icanhazip.com)
city=$(cat /etc/xray/city 2>/dev/null || echo "Tidak diketahui")
pubkey=$(cat /etc/slowdns/server.pub 2>/dev/null || echo "Public Key tidak tersedia")
ns_domain=$(cat /etc/xray/dns 2>/dev/null || echo "Domain SlowDNS belum tersedia")
domain=$(cat /etc/xray/domain 2>/dev/null || hostname -f)

mkdir -p /etc/ssh /etc/xray /var/www/html

if [ ! -f /etc/ssh/.ssh.db ]; then
    touch /etc/ssh/.ssh.db
fi

echo ""
echo -ne "${yellow}Menyiapkan Akun Premium${reset}"

for i in {1..2}; do
    for j in ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏; do
        echo -ne "\r${yellow}Menyiapkan Akun Premium $j${reset}"
        sleep 0.1
    done
done

echo -ne "\r${green}Akun Premium siap dibuat!${reset}\n"

sleep 1

clear

box_top
box_center "INTERFLAZZ.MY.ID" "$green"
box_center "BUAT AKUN SSH / OPENVPN" "$bold_white"
box_middle
box_center "MASUKKAN DATA PENGGUNA" "$yellow"
box_bottom

echo ""

while true; do
    read -p "Nama Pengguna : " user

    if [[ ${#user} -lt 3 || ! "$user" =~ ^[a-zA-Z0-9_-]+$ ]]; then
        echo -e "${red}Nama pengguna minimal 3 karakter dan hanya boleh berisi huruf, angka, _ atau -.${reset}"
        continue
    fi

    if grep -q "^### $user " /etc/ssh/.ssh.db || id "$user" >/dev/null 2>&1; then
        random_number=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 5)

        user="${random_number}${user}"

        echo -e "${yellow}Nama pengguna sudah digunakan. Nama baru: $user${reset}"

        break
    fi

    break
done

echo ""

clear

box_top
box_center "INTERFLAZZ.MY.ID" "$green"
box_center "SSH / OPENVPN PREMIUM" "$bold_white"
box_middle
box_left "Nama Pengguna : $user" "$white"
box_middle
box_left "Masukkan password akun SSH." "$yellow"
box_left "Masukkan masa aktif akun dalam hari." "$yellow"
box_left "Masukkan batas penggunaan IP akun." "$yellow"
box_bottom

echo ""

while true; do
    read -p "Password : " passwd

    if [[ -z "$passwd" ]]; then
        echo -e "${red}Password tidak boleh kosong.${reset}"
    elif [[ ${#passwd} -lt 3 ]]; then
        echo -e "${red}Password minimal 3 karakter.${reset}"
    elif ! [[ "$passwd" =~ ^[a-zA-Z0-9]+$ ]]; then
        echo -e "${red}Password hanya boleh berisi huruf dan angka.${reset}"
    else
        break
    fi
done

while true; do
    read -p "Masa Aktif (Hari) : " expired

    if [[ -z "$expired" ]]; then
        echo -e "${red}Masa aktif tidak boleh kosong.${reset}"
    elif ! [[ "$expired" =~ ^[0-9]+$ ]]; then
        echo -e "${red}Masa aktif harus berupa angka.${reset}"
    elif [[ "$expired" -lt 1 ]]; then
        echo -e "${red}Masa aktif minimal 1 hari.${reset}"
    else
        break
    fi
done

while true; do
    read -p "Batas IP : " iplim

    if [[ -z "$iplim" ]]; then
        echo -e "${red}Batas IP tidak boleh kosong.${reset}"
    elif ! [[ "$iplim" =~ ^[0-9]+$ ]]; then
        echo -e "${red}Batas IP harus berupa angka.${reset}"
    elif [[ "$iplim" -lt 1 ]]; then
        echo -e "${red}Batas IP minimal 1.${reset}"
    else
        break
    fi
done

exp=$(date -d "$expired days" +"%Y-%m-%d")

if ! useradd -e "$exp" -s /bin/false -M "$user"; then
    echo ""

    box_top
    box_center "INTERFLAZZ.MY.ID" "$green"
    box_middle
    box_center "GAGAL MEMBUAT AKUN SSH" "$red"
    box_middle
    box_left "Nama Pengguna : $user" "$yellow"
    box_left "Silakan periksa konfigurasi sistem pengguna." "$yellow"
    box_bottom

    exit 1
fi

printf "%s\n%s\n" "$passwd" "$passwd" | passwd "$user" >/dev/null 2>&1

if [ $? -ne 0 ]; then
    userdel "$user" >/dev/null 2>&1

    echo ""

    box_top
    box_center "INTERFLAZZ.MY.ID" "$green"
    box_middle
    box_center "GAGAL MENGATUR PASSWORD" "$red"
    box_middle
    box_left "Akun dibatalkan karena password gagal disimpan." "$yellow"
    box_bottom

    exit 1
fi

if [[ "$iplim" != "0" ]]; then
    echo "${iplim}" >/etc/ssh/${user}
fi

grep -v "^### ${user} " /etc/ssh/.ssh.db >/etc/ssh/.ssh.db.tmp

mv /etc/ssh/.ssh.db.tmp /etc/ssh/.ssh.db

echo "### ${user} ${exp}" >>/etc/ssh/.ssh.db

cat >/var/www/html/ssh-$user.txt <<END
────────────────────────────────────────────
INTERFLAZZ.MY.ID
AKUN SSH / OPENVPN
────────────────────────────────────────────

Username         : $user
Password         : $passwd

────────────────────────────────────────────

IP               : $ip
Host             : $domain
SlowDNS Host     : ${ns_domain}
Public Key       : ${pubkey}
Lokasi           : $city

────────────────────────────────────────────

OpenSSH Port     : 443, 80, 22
UdpSSH Port      : 1-65535
DNS Port         : 443, 53, 22
Dropbear Port    : 443, 109
Dropbear WS Port : 443, 109
SSH WS Port      : 80
SSH SSL WS Port  : 443
SSL/TLS Port     : 443
OVPN WS SSL Port : 443
OVPN SSL Port    : 443
OVPN TCP Port    : 443, 1194
OVPN UDP Port    : 2200
BadVPN UDP       : 7100, 7300, 7300

────────────────────────────────────────────

WSS Payload
GET wss://BUG.COM/ HTTP/1.1[crlf]Host: $domain[crlf]Upgrade: websocket[crlf][crlf]

────────────────────────────────────────────

OpenVPN
https://$domain:81/allovpn.zip

────────────────────────────────────────────

Simpan Akun
https://$domain:81/ssh-$user.txt

────────────────────────────────────────────

Batas IP         : $iplim
Masa Aktif       : $expired Hari
Kedaluwarsa      : $exp

────────────────────────────────────────────
INTERFLAZZ.MY.ID
────────────────────────────────────────────
END

clear -x

box_top
box_center "INTERFLAZZ.MY.ID" "$green"
box_center "AKUN SSH / OPENVPN" "$bold_white"
box_middle
box_left "Username       : $user" "$white"
box_left "Password       : $passwd" "$white"
box_middle
box_left "IP             : $ip" "$white"
box_left "Host           : $domain" "$white"
box_left "SlowDNS Host   : ${ns_domain}" "$white"
box_left "Lokasi         : $city" "$white"
box_left "Public Key     : ${pubkey}" "$white"
box_middle
box_left "OpenSSH        : 443, 80, 22" "$cyan"
box_left "UdpSSH         : 1-65535" "$cyan"
box_left "DNS            : 443, 53, 22" "$cyan"
box_left "Dropbear       : 443, 109" "$cyan"
box_left "Dropbear WS    : 443, 109" "$cyan"
box_left "SSH WS         : 80" "$cyan"
box_left "SSH SSL WS     : 443" "$cyan"
box_left "SSL/TLS        : 443" "$cyan"
box_left "OVPN WS SSL    : 443" "$cyan"
box_left "OVPN SSL       : 443" "$cyan"
box_left "OVPN TCP       : 443, 1194" "$cyan"
box_left "OVPN UDP       : 2200" "$cyan"
box_left "BadVPN UDP     : 7100, 7300, 7300" "$cyan"
box_middle
box_left "Batas IP       : $iplim" "$yellow"
box_left "Masa Aktif     : $expired Hari" "$yellow"
box_left "Kadaluarsa    : $exp" "$yellow"
box_middle
box_left "WSS Payload :" "$green"
box_left "GET wss://BUG.COM/ HTTP/1.1[crlf]Host: $domain[crlf]Upgrade: websocket[crlf][crlf]" "$white"
box_middle
box_left "Link OpenVPN :" "$green"
box_left "https://$domain:81/allovpn.zip" "$white"
box_middle
box_left "Link Simpan Akun :" "$green"
box_left "https://$domain:81/ssh-$user.txt" "$white"
box_middle
box_left "WhatsApp ~ https://wa.me/6285956131443" "$green"
box_left "Telegram ~ https://t.me/interflazz" "$cyan"
box_bottom

{
    echo "────────────────────────────────────────────"
    echo "INTERFLAZZ.MY.ID"
    echo "AKUN SSH / OPENVPN"
    echo "────────────────────────────────────────────"
    echo "Username         : $user"
    echo "Password         : $passwd"
    echo "────────────────────────────────────────────"
    echo "IP               : $ip"
    echo "Host             : $domain"
    echo "SlowDNS Host     : ${ns_domain}"
    echo "Lokasi           : $city"
    echo "Public Key       : ${pubkey}"
    echo "────────────────────────────────────────────"
    echo "OpenSSH Port     : 443, 80, 22"
    echo "UdpSSH Port      : 1-65535"
    echo "DNS Port         : 443, 53, 22"
    echo "Dropbear Port    : 443, 109"
    echo "Dropbear WS Port : 443, 109"
    echo "SSH WS Port      : 80"
    echo "SSH SSL WS Port  : 443"
    echo "SSL/TLS Port     : 443"
    echo "OVPN WS SSL Port : 443"
    echo "OVPN SSL Port    : 443"
    echo "OVPN TCP Port    : 443, 1194"
    echo "OVPN UDP Port    : 2200"
    echo "BadVPN UDP       : 7100, 7300, 7300"
    echo "────────────────────────────────────────────"
    echo "Batas IP         : $iplim"
    echo "Masa Aktif       : $expired Hari"
    echo "Kadaluarsa      : $exp"
    echo "────────────────────────────────────────────"
    echo "WSS Payload"
    echo "GET wss://BUG.COM/ HTTP/1.1[crlf]Host: $domain[crlf]Upgrade: websocket[crlf][crlf]"
    echo "────────────────────────────────────────────"
    echo "Link OpenVPN"
    echo "https://$domain:81/allovpn.zip"
    echo "────────────────────────────────────────────"
    echo "Link Simpan Akun"
    echo "https://$domain:81/ssh-$user.txt"
    echo "────────────────────────────────────────────"
    echo "WhatsApp ~ https://wa.me/6285956131443"
    echo "Telegram ~ https://t.me/interflazz"
    echo "────────────────────────────────────────────"
    echo ""
} >>"/etc/xray/log-createssh-${user}.log"