#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;
module_dir="/root/amh/modules/HYProxy-1.0/";

#info
function amh_module_info()
{
    echo 'AMH-ModuleName: HYProxy-1.0';
    echo 'AMH-ModuleDescription: HYProxy 反向代理模块。';
    echo 'AMH-ModuleButton: 安装/卸载';
    echo 'AMH-ModuleDate: 2018-09-15';
    echo 'AMH-ModuleAdmin: ./index.php?c=hyproxy';
    echo 'AMH-ModuleWebSite: http://www.huayizhiyun.com';
    echo 'AMH-ModuleScriptBy: HITSword.';
}

#install
function amh_module_install()
{
    if amh_module_status ; then
        exit;
    else
        #Fix 'Too many open files' Error
        if ! cat /etc/security/limits.conf | grep -q '* soft nofile 40960'; then 
            echo '* soft nofile 40960' >> /etc/security/limits.conf;
        fi;
        if ! cat /etc/security/limits.conf | grep -q '* hard nofile 40960'; then 
            echo '* hard nofile 40960' >> /etc/security/limits.conf;
        fi;
        #Delete other version
        for module_name in `ls /root/amh/modules/ | grep 'HYProxy' | grep -v 'HYProxy-1.0'`; do
            #amh module $module_name uninstall force;
            #amh module $module_name delete y;
            rm -rf /root/amh/modules/$module_name;
        done;
        #Delete conflict modules
        if ls /root/amh/modules/ | grep -q 'limit_req2_nginx_module-1.2'; then
            amh module limit_req2_nginx_module-1.2 uninstall force;
            amh module limit_req2_nginx_module-1.2 delete y;
        fi;
        #install PDO_MYSQL module and set InnoDB_Engine On
        if ! ls /root/amh/modules/ | grep -q 'PDO_MYSQL-1.0.2'; then 
            amh module download PDO_MYSQL-1.0.2;
        fi;
        if ! cat /etc/php.ini | grep -q 'pdo_mysql.so'; then 
            amh module PDO_MYSQL-1.0.2 install force;
        fi;
        amh SetParam mysql InnoDB_Engine On;
        #install nginx module
        Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
        nginx_configure='--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --with-ipv6';
        [ -f /usr/local/nginx/sbin/nginx ] && nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`;
        new_nginx_configure=$nginx_configure;
        if ! echo "$new_nginx_configure" | grep 'with-http_stub_status_module' > /dev/null; then 
          new_nginx_configure="${new_nginx_configure} --with-http_stub_status_module";
        fi;
        if ! echo "$new_nginx_configure" | grep 'add-module=/usr/local/ngx_cache_purge-2.3' > /dev/null; then 
          new_nginx_configure="${new_nginx_configure} --add-module=/usr/local/ngx_cache_purge-2.3";
        fi;
        if ! echo "$new_nginx_configure" | grep 'with-stream' > /dev/null; then 
            new_nginx_configure="${new_nginx_configure} --with-stream";
        fi;
        if ! echo "$new_nginx_configure" | grep 'ngx_http_upstream_check_module' > /dev/null; then 
            new_nginx_configure="${new_nginx_configure} --add-module=modules/ngx_http_upstream_check_module";
        fi;
        
        if [ "$nginx_configure" != "$new_nginx_configure" ]; then
          cd /usr/local/;
          wget https://soft.huayizhiyun.com/manage/amh/nginx/tengine-2.3.2.tar.gz;
          tar -zxf tengine-2.3.2.tar.gz;
          wget https://soft.huayizhiyun.com/manage/amh/nginx/ngx_cache_purge-2.3.tar.gz;
          tar -zxf ngx_cache_purge-2.3.tar.gz;

          cd tengine-2.3.2;
          ./configure $new_nginx_configure;
          make -j $Cpunum;
          mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-old;
          \cp -a ./objs/nginx /usr/local/nginx/sbin/;
          kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`;
          cd /usr/local/;
          rm -rf tengine-2.3.2 tengine-2.3.2.tar.gz ngx_cache_purge-2.3.tar.gz;
          #rm -rf /usr/local/nginx/sbin/nginx-old;
          kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`;
          if grep -q -e "Tengine.*<br />" /home/wwwroot/index/web/View/index.php; then
              sed -i "s|Tengine.*<br />|Tengine 2.3.2 <br />|" /home/wwwroot/index/web/View/index.php;
          fi
          if grep -q -e "Nginx.*<br />" /home/wwwroot/index/web/View/index.php; then
              sed -i "s|Nginx.*<br />|Tengine 2.3.2 <br />|" /home/wwwroot/index/web/View/index.php;
          fi
        fi;
        
        MysqlPass=`cat /home/wwwroot/index/web/Amysql/Config.php | awk '{ FS="\047Password\047] = \047"; RS="\047;" } { print $2}' | sed '/^$/d'`;
        cd /root/amh/modules/HYProxy-1.0;
        \cp Controller/hyproxy.php /home/wwwroot/index/web/Controller/hyproxy.php;
        \cp Model/hyproxys.php /home/wwwroot/index/web/Model/hyproxys.php;
        \cp View/{hyproxy_category.php,hyproxy_list.php,hyproxy_cache.php,hyproxy_cache_index.php,hyproxy_cache_del.php} /home/wwwroot/index/web/View/;
        mkdir -p /usr/local/nginx/conf/hyproxy;

        sed -i "s/server_names_hash_bucket_size 128/server_names_hash_bucket_size 512/g" /usr/local/nginx/conf/nginx.conf;
        sed -i "/include hyproxy/d" /usr/local/nginx/conf/nginx.conf;
        sed -i "/include vhost/a\ \tinclude hyproxy/*.conf;"  /usr/local/nginx/conf/nginx.conf;
        mysql -uroot -p$MysqlPass < ./hyproxy.sql && touch ./InstallComplete && amh module HYProxy-1.0 admin cache,1:2:2,256m,10g,4h,1d;

        amh nginx restart;
        amh_module_status;
    fi;
}

#admin
function amh_module_admin()
{
    if amh_module_status ; then
        param_list=${1//,/ };
        set -- $param_list;
        action=$1;
        domain=$2;

        if [ "$action" == '' ]; then
          echo "[Notice] HYProxy management, please select: (1~4)"
          select action in 'list' 'make' 'add' 'start' 'stop' 'edit' 'del' 'start-cache' 'stop-cache' 'cache' 'cache-index' 'cache-delete'; do
            break;
          done;
        fi;

        if [ "$action" == 'list' ]; then
          php ${module_dir}hyproxy-cli.php --action='list' --proxy_status='start';
          php ${module_dir}hyproxy-cli.php --action='list' --proxy_status='stop';
          exit;
        elif [ "$action" == 'make' ]; then
          php ${module_dir}hyproxy-cli.php --action='make' --server_name=${domain};
          nginx -t
          amh nginx reload
          exit;
        elif [ "$action" == 'cache' ] ; then
          levels=$2;
          keys_zone=$3;
          max_size=$4;
          valid=$5;
          inactive=$6;
          [ "$levels" == '' ] && read -p "[Notice] Please input levels (e.g: 1:2):" levels && [ "$levels" == '' ] && levels='1:2:2';
          [ "$keys_zone" == '' ] && read -p "[Notice] Please input keys_zone (e.g: 10m):" keys_zone && [ "$keys_zone" == '' ] && keys_zone='10m';
          [ "$max_size" == '' ] && read -p "[Notice] Please input max_size (e.g: 2g):" max_size && [ "$max_size" == '' ] && max_size='2g';
          [ "$valid" == '' ] && read -p "[Notice] Please input valid (e.g: 12h):" valid && [ "$valid" == '' ] && valid='12h';
          [ "$inactive" == '' ] && read -p "[Notice] Please input inactive (e.g: 10d):" inactive && [ "$inactive" == '' ] && inactive='10d';
          sed -i "/proxy_/d" /usr/local/nginx/conf/nginx.conf;
          sed -i "/fastcgi_intercept_errors/a\ \tproxy_cache_path \/home\/hyproxy_cache levels=$levels keys_zone=hyproxy:$keys_zone inactive=$inactive max_size=$max_size;" /usr/local/nginx/conf/nginx.conf;
          sed -i "/fastcgi_intercept_errors/a\ \tproxy_temp_path \/home\/hyproxy_cache_tmp;" /usr/local/nginx/conf/nginx.conf;
          sed -i "/fastcgi_intercept_errors/a\ \tproxy_cache_key \$scheme:\/\/\$host\$request_uri;" /usr/local/nginx/conf/nginx.conf;
          sed -i "/fastcgi_intercept_errors/a\ \tproxy_cache_valid 200 304 $valid;" /usr/local/nginx/conf/nginx.conf;
          kill -HUP `cat /usr/local/nginx/logs/nginx.pid` && echo "[OK] HYProxy cache-param save success." && exit;
        elif [ "$action" == 'cache-index' ] ; then
          MysqlPass=`cat /home/wwwroot/index/web/Amysql/Config.php | awk '{ FS="\047Password\047] = \047"; RS="\047;" } { print $2}' | sed '/^$/d'`;
          cmin=$2;
          mode=$3;
          [ "$cmin" == '' ] && read -p "[Notice] Please input cmin value(e.g: +10):" cmin && [ "$cmin" == '' ] && cmin='+10';
          [ ! -d /home/hyproxy_cache ] && echo "[Error] hyproxy_cache dir not exist!" && exit 1;
          cd /home/hyproxy_cache;
          i=0;
          cmin=${cmin//;/} && cmin=${cmin// /};
          rm -f ./cache-index.sql;
          for line in `find ./ -type f -cmin $cmin`; do
            echo $line;
            text=`head -18 $line`;
            key=`echo "$text" | grep 'KEY:' | awk '{print $2}'`;
            http_s=`echo "$text" | grep 'HTTP/' | awk '{print $2}'`;
            type=`echo "$text" | grep 'Content-Type:' | awk '{print $2}'`;
            [ "$type" != '' ] && type=${type:0:$((${#type}-1))};
            size=`echo "$text" | grep 'Content-Length:' | awk '{printf("%.3f", $2/1024/1024)}'`;
            time=`stat $line | grep 'Change' | awk '{print $2" "$3}' | cut -d '.' -f 1`;
            echo "('$line','$key','$http_s','$type','$size','$time')," >> ./cache-index.sql;
            i=$[i+1];
          done;
          [ "$mode" == 'truncate' ] && mysql -uroot -p${MysqlPass} -B -N -e  "TRUNCATE TABLE amh.module_hyproxy_cache";
          [ ! -f "./cache-index.sql" ] && echo '[OK] No data found.' && exit;
          sed -i "1i REPLACE INTO amh.module_hyproxy_cache (hyproxy_file,hyproxy_key,hyproxy_http_s,hyproxy_type,hyproxy_size,hyproxy_time) VALUES " ./cache-index.sql;
          sed -i "$ s/),/)\;/" ./cache-index.sql;
          mysql -uroot -p$MysqlPass < ./cache-index.sql && echo "[OK] HYProxy cache-index success. ($i row)" ;
          rm -f ./cache-index.sql;
        elif [ "$action" == 'cache-delete' ] ; then
          MysqlPass=`cat /home/wwwroot/index/web/Amysql/Config.php | awk '{ FS="\047Password\047] = \047"; RS="\047;" } { print $2}' | sed '/^$/d'`;
          url_param=$2;
          file_type=$3;
          [ "$url_param" == '' ] && read -p "[Notice] Please input url-param (e.g: amysql.com/images/):" url_param
          [ "$file_type" == '' ] && read -p "[Notice] Please input file-type (e.g: image/jpeg):" file_type

          sqls="SELECT hyproxy_file FROM amh.module_hyproxy_cache WHERE 1";
          sqld="DELETE FROM amh.module_hyproxy_cache WHERE 1";
          sql_furl=${sql_furl//;/} && sql_furl=${sql_furl// /};
          sql_ftype=${sql_ftype//;/} && sql_ftype=${sql_ftype// /};
          [ "$url_param" != '-all' ] && sql_furl=" AND hyproxy_key LIKE '$url_param' ";
          [ "$file_type" != '-all' ] && sql_ftype=" AND hyproxy_type LIKE '$file_type' ";
          i=0;
          for line in `mysql -uroot -p${MysqlPass} -B -N -e "$sqls $sql_furl $sql_ftype"`; do
            ( echo "$line" | grep '\.\.' || [ "$line" == '' ] ) && echo '[Error] Error data.' && exit 1;
            echo $line;
            rm -f "/home/hyproxy_cache/${line}" && i=$[i+1];
          done;
          mysql -uroot -p${MysqlPass} -B -N -e "$sqld $sql_furl $sql_ftype" && echo "[OK] HYProxy cache-delete success. ($i row)" ;
        else
          if [ "$action" != 'list' ] && [ "$action" != 'cache' ] ; then
            [ "$domain" == '' ] && read -p "[Notice] please input domain(e.g amysql.com):" domain
            [ "$domain" == '' ] && amh module HYProxy-1.0 admin $action;
          fi;
          domain_conf="/usr/local/nginx/conf/hyproxy/$domain.conf";

          if [ "$action" == 'add' ]; then
            [ -f "$domain_conf" ] && echo "[Error] $domain HYProxy is exist!" && exit 1;
            proxy_pass=$3;
            if [ "$proxy_pass" == '' ]; then
              read -p "[Notice] please input proxy_pass(e.g nginx.org):" proxy_pass
              [ "$proxy_pass" == '' ] && amh module HYProxy-1.0 admin $action;
            fi;
            php ${module_dir}hyproxy-cli.php --action='add' --server_name=${domain} --proxy_pass=${proxy_pass};
            nginx -t
            amh nginx reload
          elif [ "$action" == 'edit' ]; then
            ParamName=$3;
            i=0;
            for line in $*; do
              i=$[$i+1];
              [ "$i" -gt 3 ] && ParamVal="$ParamVal$line";
              ##get value
            done;

            HYProxy[0]='header_host|nginx.org';
            HYProxy[1]='proxy_pass|http://nginx.org';

            i=0;
            HYProxyParamList='';
            HYProxyParamListEG='';
            for line in ${HYProxy[*]}; do
              OldIFS=$IFS;
              IFS='|';
              set -- $line;
              i=$[$i+1];
              HYProxyParamList[$i]="$1";
              HYProxyParamListEG[$i]="$2";
              IFS=$OldIFS;
            done;

            if [ "$ParamName" == '' ]; then
              echo "[Notice] $domain proxy parameter set, please select: (1~$i)"
              select ParamName in ${HYProxyParamList[*]}; do break; done;
            fi;

            for((;i>0;i--));do 
              if [ "${HYProxyParamList[$i]}" == "$ParamName" ]; then
                php ${module_dir}hyproxy-cli.php --action='edit' --server_name=${domain} --field_name=${ParamName} --value=${ParamVal};
                amh nginx reload;
                exit;
              fi;
            done;
            amh module HYProxy-1.0 admin $action,$domain;

          elif [ "$action" == 'del' ]; then
            php ${module_dir}hyproxy-cli.php --action='del' --server_name=${domain}
            amh nginx reload;
          elif [ "$action" == 'start' ]; then
            php ${module_dir}hyproxy-cli.php --action='edit' --server_name=${domain} --field_name='status' --value='start';
            amh nginx reload;
          elif [ "$action" == 'stop' ]; then
            php ${module_dir}hyproxy-cli.php --action='edit' --server_name=${domain} --field_name='status' --value='stop';
            amh nginx reload;
          elif [ "$action" == 'start-cache' ]; then
            php ${module_dir}hyproxy-cli.php --action='edit' --server_name=${domain} --field_name='proxy_cache' --value='1';
            amh nginx reload;
          elif [ "$action" == 'stop-cache' ]; then
            php ${module_dir}hyproxy-cli.php --action='edit' --server_name=${domain} --field_name='proxy_cache' --value='0';
            amh nginx reload;
          elif [ "$action" == 'cat' ]; then
            [ ! -f "$domain_conf" ] && echo "[Error] $domain HYProxy not exist!" && exit 1;
            cat "$domain_conf";
          else
            amh module HYProxy-1.0 admin;
            exit;
          fi;
        fi;
    else
        exit;
    fi;
}

#uninstall
function amh_module_uninstall()
{
    if amh_module_status ; then

        #backup proxys
        php ${module_dir}hyproxy-cli.php --action='backup';

        #uninstall nginx modules
        Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
        nginx_configure='--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --with-ipv6';
        [ -f /usr/local/nginx/sbin/nginx ] && nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`;
        new_nginx_configure=$nginx_configure;
        if echo "$new_nginx_configure" | grep 'add-module=/usr/local/ngx_cache_purge-2.3' > /dev/null; then 
          new_nginx_configure=`echo $new_nginx_configure | sed "s|--add-module=/usr/local/ngx_cache_purge-2.3||"`;
        fi;

        if [ "$nginx_configure" != "$new_nginx_configure" ]; then
          cd /usr/local/;
          wget https://soft.huayizhiyun.com/manage/amh/nginx/tengine-2.3.2.tar.gz;
          tar -zxf tengine-2.3.2.tar.gz;
          cd tengine-2.3.2;
          ./configure $new_nginx_configure;
          make -j $Cpunum;
          rm -rf /usr/local/nginx/sbin/nginx-old;
          mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-old;
          \cp -a ./objs/nginx /usr/local/nginx/sbin/;
          kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`;
          cd /usr/local/;
          rm -rf tengine-2.3.2 tengine-2.3.2.tar.gz;
          #rm -rf  /usr/local/nginx/sbin/nginx-old;
          kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`;
        fi;

        MysqlPass=`cat /home/wwwroot/index/web/Amysql/Config.php | awk '{ FS="\047Password\047] = \047"; RS="\047;" } { print $2}' | sed '/^$/d'`;
        cd /usr/local/;
        rm -rf ngx_cache_purge-2.3 ngx_cache_purge-2.3.tar.gz;
        rm -rf /usr/local/nginx/conf/hyproxy /home/hyproxy_cache /home/hyproxy_cache_tmp /home/hyproxy_logs;
        rm -f /home/wwwroot/index/web/Controller/hyproxy.php /home/wwwroot/index/web/Model/hyproxys.php /home/wwwroot/index/web/View/{hyproxy_category.php,hyproxy_list.php,hyproxy_cache.php,hyproxy_cache_index.php,hyproxy_cache_del.php};
        sed -i "/include hyproxy/d" /usr/local/nginx/conf/nginx.conf;
        sed -i "/proxy_/d" /usr/local/nginx/conf/nginx.conf;
        rm -f /root/amh/modules/HYProxy-1.0/InstallComplete;
        mysql -uroot -p${MysqlPass} -B -N -e "DROP TABLE amh.module_hyproxy;DROP TABLE amh.module_hyproxy_cache" ;

        amh nginx restart;
        echo '[OK] HYProxy Uninstall successful.';
    else
        exit;
    fi;
}

#status //ok
function amh_module_status()
{
    if [ -f "/root/amh/modules/HYProxy-1.0/InstallComplete" ]; then
        echo '[OK] HYProxy is already installed.';
        return 0;
    else
        echo '[Notice] HYProxy is not installed.';
        return 1;
    fi;
}
