#! /bin/bash
# Afrihost - complete restart of web service for emergency situations.
 
# cpanel check
if ! [ -f "/usr/local/cpanel/cpanel" ]; then
       exit
fi

txtgrn='\e[0;32m'       # green
txtund=$(tput sgr 0 1)  # Underline
txtbld=$(tput bold)     # Bold
txtrst='\e[0m'          # Text reset
bldgrn='\e[1;32m'       # Green
bldblu='\e[1;34m'       # Blue
bldwht='\e[1;37m'       # White
bldred='\e[1;31m'       # Red
bldylw='\e[1;33m'       # Yellow
bldmag='\e[1;35m'       # Magenta
bldcyn='\e[1;36m'       # Cyan
bldgray='\e[1;30m'      # Gray

frontSP="${bldgray}-==[${txtrst}"
midSP="${bldgray}] ::${txtrst}"
backSP="${bldgray}]==-${txtrst}"

litespeed_check () {
echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ checking if server licensed for litespeed /root/.config/.litespeed_ls ]"${txtrst}
if egrep -l  ".*-.*-.*-" /root/.config/.litespeed_ls 2>/dev/null || egrep -l  ".*-.*-.*-" /usr/local/lsws/conf/serial.no  2>/dev/null ; then 
 echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ /root/.config/.litespeed_ls exists - litespeed license being paid for, thus applying litespeed ]"${txtrst}
 service nginx stop  2>/dev/null
 killall -9 lsphp ;  service lsws restart ;  touch /usr/local/lsws/admin/tmp/.lsphp_restart.txt
 if nlist| egrep  'http|ngin' ; then 
  whmapi1 set_tweaksetting key=apache_port value=0.0.0.0:80
  whmapi1 set_tweaksetting key=apache_ssl_port value=0.0.0.0:443
  /usr/local/afribin/configs/cpanel_nginx_disable
  ulimit -n 120000
  /usr/local/lsws/admin/misc/cp_switch_ws.sh lsws
  killall -9 lsphp ;  service lsws restart ;  touch /usr/local/lsws/admin/tmp/.lsphp_restart.txt
  nlist| egrep  'http|ngin|lite'
 fi
 echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ litespeed re-applied and restarted ]"${txtrst}
 echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_litespeed ::::" >> /var/log/ips.log
 exit   
fi
}

default_check_run() {
litespeed_check
# Check if both nginx and apache running as normal and restart
sed -i '/libphp.*so/d' /etc/apache2/conf.d/php.conf
 echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ Running default basic restart of web services ]"${txtrst}
if netstat -ntulp |  egrep  'http'| grep  ":8080" &&  netstat -ntulp | egrep  'http' | grep  ":8443" &&
 netstat -ntulp |  egrep  'nginx'| grep  ":80" && netstat -ntulp |  egrep  'nginx'| grep  ":443"  ; then
 echo "== apache and nginx running as expected [ Performing Basic Restart ]=="
 for i in `ipcs -s | grep nobody | awk '{print $2}'` ; do  ipcrm sem $i >/dev/null ; done 
 killall -9 nginx httpd ; service httpd start ; service nginx start
 echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_web_services ::::" | tee -a /var/log/ips.log
 nlist| egrep  'http|ngin|lite'
 exit
fi 

if netstat -ntulp |  egrep  'http' &&  netstat -ntulp | egrep  'http' | grep  ":443"  ; then
 echo "[INFO] - seems like apache only is running and both ports 80 and 443 up"
 nlist| egrep  'http|ngin|lite'
 echo "[INFO] - running a basic restart"
 /scripts/rebuildhttpdconf
 if tail -n100 /usr/local/apache/logs/error_log | grep /etc/apache2/conf.d/modsec2.cpanel.conf ; then
  rm -fv  /etc/apache2/conf.d/modsec2.cpanel.conf
  /scripts/rebuildhttpdconf
 fi
 service httpd restart 
  nlist| egrep  'http|ngin|lite'
fi

# Checks if nginx running on port 80 and 443 but apache not listening on proxy ports - quick-fix
if netstat -ntulp |  egrep  'nginx'| grep  ":80" && netstat -ntulp |  egrep  'nginx'| grep  ":443"  ; then
  if ! netstat -ntulp |  egrep  'http'| grep  ":8080" ||  netstat -ntulp | egrep  'http' | grep  ":8443" ; then
   echo "nginx running on port 80 and 443 but apache isn't running on 8080 8443"
   killall -9 nginx httpd 
   whmapi1 set_tweaksetting key=apache_port value=0.0.0.0:80
   whmapi1 set_tweaksetting key=apache_ssl_port value=0.0.0.0:443
   /scripts/rebuildhttpdconf 
   if tail -n100 /usr/local/apache/logs/error_log | grep /etc/apache2/conf.d/modsec2.cpanel.conf ; then
        rm -fv  /etc/apache2/conf.d/modsec2.cpanel.conf
        /scripts/rebuildhttpdconf
   fi
   service httpd restart
#   if timeout 5  dig @localhost google.com +short  | ipgrep | grep [0-9] >/dev/null  ; then echo -e  "nameserver 127.0.0.1\nnameserver 197.242.155.155\nnameserver 197.242.144.144"  > /etc/resolv.conf  ; else /scripts/setupnameserver --force bind  ; if timeout 5  dig @localhost google.com +short  | ipgrep | grep [0-9] >/dev/null  ; then echo -e  "nameserver 127.0.0.1\nnameserver 197.242.155.155\nnameserver 197.242.144.144"  > /etc/resolv.conf ; fi  ; fi; /usr/local/afribin/configs/engintron_install.sh ; sh  /usr/local/afribin/configs/cpanel_nginx_custom_rules_generate.sh
    echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_web_services ::::" | tee -a /var/log/ips.log
    nlist| egrep  'http|ngin|lite'
   exit
  fi
fi

# Checks if http running on proxyport but nginx not running.
if  netstat -ntulp |  egrep  'http'| grep  ":8080" &&  netstat -ntulp | egrep  'http' | grep  ":8443" ; then
 if ! netstat -ntulp |  egrep  'nginx'| grep  ":80" || ! netstat -ntulp |  egrep  'nginx'| grep  ":443" ; then
  echo "http running on proxy but nginx not on port 80/443"
   if  nginx  -t  > /dev/null 2>&1   ; then 
   sed -i   's#\.\*\.#\.#' /etc/nginx/ccnf.d/default_https.conf
   rm -fv /etc/nginx/custom_rules
   sh  /usr/local/afribin/configs/cpanel_nginx_custom_rules_generate.sh
   if ! [ -f "/etc/nginx/custom_rules" ]; then
        touch /etc/nginx/custom_rules
   fi
   killall -9 nginx httpd ; service nginx start ; service httpd start
   echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_web_services ::::" | tee -a /var/log/ips.log
   nlist| egrep  'http|ngin|lite'
   exit
   else 
    echo "Running force for Apache..."
    service nginx stop 2>/dev/null
    service httpd stop 2>/dev/null
    /usr/local/afribin/configs/cpanel_nginx_disable
    echo "[INFO] - apache set to proxy ports but nginx bust on server - setting to apache only"
    whmapi1 set_tweaksetting key=apache_port value=0.0.0.0:80
    whmapi1 set_tweaksetting key=apache_ssl_port value=0.0.0.0:443
echo "quick restart" ; killall -9 nginx ; for run in {1..60}; do killall -9 nginx httpd php-fpm php-cgi >/dev/null ; done ;/scripts/rebuildhttpdconf ;  /scripts/restartsrv_httpd ; /scripts/restartsrv_apache_php_fpm
  nlist| egrep  'http|ngin|lite'
 fi
 fi
fi
}

force_all() {
litespeed_check
sed -i '/libphp.*so/d' /etc/apache2/conf.d/php.conf
 echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ Forcefull Web service restart ]"${txtrst}
 killall -9 nginx ; { for run in {1..60}; do killall -9 nginx httpd php-fpm php-cgi > /dev/null ; done >/dev/null ; 
if ipcs -s | grep nobody   | wc -l | awk '($1 > 200 )' | grep -E '[[:alnum:]]+' ; then  ipcs -s | grep nobody   | perl -e 'while (<STDIN>) { @a=split(/\s+/); print`ipcrm sem $a[1]`}' ;  fi
/scripts/rebuildhttpdconf ;  /scripts/restartsrv_httpd ; /scripts/restartsrv_apache_php_fpm ; service httpd start ; restartfpm ; service nginx start ; }  
default_check_run
}


force_nginx() {
  litespeed_check
  sed -i '/libphp.*so/d' /etc/apache2/conf.d/php.conf
  nginx  -t  > /dev/null 2>&1   ; [ $? -eq 1 ] &&
   sed -i   's#\.\*\.#\.#' /etc/nginx/conf.d/default_https.conf
   service httpd stop
   service nginx stop
   whmapi1 set_tweaksetting key=apache_port value=0.0.0.0:8080
   whmapi1 set_tweaksetting key=apache_ssl_port value=0.0.0.0:8443
   rm -fv /etc/nginx/custom_rules
   sh  /usr/local/afribin/configs/cpanel_nginx_custom_rules_generate.sh
   if ! [ -f "/etc/nginx/custom_rules" ]; then
        touch /etc/nginx/custom_rules
   fi
   SHELLSERVERIP=$(ip addr | egrep 'lo|global'|awk '{print $2}'|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'|grep -v 127\.0\.0|head -1); 
   /usr/local/afribin/configs/secops/imunify/build_i360.remoteip.conf ; 
   /usr/local/afribin/configs/cpanel_build_remoteIP_conf 
   /scripts/rebuildhttpdconf 
   ipset add i360.ipv4.whitelist.host_ips $SHELLSERVERIP 
   killall -9 nginx httpd ; service httpd start ; service nginx start ; sleep 5 ; service nginx stop ; service nginx start
   echo "$(date +"%Y"-"%m-%d-%R") :::: $0 restarted_web_services ::::" | tee -a /var/log/ips.log
   echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ nginx - basic reset complete ]"${txtrst}
   echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ nginx should be listening on port 80|443, apache 8443|8080 ]"${txtrst}
   nlist| egrep  'http|ngin|lite' 
   exit
}

force_apache() {
    sed -i '/libphp.*so/d' /etc/apache2/conf.d/php.conf
    litespeed_check
    echo "Running force for Apache..."
    service nginx stop 2>/dev/null 
    service httpd stop 2>/dev/null 
    /usr/local/afribin/configs/cpanel_nginx_disable 
    whmapi1 set_tweaksetting key=apache_port value=0.0.0.0:80
    whmapi1 set_tweaksetting key=apache_ssl_port value=0.0.0.0:443
    /scripts/rebuildhttpdconf
    if tail -n100 /usr/local/apache/logs/error_log | grep /etc/apache2/conf.d/modsec2.cpanel.conf ; then
     rm -fv  /etc/apache2/conf.d/modsec2.cpanel.conf
     /scripts/rebuildhttpdconf
    fi
echo "quick restart" ; killall -9 nginx ; for run in {1..60}; do killall -9 nginx httpd php-fpm php-cgi >/dev/null ; done ;/scripts/rebuildhttpdconf ;  /scripts/restartsrv_httpd ; /scripts/restartsrv_apache_php_fpm 
}

web_status () {
 nlist| egrep  'http|ngin|lite'

if netstat -ntulp |  egrep  'nginx'| grep  ":80" && netstat -ntulp |  egrep  'nginx'| grep  ":443"  ; then
  if ! netstat -ntulp |  egrep  'http'| grep  ":8080" ||  netstat -ntulp | egrep  'http' | grep  ":8443" ; then
 echo -e "\n[[ Problem  ]] Nginx running but apache not running on port proxy ports\n"
 echo -e " To Resolve run $0 --nginx"
 fi
fi
 
}

show_help() {
    echo "Usage: $0 [OPTION]"
    echo
    echo "Options:"
    echo "  --force         Run force for all components"
    echo "  --forcenginx    Run force specifically for NGINX"
    echo "  --forceapache   Run force specifically for Apache"
    echo "  --help          Show this help message"
    echo
    echo "If no option is provided, the default check will be executed."
}


show_help() {
    cat <<EOF
Usage: $0 [OPTION]

Options:
  --force         Run full forced check
  --set_nginx     Force check for NGINX only
  --set_apache    Force check for Apache only
  --help          Show this help message

If no option is given, the script runs the default check.
EOF
}

case "$1" in
    --force)
         echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ Forcefull Web service restart ]"${txtrst}
        force_all >/dev/null 2>&1
         echo -e " ${frontSP}${bldgrn} INFO ${midSP} [ Done ]\n"${txtrst}
         nlist| egrep  'http|ngin|lite'
        ;;
    --set_nginx)
        force_nginx
        web_status
        ;;
    --set_apache)
        force_apache
        web_status
        ;;
    --status)
        web_status
        ;;
    --help)
        show_help
        ;;
    "")
        default_check_run
        ;;
    *)
        echo "Unknown option: $1"
        show_help
        exit 1
        ;;
esac
