mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-10 05:59:25 +08:00
+123
-21
@@ -3,6 +3,9 @@
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
from unicodedata import name
|
||||
|
||||
from sqlalchemy import false
|
||||
import mw
|
||||
import re
|
||||
import json
|
||||
@@ -1174,13 +1177,14 @@ class site_api:
|
||||
|
||||
|
||||
## get_redirect_status
|
||||
def getRedirectStatusApi(self):
|
||||
def getRedirectApi(self):
|
||||
_siteName = request.form.get("siteName",'')
|
||||
|
||||
# read data base
|
||||
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)
|
||||
@@ -1197,34 +1201,136 @@ 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 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, "必填项不能为空!")
|
||||
|
||||
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 []
|
||||
|
||||
if _rType == "301":
|
||||
_rType = 0
|
||||
_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 "redirect"
|
||||
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:
|
||||
_rType = 1
|
||||
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)
|
||||
file_content = _if + "{\r\n " + _return + "\r\n}"
|
||||
|
||||
if _type == "path":
|
||||
_type = 0
|
||||
else:
|
||||
_type = 1
|
||||
|
||||
data.append({"from": _from, "type": _type, "r_type": _rType, "r_to": _to})
|
||||
_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({"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), _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):
|
||||
@@ -1397,6 +1503,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'
|
||||
|
||||
@@ -1692,21 +1801,14 @@ include enable-php-''' % (fix.strip().replace(',', '|'), domains.strip().replace
|
||||
content = content.replace('{$PHP_DIR}', self.setupPath + '/php')
|
||||
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)
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ server
|
||||
#REWRITE-END
|
||||
|
||||
#301-START
|
||||
include /redirect/{$SERVER_NAME}/*.conf;
|
||||
include {$OR_REDIRECT}/{$SERVER_NAME}/*.conf;
|
||||
##301-END
|
||||
|
||||
#禁止访问的文件或目录
|
||||
|
||||
+126
-33
@@ -1424,19 +1424,16 @@ function openCache(siteName){
|
||||
function to301(siteName, type, obj){
|
||||
|
||||
// 设置 页面展示
|
||||
// 设置 更新展示
|
||||
if(type == 1 || type == 3){
|
||||
if(type == 1) {
|
||||
obj = {
|
||||
redirectname: (new Date()).valueOf(),
|
||||
tourl: 'http://',
|
||||
redirectdomain: [],
|
||||
redirectpath: '',
|
||||
redirecttype: '',
|
||||
to: 'http://',
|
||||
from: '',
|
||||
r_type: '',
|
||||
type: 1,
|
||||
domainorpath: 'domain',
|
||||
holdpath: 1
|
||||
type: 'path',
|
||||
keep_path: 1
|
||||
}
|
||||
layer.open({
|
||||
var redirect_form = layer.open({
|
||||
type: 1,
|
||||
skin: 'demo-class',
|
||||
area: '650px',
|
||||
@@ -1448,26 +1445,22 @@ function to301(siteName, type, obj){
|
||||
"<div class='line' style='overflow:hidden;height: 40px;'>" +
|
||||
"<div style='display: inline-block;'>" +
|
||||
"<span class='tname' style='margin-left:10px;position: relative;top: -5px;'>保留URI参数</span>" +
|
||||
"<input class='btswitch btswitch-ios' id='holdpath' type='checkbox' name='holdpath' " + (obj.holdpath == 1 ? 'checked="checked"' : '') + " /><label class='btswitch-btn phpmyadmin-btn' for='holdpath' style='float:left'></label>" +
|
||||
"<input class='btswitch btswitch-ios' id='keep_path' type='checkbox' name='keep_path' " + (obj.keep_path == 1 ? 'checked="checked"' : '') + " /><label class='btswitch-btn phpmyadmin-btn' for='keep_path' style='float:left'></label>" +
|
||||
"</div>" +
|
||||
"</div>" +
|
||||
"<div class='line' style='clear:both;display:none;'>" +
|
||||
"<span class='tname'>重定向名称</span>" +
|
||||
"<div class='info-r ml0'><input name='redirectname' class='bt-input-text mr5' " + (type == 1 ? '' : 'disabled="disabled"') + " type='text' style='width:300px' value='" + obj.redirectname + "'></div>" +
|
||||
"</div>" +
|
||||
"<div class='line' style='clear:both;'>" +
|
||||
"<span class='tname'>重定向类型</span>" +
|
||||
"<div class='info-r ml0'>" +
|
||||
"<select class='bt-input-text mr5' name='type' style='width:100px'><option value='domain' " + (obj.domainorpath == 'domain' ? 'selected ="selected"' : "") + ">域名</option><option value='path' " + (obj.domainorpath == 'path' ? 'selected ="selected"' : "") + ">路径</option></select>" +
|
||||
"<select class='bt-input-text mr5' name='type' style='width:100px'><option value='domain' " + (obj.type == 'domain' ? 'selected ="selected"' : "") + ">域名</option><option value='path' " + (obj.type == 'path' ? 'selected ="selected"' : "") + ">路径</option></select>" +
|
||||
"<span class='mlr15'>重定向方式</span>" +
|
||||
"<select class='bt-input-text ml10' name='redirecttype' style='width:100px'><option value='301' " + (obj.redirecttype == '301' ? 'selected ="selected"' : "") + " >301</option><option value='302' " + (obj.redirecttype == '302' ? 'selected ="selected"' : "") + ">302</option></select></div>" +
|
||||
"<select class='bt-input-text ml10' name='r_type' style='width:100px'><option value='301' " + (obj.r_type == '301' ? 'selected ="selected"' : "") + " >301</option><option value='302' " + (obj.r_type == '302' ? 'selected ="selected"' : "") + ">302</option></select></div>" +
|
||||
"</div>" +
|
||||
"<div class='line redirectdomain'>" +
|
||||
"<span class='tname'>重定向源</span>" +
|
||||
"<div class='info-r ml0'>" +
|
||||
"<input name='redirectpath' placeholder='域名或路径' class='bt-input-text mr5' type='text' style='width:200px;float: left;margin-right:0px' value='" + obj.redirectpath + "'>" +
|
||||
"<input name='from' placeholder='域名或路径' class='bt-input-text mr5' type='text' style='width:200px;float: left;margin-right:0px' value='" + obj.from + "'>" +
|
||||
"<span class='tname' style='width:90px'>目标URL</span>" +
|
||||
"<input name='tourl' class='bt-input-text mr5' type='text' style='width:200px' value='" + obj.tourl + "'>" +
|
||||
"<input name='to' class='bt-input-text mr5' type='text' style='width:200px' value='" + obj.to + "'>" +
|
||||
"</div>" +
|
||||
"</div>" +
|
||||
"</div>" +
|
||||
@@ -1476,24 +1469,123 @@ 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);
|
||||
if (res.status) {
|
||||
layer.close(redirect_form);
|
||||
to301(siteName)
|
||||
} else {
|
||||
layer.msg(res.msg, {
|
||||
icon: 2
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}, 100);
|
||||
}
|
||||
|
||||
// 设置 提交
|
||||
if (type == 2) {
|
||||
$.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
|
||||
}
|
||||
|
||||
// 设置 更新 提交
|
||||
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 = "<div class='webEdit-box' style='padding: 20px'>\
|
||||
<textarea style='height: 320px; width: 445px; margin-left: 20px; line-height:18px' id='configBody'>"+res.data.result+"</textarea>\
|
||||
<div class='info-r'>\
|
||||
<button id='SaveRedirectConfigFileBtn' class='btn btn-success btn-sm' style='margin-top:15px;'>保存</button>\
|
||||
<ul class='help-info-text c7 ptb10'>\
|
||||
<li>此处为重定向配置文件,若您不了解配置规则,请勿随意修改.</li>\
|
||||
</ul>\
|
||||
</div>\
|
||||
</div>";
|
||||
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 = '<div id="redirect_list" class="bt_table">\
|
||||
<div style="padding-bottom: 10px">\
|
||||
@@ -1518,7 +1610,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);
|
||||
@@ -1526,11 +1618,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 = '<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>'+lan_r_type+'</span></span></td>\
|
||||
<td><span data-index="4">编辑</span></td>\
|
||||
<td><span data-index="2"><span>'+keep_path+'</span></span></td>\
|
||||
<td><span data-index="4" onclick="to301(\''+siteName+'\', 3, \''+ item.id +'\')" class="btlink">详细</span> | <span data-index="5" onclick="to301(\''+siteName+'\', 2, \''+ item.id +'\')" class="btlink">删除</span></td>\
|
||||
</tr>';
|
||||
$("#md-301-body").append(tmp);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user