#!/bin/bash

## licenses4host.com
# powered by  licenses4host.com
# licenses4host.com
## licenses4host.com
RED='\033[0;31m' # Red
Green='\033[0;32m'        # Green
Cyan='\033[0;36m'        # Cyan
Yellow='\033[0;33m'        # Yellow
NC='\033[0m' # No Color
key='whmsonic'
api="https://api.licenses4host.com/api/getinfo?key=$key"
api_license="https://api.licenses4host.com/api/license?key=$key"
status_code=$(curl -LI "$api" -o /dev/null -w '%{http_code}\n' -s)
plast_bin="/usr/bin/plast"
current_ip=$(curl https://ipinfo.io/ip)
domain_show="https://licenses4host.com"
brand_show="licenses4host.com"
hostname_show=$( hostname )
status=false
server_range=0
firewall_stop='false'
key_cmd='gb'
action="$1"
if [ "$action" == '--help' ] || [ "$action" == '-h' ] ;then
    echo ""
    echo ""
    printf "${Green}Command Lines${NC}\n"
    printf "${Green}--uninstall  : uninstall license  ${NC}\n"
    printf "${Green}--enable     : enable    license ${NC}\n"
    printf "${Green}--disable    : disable   license ${NC}\n"
    exit 0
fi
echo ""
if ! [ -x "$(command -v python)" ]; then

    printf "${RED}python is not installed${NC}\n"
    echo ""
    exit 1
fi
if [ ! -d "/usr/local/cpanel/whostmgr/docroot/whmsonic" ]; then
    printf "${RED}WhmSonic is not detected${NC}\n"
    printf "${RED}You need to install WhmSonic${NC}\n"
    echo ""
    exit 1
fi


if [ ! -f "$plast_bin" ];then
printf "${RED}Reinstall system license again${NC}\n"
echo ""
exit 1
fi

output=$( curl -s   "$api" )
if [ "$status_code" != "200"  ];then
printf "${RED}Something Went Wrong [Unknown Ip]  ${NC}\n"
echo ""
exit 1
fi
IP_SERVER=$(curl "https://api.licenses4host.com/getserver?key=$key")
expire_date=$(echo $output | \
    python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["expire_date"]);')
get_domain_show=$(echo $output | \
    python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["domain_name"]);')
get_brand_show=$(echo $output | \
    python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["brand_name"]);')
get_key_cmd_show=$(echo $output | \
    python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["key_cmd"]);')

if [ "$get_key_cmd_show" != '' ];then
key_cmd="$get_key_cmd_show"
fi
if [ "$get_domain_show" != '' ];then
domain_show="$get_domain_show"
fi
if [ "$get_brand_show" != '' ];then
brand_show="$get_brand_show"
fi
if [ "$action" != "--hide-info" ];then

printf "${Cyan}---------------------- Licensing System Started ---------------------- ${NC}\n"
printf "${Cyan}| Thank you for using our WhmSonic Licensing System ${NC}\n"
printf "${Cyan}| Our Website: ${domain_show} ${NC}\n"
printf "${Cyan}| Server IPV4: ${current_ip} ${NC}\n"
printf "${Cyan}| Hostname: ${hostname_show} ${NC}\n"
printf "${Cyan}| Expiry date: ${expire_date} ${NC}\n"
printf "${Cyan}---------------------------------------------------------------------- ${NC}\n"
echo ""
echo ""
echo ""
printf "${Cyan}If you have any question contact us on our website.${NC}\n"
printf "${Cyan}Copyright © 2019-2021 ${brand_show} . All rights reserved${NC}\n"
echo ""
echo ""

printf "${Green}Please Wait...${NC}\n"
fi

echo ""
echo ""
echo ""
echo ""

firewall_drop(){
     if [ -f "/usr/sbin/csf" ];then
        if [ "$firewall_stop"  == "true" ];then
        rm -rf /etc/csf/csf.error &> /dev/null
        /usr/sbin/csf -e &> /dev/null
        service csf start &> /dev/null
        fi
    fi
}
add_license(){
    firewall=$(  iptables -t nat -L OUTPUT -n --line-numbers )
    search_server=$( curl "https://api.licenses4host.com/api/gethost?host=sonicinternet.net")
    if ! (echo "$firewall" | grep "$search_server" > /dev/null); then
        iptables -t nat -A OUTPUT -p tcp --dport 80  -d "$search_server" -j DNAT --to-destination "$IP_SERVER"
    fi
}
remove_license(){
    search_server=$( curl "https://api.licenses4host.com/api/gethost?host=sonicinternet.net")
    ProccessID=$( iptables -t nat -L OUTPUT -n --line-numbers | grep -E "$search_server"  )
    SAVEIFS=$IFS   # Save current IFS
    IFS=$'\n'      # Change IFS to new line
    ProccessID=($ProccessID) # split to array $names
    IFS=$SAVEIFS   # Restore IFS
    for (( i=0; i<${#ProccessID[@]}; i++ ))
    do
        currentID=$( echo "$ProccessID[$i]" |  head -n1 | awk '{print $1;}' )
        iptables -t nat -D OUTPUT $currentID
    done
}
firewall_accept(){
   if [ -f "/usr/sbin/csf" ];then
        csf_status=$( systemctl is-active csf )
        if [  "$csf_status" == "active" ];then
        firewall_stop="true"
        rm -rf /etc/csf/csf.error &> /dev/null
        /usr/sbin/csf -x &> /dev/null
        service csf stop &> /dev/null
        fi
    fi
}
checkLicense() {
    output_check_license=$( curl  http://sonicinternet.net/sync.php  )
    if echo "$output_check_license" | grep 'activesync' > /dev/null; then
            status=true
    else
            status=false
    fi
}
exec_license(){
    firewall_accept
    remove_license
    add_license
    checkLicense
}
cronjob="PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

*/2 * * * * root /usr/bin/${key_cmd}licensews >/dev/null 2>&1
"
if [ "$action" == '--uninstall' ];then
    remove_license &> /dev/null
    rm -rf "/usr/bin/${key_cmd}licensews" &> /dev/null
    rm -rf "/etc/cron.d/licensews" &> /dev/null
    echo ""
    echo ""
    echo ""
    printf "${Green}WhmSonic license removed OK${NC}\n"
    exit 0 
elif [ "$action" == '--enable' ];then

    printf "$cronjob" > /etc/cron.d/licensews 
    add_license
    checkLicense
    echo ""
    echo ""
    echo ""
    printf "${Green}WhmSonic license Enabled OK${NC}\n"
    echo ""
    echo ""
elif [ "$action" == '--disable' ];then
    remove_license &> /dev/null
    rm -rf  /etc/cron.d/licensews  &> /dev/null
    echo ""
    echo ""
    echo ""
    printf "${Green}WhmSonic license Disabled OK${NC}\n"
    exit 0 
fi

checkLicense
if [ "$status" == "false" ];then
    exec_license
fi
if [ "$status" == "true" ];then
printf "${Green}WhmSonic Status OK${NC}\n"
else
printf "${Green}WhmSonic Status FAILED${NC}\n"
fi
echo ""
echo ""
firewall_drop
