#!/bin/bash
# VPS Toolkit v1.2 - All-in-one English menu with Hy2 installer
set -e

# ===== Color Definitions =====
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
RED="\033[0;31m"
RESET="\033[0m"

function show_menu() {
  echo -e "${BLUE}======== VPS Toolkit Menu ========${RESET}"
  echo -e "${YELLOW}1)${RESET} Run Speedtest"
  echo -e "${YELLOW}2)${RESET} Install X-UI Panel"
  echo -e "${YELLOW}3)${RESET} Install 3x-UI Panel"
  echo -e "${YELLOW}4)${RESET} Check Media Unlock"
  echo -e "${YELLOW}5)${RESET} Exit"
  echo -e "${YELLOW}6)${RESET} Install Hy2 (Built-in Script)"
  echo -ne "\nSelect an option [1-6]: "
}

function run_speedtest() {
  echo -e "${GREEN}>> Running Speedtest...${RESET}"
  bash <(wget -qO- https://down.vpsaff.net/linux/speedtest/superbench.sh) --speed
}

function install_xui() {
  echo -e "${GREEN}>> Installing X-UI Panel...${RESET}"
  bash <(curl -Ls https://raw.githubusercontent.com/FranzKafkaYu/x-ui/master/install.sh)
}

function install_3xui() {
  echo -e "${GREEN}>> Installing 3x-UI Panel...${RESET}"
  bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh)
}

function run_media_unlock() {
  echo -e "${GREEN}>> Checking media unlock status...${RESET}"
  bash <(curl -L -s check.unlock.media)
}

function install_hy2() {
  # Default config
  DEFAULT_PORT=443
  DEFAULT_PASSWORD="YXRPASSWORD"

  echo -e "${YELLOW}Install Hy2 - Press Enter to use default values${RESET}"

  read -p "Enter port [default: ${DEFAULT_PORT}]: " PORT
  PORT=${PORT:-$DEFAULT_PORT}

  read -p "Enter password [default: ${DEFAULT_PASSWORD}]: " PASSWORD
  PASSWORD=${PASSWORD:-$DEFAULT_PASSWORD}

  echo -e "${GREEN}>> Installing Hysteria2...${RESET}"
  bash <(curl -fsSL https://get.hy2.sh/)
  systemctl enable hysteria-server.service

  echo -e "${GREEN}>> Generating self-signed TLS certificate...${RESET}"
  openssl req -x509 -nodes -newkey ec:<(openssl ecparam -name prime256v1) \
    -keyout /etc/hysteria/server.key \
    -out /etc/hysteria/server.crt \
    -subj "/CN=bing.com" -days 36500

  chown hysteria /etc/hysteria/server.key
  chown hysteria /etc/hysteria/server.crt

  echo -e "${GREEN}>> Writing configuration...${RESET}"
  cat << EOF > /etc/hysteria/config.yaml
listen: :$PORT
tls:
  cert: /etc/hysteria/server.crt
  key: /etc/hysteria/server.key
auth:
  type: password
  password: $PASSWORD
masquerade:
  type: proxy
  proxy:
    url: https://bing.com
    rewriteHost: true
EOF

  systemctl restart hysteria-server.service
  systemctl enable hysteria-server.service

  echo -e "\n${GREEN}Hy2 installation completed.${RESET}"
  echo -e "${YELLOW}Port: $PORT"
  echo -e "Password: $PASSWORD"
  echo -e "Fake site: https://bing.com${RESET}"
}

# ===== Main Loop =====
while true; do
  show_menu
  read -r choice
  echo ""
  case "$choice" in
    1) run_speedtest ;;
    2) install_xui ;;
    3) install_3xui ;;
    4) run_media_unlock ;;
    5)
      echo -e "${RED}Exiting script. Goodbye.${RESET}"
      exit 0
      ;;
    6) install_hy2 ;;
    *)
      echo -e "${RED}Invalid option. Please choose 1-6.${RESET}"
      ;;
  esac
  echo -e "\n${YELLOW}Press any key to return to menu...${RESET}"
  read -n 1 -s
  clear
done
