Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/op_waf/index.py

251 lines
5.6 KiB

6 years ago
# coding:utf-8
import sys
import io
import os
import time
import subprocess
import json
6 years ago
sys.path.append(os.getcwd() + "/class/core")
import public
app_debug = False
if public.isAppleSystem():
app_debug = True
def getPluginName():
return 'op_waf'
6 years ago
def getPluginDir():
return public.getPluginDir() + '/' + getPluginName()
def getServerDir():
return public.getServerDir() + '/' + getPluginName()
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, public.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, public.returnJson(True, 'ok'))
6 years ago
def getConf():
6 years ago
path = public.getServerDir() + "/openresty/nginx/conf/nginx.conf"
6 years ago
return path
6 years ago
def status():
path = getConf()
if not os.path.exists(path):
return 'stop'
6 years ago
6 years ago
conf = public.readFile(path)
if conf.find("#include luawaf.conf;") != -1:
return 'stop'
if conf.find("luawaf.conf;") == -1:
return 'stop'
return 'start'
6 years ago
6 years ago
def contentReplace(content):
service_path = public.getServerDir()
waf_path = public.getServerDir() + "/openresty/nginx/conf/waf"
content = content.replace('{$ROOT_PATH}', public.getRootDir())
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$WAF_PATH}', waf_path)
return content
6 years ago
def initDreplace():
6 years ago
path = public.getServerDir() + "/openresty/nginx/conf"
if not os.path.exists(path + '/waf'):
sdir = getPluginDir() + '/waf'
cmd = 'cp -rf ' + sdir + ' ' + path
public.execShell(cmd)
6 years ago
6 years ago
config = public.getServerDir() + "/openresty/nginx/conf/waf/lua/init.lua"
6 years ago
content = public.readFile(config)
content = contentReplace(content)
public.writeFile(config, content)
waf_conf = public.getServerDir() + "/openresty/nginx/conf/luawaf.conf"
waf_tpl = getPluginDir() + "/conf/luawaf.conf"
content = public.readFile(waf_tpl)
content = contentReplace(content)
public.writeFile(waf_conf, content)
6 years ago
def start():
6 years ago
initDreplace()
6 years ago
6 years ago
path = getConf()
conf = public.readFile(path)
conf = conf.replace('#include luawaf.conf;', "include luawaf.conf;")
6 years ago
6 years ago
public.writeFile(path, conf)
public.restartWeb()
return 'ok'
6 years ago
6 years ago
def stop():
6 years ago
path = public.getServerDir() + "/openresty/nginx/conf/waf"
if os.path.exists(path):
6 years ago
cmd = 'rm -rf ' + path
6 years ago
public.execShell(cmd)
6 years ago
path = getConf()
conf = public.readFile(path)
conf = conf.replace('include luawaf.conf;', "#include luawaf.conf;")
6 years ago
6 years ago
public.writeFile(path, conf)
public.restartWeb()
6 years ago
return 'ok'
6 years ago
def restart():
6 years ago
public.restartWeb()
6 years ago
return 'ok'
6 years ago
def reload():
6 years ago
stop()
6 years ago
public.execShell('rm -rf ' + public.getServerDir() +
"/openresty/nginx/logs/error.log")
6 years ago
start()
6 years ago
return 'ok'
6 years ago
6 years ago
def getJsonPath(name):
6 years ago
path = public.getServerDir() + "/openresty/nginx/conf/waf/" + name + ".json"
return path
6 years ago
def getRuleJsonPath(name):
path = public.getServerDir() + "/openresty/nginx/conf/waf/rule/" + name + ".json"
return path
def getRule():
args = getArgs()
data = checkArgs(args, ['rule_name'])
if not data[0]:
return data[1]
rule_name = args['rule_name']
fpath = getRuleJsonPath(rule_name)
content = public.readFile(fpath)
return public.returnJson(True, 'ok', content)
def setObjStatus():
args = getArgs()
data = checkArgs(args, ['obj', 'statusCode'])
if not data[0]:
return data[1]
conf = getJsonPath('config')
content = public.readFile(conf)
cobj = json.loads(content)
6 years ago
o = args['obj']
status = args['statusCode']
cobj[o]['status'] = status
cjson = public.getJson(cobj)
6 years ago
public.writeFile(conf, cjson)
return public.returnJson(True, '设置成功!')
def setObjOpen():
args = getArgs()
data = checkArgs(args, ['obj'])
if not data[0]:
return data[1]
conf = getJsonPath('config')
content = public.readFile(conf)
cobj = json.loads(content)
o = args['obj']
if cobj[o]["open"]:
cobj[o]["open"] = False
else:
cobj[o]["open"] = True
cjson = public.getJson(cobj)
6 years ago
public.writeFile(conf, cjson)
return public.returnJson(True, '设置成功!')
def getWafSrceen():
conf = getJsonPath('total')
return public.readFile(conf)
6 years ago
6 years ago
def getWafConf():
conf = getJsonPath('config')
return public.readFile(conf)
6 years ago
6 years ago
6 years ago
def getWafSite():
return ''
6 years ago
6 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
print status()
elif func == 'start':
print start()
elif func == 'stop':
print stop()
elif func == 'restart':
print restart()
elif func == 'reload':
print reload()
elif func == 'conf':
print getConf()
6 years ago
elif func == 'get_rule':
print getRule()
elif func == 'set_obj_status':
print setObjStatus()
elif func == 'set_obj_open':
print setObjOpen()
elif func == 'waf_srceen':
print getWafSrceen()
6 years ago
elif func == 'waf_conf':
print getWafConf()
6 years ago
elif func == 'waf_site':
print getWafSite()
6 years ago
else:
print 'error'