From a119b9847df19f6cdeb1966188f8ebaf3bfcbd51 Mon Sep 17 00:00:00 2001 From: soxft Date: Mon, 27 Jun 2022 17:03:08 +0000 Subject: [PATCH] Edit: site --- class/core/site_api.py | 124 +++++++++++++++++------------ data/tpl/nginx.conf | 4 + route/static/app/site.js | 163 ++++++++++++++++++++++++++++----------- 3 files changed, 196 insertions(+), 95 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index 36a0be774..6c38d8fed 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -6,15 +6,11 @@ import sys import mw import re import json -import pwd import shutil -import psutil from flask import request -# request.urllib3.disable_warnings() - class site_api: siteName = None # 网站名称 @@ -27,6 +23,7 @@ class site_api: logsPath = None passPath = None rewritePath = None + redirectPath = None sslDir = None # ssl目录 def __init__(self): @@ -41,6 +38,10 @@ class site_api: self.passPath = self.setupPath + '/nginx/pass' # if not os.path.exists(pp): # mw.execShell("mkdir -p " + rw + " && chmod -R 755 " + rw) + + self.redirectPath = self.setupPath + '/nginx/redirect' + if not os.path.exists(self.redirectPath): + mw.execShell("mkdir -p " + self.redirectPath + " && chmod -R 755 " + self.redirectPath) self.logsPath = mw.getRootDir() + '/wwwlogs' # ssl conf @@ -1170,6 +1171,61 @@ class site_api: webname = request.form.get('webname', '') path = request.form.get('path', '0') return self.delete(sid, webname, path) + + + ## get_redirect_status + def getRedirectStatusApi(self): + _siteName = request.form.get("siteName",'') + + # read data base + data_path = self.getRedirectDataPath(_siteName) + data_content = mw.readFile(data_path) + if data_content == False: + return mw.returnJson(True, "", {"result": [], "count": 0}) + # get + # conf_path = "{}/{}/*.conf".format(self.redirectPath, siteName) + # conf_list = glob.glob(conf_path) + # if conf_list == []: + # return mw.returnJson(True, "", {"result": [], "count": 0}) + try: + data = json.loads(data_content) + except: + mw.execShell("rm -rf {}/{}".format(self.redirectPath, _siteName)) + return mw.returnJson(True, "", {"result": [], "count": 0}) + + # 处理301信息 + return mw.returnJson(True, "ok", {"result": data, "count": len(data)}) + + + # get redirect status + def setRedirectStatusApi(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 + + 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 {} + + if _rType == "301": + _rType = 0 + else: + _rType = 1 + + if _type == "path": + _type = 0 + else: + _type = 1 + + data.append({"from": _from, "type": _type, "r_type": _rType, "r_to": _to}) + mw.writeFile(data_path, json.dumps(data)) + return mw.returnJson(True, "ok") + def getProxyListApi(self): siteName = request.form.get('siteName', '') @@ -1179,31 +1235,6 @@ class site_api: url_rep = "proxy_pass (.*);|ProxyPass\s/\s(.*)|Host\s(.*);" host_rep = "Host\s(.*);" - if re.search(rep, old_conf): - # 构造代理配置 - if w == "nginx": - get.todomain = str(re.search(host_rep, old_conf).group(1)) - get.proxysite = str(re.search(url_rep, old_conf).group(1)) - else: - get.todomain = "" - get.proxysite = str(re.search(url_rep, old_conf).group(2)) - get.proxyname = "旧代理" - get.type = 1 - get.proxydir = "/" - get.advanced = 0 - get.cachetime = 1 - get.cache = 0 - get.subfilter = "[{\"sub1\":\"\",\"sub2\":\"\"},{\"sub1\":\"\",\"sub2\":\"\"},{\"sub1\":\"\",\"sub2\":\"\"}]" - - # proxyname_md5 = self.__calc_md5(get.proxyname) - # 备份并替换老虚拟主机配置文件 - os.system("cp %s %s_bak" % (conf_path, conf_path)) - conf = re.sub(rep, "", old_conf) - mw.writeFile(conf_path, conf) - - # self.createProxy(get) - mw.restartWeb() - proxyUrl = self.__read_config(self.__proxyfile) sitename = sitename proxylist = [] @@ -1363,6 +1394,9 @@ class site_api: def getRewriteConf(self, siteName): return self.rewritePath + '/' + siteName + '.conf' + def getRedirectDataPath(self, siteName): + return "{}/{}/data.json".format(self.redirectPath, siteName) + def getDirBindRewrite(self, siteName, dirname): return self.rewritePath + '/' + siteName + '_' + dirname + '.conf' @@ -1526,13 +1560,9 @@ class site_api: def getSecurity(self, sid, name): filename = self.getHostConf(name) conf = mw.readFile(filename) - - if type(conf) == bool: - return mw.returnJson(False, '读取配置文件失败!') - data = {} if conf.find('SECURITY-START') != -1: - rep = "#SECURITY-START(.|\n)*#SECURITY-END" + rep = "#SECURITY-START(\n|.){1,500}#SECURITY-END" tmp = re.search(rep, conf).group() data['fix'] = re.search( "\(.+\)\$", tmp).group().replace('(', '').replace(')$', '').replace('|', ',') @@ -1557,22 +1587,22 @@ class site_api: if os.path.exists(file): conf = mw.readFile(file) if conf.find('SECURITY-START') != -1: - rep = "\s{0,4}#SECURITY-START(\n|.)*#SECURITY-END\n?" + rep = "\s{0,4}#SECURITY-START(\n|.){1,500}#SECURITY-END\n?" conf = re.sub(rep, '', conf) mw.writeLog('网站管理', '站点[' + name + ']已关闭防盗链设置!') else: rconf = '''#SECURITY-START 防盗链配置 - location ~ .*\.(%s)$ - { - expires 30d; - access_log /dev/null; - valid_referers none blocked %s; - if ($invalid_referer){ - return 404; - } +location ~ .*\.(%s)$ +{ + expires 30d; + access_log /dev/null; + valid_referers none blocked %s; + if ($invalid_referer){ + return 404; } - #SECURITY-END - include enable-php-''' % (fix.strip().replace(',', '|'), domains.strip().replace(',', ' ')) +} +# SECURITY-END +include enable-php-''' % (fix.strip().replace(',', '|'), domains.strip().replace(',', ' ')) conf = re.sub("include\s+enable-php-", rconf, conf) mw.writeLog('网站管理', '站点[' + name + ']已开启防盗链!') mw.writeFile(file, conf) @@ -1623,8 +1653,6 @@ class site_api: os.makedirs(path) if not mw.isAppleSystem(): mw.execShell('chown -R www:www ' + path) - - mw.writeFile(path + '/index.html', '已经开始工作!!!') mw.execShell('chmod -R 755 ' + path) def nginxAddDomain(self, webname, domain, port): @@ -1876,4 +1904,4 @@ location /{ def strfToTime(self, sdate): import time return time.strftime('%Y-%m-%d', time.strptime(sdate, '%b %d %H:%M:%S %Y %Z')) - # ssl相关方法 end + # ssl相关方法 end \ No newline at end of file diff --git a/data/tpl/nginx.conf b/data/tpl/nginx.conf index ac97996bd..32c63f291 100755 --- a/data/tpl/nginx.conf +++ b/data/tpl/nginx.conf @@ -21,6 +21,10 @@ server #REWRITE-START include {$OR_REWRITE}/{$SERVER_NAME}.conf; #REWRITE-END + + #301-START + include /redirect/{$SERVER_NAME}/*.conf; + ##301-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) diff --git a/route/static/app/site.js b/route/static/app/site.js index 435e40b0d..3e64c0055 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -3,7 +3,7 @@ * @param {Number} page 当前页 * @param {String} search 搜索条件 */ -function getWeb(page, search, type_id) { + function getWeb(page, search, type_id) { search = $("#SearchValue").prop("value"); page = page == undefined ? '1':page; var order = getCookie('order'); @@ -993,8 +993,8 @@ function webEdit(id,website,endTime,addtime){ +"

配置文件

" +"

SSL

" +"

PHP版本

" - // +"

重定向

" - // +"

反向代理

" + +"

重定向

" + +"

反向代理

" +"

防盗链

" +"

响应日志

"; layer.open({ @@ -1085,7 +1085,7 @@ function setSecurity(name,id){ $.post('/site/set_security',data,function(rdata){ layer.close(loadT); layer.msg(rdata.msg,{icon:rdata.status?1:2}); - if(rdata.status) setTimeout(function(){security(id,name);},1000); + if(rdata.status) setTimeout(function(){Security(id,name);},1000); },'json'); } @@ -1421,47 +1421,125 @@ function openCache(siteName){ } //301重定向 -function to301(siteName,type){ - if(type == 1){ - type = $("input[name='status']").attr('checked')?'0':'1'; - toUrl = encodeURIComponent($("input[name='toUrl']").val()); - srcDomain = encodeURIComponent($("select[name='srcDomain']").val()); - var data = 'siteName='+siteName+'&type='+type+'&toDomain='+toUrl+'&srcDomain='+srcDomain; - $.post('site?action=Set301Status',data,function(rdata){ - To301(siteName); - layer.msg(rdata.msg,{icon:rdata.status?1:2}); +function to301(siteName, type, obj){ + + // 设置 页面展示 + // 设置 更新展示 + if(type == 1 || type == 3){ + obj = { + redirectname: (new Date()).valueOf(), + tourl: 'http://', + redirectdomain: [], + redirectpath: '', + redirecttype: '', + type: 1, + domainorpath: 'domain', + holdpath: 1 + } + layer.open({ + type: 1, + skin: 'demo-class', + area: '650px', + title: type == 1 ? '创建重定向' : '修改重定向[' + obj.redirectname + ']', + closeBtn: 2, + shift: 5, + shadeClose: false, + content: "
" + + "
" + + "
" + + "保留URI参数" + + "" + + "
" + + "
" + + "" + + "
" + + "重定向类型" + + "
" + + "" + + "重定向方式" + + "
" + + "
" + + "
" + + "重定向源" + + "
" + + "" + + "目标URL" + + "" + + "
" + + "
" + + "" + + "
" + + "
" }); - return; + setTimeout(function() { + $('.btn-colse-prosy').click(function() { + form_redirect.close(); + }); + $('.btn-submit-redirect').click(function() { + var holdpath = $('[name="holdpath"]').prop('checked') ? 1 : 0; + var redirectname = $('[name="redirectname"]').val(); + var redirecttype = $('[name="redirecttype"]').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); + }); + }, 100); } + + // 设置 提交 + + // 设置 更新 提交 + + var body = '
\ +
\ + \ +
\ +
\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +
重定向类型重定向方式保留URL参数操作
\ +
\ +
'; + $("#webedit-con").html(body); + var loadT = layer.msg(lan.site.the_msg,{icon:16,time:0,shade: [0.3, '#000']}); - $.post('/site?action=Get301Status','siteName='+siteName,function(rdata){ + + $.post('/site/get_redirect_status','siteName='+siteName, function(response) { layer.close(loadT); - var domain_tmp = rdata.domain.split(','); - var domains = ''; - var selected = ''; - for(var i=0;i"+domain_tmp[i]+""; + $("#md-301-loading").remove(); + let res = JSON.parse(response); + if (res.status === true) { + let data = res.data.result; + data.forEach(function(item){ + lan_r_type = item.r_type == 0 ? "永久重定向" : "临时重定向" + let tmp = '\ + '+item.from+'\ + '+lan_r_type+'\ + '+lan_r_type+'\ + 编辑\ + '; + $("#md-301-body").append(tmp); + }) + } else { + layer.msg(res.msg, {icon:2}); } - - if(rdata.url == null) rdata.url = ''; - var status_selected = rdata.status?'checked':''; - var isRead = rdata.status?'readonly':''; - var body = "
" - +"

"+lan.site.access_domain+"

" - +"

"+lan.site.target_url+"

" - +'
' - +'
    ' - +'
  • '+lan.site.to301_help_1+'
  • ' - +'
  • '+lan.site.to301_help_2+'
  • ' - +'
' - +"
"; - $("#webedit-con").html(body); }); } - //文件验证 // function file_check(){ // $(".check_message").html('
\ @@ -2268,13 +2346,4 @@ function tryRestartPHP(siteName){ layer.msg(data.msg,{icon:data.status?1:2,time:3000,shade: [0.3, '#000']}); },'json'); },'json'); -} - - - - - - - - - +} \ No newline at end of file