#!/bin/bash
ipv4=$(curl --connect-timeout 5 -s ipv4.icanhazip.com)
sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config
po=$(cat /etc/ssh/sshd_config | grep "^Port")
port=$(echo "$po" | sed "s/Port //g")
ipv6=$(curl --connect-timeout 5 -s ipv6.icanhazip.com)
panelversion=$(cat /var/www/html/p/versionp.txt)
panelporttmp=$(sudo lsof -i -P -n | grep -i LISTEN | grep apache2 | awk '{if(!seen[$9]++)print $9;exit}')
panelport=$(echo $panelporttmp | sed 's/[^0-9]*//g' )
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
CYAN="\e[36m"
ENDCOLOR="\e[0m"
adminuser=$(mysql -s -N -e  "use ShaHaN;select adminuser from setting where id='1';")
adminpass=$(mysql -s -N -e  "use ShaHaN;select adminpassword from setting where id='1';")
usercount=$(mysql -s -N -e  "use ShaHaN;select COUNT(*) from users;")
activeuser=$(mysql -s -N -e  "use ShaHaN;select COUNT(*) from users where enable='true';")
deactiveuser=$(mysql -s -N -e  "use ShaHaN;select COUNT(*) from users where enable not like 'true';")
online=$(sudo lsof -i  -n | grep " 4u" | grep -v root | grep ESTABLISHED | grep -v "videocall" | wc -l)

cpuarch=$(uname -i)
linuxversion=$(lsb_release -sd)

uninstall_shahan(){
    printf "Are you sure uninstall Shahan? (y/n) "
    printf "\n"
    read -p "(Default: n):" answer
    [ -z ${answer} ] && answer="n"
    if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then
        rm -fr /var/www/html/*
        mysql -e "drop database ShaHaN;"
        clear
        echo "Shahan uninstall successfully!"
    else
        clear
        echo
        echo "uninstall cancelled, nothing to do..."
        echo
    fi
}

uninstall_openvpn(){
systemctl stop ovpmd
apt remove ovpm -y 
rm -fr /var/db/ovpm
clear
echo "OpenVpn uninstall successfully!"
}

uninstall_tuic(){
systemctl stop tuic
rm -fr /var/www/config.json
rm -fr /etc/systemd/system/tuic.service
rm -fr /root/tuic
clear
echo "TUIC uninstall successfully!"
}
uninstall_shadowsocks(){
    printf "Are you sure uninstall Shadowsocks? (y/n) "
    printf "\n"
    read -p "(Default: n):" answer
    [ -z ${answer} ] && answer="n"
    if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then
        ps -ef | grep -v grep | grep -i "ssserver" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            /etc/init.d/shadowsocks stop
        fi
        # delete config file
        rm -f /var/www/shadowsocks.json
        rm -f /var/run/shadowsocks.pid
        rm -f /etc/init.d/shadowsocks
        rm -f /var/log/shadowsocks.log
        if [ -f /usr/local/shadowsocks_install.log ]; then
            cat /usr/local/shadowsocks_install.log | xargs rm -rf
        fi
        clear
        echo "Shadowsocks uninstall success!"
    else
        clear
        echo
        echo "uninstall cancelled, nothing to do..."
        echo
    fi
}

if ! [ $(id -u) = 0 ]; then
   echo -e "${RED}Plese run the command with root privilages!${ENDCOLOR}"
   exit 1
fi

clear
echo -e "${YELLOW}********** ShaHaN SSH PANEL **********"
echo -e "${RED}-----------------------------------"
echo -e "${YELLOW}-------- Server Details -----------"
echo -e "${GREEN}Linux Version : $linuxversion ( $cpuarch )"
echo -e "${GREEN}IPv4 : $ipv4"
echo -e "Ipv6 : $ipv6"
echo -e "SSH port : $port"
echo -e "Panel Version : $panelversion"
echo -e "Panel Link : http://$ipv4:$panelport/p"
echo -e "Panel User : $adminuser"
echo -e "Panel Password : $adminpass"
echo -e " "
echo -e "${RED}-----------------------------------\n ${ENDCOLOR}"
echo -e "  1 ${CYAN}Instal & Update Panel ${ENDCOLOR}"
echo -e "  2 ${CYAN}Install SSH-Calls ${ENDCOLOR}"
echo -e "  3 ${CYAN}Install SSL For Domain ${ENDCOLOR}"
echo -e "  4 ${CYAN}Change SSH Port ${ENDCOLOR}"
echo -e "  5 ${CYAN}Block Iran IP ( Outgoing ) ${ENDCOLOR}"
echo -e "  6 ${CYAN}Change Panel Port ${ENDCOLOR}"
echo -e "  8 ${CYAN}Backup Users ${ENDCOLOR}"
echo -e "  9 ${CYAN}Remove ShaHaN ${ENDCOLOR}"
echo -e " "
echo -e "  10 ${GREEN}Install Cisco Anyconnect ${ENDCOLOR}"
echo -e "  11 ${GREEN}Install DropBear ${ENDCOLOR}"
echo -e "  12 ${GREEN}Install TUIC ${ENDCOLOR}"
echo -e "  13 ${GREEN}Install Openvpn ${ENDCOLOR}"
echo -e "  14 ${GREEN}Install Fake Website ${ENDCOLOR}"
echo -e "  15 ${GREEN}Install BBR ${ENDCOLOR}"
echo -e "  16 ${GREEN}Install ShadowSocks ${ENDCOLOR}"
echo -e "  17 ${YELLOW}Install WireGuard ${ENDCOLOR}"
echo -e "  18 ${GREEN}Install Singbox ${ENDCOLOR}"
echo -e "  19 ${GREEN}Install SSH-TLS ${ENDCOLOR}"
echo -e " "
echo -e "  20 ${RED}Remove CisCo Anyconnect${ENDCOLOR}"
echo -e "  21 ${RED}Remove SSH TLS${ENDCOLOR}"
echo -e "  22 ${RED}Remove Apache And PHP${ENDCOLOR}"
echo -e "  23 ${RED}Disable IPV6${ENDCOLOR}"
echo -e "  24 ${RED}Remove SSL${ENDCOLOR}"
echo -e "  25 ${RED}Remove OpenVpn${ENDCOLOR}"
echo -e "  26 ${RED}Remove Singbox${ENDCOLOR}"
echo -e "  27 ${RED}Remove Shadowsocks${ENDCOLOR}"
echo -e "  28 ${RED}Remove TUIC${ENDCOLOR}"
echo -e "  29 ${RED}Remove WireGuard${ENDCOLOR}"
echo -e " "
echo -e "  30 ${CYAN}CPU Cooler${ENDCOLOR}"
echo -e "  31 ${CYAN}Enable AutoUpdate Panel${ENDCOLOR}"
echo -e "  32 ${CYAN}Disable AutoUpdate Panel${ENDCOLOR}"
echo -e "  33 ${CYAN}Check Shahan Panel${ENDCOLOR}"
echo -e "  34 ${GREEN}Enable Ipv6${ENDCOLOR}"
echo -e " "
echo -e "  99 ${GREEN}Update Singbox Configs${ENDCOLOR}"
echo -e "  0)Exit"

echo -ne "${GREEN}\nSelect Operation : ${ENDCOLOR}" ;read n

case $n in
  1) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/master/install.sh --ipv4) ;;
  2) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/master/ssh-calls.sh --ipv4) ;;
  3) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/master/ssl.sh --ipv4) ;;
  4) echo  -ne "${YELLOW}Please enter new port: ";read new_port;sudo sed -i "s@Port $port@Port $new_port@g" /etc/ssh/sshd_config;systemctl restart sshd;echo "Port Changed Successfully${ENDCOLOR}";;
  5) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/master/b.sh --ipv4) ;;
  6) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/master/screenshot/chp.sh --ipv4) ;;
  8) mysqldump -u root ShaHaN > /root/bckup-new.sql || echo "New Backup Created : /root/bckup-new.sql" ;;
  9) uninstall_shahan;;
  10) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ocserv-Ubuntu/main/c.sh --ipv4) ;;
  11) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/drop.sh --ipv4) ;;
  12) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/t.sh --ipv4) ;;
  13) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/open.sh --ipv4) ;;
  14) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/fakewebsite.sh --ipv4) ;;
  15) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/bbr.sh --ipv4) ;;
  16) /root/shadow.sh.x ;;
  17) /root/wg.sh.x ;;
  18) /root/signbox.sh.x ;;
  19) /root/tls.sh.x ;;
  20) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ocserv-Ubuntu/main/rc.sh --ipv4) ;;
  21) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/ssh-tls-remove.sh --ipv4) ;;
  22) apt purge php* php8* php7* apache2 -y && apt autoremove -y ;;
  23) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/disableipv6.sh --ipv4) ;;
  24) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/removessl.sh --ipv4) ;;
  25) uninstall_openvpn ;;
  26) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/removesignbox.sh --ipv4) ;;
  27) uninstall_shadowsocks ;;
  28) uninstall_tuic ;;
  29) /root/wg.sh.x remove ;;
  30) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/cpu.sh --ipv4) ;;
  31) (crontab -l ; echo '4 4 * * * /bin/bash -c "sudo bash /root/updateshahan.sh"' ) | crontab -  ;;
  32) crontab -l | grep -v '/root/updateshahan.sh'  | crontab  - ;;
  33) shahancheck ;;
  34) bash <(curl -Ls https://raw.githubusercontent.com/HamedAp/Ssh-User-management/main/enableipv6.sh) ;;

  99) /root/updatesignbox.sh.x ;;
  0) clear;exit;;
  *) echo -e "${RED}\nInvalid Option. Press Enter to return back to the menu${ENDCOLOR}" ; read && shahan ;;
esac
