#!/bin/bash

ip=$(curl -sS ipv4.icanhazip.com)
data=$(curl -s https://raw.githubusercontent.com/RozTun/permission/main/ip)
key=$(echo "$data" | grep "$ip" | awk '{print $2}')

# Nonaktifkan IPv6
sysctl -w net.ipv6.conf.all.disable_ipv6=1 >/dev/null 2>&1
sysctl -w net.ipv6.conf.default.disable_ipv6=1 >/dev/null 2>&1

# Periksa apakah script dijalankan sebagai root
if [ "${EUID}" -ne 0 ]; then
  echo "Script ini harus dijalankan dengan akses root."
  exit 1
fi

# Periksa jenis virtualisasi
if [ "$(systemd-detect-virt)" == "openvz" ]; then
  echo "Jenis virtualisasi OpenVZ tidak didukung."
  exit 1
fi

# Konfigurasi hostname dan IP
localip=$(hostname -I | cut -d' ' -f1)
hst=$(hostname)
dart=$(grep -w "$hst" /etc/hosts | awk '{print $2}')
if [[ "$hst" != "$dart" ]]; then
  echo "$localip $(hostname)" >> /etc/hosts
fi

# Membuat direktori yang diperlukan
mkdir -p /etc/xray
mkdir -p /var/lib/ >/dev/null 2>&1
echo "IP=" > /var/lib/ipvps.conf
clear

# Fungsi untuk meminta domain pengguna
function set_domain() {
  clear

  orange="\e[38;5;130m"
  green="\e[38;5;82m"
  blue="\e[38;5;39m"
  yellow="\e[38;5;226m"
  neutral="\e[0m"
  reset="\e[0m"
  
  # Fungsi untuk mencetak garis dengan warna pelangi
  print_rainbow() {
    local text="$1"
    local length=${#text}
    local start_color=(255 255 0) # yellow
    local mid_color=(0 255 0)     # green
    local end_color=(255 255 0)   # yellow

    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) / 100 + mid_color[0] * factor / 100))
            g=$((start_color[1] * (100 - factor) / 100 + mid_color[1] * factor / 100))
            b=$((start_color[2] * (100 - factor) / 100 + mid_color[2] * factor / 100))
        else
            local factor=$(((progress - 50) * 2))
            r=$((mid_color[0] * (100 - factor) / 100 + end_color[0] * factor / 100))
            g=$((mid_color[1] * (100 - factor) / 100 + end_color[1] * factor / 100))
            b=$((mid_color[2] * (100 - factor) / 100 + end_color[2] * factor / 100))
        fi

        printf "\e[38;2;%d;%d;%dm%s" "$r" "$g" "$b" "${text:$i:1}"
    done
    echo -e "$reset"
}

  echo -e "${orange}─────────────────────────────────────────${neutral}" 
  echo -e "     ${green}.::::. HunterTunnel  .::::.${neutral}"
  echo -e "${orange}─────────────────────────────────────────${neutral}"
  echo -e "${blue}The Best Place for Secure and Fast Connection!${neutral}"
  echo -e "${yellow}Enter your domain to start the installer:${neutral}"
  echo -e ""
  print_rainbow "────────────────────────────────────"

  until [[ $dnss =~ ^[a-zA-Z0-9_.-]+$ ]]; do
    read -rp "Enter your domain : " -e dnss
  done

  # Konfigurasi domain
  rm -rf /etc/v2ray
  mkdir -p /etc/xray /etc/v2ray
  touch /etc/xray/domain /etc/v2ray/domain

  echo "$dnss" > /root/domain
  echo "$dnss" > /etc/xray/domain
  echo "$dnss" > /etc/v2ray/domain
  echo "IP=$dnss" > /var/lib/ipvps.conf

  clear
  echo -e "\033[32;1mDomain berhasil diatur menjadi: $dnss\033[0m"
  sleep 2
}

function Pasang() {
  cd
  rm -f tools.sh
  wget https://nay-tra.my.id/ht/tools.sh -O tools.sh || { echo "Gagal mengunduh tools.sh"; exit 1; }
  sed -i 's/\r//' tools.sh
  chmod +x tools.sh
  bash tools.sh || { echo "Eksekusi tools.sh gagal"; exit 1; }
  clear
  ln -fs /usr/share/zoneinfo/Asia/Jakarta /etc/localtime
  apt install git curl python -y >/dev/null 2>&1
}

# Menjalankan fungsi CEKIP terlebih dahulu
clear
CEKIP

# Setelah izin IP terverifikasi, jalankan fungsi set_domain dan Pasang
set_domain
Pasang

# Fungsi Progress Bar
fun_bar() {
    CMD="$1"
    (
        [[ -e /tmp/fim ]] && rm -f /tmp/fim
        eval "$CMD" >/dev/null 2>&1
        touch /tmp/fim
    ) &
    
    tput civis
    echo -ne "  \033[0;33mProses Install \033[1;37m- \033[0;33m["
    while true; do
        for ((i = 0; i < 18; i++)); do
            echo -ne "\033[0;32m#"
            sleep 0.1
        done
        if [[ -e /tmp/fim ]]; then
            rm -f /tmp/fim
            break
        fi
        echo -e "\033[0;33m]"
        sleep 1
        tput cuu1
        tput dl1
        echo -ne "  \033[0;33mProses Install \033[1;37m- \033[0;33m["
    done
    echo -e "\033[0;33m]\033[1;37m -\033[1;32m Done.!\033[1;37m"
    tput cnorm
}

# Fungsi Instalasi
res() {
    wget "$1" -O "$2" && sed -i 's/\r//' "$2" && chmod +x "$2" && ./"$2"
    clear
}

# Fungsi Instalasi untuk Debian
function setup_debian() {
    echo -e "\033[1;32mMenginstal SSH dan OpenVPN...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/ssh-vpn.sh" "ssh-vpn.sh"'

    echo -e "\033[1;32mMenginstal Xray...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/ins-xray.sh" "ins-xray.sh"'

    echo -e "\033[1;32mMenginstal WebSocket SSH...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/sshws/insshws.sh" "insshws.sh"'

    echo -e "\033[1;32mMenginstal Limit Quota...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/set-br.sh" "set-br.sh"'

    echo -e "\033[1;32mMenginstal OHP...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/sshws/ohp.sh" "ohp.sh"'

    echo -e "\033[1;32mMengunduh Menu System...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/menu/update.sh" "update.sh"'
    
    echo -e "\033[1;32mMengunduh Menu Tambahan...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/road.sh" "road.sh"'

    echo -e "\033[1;32mMenginstal SlowDNS...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/slowdns/installsl.sh" "installsl.sh"'

    echo -e "\033[1;32mRestart All Service...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/end.sh" "end.sh"'
}

# Fungsi Instalasi untuk Ubuntu
function setup_ubuntu() {
    echo -e "\033[1;32mMenginstal SSH dan OpenVPN...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/ssh-vpn.sh" "ssh-vpn.sh"'

    echo -e "\033[1;32mMenginstal Xray...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/ins-xray.sh" "ins-xray.sh"'

    echo -e "\033[1;32mMenginstal WebSocket SSH...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/sshws/insshws.sh" "insshws.sh"'

    echo -e "\033[1;32mMenginstal Limit Quota...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/set-br.sh" "set-br.sh"'

    echo -e "\033[1;32mMenginstal OHP...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/sshws/ohp.sh" "ohp.sh"'

    echo -e "\033[1;32mMengunduh Menu System...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/menu/update.sh" "update.sh"'
    
    echo -e "\033[1;32mMengunduh Menu Tambahan...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/road.sh" "road.sh"'

    echo -e "\033[1;32mMenginstal SlowDNS...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/slowdns/installsl.sh" "installsl.sh"'

    echo -e "\033[1;32mRestart All Service...\033[0m"
    fun_bar 'res "https://nay-tra.my.id/ht/install/end.sh" "end.sh"'
}

# Deteksi Sistem Operasi
OS_NAME=$(grep -w ID /etc/os-release | head -n1 | sed 's/ID=//g' | tr -d '"')
OS_PRETTY_NAME=$(grep -w PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '"')

if [[ "$OS_NAME" == "ubuntu" ]]; then
    print_rainbow "─────────────────────────────────────────"
    echo -e "\033[1;32m  Sistem Operasi: \033[1;36m$OS_PRETTY_NAME\033[0m"
    print_rainbow "─────────────────────────────────────────"
    setup_ubuntu
elif [[ "$OS_NAME" == "debian" ]]; then
    print_rainbow "─────────────────────────────────────────"
    echo -e "\033[1;32m  Sistem Operasi: \033[1;36m$OS_PRETTY_NAME\033[0m"
    print_rainbow "─────────────────────────────────────────"
    setup_debian
else
    print_rainbow "─────────────────────────────────────────"
    echo -e "\033[1;31m  Sistem Operasi tidak didukung: \033[1;36m$OS_PRETTY_NAME\033[0m"
    print_rainbow "─────────────────────────────────────────"
    exit 1
fi

# Konfigurasi sistem
NEW_FILE_MAX=65535
SYSCTL_CONF="/etc/sysctl.conf"
NF_CONNTRACK_MAX="net.netfilter.nf_conntrack_max=262144"
NF_CONNTRACK_TIMEOUT="net.netfilter.nf_conntrack_tcp_timeout_time_wait=30"

# Update fs.file-max jika belum sesuai
if ! grep -q "^fs.file-max" "$SYSCTL_CONF"; then
    echo "fs.file-max = $NEW_FILE_MAX" >> "$SYSCTL_CONF"
else
    sed -i "s/^fs.file-max.*/fs.file-max = $NEW_FILE_MAX/" "$SYSCTL_CONF"
fi

# Update konfigurasi netfilter
if ! grep -q "^net.netfilter.nf_conntrack_max" "$SYSCTL_CONF"; then
    echo "$NF_CONNTRACK_MAX" >> "$SYSCTL_CONF"
fi

if ! grep -q "^net.netfilter.nf_conntrack_tcp_timeout_time_wait" "$SYSCTL_CONF"; then
    echo "$NF_CONNTRACK_TIMEOUT" >> "$SYSCTL_CONF"
fi

# Terapkan perubahan
sysctl -p >/dev/null 2>&1

# Konfigurasi profil root
cat > /root/.profile << END
if [ "\$BASH" ]; then
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
fi
mesg n || true
clear
ht dashboard
END
chmod 644 /root/.profile

# Membersihkan file sementara
rm -f /root/{tools.sh,setup.sh,pointing.sh,ssh-vpn.sh,ins-xray.sh,insshws.sh,set-br.sh,ohp.sh,update.sh,installsl.sh,road.sh,end.sh,setrsyslog.sh}

# Simpan informasi versi
echo "00" > /home/daily_reboot

# Perbarui informasi IP dan lokasi
curl -sS ifconfig.me > /etc/myipvps
curl -s ipinfo.io/city?token=75082b4831f909 > /etc/xray/city
curl -s ipinfo.io/org?token=75082b4831f909 | cut -d " " -f 2-10 > /etc/xray/isp

if [ -f /usr/bin/user ]; then
    rm -f /usr/bin/user
fi
echo "$key" >>/usr/bin/user

clear
echo -e "${orange}─────────────────────────────────────────${neutral}"
echo -e "${green}         INSTALLASI SELESAI            ${neutral}"
print_rainbow "─────────────────────────────────────────"
echo -e "${green}  Selamat! Proses instalasi selesai${neutral}"
print_rainbow "─────────────────────────────────────────"
echo ""

# Pertanyaan reboot
read -p "Do you want to reboot now? (y/n): " answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
    reboot
else
    exit 0
fi