diff --git a/plugins/op_waf/conf/luawaf.conf b/plugins/op_waf/conf/luawaf.conf index af9993e83..3c93a082d 100755 --- a/plugins/op_waf/conf/luawaf.conf +++ b/plugins/op_waf/conf/luawaf.conf @@ -1,10 +1,3 @@ lua_shared_dict waf_limit 30m; lua_shared_dict waf_drop_ip 10m; -lua_shared_dict waf_drop_sum 10m; -lua_package_path "{$WAF_PATH}/html/?.lua;{$WAF_PATH}/conf/?.lua;{$WAF_PATH}/lua/?.lua;{$ROOT_PATH}/openresty/lualib/?.lua;;"; -lua_package_cpath "{$WAF_PATH}/conf/?.so;{$ROOT_PATH}/openresty/lualib/?.so;;"; - -init_worker_by_lua_file {$WAF_PATH}/lua/init_worker.lua; -access_by_lua_file {$WAF_PATH}/lua/init.lua; - -# init_by_lua_file {$WAF_PATH}/lua/init.lua; +lua_shared_dict waf_drop_sum 10m; \ No newline at end of file diff --git a/plugins/op_waf/index.py b/plugins/op_waf/index.py index 99cdeac13..d3325ae9f 100755 --- a/plugins/op_waf/index.py +++ b/plugins/op_waf/index.py @@ -6,6 +6,7 @@ import os import time import subprocess import json +import re sys.path.append(os.getcwd() + "/class/core") import mw @@ -228,17 +229,8 @@ def initTotalInfo(): mw.writeFile(path_total, cjson) -def status(): - path = getConf() - if not os.path.exists(path): - return 'stop' - - conf = mw.readFile(path) - if conf.find("#include luawaf.conf;") != -1: - return 'stop' - if conf.find("luawaf.conf;") == -1: - return 'stop' - return 'start' +def dstWafConf(): + return mw.getServerDir() + "/web_conf/nginx/vhost/opwaf.conf" def contentReplace(content): @@ -328,7 +320,8 @@ def autoMakeConfig(): def restartWeb(): autoMakeConfig() - mw.restartWeb() + mw.opWeb('stop') + mw.opWeb('start') def initDreplace(): @@ -370,11 +363,12 @@ def initDreplace(): content = contentReplace(content) mw.writeFile(init_worker, content) - waf_conf = mw.getServerDir() + "/openresty/nginx/conf/luawaf.conf" - waf_tpl = getPluginDir() + "/conf/luawaf.conf" - content = mw.readFile(waf_tpl) - content = contentReplace(content) - mw.writeFile(waf_conf, content) + waf_conf = dstWafConf() + if not os.path.exists(waf_conf): + waf_tpl = getPluginDir() + "/conf/luawaf.conf" + content = mw.readFile(waf_tpl) + content = contentReplace(content) + mw.writeFile(waf_conf, content) initDomainInfo() initSiteInfo() @@ -388,37 +382,61 @@ def initDreplace(): mw.execShell("chown -R www:www " + path) +def status(): + path = getConf() + if not os.path.exists(path): + return 'stop' + + waf_conf = dstWafConf() + if not os.path.exists(waf_conf): + return 'stop' + return 'start' + + def start(): initDreplace() - path = getConf() + path = mw.getServerDir() + '/web_conf/nginx/lua/lua.conf' + init_worker_lua = getServerDir() + '/waf/lua/init_worker.lua' + init_lua = getServerDir() + '/waf/lua/init.lua' conf = mw.readFile(path) - conf = conf.replace('#include luawaf.conf;', "include luawaf.conf;") + conf = re.sub('init_worker_by_lua_file (.*);', + "init_worker_by_lua_file " + init_worker_lua + ";", conf) + conf = re.sub('access_by_lua_file (.*);', + "access_by_lua_file " + init_lua + ";", conf) mw.writeFile(path, conf) import tool_task tool_task.createBgTask() - mw.restartWeb() + restartWeb() return 'ok' def stop(): - path = getConf() - conf = mw.readFile(path) - conf = conf.replace('include luawaf.conf;', "#include luawaf.conf;") + path = mw.getServerDir() + '/web_conf/nginx/lua/lua.conf' + empty_lua = mw.getServerDir() + '/web_conf/nginx/lua/empty.lua' + conf = mw.readFile(path) + conf = re.sub('init_worker_by_lua_file (.*);', + "init_worker_by_lua_file " + empty_lua + ";", conf) + conf = re.sub('access_by_lua_file (.*);', + "access_by_lua_file " + empty_lua + ";", conf) mw.writeFile(path, conf) + wafconf = dstWafConf() + if os.path.exists(wafconf): + os.remove(wafconf) + import tool_task tool_task.removeBgTask() - mw.restartWeb() + restartWeb() return 'ok' def restart(): - mw.restartWeb() + restartWeb() return 'ok' @@ -428,6 +446,18 @@ def reload(): path = getServerDir() path_tpl = getPluginDir() + config = path + "/waf/lua/common.lua" + config_tpl = path_tpl + "/waf/lua/common.lua" + content = mw.readFile(config_tpl) + content = contentReplace(content) + mw.writeFile(config, content) + + config = path + "/waf/lua/init_worker.lua" + config_tpl = path_tpl + "/waf/lua/init_worker.lua" + content = mw.readFile(config_tpl) + content = contentReplace(content) + mw.writeFile(config, content) + config = path + "/waf/lua/init.lua" config_tpl = path_tpl + "/waf/lua/init.lua" content = mw.readFile(config_tpl) @@ -438,6 +468,8 @@ def reload(): mw.execShell('rm -rf ' + errlog) start() + + restartWeb() return 'ok' @@ -1167,6 +1199,28 @@ def getWafConf(): return mw.readFile(conf) +def cleanDropIp(): + url = "http://127.0.0.1/clean_waf_drop_ip" + data = mw.httpGet(url) + return mw.returnJson(True, 'ok!', data) + + +def testRun(): + # args = getArgs() + # data = checkArgs(args, ['siteName']) + # if not data[0]: + # return data[1] + + conf_json = getServerDir() + "/waf/default.json" + data = mw.readFile(conf_json) + pdata = json.loads(data) + default_site = pdata['default'] + + url = "http://" + default_site + '/?t=../etc/passwd' + returnData = mw.httpGet(url, 10) + return mw.returnJson(True, '测试运行成功!', returnData) + + def installPreInspection(): check_op = mw.getServerDir() + "/openresty" if not os.path.exists(check_op): @@ -1174,12 +1228,6 @@ def installPreInspection(): return 'ok' -def cleanDropIp(): - url = "http://127.0.0.1/clean_waf_drop_ip" - data = mw.httpGet(url) - return mw.returnJson(True, 'ok!', data) - - if __name__ == "__main__": func = sys.argv[1] if func == 'status': @@ -1268,5 +1316,7 @@ if __name__ == "__main__": print(getWafSite()) elif func == 'clean_drop_ip': print(cleanDropIp()) + elif func == 'test_run': + print(testRun()) else: print('error') diff --git a/plugins/op_waf/info.json b/plugins/op_waf/info.json index 841cf2c6f..e37a26fdb 100755 --- a/plugins/op_waf/info.json +++ b/plugins/op_waf/info.json @@ -12,5 +12,5 @@ "home":"https://github.com/loveshell/ngx_lua_waf", "date":"2019-04-21", "pid": "1", - "versions": ["0.2.3"] + "versions": ["0.2.4"] } \ No newline at end of file diff --git a/plugins/op_waf/js/op_waf.js b/plugins/op_waf/js/op_waf.js index 170c7a871..469029adb 100755 --- a/plugins/op_waf/js/op_waf.js +++ b/plugins/op_waf/js/op_waf.js @@ -1712,9 +1712,12 @@ function wafLogs(){ \ \ \ - \ + \ + \ +