Merge pull request #740 from midoks/dev

webhook优化
This commit is contained in:
Mr Chen 2025-05-18 11:09:20 +08:00 committed by GitHub
commit 7872391c54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 225 additions and 18 deletions

View File

@ -30,7 +30,8 @@ unset GIT_DIR
cd $GIT_PROJECT_DIR && git pull
# cd $GIT_PROJECT_DIR && git pull
cd $GIT_PROJECT_DIR && sudo git pull
# func 2
# cd $GIT_PROJECT_DIR && env -i git pull origin master

View File

@ -14,11 +14,11 @@
<thead>
<tr>
<th>名称</th>
<th>添加时间</th>
<th>近期调用</th>
<th>调用次数</th>
<th>密钥</th>
<th style="text-align: right;" width="150">操作</th>
<th width="150">添加时间</th>
<th width="150">近期调用</th>
<th width="100">调用次数</th>
<th width="100">密钥</th>
<th style="text-align: right;" width="180">操作</th>
</tr>
</thead>
<tbody id="zipBody"></tbody>

View File

@ -97,7 +97,30 @@ def addHook():
hook = {}
hook['title'] = args['title']
hook['access_key'] = mw.getRandomString(48)
if hook['title'] == '':
return mw.returnJson(False, '名称不能为空!')
hook['access_key'] = mw.md5(hook['title'])
hook['count'] = 0
hook['addtime'] = int(time.time())
hook['uptime'] = 0
script_dir = getServerDir() + "/scripts"
if not os.path.exists(script_dir):
os.mkdir(script_dir)
addCfg(hook)
shellFile = script_dir + '/' + hook['access_key']
mw.writeFile(shellFile, args['shell'])
return mw.returnJson(True, '添加成功!')
def addHookShell(args):
if args['title'] == '':
return mw.returnJson(False, '名称不能为空!')
hook = {}
hook['title'] = args['title']
hook['access_key'] = mw.md5(hook['title'])
hook['count'] = 0
hook['addtime'] = int(time.time())
hook['uptime'] = 0
@ -129,7 +152,7 @@ def getLog():
logPath = args['path']
content = mw.getLastLine(logPath, 16)
content = mw.getLastLine(logPath, 100)
return mw.returnJson(True, 'ok', content)
@ -155,6 +178,21 @@ def runShellArgs(args):
return mw.returnJson(True, '运行成功!')
return mw.returnJson(False, '指定Hook不存在!')
def getRunShellCmd():
args = getArgs()
check_arg = checkArgs(args, ['access_key'])
if not check_arg[0]:
return check_arg[1]
script_dir = getServerDir() + "/scripts"
shellFile = script_dir + '/' + args['access_key']
param = ''
if 'params' in args:
param = args['params']
param = re.sub("\"", '', param)
cmd = "bash {} {}".format(shellFile, param)
return mw.returnJson(True, 'ok', cmd)
def runShell():
args = getArgs()
@ -191,6 +229,39 @@ def delHook():
mw.writeFile(jsonFile, json.dumps(newdata))
return mw.returnJson(True, '删除成功!')
def contentReplace(content):
service_path = mw.getServerDir()
content = content.replace('{$ROOT_PATH}', mw.getFatherDir())
return content
def configTpl():
path = getPluginDir() + '/tpl'
pathFile = os.listdir(path)
tmp = []
for one in pathFile:
file = path + '/' + one
tmp.append(file)
return mw.getJson(tmp)
def readConfigTpl():
args = getArgs()
data = checkArgs(args, ['file', 'title'])
if not data[0]:
return data[1]
if args['title'] == '':
return mw.returnJson(False, '名称不能为空!')
content = mw.readFile(args['file'])
content = contentReplace(content)
content = content.replace('{$REPO}', args['title'])
content = content.replace('{$REPO_NAME}', mw.md5(args['title']))
return mw.returnJson(True, 'ok', content)
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
@ -201,9 +272,15 @@ if __name__ == "__main__":
print(getList())
elif func == "run_shell":
print(runShell())
elif func == 'run_shell_cmd':
print(getRunShellCmd())
elif func == 'del_hook':
print(delHook())
elif func == 'get_log':
print(getLog())
elif func == 'config_tpl':
print(configTpl())
elif func == 'read_config_tpl':
print(readConfigTpl())
else:
print('error')

View File

@ -26,7 +26,6 @@ function whPost(method, args,callback){
},'json');
}
function whPostNoMessage(method, args,callback){
var req_data = {};
@ -79,29 +78,43 @@ function whPostCallbak(method, version, args,callback){
},'json');
}
function getFileName(file){
var list = file.split('/');
var f = list[list.length-1];
return f;
}
var whEditor = null;
//添加
function addHook(){
layer.open({
type: 1,
area: '600px',
area: '650px',
title: '添加Hook',
closeBtn: 1,
closeBtn: 2,
shift: 5,
shadeClose: false,
btn:['提交','关闭'],
content: "<div class='bt-form pd20'>\
<div class='line'>\
<span class='tname'>名称</span>\
<div class='info-r'><input class='bt-input-text' placeholder='Hook名称' type='text' id='hook_title' name='title' style='width:380px' /></div>\
<span class='tname'>名称</span>\
<div class='info-r'><input class='bt-input-text' placeholder='Hook名称' type='text' id='hook_title' name='title' style='width:380px' /></div>\
</div>\
<div class='line'>\
<span class='tname'>执行脚本</span>\
<div class='info-r'>\
<textarea id='hook_shell' style='width:380px; height:300px;border:1px solid #ccc;font-size:15px'></textarea>\
<span class='tname'>执行脚本</span>\
<div class='info-r'>\
<textarea id='hook_shell' style='width:380px; height:300px;border:1px solid #ccc;font-size:15px'></textarea>\
</div>\
</div>\
<div class='line'>\
<span class='tname'>模板选择</span>\
<div class='info-r'>\
<select class='bt-input-text' id='hook_tpl'>\
<option value='0'></option>\
</select>\
</div>\
</div>\
</div>\
</div>",
success:function(){
@ -121,6 +134,51 @@ function addHook(){
$(".CodeMirror-scroll").css({"height":"300px","margin":0,"padding":0});
whEditor.focus();
whPostNoMessage('config_tpl','', function(data){
var rdata = $.parseJSON(data.data);
for (var i = 0; i < rdata.length; i++) {
$('#hook_tpl').append('<option value="'+rdata[i]+'"">'+getFileName(rdata[i])+'</option>');
}
$('#hook_tpl').change(function(){
var selected = $(this).val();
if (selected == '0') {
return;
}
var title = $('#hook_title').val();
var loadT = layer.msg('配置模版获取中...',{icon:16,time:0,shade: [0.3, '#000']});
whPostNoMessage('read_config_tpl', {file:selected,title:title}, function(data){
layer.close(loadT);
var rdata = $.parseJSON(data.data);
if (!rdata.status){
layer.msg(rdata.msg,{icon:16,time:2000,shade: [5, '#000']});
return;
}
$("#hook_shell").empty().text(rdata.data);
$(".CodeMirror").remove();
whEditor = CodeMirror.fromTextArea(document.getElementById("hook_shell"), {
extraKeys: {
"Ctrl-Space": "autocomplete",
"Ctrl-F": "findPersistent",
"Ctrl-H": "replaceAll",
"Ctrl-S": function() {}
},
lineNumbers: true,
matchBrackets:true,
mode:"sh",
});
$(".CodeMirror-scroll").css({"height":"300px","margin":0,"padding":0});
whEditor.focus();
});
});
});
},
yes:function(indexs){
var loadT = layer.msg("提交中...",{icon:16,time:0});
@ -128,7 +186,19 @@ function addHook(){
title: $("#hook_title").val(),
shell: whEditor.getValue(),
}
whPost('add_hook', data, function(rdata){
// whPost('add_hook', data, function(rdata){
// var rdata = $.parseJSON(rdata.data);
// if (!rdata.status){
// layer.msg(rdata.msg,{icon:2});
// return;
// }
// layer.close(indexs);
// showMsg(rdata.msg, function(){
// getHookList();
// }, {icon:1}, 2000);
// });
whPostCallbak('addHookShell', '', data, function(rdata){
var rdata = $.parseJSON(rdata.data);
if (!rdata.status){
layer.msg(rdata.msg,{icon:2});
@ -157,6 +227,7 @@ function getHookList(){
+'<td>'+mlist[i].count+'</td>'
+'<td><a href="javascript:showWebHookCode(\''+mlist[i].url+'\',\''+mlist[i].access_key+'\')" class="btlink">查看密钥</a></td>'
+'<td><a href="javascript:runHook(\''+mlist[i].access_key+'\');" class="btlink">测试</a> | '
+'<a href="javascript:getRunHookCmd(\''+mlist[i].access_key+'\');" class="btlink">命令</a> | '
+'<a href="javascript:onlineEditFile(0,\''+ script_dir + '/'+ mlist[i].access_key+'\',\'sh\');" class="btlink">编辑</a> | '
+'<a href="javascript:getLogs(\''+ script_dir + '/' + mlist[i].access_key+'.log\');" class="btlink">日志</a> | '
+'<a href="javascript:deleteHook(\''+mlist[i].access_key+'\',\''+ mlist[i].title +'\');" class="btlink">删除</a></td>'
@ -246,6 +317,36 @@ function runHook(key){
},{icon:1},2000);
});
}
function getRunHookCmd(key) {
whPost('run_shell_cmd', {"access_key":key}, function(rdata){
var rdata = $.parseJSON(rdata.data);
if (!rdata.status){
layer.msg(rdata.msg,{icon:2});
}
layer.open({
title: "手动执行命令CMD",
area: ['600px', '180px'],
type:1,
closeBtn: 1,
shadeClose: false,
btn:["复制","取消"],
content: '<div class="pd15">\
<div class="divtable">\
<pre class="layui-code">'+rdata.data+'</pre>\
</div>\
</div>',
success:function(){
copyText(rdata.data);
},
yes:function(){
copyText(rdata.data);
}
});
});
}
//删除
function deleteHook(key, title){
layer.confirm('删除Hook-['+ title +']',{

View File

@ -0,0 +1,28 @@
echo "" > {$ROOT_PATH}/server/webhook/scripts/{$REPO_NAME}.log
export HOME=/tmp
date "+%Y-%m-%d %H:%M:%S"
git config --global credential.helper store
git config --global pull.rebase false
if [ ! -d {$ROOT_PATH}/gitcode ];then
mkdir -p {$ROOT_PATH}/gitcode
fi
if [ -d {$ROOT_PATH}/gitcode/{$REPO} ];then
cd {$ROOT_PATH}/gitcode/{$REPO} && sudo git pull
else
cd {$ROOT_PATH}/gitcode && git clone http://0.0.0.0:6660/xx/{$REPO}
fi
if [ ! -d {$ROOT_PATH}/wwwroot/{$REPO} ];then
mkdir -p {$ROOT_PATH}/wwwroot/{$REPO}
fi
rsync -vauP '--exclude=.*' {$ROOT_PATH}/gitcode/{$REPO}/ {$ROOT_PATH}/wwwroot/{$REPO}
chown -R www:www {$ROOT_PATH}/wwwroot/{$REPO}