#!/bin/bash

#====================================================
#  CODIGO FUENTE:         iLLuminati Dev Team
#====================================================
#  SCRIPT: SSHPlus        Keygen
#  DATA ATUALIZACAO:      01-03-2022 
#  CONTATO TELEGRAM:      @AAAAAEXQOSyIpN2JZ0ehUQ
#  GRUPO TELEGRAM:        https://t.me/AAAAAEXQOSyIpN2JZ0ehUQ
#====================================================

# Colores
CYAN='\033[1;36m'
WHITE='\033[1;37m'
RED='\033[1;31m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
RESET='\033[0m'

# Cuál es mi dirección IP
fun_ip () {
MIP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
MIP2=$(wget -qO- ipv4.icanhazip.com)
[[ "$MIP" != "$MIP2" ]] && IP="$MIP2" || IP="$MIP"
echo -e "$IP" >/etc/IP
}

IP=$(cat /etc/IP)
MYINSTALL="SEU-IP-AKI"

# Crear el directorio /etc/sshplus-server/keyssh si no existe
[[ ! -d /etc/sshplus-server/keyssh ]] && mkdir -p /etc/sshplus-server/keyssh

# Crear el archivo /var/www/html/index.html si no existe
[ ! -e "/var/www/html/index.html" ] && touch "/var/www/html/index.html"

fun_gerkey() {
  echo -e "\n${CYAN}Informe o nome do Cliente:${WHITE} \c"
  read client
  [[ -z "$client" ]] && echo -e "${RED}Nome inválido ou vazio${RESET}" && sleep 2 && return

  echo -e "\n${CYAN}Quantos minutos essa key deve durar${YELLOW}"
  echo -e "\n${GREEN} m=Minutos${YELLOW}"
  echo -e "${GREEN} h=Horas${YELLOW}"
  echo -e "${GREEN} d=Dias${YELLOW}"
  echo -e "\n${RED}EX: ${GREEN}20m${WHITE}: \c" 
  read _temp
  [[ -z "$_temp" ]] && echo -e "${RED}Tempo inválido ou vazio${RESET}" && sleep 2 && return

  # Generar pasta1 y pasta2 (aleatorios de 4 dígitos)
  pasta1=$((1000 + RANDOM % 9000))
  pasta2=$((1000 + RANDOM % 9000))
  signus=":"

  # Detectar diretório correto baseado na versão do sistema
  OS_DIR="/var/www/html"
  grep -q 'VERSION_ID="14.04"' /etc/os-release && OS_DIR="/var/www"

  [ ! -e "$OS_DIR/index.html" ] && touch "$OS_DIR/index.html"

  mkdir -p "$OS_DIR/$pasta1/$pasta2"
  touch "/etc/sshplus-server/keyssh/$client"

  [ ! -e "$OS_DIR/$pasta1/index.html" ] && touch "$OS_DIR/$pasta1/index.html"
  [ ! -e "$OS_DIR/$pasta1/$pasta2/index.html" ] && touch "$OS_DIR/$pasta1/$pasta2/index.html"
  [ -d "$OS_DIR/$pasta1/$pasta2/list" ] && [ ! -e "$OS_DIR/$pasta1/$pasta2/list/index.html" ] && touch "$OS_DIR/$pasta1/$pasta2/list/index.html"

  cp /etc/sshplus-server/list "$OS_DIR/$pasta1/$pasta2/list"
  cp -a /etc/sshplus-server/_script_$/ "$OS_DIR/$pasta1"
  chmod -R 777 "$OS_DIR/$pasta1/crz" 2>/dev/null

  # Pantalla limpia
  clear
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "${YELLOW}         ⇱ SSHPLUS KEYGEN MANAGER ⇲${RESET}"
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "\n${WHITE}wget $MYINSTALL/scripts/Plus; chmod 777 Plus; ./Plus"
  echo -e "\n${GREEN}KEY: ${WHITE}${pasta1}${signus}${pasta2}${RESET}"
  echo -e "\n\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "${YELLOW}Obrigado pela sua contribuição! 👍\nDEV: CRAZY ✌️😎${RESET}\n"
  echo -e "${GREEN}Usuário: ${WHITE}$client"
  echo -e "${GREEN}Validade: ${WHITE}$_temp"
  echo -e "${GREEN}Versão: ${WHITE}$(cat /etc/sshplus-server/_script_$/crz/versao)"
  
# Script para eliminar la key después del tempo definido
cat << EOF > "/etc/sshplus-server/keyssh/$client"
#!/bin/bash
#Cliente: $client
#Key: $pasta1$signus$pasta2
#Validade: $_temp
sleep $_temp
rm -rf "$OS_DIR/$pasta1"
rm -f "/etc/sshplus-server/keyssh/$client"
exit
EOF

chmod +x "/etc/sshplus-server/keyssh/$client"
bash "/etc/sshplus-server/keyssh/$client" &
}

fun_gerkey_fixa() {
  echo -e "\n${CYAN}Informe o nome do Cliente:${WHITE} \c"
  read client
  [[ -z "$client" ]] && echo -e "${RED}Nome inválido ou vazio${RESET}" && sleep 2 && return

  # Gerar pasta1 e pasta2 (aleatórios de 4 dígitos)
  pasta1=$((1000 + RANDOM % 9000))
  pasta2=$((1000 + RANDOM % 9000))
  signus=":"

  # Detectar diretório correto baseado na versão do sistema
  OS_DIR="/var/www/html"
  grep -q 'VERSION_ID="14.04"' /etc/os-release && OS_DIR="/var/www"

  [ ! -e "$OS_DIR/index.html" ] && touch "$OS_DIR/index.html"

  # Criar estrutura de pastas e copiar arquivos
  mkdir -p "$OS_DIR/$pasta1/$pasta2"
  touch "/etc/sshplus-server/keyssh/$client"
  cp /etc/sshplus-server/list "$OS_DIR/$pasta1/$pasta2/list"
  cp -a /etc/sshplus-server/_script_$/ "$OS_DIR/$pasta1"
  chmod -R 777 "$OS_DIR/$pasta1/crz" 2>/dev/null

  [ ! -e "$OS_DIR/$pasta1/index.html" ] && touch "$OS_DIR/$pasta1/index.html"
  [ ! -e "$OS_DIR/$pasta1/$pasta2/index.html" ] && touch "$OS_DIR/$pasta1/$pasta2/index.html"
  [ -d "$OS_DIR/$pasta1/$pasta2/list" ] && [ ! -e "$OS_DIR/$pasta1/$pasta2/list/index.html" ] && touch "$OS_DIR/$pasta1/$pasta2/list/index.html"

  # Exibir dados na tela
  clear
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "${YELLOW}         ⇱ SSHPLUS KEYGEN FIXA ⇲${RESET}"
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "\n${WHITE}wget $MYINSTALL/scripts/Plus; chmod 777 Plus; ./Plus"
  echo -e "\n${GREEN}KEY: ${WHITE}${pasta1}${signus}${pasta2}${RESET}"
  echo -e "\n\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "${YELLOW}Obrigado pela sua contribuição! 👍\nDEV: CRAZY ✌️😎${RESET}\n"
  echo -e "${GREEN}Usuário: ${WHITE}$client"
  echo -e "${GREEN}Validade: ${WHITE}Permanente"
  echo -e "${GREEN}Versão: ${WHITE}$(cat /etc/sshplus-server/_script_$/crz/versao)"

# Script de remoção manual
cat << EOF > "/etc/sshplus-server/keyssh/$client"
#!/bin/bash
#Cliente: $client
#Key: $pasta1$signus$pasta2
#Validade: Permanente
rm -rf "$OS_DIR/$pasta1"
rm -f "/etc/sshplus-server/keyssh/$client"
exit
EOF

chmod +x "/etc/sshplus-server/keyssh/$client"
# A remoção é manual, então o script não é executado automaticamente
}

fun_gerkey_manual() {
  echo -e "\n${CYAN}Informe o nome do Cliente:${WHITE} \c"
  read client
  [[ -z "$client" ]] && echo -e "${RED}Nome inválido ou vazio${RESET}" && sleep 2 && return

  echo -e "\n${CYAN}Quantos minutos essa key deve durar${YELLOW}"
  echo -e "\n${GREEN} m=Minutos${YELLOW}"
  echo -e "${GREEN} h=Horas${YELLOW}"
  echo -e "${GREEN} d=Dias${YELLOW}"
  echo -e "\n${RED}EX: ${GREEN}20m${WHITE}: \c" 
  read _temp
  [[ -z "$_temp" ]] && echo -e "${RED}Tempo inválido ou vazio${RESET}" && sleep 2 && return

  echo -ne "\n\033[1;36mDigite a Key [ formato: xxxxx:xxxxx ]\033[1;33m"
  echo -e "\n\033[1;31mEX: \033[1;32m1427:0508\033[1;33m${WHITE}: \c"
  read -r key
  if [[ -z "$key" || "$key" != *:* ]]; then
    echo -e "\033[1;31mFormato de key inválido!\033[0m"
    sleep 2
    return 1
  fi

  key1p="${key%%:*}"
  key2p="${key##*:}"
  signus=":"

  [[ -z "$key1p" || -z "$key2p" ]] && {
    echo -e "\033[1;31mKey incompleta!\033[0m"
    return 1
  }

  # Detectar diretório correto baseado na versão do sistema
  OS_DIR="/var/www/html"
  grep -q 'VERSION_ID="14.04"' /etc/os-release && OS_DIR="/var/www"

  [ ! -e "$OS_DIR/index.html" ] && touch "$OS_DIR/index.html"

  mkdir -p "$OS_DIR/$key1p/$key2p"
  touch "/etc/sshplus-server/keyssh/$client"
  
  # Copiar arquivos
  cp /etc/sshplus-server/list "$OS_DIR/$key1p/$key2p/list" 2>/dev/null
  cp -a /etc/sshplus-server/_script_$/ "$OS_DIR/$key1p" 2>/dev/null
  chmod -R 777 "$OS_DIR/$key1p/crz" 2>/dev/null

  # Criar index.html de segurança
  [ ! -e "$OS_DIR/$key1p/index.html" ] && touch "$OS_DIR/$key1p/index.html"
  [ ! -e "$OS_DIR/$key1p/$key2p/index.html" ] && touch "$OS_DIR/$key1p/$key2p/index.html"
  [ -d "$OS_DIR/$key1p/$key2p/list" ] && [ ! -e "$OS_DIR/$key1p/$key2p/list/index.html" ] && touch "$OS_DIR/$key1p/$key2p/list/index.html"

  # Tela de saída
  clear
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "${YELLOW}         ⇱ SSHPLUS KEYGEN MANUAL ⇲${RESET}"
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "\n\033[1;37mwget $MYINSTALL/scripts/Plus; chmod 777 Plus; ./Plus\033[0m"
  echo -e "\n\033[1;32mKEY: \033[1;37m$key1p$signus$key2p\033[0m"
  echo -e "\n\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "\033[1;33mObrigado Pela Sua Contribuição! 👍\nDEV: CRAZY ✌️😎\033[0m\n"
  echo -e "\033[1;32mUsuário: \033[1;37m$client\033[0m"
  echo -e "\033[1;32mValidade: \033[1;37m$_temp\033[0m"
  echo -e "\033[1;32mVersão: \033[1;37m$(< /etc/sshplus-server/_script_$/crz/versao)\033[0m\n"

  # Script de autodestruição
  cat << EOF > "/etc/sshplus-server/keyssh/$client"
#!/bin/bash
#Cliente: $client
#Key: $key1p$signus$key2p
#Validade: $_temp
sleep $_temp
rm -rf "$OS_DIR/$key1p"
rm -f "/etc/sshplus-server/keyssh/$client"
exit
EOF

  chmod +x "/etc/sshplus-server/keyssh/$client"
  bash "/etc/sshplus-server/keyssh/$client" &
}

apache_status () {
  if [ ! -e /etc/PlusKeygen-Active ]; then
    # Detectar si Apache ya está instalado
    if ! dpkg -l | grep -qw apache2; then
      echo -e "\n\033[1;33m[!] Apache2 no encontrado. Instalando...\033[0m"
      apt-get update -y
      apt-get install apache2 -y
    else
      echo -e "\n\033[1;33m[!] Apache2 ya está instalado. Continuando...\033[0m"
    fi

    # Asegurar puerto 80 en la configuración
    if grep -q "Listen 81" /etc/apache2/ports.conf; then
      sed -i "s;Listen 81;Listen 80;g" /etc/apache2/ports.conf
    fi

    # Reiniciar servicio apache2
    systemctl restart apache2

    # Crear archivo de estado
    echo "#KEYGEN SSHPPLUS ACTIVE" > /etc/PlusKeygen-Active

    echo -e "\033[1;32m[+] Apache2 activo en puerto 80\033[0m"
  else
    # Solo detener Apache, no eliminar ni desinstalar
    echo -e "\n\033[1;33m[!] Deteniendo Apache2...\033[0m"
    systemctl stop apache2

    #apt-get purge apache2 -y
    #apt-get autoremove -y

    rm -f /etc/PlusKeygen-Active

    echo -e "\033[1;32m[+] Apache2 detenido, Keygen en pausa\033[0m"
  fi
}

fun_keyson () {
  clear
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "\033[1;32mKEYS ONLINE         \033[0m"
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo ""

  if [ ! "$(ls -A /etc/sshplus-server/keyssh 2>/dev/null)" ]; then
    echo -e " \033[1;31mNenhuma Key Online!\033[0m"
    echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
    return
  fi

  printf "\033[1;34m%-20s %-25s %-20s %-10s\033[0m\n" "Cliente" "Key" "Validade" "Script"
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"

  total=0
  ativas=0
  permanentes=0

  for arq in /etc/sshplus-server/keyssh/*; do
    [ -f "$arq" ] || continue

    nome=$(basename "$arq")
    key=$(grep -oP "^#Key: \K.*" "$arq" || echo "N/A")
    cliente=$(grep -oP "^#Cliente: \K.*" "$arq" || echo "$nome")
    validade=$(grep -oP "^#Validade: \K.*" "$arq" || echo "Permanente")

    if pgrep -f "/etc/sshplus-server/keyssh/$nome" > /dev/null 2>&1; then
      status="Ativa"
      ((ativas++))
    else
      status="Permanente"
      ((permanentes++))
    fi

    ((total++))
    printf "%-20s %-25s %-20s %-10s\n" "$cliente" "$key" "$validade" "$status"
  done

  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo -e "\033[1;32mTotal de Keys: $total | Ativas: $ativas | Permanentes: $permanentes\033[0m"
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo ""
}

fun_delkey () {
  local continuar="s"
  local key_dir="/etc/sshplus-server/keyssh"

  while [[ "$continuar" =~ ^[sS]$ ]]; do
    clear
    echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
    echo -e "         \033[1;32mREMOVER KEY ONLINE         \033[0m"
    echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
    echo ""

    local total=0
    local ativas=0
    local permanentes=0
    declare -a keys=()

    # Mostrar lista
    printf "\033[1;34m%-4s %-20s %-25s %-20s %-10s\033[0m\n" "No." "Cliente" "Key" "Validade" "Status"
    echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"

    for arq in "$key_dir"/*; do
      [ -f "$arq" ] || continue
      nome=$(basename "$arq")
      key=$(grep -oP "^#Key: \K.*" "$arq" || echo "N/A")
      cliente=$(grep -oP "^#Cliente: \K.*" "$arq" || echo "$nome")
      validade=$(grep -oP "^#Validade: \K.*" "$arq" || echo "Permanente")

      if pgrep -f "$arq" > /dev/null 2>&1; then
        status="Ativa"
        ((ativas++))
      else
        status="Permanente"
        ((permanentes++))
      fi

      ((total++))
      keys+=("$nome")
      printf "%-4s %-20s %-25s %-20s %-10s\n" "$total" "$cliente" "$key" "$validade" "$status"
    done

    if (( total == 0 )); then
      echo -e " \033[1;31mNenhuma Key Encontrada!\033[0m"
      break
    fi

    echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
    echo ""

    # BLOQUE MEJORADO DE INPUT
    while true; do
      echo -ne "\033[1;36mInforme o número ou nome da key para remover (\033[1;33m0 para voltar ao menu\033[0;36m): \033[0m"; read input

      if [[ -z "$input" ]]; then
        echo -e "\n\033[1;33mEntrada vazia. Retornando ao menu...\033[0m"
        break 2
      fi

      if [[ "$input" == "0" ]]; then
        echo -e "\n\033[1;34mRetornando ao menu inicial...\033[0m"
        break 2
      fi

      if [[ "$input" =~ ^[0-9]+$ ]] && (( input >= 1 && input <= total )); then
        client="${keys[$((input-1))]}"
        break
      elif [[ -e "$key_dir/$input" ]]; then
        client="$input"
        break
      else
        echo -e "\n\033[1;31mEntrada inválida. Tente novamente.\033[0m"
      fi
    done

    keypath="$key_dir/$client"

    if [ -e "$keypath" ]; then
      echo -ne "\n\033[1;33mTem certeza que deseja remover a key '$client'? (s/n): \033[0m"; read confirma
      if [[ "$confirma" =~ ^[sS]$ ]]; then
        pid=$(pgrep -f "$keypath")
        [[ -n "$pid" ]] && kill "$pid" &>/dev/null

        keyinfo=$(grep -oP "^#Key: \K.*" "$keypath")
        if [[ "$keyinfo" == *:* ]]; then
          key1="${keyinfo%%:*}"
          OS_DIR="/var/www/html"
          grep -q 'VERSION_ID="14.04"' /etc/os-release && OS_DIR="/var/www"
          dir_to_delete="$OS_DIR/$key1"
          [ -d "$dir_to_delete" ] && rm -rf "$dir_to_delete"
        fi

        rm -f "$keypath"
        echo -e "\n\033[1;32mKey '$client' removida com sucesso.\033[0m"
      else
        echo -e "\n\033[1;33mRemoção cancelada.\033[0m"
      fi
    else
      echo -e "\n\033[1;31mKey '$client' não encontrada.\033[0m"
    fi

    echo ""
    echo -ne "\033[1;36mDeseja remover outra key? (s/n): \033[0m"; read continuar
  done

  echo -e "\n\033[1;34mResumo final:\033[0m\n"
  echo -e " Total de keys: \033[1;32m$total\033[0m"
  echo -e " Keys Ativas: \033[1;32m$ativas\033[0m"
  echo -e " Keys Permanentes: \033[1;32m$permanentes\033[0m"
  echo ""
  echo -e "\033[0m\e[34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
  echo ""
}

actualiza_plus () {
  echo -ne "\n\033[1;33mDesea actualizar el script? (s/n): \033[0m"; read resp
  if [[ "$resp" =~ ^[sS]$ ]]; then
    echo -e "\033[1;32mActualizando...\033[0m"
    rm -f "$HOME"/instgerador.sh* > /dev/null 2>&1
    wget https://raw.githubusercontent.com/AAAAAEXQOSyIpN2JZ0ehUQ/SSHPLUS-MANAGER-FREE/refs/heads/master/Proyectos/SSHPlus_Keygen/instgerador.sh
    chmod +x instgerador.sh*
    ./instgerador.sh*
  else
    echo -e "\033[1;33mActualización cancelada.\033[0m"
  fi
}

remover_plus () {
  echo -ne "\n\033[1;33mDesea remover el script y sus archivos relacionados? (s/n): \033[0m"; read resp
  if [[ "$resp" =~ ^[sS]$ ]]; then
    echo -e "\033[1;31mRemoviendo...\033[0m"
    rm -rf /etc/sshplus-server/list /etc/sshplus-server/index.html /etc/PlusKeygen-Active /etc/sshplus-server/_script_$ /etc/sshplus-server/keyssh > /dev/null 2>&1
    rm -rf /bin/keyssh /bin/key /bin/otimizar > /dev/null 2>&1
    rm -rf /var/www/html/Index.php /var/www/html/script /var/www/html/scripts > /dev/null 2>&1
    service apache2 restart > /dev/null 2>&1
    echo -e "\033[1;32mRemoción completada.\033[0m"
  else
    echo -e "\033[1;33mRemoción cancelada.\033[0m"
  fi
}

# Función de menú principal
menu () {

while true; do

total_keys=$(ls -1 /etc/sshplus-server/keyssh 2>/dev/null | wc -l)
sts=$([ "$total_keys" -gt 0 ] && echo -e "\033[1;32m$total_keys Keys Online\033[0m" || echo -e "\033[1;31mNinguna Key Online!\033[0m")
_ram=$(printf ' %-9s' "$(free -h | grep -i mem | awk {'print $2'})")
_usor=$(printf '%-8s' "$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')")
_usop=$(printf '%-1s' "$(top -bn1 | awk '/Cpu/ { cpu = "" 100 - $8 "%" }; END { print cpu }')")
_core=$(printf '%-1s' "$(grep -c cpu[0-9] /proc/stat)")

mine_port () {
local portasVAR=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" |grep -v "COMMAND" | grep "LISTEN")
local NOREPEAT
local reQ
local Port
while read port; do
reQ=$(echo ${port}|awk '{print $1}')
Port=$(echo {$port} | awk '{print $9}' | awk -F ":" '{print $2}')
[[ $(echo -e $NOREPEAT|grep -w "$Port") ]] && continue
NOREPEAT+="$Port\n"
case ${reQ} in
apache|apache2)
[[ -z $APC ]] && echo -e "\033[0;34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m" && local APC="\033[1;37mApache:\033[1;32m "
APC+="$Port ";;
esac
done <<< "${portasVAR}"
[[ ! -z $APC ]] && echo -e $APC
}

clear
echo -e "\033[0;34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
echo -e "\E[41;1;37m                ⇱ SSHPLUS KEYGEN ⇲                \E[0m"
echo -e "\033[0;34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
echo -e "\033[1;37mVersión:\033[1;32m 2.0 BETA"
echo -e "\033[1;37mEstado:\033[0m $sts"
echo -e "\033[1;37mRAM:\033[1;32m $_usor \033[1;37m| Núcleo(s):\033[1;32m $_core \033[1;37m| CPU:\033[1;32m $_usop\033[0m"
mine_port
echo -e "\033[0;34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
echo ""

echo -e "\033[1;31m[\033[1;36m01\033[1;31m] \033[1;37m• \033[1;33mGenerar Key Aleatoria - \033[1;37mCon Fecha"
echo -e "\033[1;31m[\033[1;36m02\033[1;31m] \033[1;37m• \033[1;33mGenerar Key Aleatoria Fija - \033[1;37mSin Fecha"
echo -e "\033[1;31m[\033[1;36m03\033[1;31m] \033[1;37m• \033[1;33mGenerar Key Manual - \033[1;37mCon Fecha"

if ss -tln | grep -q ':80'; then
  echo -e "\033[1;31m[\033[1;36m04\033[1;31m] \033[1;37m• \033[1;32m(RODANDO)\033[1;33m Parar Keygen"
else
  echo -e "\033[1;31m[\033[1;36m04\033[1;31m] \033[1;37m• \033[1;31m(PARADO)\033[1;33m Iniciar Keygen"
fi

echo -e "\033[1;31m[\033[1;36m05\033[1;31m] \033[1;37m• \033[1;33mVer Keys Online"
echo -e "\033[1;31m[\033[1;36m06\033[1;31m] \033[1;37m• \033[1;33mRemover Key"
echo -e "\033[1;31m[\033[1;36m07\033[1;31m] \033[1;37m• \033[1;32mActualizar Script"
echo -e "\033[1;31m[\033[1;36m08\033[1;31m] \033[1;37m• \033[1;31mDesinstalar Keygen"
echo -e "\033[1;31m[\033[1;36m00\033[1;31m] \033[1;37m• \033[1;33mSalir \033[1;32m<\033[1;33m<\033[1;31m<"
echo ""
echo -e "\033[0;34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
echo ""
echo -ne "\033[1;32m¿QUÉ DESEA HACER \033[1;33m?\033[1;31m?\033[1;37m : "; read x

case "$x" in
   1 | 01)
   fun_gerkey
   echo -ne "\n\033[1;31mENTER \033[1;33mpara retornar ao \033[1;32mMENU!\033[0m"; read
   ;;
   2 | 02)
   fun_gerkey_fixa
   echo -ne "\n\033[1;31mENTER \033[1;33mpara retornar ao \033[1;32mMENU!\033[0m"; read
   ;;
   3 | 03)
   fun_gerkey_manual
   echo -ne "\n\033[1;31mENTER \033[1;33mpara retornar ao \033[1;32mMENU!\033[0m"; read
   ;;
   4 | 04)
   apache_status
   echo -ne "\n\033[1;31mENTER \033[1;33mpara retornar ao \033[1;32mMENU!\033[0m"; read
   ;;
   5 | 05)
   clear
   fun_keyson
   echo -ne "\n\033[1;31mENTER \033[1;33mpara retornar ao \033[1;32mMENU!\033[0m"; read
   ;;
   6 | 06)
   clear
   fun_delkey
   echo -ne "\n\033[1;31mENTER \033[1;33mpara retornar ao \033[1;32mMENU!\033[0m"; read
   ;;      
   7 | 07)
   actualiza_plus
   exit;
   ;;
   8 | 08)
   remover_plus
   exit;
   ;;
   0 | 00)
   echo -e "\033[1;31mSalindo...\033[0m"
   sleep 1
   clear
   exit;
   ;;
   *)
   echo -e "\n\033[1;31mOpcao invalida !\033[0m"
   sleep 1
esac
done
}
menu
#fim