#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH public_file=/www/server/panel/install/public.sh if [ ! -f $public_file ];then wget -O $public_file http://download.bt.cn/install/public.sh -T 5; fi . $public_file download_Url=$NODE_URL install_tmp='/tmp/bt_install.pl' pluginPath=/www/server/panel/plugin/free_waf waf_path=/www/server/free_waf Get_lua_version(){ echo `/www/server/free_waf/lua515/bin/lua -e 'print(_VERSION:sub(5))'` } Get_platform() { case $(uname -s 2>/dev/null) in Linux ) echo "linux" ;; FreeBSD ) echo "freebsd" ;; *BSD* ) echo "bsd" ;; Darwin ) echo "macosx" ;; CYGWIN* | MINGW* | MSYS* ) echo "mingw" ;; AIX ) echo "aix" ;; SunOS ) echo "solaris" ;; * ) echo "unknown" esac } Add_path() { local prefix=$1 local new_path new_path=$(echo "${PATH}" | sed \ -e "s#${prefix}/[^/]*/bin[^:]*:##g" \ -e "s#:${prefix}/[^/]*/bin[^:]*##g" \ -e "s#${prefix}/[^/]*/bin[^:]*##g") export PATH="${prefix}:${new_path}" } Install_lua515(){ local install_path="/www/server/free_waf/lua515" local version version=$(Get_lua_version) if [[ $version == *GLIBC* ]]; then rm -rf $install_path fi echo "Current lua version: "$version if [ -d "${install_path}/bin" ] then Add_path "${install_path}/bin" echo "Lua 5.1.5 has installed." return 1 fi local lua_version="lua-5.1.5" local package_name="${lua_version}.tar.gz" mkdir -p $install_path cd $pluginPath tar xvzf $pluginPath/$package_name cd $lua_version platform=$(Get_platform) if [ "${platform}" = "unknown" ] then platform="linux" fi make "${platform}" install INSTALL_TOP=$install_path Add_path "${install_path}/bin" #rm -rf "${pluginPath}/${lua_version}*" version=$(Get_lua_version) if [ ${version} == "5.1" ] then echo "Lua 5.1.5 has installed." return 1 fi return 0 } Install_cjson() { if [ -f /www/server/free_waf/cjson.so ];then cd /www/server/free_waf/ is_jit_cjson=$(luajit -e "require 'cjson'" 2>&1|grep 'undefined symbol: ') if [ "$is_jit_cjson" != "" ];then rm -f /www/server/free_waf/cjson.so rm -rf /www/server/free_waf/cjson.so fi fi if [ ! -f /www/server/free_waf/cjson.so ];then cd $pluginPath/ echo "安装Cjson" tar zxvf lua-cjson-2.1.0.tar.gz #rm -f lua-cjson-2.1.0.tar.gz cd $pluginPath/lua-cjson-2.1.0 make clean make make install cd .. #rm -rf lua-cjson-2.1.0 cp -a -r /usr/local/lib/lua/5.1/cjson.so /www/server/free_waf/cjson.so cp -a -r /usr/local/lib/lua/5.1/cjson.so /usr/lib64/lua/5.1/cjson.so cp -a -r /usr/local/lib/lua/5.1/cjson.so /usr/lib/lua/5.1/cjson.so else if [ -d "/usr/lib64/lua/5.1" ];then ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib64/lua/5.1/cjson.so fi if [ -d "/usr/lib/lua/5.1" ];then ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib/lua/5.1/cjson.so fi fi if [ -f /usr/local/lib/lua/5.1/cjson.so ];then is_jit_cjson=$(luajit -e "require 'cjson'" 2>&1|grep 'undefined symbol: ') rm -f /usr/local/lib/lua/5.1/cjson.so chmod +x $pluginPath/cjson.so \cp -a -r $pluginPath/cjson.so /www/server/free_waf/cjson.so \cp -a -r $pluginPath/cjson.so /usr/local/lib/lua/5.1/cjson.so fi } Install_btwaf() { if [ -f /www/server/panel/vhost/nginx/btwaf.conf ];then rm -rf /www/server/panel/vhost/nginx/btwaf.conf fi mkdir -p $pluginPath mkdir -p $waf_path Install_lua515 Install_cjson mkdir -p /www/wwwlogs/free_waf_log chmod 777 /www/wwwlogs/free_waf_log cp -a -r $pluginPath/free_waf.conf /www/server/panel/vhost/nginx/free_waf.conf \cp -a -r /www/server/panel/plugin/free_waf/icon.png /www/server/panel/static/img/soft_ico/ico-free_waf.png unzip -o $pluginPath/free_waf_data.zip -d /tmp/ > /dev/null rm -f $pluginPath/free_waf_data.zip if [ ! -f $waf_path/html/get.html ];then mkdir $waf_path/html/ \cp -a -r /tmp/free_waf/html/get.html $waf_path/html/get.html \cp -a -r /tmp/free_waf/html/get.html $waf_path/html/post.html \cp -a -r /tmp/free_waf/html/get.html $waf_path/html/cookie.html \cp -a -r /tmp/free_waf/html/get.html $waf_path/html/user_agent.html \cp -a -r /tmp/free_waf/html/get.html $waf_path/html/other.html fi if [ ! -f $waf_path/rule/url.json ];then mkdir $waf_path/rule/ \cp -a -r /tmp/free_waf/rule/url.json $waf_path/rule/url.json \cp -a -r /tmp/free_waf/rule/args.json $waf_path/rule/args.json \cp -a -r /tmp/free_waf/rule/post.json $waf_path/rule/post.json \cp -a -r /tmp/free_waf/rule/cn.json $waf_path/rule/cn.json \cp -a -r /tmp/free_waf/rule/head_white.json $waf_path/rule/head_white.json \cp -a -r /tmp/free_waf/rule/ip_black.json $waf_path/rule/ip_black.json \cp -a -r /tmp/free_waf/rule/ip_white.json $waf_path/rule/ip_white.json \cp -a -r /tmp/free_waf/rule/scan_black.json $waf_path/rule/scan_black.json \cp -a -r /tmp/free_waf/rule/url_black.json $waf_path/rule/url_black.json \cp -a -r /tmp/free_waf/rule/url_white.json $waf_path/rule/url_white.json \cp -a -r /tmp/free_waf/rule/user_agent.json $waf_path/rule/user_agent.json fi \cp -a -r /tmp/free_waf/rule/args.json $waf_path/rule/args.json \cp -a -r /tmp/free_waf/rule/post.json $waf_path/rule/post.json \cp -a -r /tmp/free_waf/4.7.lua $waf_path/init.lua \cp -a -r /tmp/free_waf/multipart.lua $waf_path/multipart.lua \cp -a -r /tmp/free_waf/waf.lua $waf_path/waf.lua \cp -a -r /tmp/free_waf/1.json $waf_path/1.json \cp -a -r /tmp/free_waf/2.json $waf_path/2.json \cp -a -r /tmp/free_waf/3.json $waf_path/3.json \cp -a -r /tmp/free_waf/4.json $waf_path/4.json \cp -a -r /tmp/free_waf/5.json $waf_path/5.json \cp -a -r /tmp/free_waf/6.json $waf_path/6.json \cp -a -r /tmp/free_waf/7.json $waf_path/7.json \cp -a -r /tmp/free_waf/zhi.lua $waf_path/zhi.lua \cp -a -r /tmp/free_waf/dns.lua $waf_path/dns.lua \cp -a -r /tmp/free_waf/waf.lua $waf_path/waf.lua if [ ! -f $waf_path/zhizhu1.json ];then \cp -a -r /tmp/free_waf/zhizhu1.json $waf_path/zhizhu1.json fi if [ ! -f $waf_path/zhizhu2.json ];then \cp -a -r /tmp/free_waf/zhizhu1.json $waf_path/zhizhu2.json fi if [ ! -f $waf_path/zhizhu4.json ];then \cp -a -r /tmp/free_waf/zhizhu1.json $waf_path/zhizhu4.json fi if [ ! -f $waf_path/zhizhu5.json ];then \cp -a -r /tmp/free_waf/zhizhu1.json $waf_path/zhizhu5.json fi if [ ! -f $waf_path/zhizhu6.json ];then \cp -a -r /tmp/free_waf/zhizhu1.json $waf_path/zhizhu6.json fi if [ ! -f $waf_path/site.json ];then \cp -a -r /tmp/free_waf/site.json $waf_path/site.json fi if [ ! -f $waf_path/config.json ];then \cp -a -r /tmp/free_waf/config.json $waf_path/config.json fi if [ ! -f $waf_path/total.json ];then \cp -a -r /tmp/free_waf/total.json $waf_path/total.json fi if [ ! -f $waf_path/drop_ip.log ];then \cp -a -r /tmp/free_waf/drop_ip.log $waf_path/drop_ip.log fi #if [ ! -f $waf_path/resty/memcached.lua ];then #做软连 # if [ -f /www/server/nginx/lualib/resty/memcached.lua ];then #openrestry 兼容 # echo "openresty兼容" # ln -s /www/server/nginx/lualib/resty /www/server/free_waf # fi #fi rm -rf /www/server/free_waf/ngx rm -rf /www/server/free_waf/resty NGINX_VER=$(/www/server/nginx/sbin/nginx -v 2>&1|grep -oE 1.2[34]) if [ "${NGINX_VER}" ];then sed -i "/lua_package_path/d" /www/server/nginx/conf/nginx.conf \cp -rpa /www/server/nginx/lib/lua/* /www/server/free_waf fi NGINX_VER=$(/www/server/nginx/sbin/nginx -v 2>&1|grep -oE openresty) if [ "${NGINX_VER}" ];then sed -i "/lua_package_path/d" /www/server/nginx/conf/nginx.conf \cp -rpa /www/server/nginx/lualib/* /www/server/free_waf fi chown www:www /www/server/free_waf/drop_ip.log chown www:www /www/server/free_waf/total.json chmod -R 755 /www/server/free_waf chmod -R 666 /www/server/free_waf/rule chmod -R 666 /www/server/free_waf/total.json chown www:www /www/server/free_waf/zhi*.json chmod -R 666 /www/server/free_waf/drop_ip.log rm -rf /tmp/free_waf /etc/init.d/nginx restart is_status=$(netstat -nltp|grep nginx| grep 80|wc -l) if [ $is_status -eq 0 ]; then rm -rf /www/server/panel/vhost/nginx/free_waf.conf /etc/init.d/nginx restart rm -rf /www/server/panel/plugin/free_waf echo '安装失败,可以重新安装一次试试' echo '安装失败,可能不兼容该Nginx、请切换一个Nginx版本后重新安装' echo '5秒后结束安装进程!!!!' sleep 7 echo '安装失败,可能不兼容该Nginx、请切换一个Nginx版本后重新安装' > $install_tmp else echo '安装完成' echo '安装完成' > $install_tmp fi } Install_cjson2() { if [ -f /usr/bin/yum ];then isInstall=`rpm -qa |grep lua-devel` if [ "$isInstall" == "" ];then yum install lua lua-devel -y fi else isInstall=`dpkg -l|grep liblua5.1-0-dev` if [ "$isInstall" == "" ];then apt-get install lua5.1 lua5.1-dev -y fi fi if [ -f /usr/local/lib/lua/5.1/cjson.so ];then is_jit_cjson=$(luajit -e "require 'cjson'" 2>&1|grep 'undefined symbol: ') if [ "$is_jit_cjson" != "" ];then rm -f /usr/local/lib/lua/5.1/cjson.so fi fi if [ ! -f /usr/local/lib/lua/5.1/cjson.so ];then wget -O lua-cjson-2.1.0.tar.gz $download_Url/install/src/lua-cjson-2.1.0.tar.gz -T 20 tar xvf lua-cjson-2.1.0.tar.gz rm -f lua-cjson-2.1.0.tar.gz cd lua-cjson-2.1.0 make clean make -B make install cd .. rm -rf lua-cjson-2.1.0 ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib64/lua/5.1/cjson.so ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib/lua/5.1/cjson.so else if [ -d "/usr/lib64/lua/5.1" ];then ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib64/lua/5.1/cjson.so fi if [ -d "/usr/lib/lua/5.1" ];then ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib/lua/5.1/cjson.so fi fi if [ -f /usr/local/lib/lua/5.1/cjson.so ];then is_jit_cjson=$(luajit -e "require 'cjson'" 2>&1|grep 'undefined symbol: ') if [ "$is_jit_cjson" != "" ];then rm -f /usr/local/lib/lua/5.1/cjson.so chmod +x /tmp/free_waf/cjson.so \cp -a -r /tmp/free_waf/cjson.so $waf_path/cjson.so \cp -a -r /tmp/free_waf/cjson.so /usr/local/lib/lua/5.1/cjson.so fi fi } Uninstall_btwaf() { rm -rf $pluginPath rm -rf $waf_path NGINX_VER=$(/www/server/nginx/sbin/nginx -v 2>&1|grep -oE 1.2[34]) if [ "${NGINX_VER}" ];then sed -i '/include proxy\.conf;/a \ lua_package_path "/www/server/nginx/lib/lua/?.lua;;";' /www/server/nginx/conf/nginx.conf fi rm -rf /www/server/panel/vhost/nginx/free_waf.conf /etc/init.d/nginx reload } if [ "${1}" == 'install' ];then Install_btwaf elif [ "${1}" == 'update' ];then Install_btwaf elif [ "${1}" == 'uninstall' ];then Uninstall_btwaf fi