From 1eb490837063f7536942de469fc831a1d0513c37 Mon Sep 17 00:00:00 2001 From: midoks Date: Tue, 5 Jul 2022 21:29:48 +0800 Subject: [PATCH] update --- class/core/site_api.py | 73 ++++++++++++++++++++++++++++++---------- route/static/app/site.js | 68 +++++++++++++++++++++++++++++-------- 2 files changed, 110 insertions(+), 31 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index 2c9e01968..248e20278 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -135,7 +135,7 @@ class site_api: mid = request.form.get('id', '') name = request.form.get('name', '') - return self.stop(self, mid, name) + return self.stop(mid, name) def stop(self, mid, name): path = self.setupPath + '/stop' @@ -1219,13 +1219,13 @@ class site_api: content = mw.readFile(vhost_file) cnf_301 = ''' - #301-START + # 301-START include %s/*.conf; - #301-END + # 301-END ''' % (self.getRedirectPath( siteName)) cnf_301_source = ''' - #301-START + # 301-START ''' # print('operateRedirectConf', content.find('#301-END')) if content.find('#301-END') != -1: @@ -1411,13 +1411,13 @@ class site_api: content = mw.readFile(vhost_file) proxy_cnf = ''' - #PROXY-START + # PROXY-START include %s/*.conf; - #PROXY-END + # PROXY-END ''' % (self.getProxyPath(siteName)) proxy_cnf_source = ''' - #PROXY-START + # PROXY-START ''' if content.find('#PROXY-END') != -1: @@ -1436,12 +1436,34 @@ class site_api: if _id == '' or _siteName == '': return mw.returnJson(False, "必填项不能为空!") - data = mw.readFile( - "{}/{}/{}.conf".format(self.proxyPath, _siteName, _id)) + conf_file = "{}/{}/{}.conf".format(self.proxyPath, _siteName, _id) + if not os.path.exists(conf_file): + conf_file = "{}/{}/{}.conf.txt".format( + self.proxyPath, _siteName, _id) + + data = mw.readFile(conf_file) if data == False: return mw.returnJson(False, "获取失败!") return mw.returnJson(True, "ok", {"result": data}) + def setProxyStatusApi(self): + _siteName = request.form.get("siteName", '') + _status = request.form.get("status", '') + _id = request.form.get("id", '') + if _status == '' or _siteName == '' or _id == '': + return mw.returnJson(False, "必填项不能为空!") + + conf_file = "{}/{}/{}.conf".format(self.proxyPath, _siteName, _id) + conf_txt = "{}/{}/{}.conf.txt".format(self.proxyPath, _siteName, _id) + + if _status == '1': + mw.execShell('mv ' + conf_txt + ' ' + conf_file) + else: + mw.execShell('mv ' + conf_file + ' ' + conf_txt) + + mw.restartWeb() + return mw.returnJson(True, "OK") + def saveProxyConfApi(self): _siteName = request.form.get("siteName", '') _id = request.form.get("id", '') @@ -1484,6 +1506,16 @@ class site_api: mw.execShell("rm -rf {}/{}".format(self.proxyPath, _siteName)) return mw.returnJson(True, "", {"result": [], "count": 0}) + tmp = [] + for proxy in data: + proxy_dir = "{}/{}".format(self.proxyPath, _siteName) + proxy_dir_file = proxy_dir + '/' + proxy['id'] + '.conf' + if os.path.exists(proxy_dir_file): + proxy['status'] = True + else: + proxy['status'] = False + tmp.append(proxy) + return mw.returnJson(True, "ok", {"result": data, "count": len(data)}) # 设置 网站 反向代理列表 @@ -1492,6 +1524,7 @@ class site_api: _from = request.form.get('from', '') _to = request.form.get('to', '') _host = request.form.get('host', '') + _open_proxy = request.form.get('open_proxy', '') if _siteName == "" or _from == "" or _to == "" or _host == "": return mw.returnJson(False, "必填项不能为空") @@ -1514,7 +1547,7 @@ class site_api: return mw.returnJson(False, "错误的目标地址") tpl = """ -#PROXY-START/ +# PROXY-START/ location ~* \.(gif|png|jpg|css|js|woff|woff2)$ { proxy_pass {to}; @@ -1535,7 +1568,7 @@ location ~* ^{from}(.*)$ { proxy_ignore_headers Set-Cookie Cache-Control expires; add_header Cache-Control no-cache; } -#PROXY-END/ +# PROXY-END/ """ # replace @@ -1550,16 +1583,21 @@ location ~* ^{from}(.*)$ { if item["id"] == _id: return mw.returnJson(False, "已存在该规则!") if item["from"] == _from: - return mw.returnJson(False, "已存在该规则!") + return mw.returnJson(False, "代理目录已存在!") data.append({ "from": _from, "to": _to, "host": _host, "id": _id }) + + conf_file = "{}/{}.conf".format(self.getProxyPath(_siteName), _id) + if _open_proxy != 'on': + conf_file = "{}/{}.conf.txt".format( + self.getProxyPath(_siteName), _id) + mw.writeFile(data_path, json.dumps(data)) - mw.writeFile( - "{}/{}.conf".format(self.getProxyPath(_siteName), _id), tpl) + mw.writeFile(conf_file, tpl) self.operateProxyConf(_siteName, 'start') mw.restartWeb() @@ -1587,8 +1625,9 @@ location ~* ^{from}(.*)$ { if len(data) == 0: self.operateProxyConf(_siteName, 'stop') # remove conf file - mw.execShell( - "rm -rf {}/{}.conf".format(self.getProxyPath(_siteName), _id)) + cmd = "rm -rf {}/{}.conf*".format( + self.getProxyPath(_siteName), _id) + mw.execShell(cmd) except: return mw.returnJson(False, "删除失败!") @@ -1970,7 +2009,7 @@ location ~* ^{from}(.*)$ { return 404; } } - #SECURITY-END + # SECURITY-END include %s/enable-php-''' % (fix.strip().replace(',', '|'), domains.strip().replace(',', ' '), pre_path) conf = re.sub(re_path, rconf, conf) mw.writeLog('网站管理', '站点[' + name + ']已开启防盗链!') diff --git a/route/static/app/site.js b/route/static/app/site.js index cbd22d6d7..4656a6580 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -1611,9 +1611,17 @@ function to301(siteName, type, obj){ } +function toProxySwitch(){ + var status = $("input[name='open_proxy']").prop("checked")==true?1:0; + if(status==1){ + $("input[name='open_proxy']").prop("checked",false); + }else{ + $("input[name='open_proxy']").prop("checked",true); + } +} + //反向代理 function toProxy(siteName, type, obj) { - // 设置 页面展示 if(type == 1) { var proxy_form = layer.open({ @@ -1625,34 +1633,44 @@ function toProxy(siteName, type, obj) { shadeClose: false, btn: ['提交','关闭'], content: "
" + + "
" + + '开启代理'+ + "
" + + "" + + "
" + + "
" + "
"+ - "目标URL" + + "代理目录" + "
" + - "" + + "" + "
" + "
" + "
" + - "代理目录" + + "目标URL" + "
" + - "" + + "" + "发送域名" + "" + "
" + "
" + "
" + "
    " + - "
  • 代理目录:访问这个目录时将会把目标URL的内容返回并显示(需要开启高级功能)
  • " + + "
  • 代理目录:访问这个目录时将会把目标URL的内容返回并显示
  • " + "
  • 目标URL:可以填写你需要代理的站点,目标URL必须为可正常访问的URL,否则将返回错误
  • " + "
  • 发送域名:将域名添加到请求头传递到代理服务器,默认为目标URL域名,若设置不当可能导致代理无法正常运行
  • " + "
" + "
" + "
", yes:function(){ - - var to = $('[name="to"]').val(); - var from = $('[name="from"]').val(); - var host = $('[name="host"]').val(); - $.post('/site/set_proxy', {siteName: siteName,from: from,to: to,host: host}, function(res) { + var data = $('#form_redirect').serializeArray(); + var t = {}; + t['name'] = 'siteName'; + t['value'] = siteName; + data.push(t); + + var loading = layer.msg('添加中...',{icon:16,time:0,shade: [0.3, '#000']}); + $.post('/site/set_proxy',data, function(res) { + layer.close(loading); if (res.status) { layer.close(proxy_form); toProxy(siteName) @@ -1683,7 +1701,6 @@ function toProxy(siteName, type, obj) { var data = {siteName: siteName,id: obj}; $.post('/site/get_proxy_conf', data, function(res) { layer.close(laoding); - res = JSON.parse(res); if (res.status == true) { var mBody = "
\ \ @@ -1720,6 +1737,7 @@ function toProxy(siteName, type, obj) { id: obj, config: editor.getValue(), }; + $.post('/site/save_proxy_conf', data, function(res) { layer.close(load) if (res.status == true) { @@ -1735,10 +1753,25 @@ function toProxy(siteName, type, obj) { } else { layer.msg('请求错误!!', {time: 3000,icon: 2}); } - }); + },'json'); return } + if (type == 10 || type == 11) { + //[11]启动 或 停止[10] + status = type==10 ? '0' : '1'; + var loading = layer.msg(lan.site.the_msg,{icon:16,time:0,shade: [0.3, '#000']}); + $.post('/site/set_proxy_status', {siteName: siteName,'status':status,'id':obj}, function(res) { + layer.close(loading); + if (res.status == true) { + layer.msg('设置成功', {icon: 1,time: 1000}); + setTimeout(function(){toProxy(siteName);},1000); + } else { + layer.msg(res.msg, {time: 3000,icon: 2}); + } + },'json'); + } + var body = '
\
\ \ @@ -1749,6 +1782,7 @@ function toProxy(siteName, type, obj) { \ 代理目录\ 目标地址\ + 状态\ 操作\ \ \ @@ -1761,13 +1795,19 @@ function toProxy(siteName, type, obj) { var loadT = layer.msg(lan.site.the_msg,{icon:16,time:0,shade: [0.3, '#000']}); $.post("/site/get_proxy_list", {siteName: siteName},function (res) { layer.close(loadT); - $("#md-301-loading").remove(); if (res.status === true) { let data = res.data.result; data.forEach(function(item){ + // console.log(item); + var switchProxy = ''; + if (!item['status']){ + switchProxy = ''; + } + let tmp = '\ '+item.from+'\ '+item.to+'\ + '+switchProxy+'\ \ 详细 |\ 删除\