From 44bcdef9bf666ae476dbaff267a3a87495097dd4 Mon Sep 17 00:00:00 2001 From: soxft Date: Mon, 27 Jun 2022 17:26:19 +0000 Subject: [PATCH 1/7] test: setRedirectApi --- class/core/site_api.py | 44 ++++++++++++++++++++++++++++++---------- route/static/app/site.js | 2 +- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index 6c38d8fed..404848e25 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -1174,7 +1174,7 @@ class site_api: ## get_redirect_status - def getRedirectStatusApi(self): + def getRedirectApi(self): _siteName = request.form.get("siteName",'') # read data base @@ -1198,12 +1198,13 @@ class site_api: # get redirect status - def setRedirectStatusApi(self): + def setRedirectApi(self): _siteName = request.form.get("siteName",'') _from = request.form.get("from",'') # from (example.com / /test/) _to = request.form.get("to",'') # redirect to _type = request.form.get("type",'') # path / domain _rType = request.form.get("r_type",'') # redirect type + _keepPath = request.form.get("keep_path",'') # keep path if _siteName == '' or _from == '' or _to == '' or _type == '' or _rType == '': return mw.returnJson(False, "必填项不能为空!") @@ -1212,18 +1213,36 @@ class site_api: data_content = mw.readFile(data_path) if os.path.exists(data_path) else "" data = json.loads(data_content) if data_content != "" else {} - if _rType == "301": - _rType = 0 - else: - _rType = 1 - - if _type == "path": - _type = 0 + _rTypeCode = 0 if _rType == "301" else 1 + _typeCode = 0 if _type == "path" else 1 + _keepPath = 1 if _keepPath == "true" else 0 + + redirect_type = "permanent" if _rType == 0 else "temporary" + pathQuery = "" + if _keepPath == 1: + # path + if _rTypeCode == 0: + pathQuery = "$1" + _from = "{}(.*)".format(_from) + #domain + else: + pathQuery = "$request_uri" + + file_content = "" + # path + if _typeCode == 0: + file_content = """if ($host ~ '^{}'){ + return {} {}{}; + }""".format(_from, _rType ,_to, pathQuery) else: - _type = 1 + file_content = "rewrite ^{} {}{} {};".format(_from, _to, pathQuery, redirect_type) - data.append({"from": _from, "type": _type, "r_type": _rType, "r_to": _to}) + _hash = mw.md5("{}+{}".format(file_content, time.time())) + + # write data json file + data.append({"from": _from, "type": _typeCode, "r_type": _rTypeCode, "r_to": _to, 'keep_path': _keepPath, 'id': _hash}) mw.writeFile(data_path, json.dumps(data)) + mw.writeFile("{}/{}.conf".format(self.getRedirectPath(_siteName), _hash), file_content) return mw.returnJson(True, "ok") @@ -1397,6 +1416,9 @@ class site_api: def getRedirectDataPath(self, siteName): return "{}/{}/data.json".format(self.redirectPath, siteName) + def getRedirectPath(self, siteName): + return "{}/{}/".format(self.redirectPath, siteName) + def getDirBindRewrite(self, siteName, dirname): return self.rewritePath + '/' + siteName + '_' + dirname + '.conf' diff --git a/route/static/app/site.js b/route/static/app/site.js index 3e64c0055..cf119feb3 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -1518,7 +1518,7 @@ function to301(siteName, type, obj){ var loadT = layer.msg(lan.site.the_msg,{icon:16,time:0,shade: [0.3, '#000']}); - $.post('/site/get_redirect_status','siteName='+siteName, function(response) { + $.post('/site/get_redirect','siteName='+siteName, function(response) { layer.close(loadT); $("#md-301-loading").remove(); let res = JSON.parse(response); From 88a41bb561b030bf3806abd413c86e68893b7679 Mon Sep 17 00:00:00 2001 From: soxft Date: Tue, 28 Jun 2022 03:11:33 +0000 Subject: [PATCH 2/7] =?UTF-8?q?feta:=20=E5=9F=BA=E6=9C=AC=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0/=E8=8E=B7=E5=8F=96=20301=20=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class/core/site_api.py | 16 ++++++----- route/static/app/site.js | 59 ++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index 404848e25..fb9c623c1 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -1181,6 +1181,7 @@ class site_api: data_path = self.getRedirectDataPath(_siteName) data_content = mw.readFile(data_path) if data_content == False: + mw.execShell("mkdir {}/{}".format(self.redirectPath, _siteName)) return mw.returnJson(True, "", {"result": [], "count": 0}) # get # conf_path = "{}/{}/*.conf".format(self.redirectPath, siteName) @@ -1211,13 +1212,11 @@ class site_api: data_path = self.getRedirectDataPath(_siteName) data_content = mw.readFile(data_path) if os.path.exists(data_path) else "" - data = json.loads(data_content) if data_content != "" else {} + data = json.loads(data_content) if data_content != "" else [] _rTypeCode = 0 if _rType == "301" else 1 _typeCode = 0 if _type == "path" else 1 - _keepPath = 1 if _keepPath == "true" else 0 - redirect_type = "permanent" if _rType == 0 else "temporary" pathQuery = "" if _keepPath == 1: # path @@ -1231,11 +1230,14 @@ class site_api: file_content = "" # path if _typeCode == 0: - file_content = """if ($host ~ '^{}'){ - return {} {}{}; - }""".format(_from, _rType ,_to, pathQuery) - else: + redirect_type = "permanent" if _rTypeCode == 0 else "temporary" file_content = "rewrite ^{} {}{} {};".format(_from, _to, pathQuery, redirect_type) + else: + redirect_type = "301" if _rTypeCode == 0 else "302" + _if = "if ($host ~ '^{}')".format(_from) + _return = "return {} {}{}; ".format(redirect_type, _to, pathQuery) + file_content = _if + "{\r\n " + _return + "\r\n}" + _hash = mw.md5("{}+{}".format(file_content, time.time())) diff --git a/route/static/app/site.js b/route/static/app/site.js index cf119feb3..1952077c3 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -1427,16 +1427,14 @@ function to301(siteName, type, obj){ // 设置 更新展示 if(type == 1 || type == 3){ obj = { - redirectname: (new Date()).valueOf(), - tourl: 'http://', - redirectdomain: [], - redirectpath: '', - redirecttype: '', + to: 'http://', + from: '', + r_type: '', type: 1, - domainorpath: 'domain', - holdpath: 1 + type: 'domain', + keep_path: 1 } - layer.open({ + var redirect_form = layer.open({ type: 1, skin: 'demo-class', area: '650px', @@ -1448,26 +1446,22 @@ function to301(siteName, type, obj){ "
" + "
" + "保留URI参数" + - "" + - "
" + + "" + "
" + - "" + "
" + "重定向类型" + "
" + - "" + + "" + "重定向方式" + - "
" + + "
" + "" + "
" + "重定向源" + "
" + - "" + + "" + "目标URL" + - "" + + "" + "
" + "
" + "" + @@ -1476,17 +1470,27 @@ function to301(siteName, type, obj){ }); setTimeout(function() { $('.btn-colse-prosy').click(function() { - form_redirect.close(); + layer.close(redirect_form); }); $('.btn-submit-redirect').click(function() { - var holdpath = $('[name="holdpath"]').prop('checked') ? 1 : 0; - var redirectname = $('[name="redirectname"]').val(); - var redirecttype = $('[name="redirecttype"]').val(); + var keep_path = $('[name="keep_path"]').prop('checked') ? 1 : 0; + var r_type = $('[name="r_type"]').val(); var type = $('[name="type"]').val(); - var redirectpath = $('[name="redirectpath"]').val(); - var redirectdomain = JSON.stringify($('.selectpicker').val() || []); - var tourl = $(domainorpath == 'path' ? '[name="tourl1"]' : '[name="tourl"]').val(); - console.log(type, holdpath, redirectname, redirecttype, domainorpath, redirectpath, redirectdomain, tourl); + var from = $('[name="from"]').val(); + var to = $('[name="to"]').val(); + + $.post('/site/set_redirect', { + siteName: siteName, + type: type, + r_type: r_type, + from: from, + to: to, + keep_path: keep_path + }, function(res) { + res = JSON.parse(res); + console.log(res) + to301(siteName) + }); }); }, 100); } @@ -1526,11 +1530,12 @@ function to301(siteName, type, obj){ let data = res.data.result; data.forEach(function(item){ lan_r_type = item.r_type == 0 ? "永久重定向" : "临时重定向" + keep_path = item.keep_path == 0 ? "不保留" : "保留" let tmp = '\ '+item.from+'\ '+lan_r_type+'\ - '+lan_r_type+'\ - 编辑\ + '+keep_path+'\ + 编辑\ '; $("#md-301-body").append(tmp); }) From 63df48fba43c89d9e48d941813fec615aebb4ac3 Mon Sep 17 00:00:00 2001 From: soxft Date: Tue, 28 Jun 2022 06:02:23 +0000 Subject: [PATCH 3/7] fix --- class/core/site_api.py | 13 +++---------- data/tpl/nginx.conf | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index fb9c623c1..df6c22dfb 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -1714,21 +1714,14 @@ include enable-php-''' % (fix.strip().replace(',', '|'), domains.strip().replace content = content.replace('{$ROOT_DIR}', self.sitePath) content = content.replace('{$PHPVER}', self.phpVersion) content = content.replace('{$OR_REWRITE}', self.rewritePath) + content = content.replace('{$OR_REDIRECT}', self.redirectPath) logsPath = mw.getLogsDir() content = content.replace('{$LOGPATH}', logsPath) mw.writeFile(vhost_file, content) - - rewrite_content = ''' -location /{ - if (!-e $request_filename) { - rewrite ^(.*)$ /index.php/$1 last; - break; - } -} - ''' + rewrite_file = self.rewritePath + '/' + self.siteName + '.conf' - mw.writeFile(rewrite_file, rewrite_content) + mw.writeFile(rewrite_file, "") def add(self, webname, port, ps, path, version): siteMenu = json.loads(webname) diff --git a/data/tpl/nginx.conf b/data/tpl/nginx.conf index 32c63f291..5c3630f7e 100755 --- a/data/tpl/nginx.conf +++ b/data/tpl/nginx.conf @@ -23,7 +23,7 @@ server #REWRITE-END #301-START - include /redirect/{$SERVER_NAME}/*.conf; + include {$OR_REDIRECT}/{$SERVER_NAME}/*.conf; ##301-END #禁止访问的文件或目录 From 217ed048ca6975b68ddd519b25ca66b1bfe0a7c9 Mon Sep 17 00:00:00 2001 From: soxft Date: Tue, 28 Jun 2022 06:35:33 +0000 Subject: [PATCH 4/7] fix: setRedirect --- class/core/site_api.py | 66 ++++++++++++++++++++++++++++++---------- route/static/app/site.js | 39 +++++++++++++++++++++--- 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index df6c22dfb..d211507ee 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -1216,36 +1216,70 @@ class site_api: _rTypeCode = 0 if _rType == "301" else 1 _typeCode = 0 if _type == "path" else 1 - - pathQuery = "" - if _keepPath == 1: - # path - if _rTypeCode == 0: - pathQuery = "$1" - _from = "{}(.*)".format(_from) - #domain - else: - pathQuery = "$request_uri" - + + file_content = "" # path if _typeCode == 0: redirect_type = "permanent" if _rTypeCode == 0 else "temporary" - file_content = "rewrite ^{} {}{} {};".format(_from, _to, pathQuery, redirect_type) + if not _from.startswith("/"): + _from = "/{}".format(_from) + if _keepPath == 1: + _to = "{}$1".format(_to) + _from = "{}(.*)".format(_from) + file_content = "rewrite ^{} {} {};".format(_from, _to, redirect_type) + # domain else: + if _keepPath == 1: + _to = "{}$request_uri".format(_to) + redirect_type = "301" if _rTypeCode == 0 else "302" _if = "if ($host ~ '^{}')".format(_from) - _return = "return {} {}{}; ".format(redirect_type, _to, pathQuery) + _return = "return {} {}; ".format(redirect_type, _to) file_content = _if + "{\r\n " + _return + "\r\n}" - _hash = mw.md5("{}+{}".format(file_content, time.time())) + _id = mw.md5("{}+{}".format(file_content, _siteName)) + # 防止规则重复 + for item in data: + if item["r_from"] == _from: + return mw.returnJson(False, "重复的规则!") + + rep = "http(s)?\:\/\/([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+([a-zA-Z0-9][a-zA-Z0-9]{0,62})+.?" + if not re.match(rep, _to): + return mw.returnJson(False, "错误的目标地址") + # write data json file - data.append({"from": _from, "type": _typeCode, "r_type": _rTypeCode, "r_to": _to, 'keep_path': _keepPath, 'id': _hash}) + data.append({"r_from": _from, "type": _typeCode, "r_type": _rTypeCode, "r_to": _to, 'keep_path': _keepPath, 'id': _id}) mw.writeFile(data_path, json.dumps(data)) - mw.writeFile("{}/{}.conf".format(self.getRedirectPath(_siteName), _hash), file_content) + mw.writeFile("{}/{}.conf".format(self.getRedirectPath(_siteName), _id), file_content) + mw.restartWeb() return mw.returnJson(True, "ok") + + + # 删除指定重定向 + def delRedirectApi(self): + _siteName = request.form.get("siteName",'') + _id = request.form.get("id",'') + if _id == '' or _siteName == '': + return mw.returnJson(False, "必填项不能为空!") + + try: + data_path = self.getRedirectDataPath(_siteName) + data_content = mw.readFile(data_path) if os.path.exists(data_path) else "" + data = json.loads(data_content) if data_content != "" else [] + for item in data: + if item["id"] == _id: + data.remove(item) + break + # write database + mw.writeFile(data_path, json.dumps(data)) + # remove conf file + mw.execShell("rm -rf {}/{}.conf".format(self.getRedirectPath(_siteName), _id)) + except: + return mw.returnJson(False, "删除失败!") + return mw.returnJson(True, "删除成功!") def getProxyListApi(self): diff --git a/route/static/app/site.js b/route/static/app/site.js index 1952077c3..2abee7936 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -1425,7 +1425,7 @@ function to301(siteName, type, obj){ // 设置 页面展示 // 设置 更新展示 - if(type == 1 || type == 3){ + if(type == 1 || type == 2){ obj = { to: 'http://', from: '', @@ -1488,13 +1488,42 @@ function to301(siteName, type, obj){ keep_path: keep_path }, function(res) { res = JSON.parse(res); - console.log(res) - to301(siteName) + if (res.status) { + layer.close(redirect_form); + setTimeout(function() { + location.reload(); + }, 1000); + to301(siteName) + } else { + layer.msg(res.msg); + } }); }); }, 100); } + if (type == 3) { + $.post('/site/del_redirect', { + siteName: siteName, + id: obj, + }, function(res) { + res = JSON.parse(res); + if (res.status == true) { + layer.msg('删除成功', { + time: 1000, + icon: 1 + }); + to301(siteName) + } else { + layer.msg(res.msg, { + time: 1000, + icon: 2 + }); + } + }); + return + } + // 设置 提交 // 设置 更新 提交 @@ -1532,10 +1561,10 @@ function to301(siteName, type, obj){ lan_r_type = item.r_type == 0 ? "永久重定向" : "临时重定向" keep_path = item.keep_path == 0 ? "不保留" : "保留" let tmp = '\ - '+item.from+'\ + '+item.r_from+'\ '+lan_r_type+'\ '+keep_path+'\ - 编辑\ + 编辑 | 删除\ '; $("#md-301-body").append(tmp); }) From 9398e6eead5b761d41f2fca2c1f88dbe74fb6a92 Mon Sep 17 00:00:00 2001 From: soxft Date: Tue, 28 Jun 2022 06:37:29 +0000 Subject: [PATCH 5/7] fix: keep_path --- class/core/site_api.py | 2 +- route/static/app/site.js | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index d211507ee..82415c579 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -1216,7 +1216,7 @@ class site_api: _rTypeCode = 0 if _rType == "301" else 1 _typeCode = 0 if _type == "path" else 1 - + _keepPath = 1 if _keepPath == "1" else 0 file_content = "" # path diff --git a/route/static/app/site.js b/route/static/app/site.js index 2abee7936..2f0480299 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -1490,9 +1490,6 @@ function to301(siteName, type, obj){ res = JSON.parse(res); if (res.status) { layer.close(redirect_form); - setTimeout(function() { - location.reload(); - }, 1000); to301(siteName) } else { layer.msg(res.msg); From 87c7492555b7f85ee136c1bd56cf50ca7950151c Mon Sep 17 00:00:00 2001 From: soxft Date: Wed, 29 Jun 2022 02:35:43 +0000 Subject: [PATCH 6/7] fix --- class/core/site_api.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index 82415c579..88375b710 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -3,6 +3,9 @@ import time import os import sys +from unicodedata import name + +from sqlalchemy import false import mw import re import json @@ -1217,11 +1220,26 @@ class site_api: _rTypeCode = 0 if _rType == "301" else 1 _typeCode = 0 if _type == "path" else 1 _keepPath = 1 if _keepPath == "1" else 0 - + + # check if domain exists in site + if _typeCode == 1: + pid = mw.M('domain').where("name=?", (_siteName,)).field( + 'id,pid,name,port,addtime').select() + site_domain_lists = mw.M('domain').where("pid=?", (pid[0]['pid'],)).field( + 'name').select() + found = False + for item in site_domain_lists: + if item['name'] == _from: + found = True + break + if found == False: + return mw.returnJson(False, "域名不存在!") + + file_content = "" # path if _typeCode == 0: - redirect_type = "permanent" if _rTypeCode == 0 else "temporary" + redirect_type = "permanent" if _rTypeCode == 0 else "redirect" if not _from.startswith("/"): _from = "/{}".format(_from) if _keepPath == 1: From e45d2a06094b195b4ab45d8c94c87f41c2c2ebee Mon Sep 17 00:00:00 2001 From: soxft Date: Wed, 29 Jun 2022 04:00:52 +0000 Subject: [PATCH 7/7] test --- class/core/site_api.py | 33 +++++++++++++++++ route/static/app/site.js | 80 +++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 9 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index 7dd4dd1c1..4f059dbb3 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -1201,6 +1201,39 @@ class site_api: return mw.returnJson(True, "ok", {"result": data, "count": len(data)}) + def getRedirectConfApi(self): + _siteName = request.form.get("siteName",'') + _id = request.form.get("id",'') + if _id == '' or _siteName == '': + return mw.returnJson(False, "必填项不能为空!") + + data = mw.readFile("{}/{}/{}.conf".format(self.redirectPath, _siteName, _id)) + if data == False: + return mw.returnJson(False, "获取失败!") + return mw.returnJson(True, "ok", {"result": data}) + + + def saveRedirectConfApi(self): + _siteName = request.form.get("siteName",'') + _id = request.form.get("id",'') + _config = request.form.get("config", "") + if _id == '' or _siteName == '': + return mw.returnJson(False, "必填项不能为空!") + + _old_config = mw.readFile("{}/{}/{}.conf".format(self.redirectPath, _siteName, _id)) + if _old_config == False: + return mw.returnJson(False, "非法操作") + + mw.writeFile("{}/{}/{}.conf".format(self.redirectPath, _siteName, _id), _config) + rule_test = mw.checkWebConfig() + if rule_test != True: + mw.writeFile("{}/{}/{}.conf".format(self.redirectPath, _siteName, _id), _old_config) + return mw.returnJson(False, "Nginx 配置测试不通过, 请重试: {}".format(rule_test)) + + mw.restartWeb() + return mw.returnJson(True, "ok") + + # get redirect status def setRedirectApi(self): _siteName = request.form.get("siteName",'') diff --git a/route/static/app/site.js b/route/static/app/site.js index 8d8739201..6694c71cc 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -1424,14 +1424,13 @@ function openCache(siteName){ function to301(siteName, type, obj){ // 设置 页面展示 - // 设置 更新展示 - if(type == 1 || type == 2){ + if(type == 1) { obj = { to: 'http://', from: '', r_type: '', type: 1, - type: 'domain', + type: 'path', keep_path: 1 } var redirect_form = layer.open({ @@ -1492,14 +1491,16 @@ function to301(siteName, type, obj){ layer.close(redirect_form); to301(siteName) } else { - layer.msg(res.msg); + layer.msg(res.msg, { + icon: 2 + }); } }); }); }, 100); } - if (type == 3) { + if (type == 2) { $.post('/site/del_redirect', { siteName: siteName, id: obj, @@ -1521,9 +1522,70 @@ function to301(siteName, type, obj){ return } - // 设置 提交 - - // 设置 更新 提交 + if (type == 3) { + var laoding = layer.load(); + $.post('/site/get_redirect_conf', { + siteName: siteName, + id: obj, + }, function(res) { + layer.close(laoding); + res = JSON.parse(res); + if (res.status == true) { + var mBody = "
\ + \ +
\ + \ +
    \ +
  • 此处为重定向配置文件,若您不了解配置规则,请勿随意修改.
  • \ +
\ +
\ +
"; + var index = layer.open({ + type: 1, + title: '编辑配置文件', + closeBtn: 1, + shadeClose: false, + area: ['500px', '500px'], + content: mBody, + success: function () { + $("#SaveRedirectConfigFileBtn").click(function(){ + $("#configBody").empty(); + $("#configBody").text(editor.getValue()); + var load = layer.load() + $.post('/site/save_redirect_conf', { + siteName: siteName, + id: obj, + config: editor.getValue() + }, function(res) { + layer.close(load) + var res = JSON.parse(res); + if (res.status == true) { + layer.msg('保存成功', { + icon: 1 + }); + layer.close(index); + } else { + layer.msg(res.msg, { + time: 1000, + icon: 2 + }); + } + }); + }) + } + }); + var editor = CodeMirror.fromTextArea(document.getElementById("configBody"), { + extraKeys: {"Ctrl-Space": "autocomplete"}, + lineNumbers: true, + matchBrackets:true, + }); + $(".CodeMirror-scroll").css({"height":"300px","margin":0,"padding":0}); + } else { + + } + }); + return + } var body = '
\
\ @@ -1561,7 +1623,7 @@ function to301(siteName, type, obj){ '+item.r_from+'\ '+lan_r_type+'\ '+keep_path+'\ - 编辑 | 删除\ + 详细 | 删除\ '; $("#md-301-body").append(tmp); })