#!/bin/bash
clear
ipsaya=$(wget -qO- ipinfo.io/ip)
data_server=$(curl -v --insecure --silent https://google.com/ 2>&1 | grep Date | sed -e 's/< Date: //')
date_list=$(date +"%Y-%m-%d" -d "$data_server")
data_ip="https://raw.githubusercontent.com/RozTun/permission/main/ip"
ISP=$(cat /etc/xray/isp)
CITY=$(cat /etc/xray/city)
PUB=$(cat /etc/slowdns/server.pub)
NS=$(cat /etc/xray/dns)
domain=$(cat /etc/xray/domain)
checking_sc() {
  useexp=$(wget -qO- $data_ip | grep $ipsaya | awk '{print $3}')
  if [[ $date_list < $useexp ]]; then
    echo -ne
  else
    echo -e "${OR}────────────────────────────────────────────${NC}"
    echo -e "\033[42m          HUNTERTUNNEL AUTOSCRIPT          ${NC}"
    echo -e "${OR}────────────────────────────────────────────${NC}"
    echo -e ""
    echo -e "            ${RED}PERMISSION DENIED !${NC}"
    echo -e "   \033[0;33mYour VPS${NC} $ipsaya \033[0;33mHas been Banned${NC}"
    echo -e "     \033[0;33mBuy access permissions for scripts${NC}"
    echo -e "             \033[0;33mContact Admin :${NC}"
    echo -e "      ${Green}WhatsApp${NC} wa.me/6285759375937"
    echo -e "         \033[0;36mTelegram${NC} t.me/RozTun"
    echo -e "${OR}────────────────────────────────────────────${NC}"
    exit
  fi
}
checking_sc

        # 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"

# Function to print rainbow text
        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 display a loading bar
        fun_bar() {
            CMD[0]="$1"
            CMD[1]="$2"
            (
                [[ -e $HOME/fim ]] && rm $HOME/fim
                ${CMD[0]} -y >/dev/null 2>&1
                ${CMD[1]} -y >/dev/null 2>&1
                touch $HOME/fim
            ) >/dev/null 2>&1 &
            tput civis
            echo -ne "  ${orange}Please wait while loading ${neutral}- ${orange}["
            while true; do
                for ((i = 0; i < 18; i++)); do
                    echo -ne "${green}#"
                    sleep 0.1s
                done
                [[ -e $HOME/fim ]] && rm $HOME/fim && break
                echo -e "${orange}]"
                sleep 1s
                tput cuu1
                tput dl1
                echo -ne "  ${orange}Please wait while loading ${neutral}- ${orange}["
            done
            echo -e "${orange}]${neutral} -${green} OK !${neutral}"
            tput cnorm
        }

      restart_services() {
    echo -e "\033[0;33mReloading system daemon...\033[0m"
    fun_bar "systemctl daemon-reload"

    services=(
        "nginx" "xray" "rc-local" "client" "server" "dropbear"
        "ws" "openvpn" "cron" "haproxy" "netfilter-persistent"
        "squid" "badvpn1" "badvpn2" "badvpn3" "udp-custom"
    )

    for service in "${services[@]}"; do
        echo -e "\033[0;33mRestarting service: \033[0;32m$service\033[0m"
        fun_bar "systemctl restart $service"
    done

    clear
    echo -e "\033[0;33m─────────────────────────────────────────\033[0m"
    echo -e "   \033[0;32m .:::. RESTART SERVER SERVICES .:::. \033[0m"
    echo -e "\033[0;33m─────────────────────────────────────────\033[0m"
    echo -e ""
}
 
        # Function to check bandwidth
        check_bw() {
            clear
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e " ${green} .:::. VNSTAT BANDWIDTH STATISTICS .:::.${neutral}"
            echo -e "${orange}─────────────────────────────────────────${neutral}"

            # Check if vnstat is installed
            if ! command -v vnstat &>/dev/null; then
                echo -e "${red}Error: vnstat is not installed.${neutral}"
                echo -e "Please install vnstat first."
                read -n 1 -s -r -p "Press any key to return to the main menu"
                return
            fi

            # Check if there are any network interfaces available
            if [ -z "$(vnstat --iflist)" ]; then
                echo -e "${red}Error: No network interfaces available for vnstat.${neutral}"
                read -n 1 -s -r -p "Press any key to return to the main menu"
                return
            fi

            vnstat
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "     ${green}1.${neutral} Show bandwidth details"
            echo -e "     ${green}2.${neutral} Return to main menu"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            print_rainbow "───────────────────────────────────────"

            echo -e ""
            read -p " Select an option (1-2): " choice

            case $choice in
            1)
                echo -e "\nSelect period:"
                echo -e "     ${green}1.${neutral} Today"
                echo -e "     ${green}2.${neutral} Yesterday"
                echo -e "     ${green}3.${neutral} This month"
                echo -e "     ${green}4.${neutral} This year"
                read -p " Select period (1-4): " period

                case $period in
                1) vnstat -d ;;
                2) vnstat -d 1 ;;
                3) vnstat -m ;;
                4) vnstat -y ;;
                *) echo -e "   ${red}Invalid choice${neutral}" ;;
                esac
                ;;
            2) return ;;
            *) echo -e "   ${red}Invalid choice${neutral}" ;;
            esac

            read -n 1 -s -r -p "Press any key to continue"
            check_bw
        }

        # Function to display port information
        info_port() {
            clear
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "${green}    .:::. PORT INFORMATION .:::.${neutral}"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "  ${green}• SSH                : ${neutral}22, 443, 80"
            echo -e "  ${green}• Dropbear           : ${neutral}443, 109, 143"
            echo -e "  ${green}• SSH Websocket      : ${neutral}80, 443"
            echo -e "  ${green}• OpenVPN            : ${neutral}443, 1194, 2200"
            echo -e "  ${green}• Nginx              : ${neutral}80, 81, 443"
            echo -e "  ${green}• Haproxy            : ${neutral}80, 443"
            echo -e "  ${green}• DNS                : ${neutral}53, 443"
            echo -e "  ${green}• XRAY Vmess         : ${neutral}80, 443"
            echo -e "  ${green}• XRAY Vless         : ${neutral}80, 443"
            echo -e "  ${green}• Trojan             : ${neutral}443"
            echo -e "  ${green}• Shadowsocks        : ${neutral}443"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "  ${yellow}• Time Zone          : ${neutral}Asia/Jakarta (GMT +7)"
            if [[ -f /etc/cron.d/daily_reboot ]]; then
                reboot_time=$(grep -oP '^\d+\s+\d+' /etc/cron.d/daily_reboot | head -1 | awk '{printf "%02d:%02d", $2, $1}')
                echo -e "  ${yellow}• Automatic Restart  : ${neutral}${reboot_time} GMT +7"
            else
                echo -e "  ${yellow}• Automatic Restart  : ${neutral}Not Set"
            fi
            echo -e "  ${yellow}• Auto Delete Expired: ${neutral}Yes"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e ""
            read -n 1 -s -r -p "Press any key to return to the menu"
        }
        
change_domain() {
    clear
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo -e "${green}      .:::. CHANGE VPS DOMAIN .:::.      ${neutral}"
    echo -e "${orange}─────────────────────────────────────────${neutral}"

    echo -e "${yellow}Sebelum Ganti Domain, Harap Pointing Dulu IP VPS Kalian.${neutral}"
    sleep 1

    # Input domain baru
    read -rp "Input domain Anda: " -e pp
    if [ -z "$pp" ]; then
        echo -e "\n\033[0;31mTidak ada domain yang diinput.\033[0m"
        return 1
    fi

    # Simpan domain ke file konfigurasi
    echo "$pp" > /etc/xray/domain
    echo "$pp" > /root/domain
    echo "IP=$pp" > /var/lib/biji/ipvps.conf
    echo -e "\033[0;32mDomain berhasil diganti menjadi: $pp\033[0m"

    # Pasang SSL
    echo -e "\033[0;32mMemasang SSL untuk domain: $pp\033[0m"
    sleep 1
    rm -rf /etc/xray/xray.key /etc/xray/xray.crt

    # Hentikan web server di port 80
    STOPWEBSERVER=$(lsof -i:80 | awk 'NR==2 {print $1}')
    systemctl stop "$STOPWEBSERVER"
    systemctl stop nginx
    systemctl stop haproxy

    # Instalasi ACME.sh
    rm -rf /root/.acme.sh
    mkdir -p /root/.acme.sh
    curl https://acme-install.netlify.app/acme.sh -o /root/.acme.sh/acme.sh
    chmod +x /root/.acme.sh/acme.sh
    /root/.acme.sh/acme.sh --upgrade --auto-upgrade
    /root/.acme.sh/acme.sh --set-default-ca --server letsencrypt

    # Generate sertifikat SSL
    /root/.acme.sh/acme.sh --issue -d "$pp" --standalone -k ec-256
    ~/.acme.sh/acme.sh --installcert -d "$pp" \
        --fullchainpath /etc/xray/xray.crt \
        --keypath /etc/xray/xray.key --ecc

    # Set izin dan restart layanan
    chmod 777 /etc/xray/xray.key
    systemctl restart nginx
    systemctl restart xray
    systemctl restart haproxy
    echo -e "\033[0;32mSSL berhasil dipasang untuk domain: $pp\033[0m"
    read -n 1 -s -r -p "Press any key to return to the menu"
    supremo
}


services=(
    "nginx" "xray" "rc-local" "dropbear"
    "ws" "openvpn" "cron" "haproxy" 
    "squid" "badvpn1" "badvpn2" "badvpn3" "udp-custom"
)

running() {
    # Fungsi untuk memeriksa status layanan
    check_status() {
        status=$(systemctl is-active --quiet $1 && echo "active" || echo "inactive")
        if [ "$status" = "active" ]; then
            echo -e "\033[0;32mGOOD\033[0m"
        else
            echo -e "\033[0;31mBAD\033[0m"
        fi
    }

    # Deklarasikan status layanan
    for service in "${services[@]}"; do
        declare "${service}"="$(check_status $service)"
    done

    # Fungsi untuk mencetak informasi layanan
    print_info() {
        printf "   \033[1;33m%-22s\033[0m : %-15s\n" "$1" "$2"
    }

    # Header tampilan
    clear
    
        echo -e "${orange}─────────────────────────────────────────${neutral}"
        echo -e "${green}       .:::. SYSTEM INFORMATION .:::.        ${neutral}"
        echo -e "${orange}─────────────────────────────────────────${neutral}"

   service_info=(
        "Nginx" "${nginx}"
        "Xray Core" "${xray}"
        "Dropbear" "${dropbear}"
        "Websocket" "${ws}"
        "OpenVPN" "${openvpn}"
        "Cron" "${cron}"
        "HAProxy" "${haproxy}"
        "Squid" "${squid}"
        "BadVPN 1" "${badvpn1}"
        "BadVPN 2" "${badvpn2}"
        "BadVPN 3" "${badvpn3}"
    )

    # Cetak informasi layanan
    for ((i = 0; i < ${#service_info[@]}; i += 2)); do
        print_info "${service_info[i]}" "${service_info[i + 1]}"
    done

    # Footer tampilan
   echo -e "${orange}─────────────────────────────────────────${neutral}"
    print_rainbow "─────────────────────────────────────────"
}
         
auto_backup() {
    local status=$(grep -qE "^SHELL" /etc/cron.d/backup && echo -e "\033[32m[ON]\033[0m" || echo -e "\033[31m[OFF]\033[0m")
    clear
    echo -e "${orange}┌──────────────────────────────────────┐${neutral}"
    echo -e "${orange}│${green}         AUTO BACKUP SETTINGS         ${orange}│${neutral}"
    echo -e "${orange}└──────────────────────────────────────┘${neutral}"
    echo -e "${orange}┌──────────────────────────────────────┐${neutral}"
    echo -e "${orange}│${neutral} AutoBackup Status: $status"
    echo -e "${orange}│${neutral} 1) Every 6 Hours"
    echo -e "${orange}│${neutral} 2) Every 1 Day"
    echo -e "${orange}│${neutral} 3) Every 1 Week"
    echo -e "${orange}│${neutral} 4) Every 1 Month"
    echo -e "${orange}│${neutral} 5) Disable AutoBackup"
    echo -e "${orange}│${neutral} x) Exit"
    echo -e "${orange}└──────────────────────────────────────┘${neutral}"
    
    read -p "Choose an option [1-5 or x]: " pilihan
    case $pilihan in
        1) echo -e "SHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n0 */6 * * * root /usr/bin/clone backup" > /etc/cron.d/backup
        clear
        print_rainbow "────────────────────────────────────────"
        echo -e "${orange}│${neutral}"
        echo -e "${orange}│${neutral}      AutoBackup: Every 6 Hours"
        echo -e "${orange}│${neutral}"
        print_rainbow "────────────────────────────────────────"
        service cron restart > /dev/null 2>&1
        ;;
        2) echo -e "SHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n0 0 * * * root /usr/bin/clone backup" > /etc/cron.d/backup
        clear
        print_rainbow "────────────────────────────────────────"
        echo -e "${orange}│${neutral}"
        echo -e "${orange}│${neutral}      AutoBackup: Every 1 Day"
        echo -e "${orange}│${neutral}"
        print_rainbow "────────────────────────────────────────"
        service cron restart > /dev/null 2>&1
        ;;
        3) echo -e "SHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n0 0 * * 7 root /usr/bin/clone backup" > /etc/cron.d/backup
        clear
        print_rainbow "────────────────────────────────────────"
        echo -e "${orange}│${neutral}"
        echo -e "${orange}│${neutral}      AutoBackup: Every 1 Week"
        echo -e "${orange}│${neutral}"
        print_rainbow "────────────────────────────────────────"
        service cron restart > /dev/null 2>&1
        ;;
        4) echo -e "SHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n0 0 1 * * root /usr/bin/clone backup" > /etc/cron.d/backup
        clear
        print_rainbow "────────────────────────────────────────"
        echo -e "${orange}│${neutral}"
        echo -e "${orange}│${neutral}      AutoBackup: Every 1 Month"
        echo -e "${orange}│${neutral}"
        print_rainbow "────────────────────────────────────────"
        service cron restart > /dev/null 2>&1
        ;;
        5) 
            if [ ! -f /etc/cron.d/backup ]; then
                clear
                print_rainbow "────────────────────────────────────────"
                echo -e "${orange}│${neutral}"
                echo -e "${orange}│${neutral} AutoBackup Already ${red}Disabled${neutral}"
                echo -e "${orange}│${neutral}"
                print_rainbow "────────────────────────────────────────"
                return
            fi
            rm -f /etc/cron.d/backup 
            clear
            print_rainbow "────────────────────────────────────────"
            echo -e "${orange}│${neutral}"
            echo -e "${orange}│${neutral}      AutoBackup ${red}Disabled${neutral}"
            echo -e "${orange}│${neutral}"
            print_rainbow "────────────────────────────────────────"
            service cron restart > /dev/null 2>&1
            return
            ;;
        x|X) clear; return ;;
        *) echo "Invalid choice. Please try again."Bu; return ;;
    esac
}
    
        setting_reboot_jam_reboot() {
            clear
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "${green}     .:::. SETTING AUTO REBOOT .:::.        ${neutral}"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e ""
            read -p "Enter Autoreboot (24-hour, ex: 02 for 2 AM): " reboot_hour
            
            # Exception if the input hour is empty or invalid
            if [[ -z "$reboot_hour" || ! "$reboot_hour" =~ ^[0-9]{2}$ || "$reboot_hour" -lt 0 || "$reboot_hour" -gt 23 ]]; then
                echo -e "${red}Invalid hour input. Please enter an hour between 00 and 23.${neutral}"
                return
            fi
            
            # Menambahkan cron job untuk auto reboot
            echo -e "SHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n0 $reboot_hour * * * root /sbin/reboot" > /etc/cron.d/daily_reboot
            systemctl restart cron
            clear
            print_rainbow "────────────────────────────────────────"
            echo -e "${orange}│${neutral}"
            echo -e "${orange}│${neutral}      AutoReboot Every: Hour $reboot_hour"
            echo -e "${orange}│${neutral}"
            print_rainbow "────────────────────────────────────────"
            read -n 1 -s -r -p "Press any key to return to the menu"
        }

clear
echo -e "${orange}─────────────────────────────────────────${neutral}"
echo -e "${green}     .:::. SYSTEM SETTINGS .:::.        ${neutral}"
echo -e "${orange}─────────────────────────────────────────${neutral}"
echo -e "    ${green}01.${neutral} Setup bot telegram"
echo -e "    ${green}02.${neutral} Check running system"
echo -e "    ${green}03.${neutral} Check bandwidth usage"
echo -e "    ${green}04.${neutral} Auto backup server data"
echo -e "    ${green}05.${neutral} Restore Direct link"
echo -e "    ${green}06.${neutral} Port information"
echo -e "    ${green}07.${neutral} Restart all services"
echo -e "    ${green}08.${neutral} Change server domain"
echo -e "    ${green}09.${neutral} Change server banner"
echo -e "    ${green}10.${neutral} Load server system"
echo -e "    ${green}11.${neutral} Setting Auto Reboot"
echo -e "    ${green}12.${neutral} Restart/Reboot server"
echo -e "    ${green}xX.${neutral} Exit system settings"
echo -e "${orange}─────────────────────────────────────────${neutral}"
print_rainbow "─────────────────────────────────────────"
read -p " Choose an option or 'x' to exit: " menu
case $menu in
    1) holand ;;
    2) running ;;
    3) check_bw ;;
    4) auto_backup ;;
    5) retorika ;;
    6) info_port ;;
    7) restart_services ;;
    8) change_domain ;;
    9) nano /etc/issue.net ; clear ;;
    10) gotop ;;
    11) setting_reboot_jam_reboot ;;
    12) reboot ;;
    x | X)
        echo -e ""
        echo -e "   ${green}Thank you for using our service.${neutral}"
        exit 0
        ;;
    *)
        echo -e "   ${red}Invalid choice. Please try again.${neutral}"
        sleep 2
        ;;
esac
   