mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-10 01:29:26 +08:00
fix: setRedirect
This commit is contained in:
+50
-16
@@ -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):
|
||||
|
||||
@@ -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 = '<tr>\
|
||||
<td><span data-index="1"><span>'+item.from+'</span></span></td>\
|
||||
<td><span data-index="1"><span>'+item.r_from+'</span></span></td>\
|
||||
<td><span data-index="2"><span>'+lan_r_type+'</span></span></td>\
|
||||
<td><span data-index="2"><span>'+keep_path+'</span></span></td>\
|
||||
<td><span data-index="4" class="btlink">编辑</span></td>\
|
||||
<td><span data-index="4" class="btlink">编辑</span> | <span data-index="5" onclick="to301(\''+siteName+'\', 3, \''+ item.id +'\')" class="btlink">删除</span></td>\
|
||||
</tr>';
|
||||
$("#md-301-body").append(tmp);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user