From 39d76f5621911b0444b2975d2422a69f7bb59a86 Mon Sep 17 00:00:00 2001 From: midoks Date: Sat, 14 Jan 2023 16:42:22 +0800 Subject: [PATCH] #338 --- plugins/webhook/index.py | 18 ++++--- plugins/webhook/js/webhook.js | 92 +++++++++++++++++++++++++---------- 2 files changed, 79 insertions(+), 31 deletions(-) diff --git a/plugins/webhook/index.py b/plugins/webhook/index.py index a2fe7a073..106da859b 100755 --- a/plugins/webhook/index.py +++ b/plugins/webhook/index.py @@ -122,7 +122,7 @@ def getLog(): logPath = args['path'] - content = mw.getLastLine(logPath, 100) + content = mw.getLastLine(logPath, 16) return mw.returnJson(True, 'ok', content) @@ -132,11 +132,16 @@ def runShellArgs(args): if data[i]['access_key'] == args['access_key']: script_dir = getServerDir() + "/scripts" shellFile = script_dir + '/' + args['access_key'] - param = '' - if hasattr(args, 'param'): - param = args['param'] - os.system("bash {} \"{}\" >> {}.log &".format( - shellFile, param.replace('"', r'\"'), shellFile)) + param = args['params'] + if param == '': + param = 'no-parameters' + + param = re.sub("\"", '', param) + + cmd = "bash {} {} >> {}.log 2>&1 &".format( + shellFile, param, shellFile) + # print(cmd) + os.system(cmd) data[i]['count'] += 1 data[i]['uptime'] = int(time.time()) mw.writeFile(getCfgFilePath(), json.dumps(data)) @@ -150,6 +155,7 @@ def runShell(): if not check_arg[0]: return check_arg[1] + args['params'] = 'panel-test' return runShellArgs(args) diff --git a/plugins/webhook/js/webhook.js b/plugins/webhook/js/webhook.js index dbe6167f2..231219989 100755 --- a/plugins/webhook/js/webhook.js +++ b/plugins/webhook/js/webhook.js @@ -26,6 +26,32 @@ function whPost(method, args,callback){ },'json'); } + +function whPostNoMessage(method, args,callback){ + + var req_data = {}; + req_data['name'] = 'webhook'; + req_data['func'] = method; + + if (typeof(args) == 'string'){ + req_data['args'] = JSON.stringify(toArrayObject(args)); + } else { + req_data['args'] = JSON.stringify(args); + } + + $.post('/plugins/run', req_data, function(data) {; + if (!data.status){ + //错误展示10S + layer.msg(data.msg,{icon:0,time:2000,shade: [10, '#000']}); + return; + } + + if(typeof(callback) == 'function'){ + callback(data); + } + },'json'); +} + function whPostCallbak(method, version, args,callback){ var loadT = layer.msg('正在获取...', { icon: 16, time: 0, shade: 0.3 }); @@ -153,42 +179,60 @@ function showWebHookCode(url,code){
\ WebHook使用方法:
\ GET/POST:
\ - '+window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '')+'/hook?access_key='+code+'&param=aaa
\ + '+window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '')+'/hook?access_key='+code+'&params=aaa
\ @param access_key string HOOK密钥
\ - @param param string 自定义参数(在hook脚本中使用$1接收)
\ + @param params string 自定义参数(在hook脚本中使用$1接收)| 多个参数 "1 2" -> $1为1, $2为2
\
\ ' }) } -//查看日志 -function getLogs(path){ - - - whPost('get_log', {"path":path}, function(rdata){ +function getLogsTimer(path,success,error){ + whPostNoMessage('get_log', {"path":path}, function(rdata){ var rdata = $.parseJSON(rdata.data); if (!rdata.status){ - layer.msg(rdata.msg,{icon:2}); - } - - layer.open({ - type:1, - title:'任务执行日志', - area: ['600px','400px'], - shadeClose:false, - closeBtn:2, - content:'
\ - \ -
', - success:function(){ - $('[name="site_logs"]').scrollTop($('[name="site_logs"]')[0].scrollHeight) + if (typeof(error) == 'function'){ + error(); } - }); - + return; + } + $('[name="site_logs"]').text(rdata.data); + if (typeof(success) == 'function'){ + success(); + } }); } +//查看日志 +function getLogs(path){ + logs_web = layer.open({ + type:1, + title:'任务执行日志', + area: ['600px','400px'], + shadeClose:false, + closeBtn:2, + content:'
\ + \ +
', + success:function(){ + $('[name="site_logs"]').scrollTop($('[name="site_logs"]')[0].scrollHeight); + + logs_timer = setInterval(function(){ + getLogsTimer(path,function(){ + + },function(){ + layer.msg(rdata.msg,{icon:2}); + layer.close(logs_web); + }); + },1000); + }, + cancel:function(){ + clearInterval(logs_timer); + } + }); +} + //运行 function runHook(key){ whPost('run_shell', {"access_key":key}, function(rdata){ @@ -220,5 +264,3 @@ function deleteHook(key, title){ }); } - -