From da895407db31424c1d0617fc1408648ff5ed79a6 Mon Sep 17 00:00:00 2001 From: midoks Date: Sun, 24 Feb 2019 18:04:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=90=E7=9B=AE=E5=BD=95=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class/core/site_api.py | 217 +++++++++++++++++++++--------------- data/tpl/nginx_dirbind.conf | 15 ++- route/static/app/site.js | 51 ++++----- 3 files changed, 160 insertions(+), 123 deletions(-) diff --git a/class/core/site_api.py b/class/core/site_api.py index 81b110c18..0ec168bd9 100755 --- a/class/core/site_api.py +++ b/class/core/site_api.py @@ -575,6 +575,57 @@ class site_api: port = request.form.get('port', '').encode('utf-8') return self.add(webname, port, ps, path, version) + def addDomainApi(self): + isError = public.checkWebConfig() + if isError != True: + return public.returnJson(False, 'ERROR: 检测到配置文件有错误,请先排除后再操作

' + isError.replace("\n", '
') + '
') + + domain = request.form.get('domain', '').encode('utf-8') + webname = request.form.get('webname', '').encode('utf-8') + pid = request.form.get('id', '').encode('utf-8') + if len(domain) < 3: + return public.returnJson(False, '域名不能为空!') + domains = domain.split(',') + for domain in domains: + if domain == "": + continue + domain = domain.split(':') + # print domain + domain_name = self.toPunycode(domain[0]) + domain_port = '80' + + reg = "^([\w\-\*]{1,100}\.){1,4}([\w\-]{1,24}|[\w\-]{1,24}\.[\w\-]{1,24})$" + if not re.match(reg, domain_name): + return public.returnJson(False, '域名格式不正确!') + + if len(domain) == 2: + domain_port = domain[1] + if domain_port == "": + domain_port = "80" + + if not public.checkPort(domain_port): + return public.returnJson(False, '端口范围不合法!') + + opid = public.M('domain').where( + "name=? AND (port=? OR pid=?)", (domain, domain_port, pid)).getField('pid') + if opid: + if public.M('sites').where('id=?', (opid,)).count(): + return public.returnJson(False, '指定域名已绑定过!') + public.M('domain').where('pid=?', (opid,)).delete() + + if public.M('binding').where('domain=?', (domain,)).count(): + return public.returnJson(False, '您添加的域名已存在!') + + self.nginxAddDomain(webname, domain_name, domain_port) + + public.restartWeb() + msg = public.getInfo('网站[{1}]添加域名[{2}]成功!', (webname, domain_name)) + public.writeLog('TYPE_SITE', msg) + public.M('domain').add('pid,name,port,addtime', + (pid, domain_name, domain_port, public.getDate())) + + return public.returnJson(True, '域名添加成功!') + def addDirBindApi(self): pid = request.form.get('id', '').encode('utf-8') domain = request.form.get('domain', '').encode('utf-8') @@ -595,58 +646,29 @@ class site_api: "id=?", (pid,)).field('id,path,name').find() webdir = siteInfo['path'] + '/' + dirName - sql = public.M('binding') - if sql.where("domain=?", (domain,)).count() > 0: + if public.M('binding').where("domain=?", (domain,)).count() > 0: return public.returnJson(False, '您添加的域名已存在!') if public.M('domain').where("name=?", (domain,)).count() > 0: return public.returnJson(False, '您添加的域名已存在!') filename = self.getHostConf(siteInfo['name']) - + conf = public.readFile(filename) if conf: rep = "enable-php-([0-9]{2,3})\.conf" tmp = re.search(rep, conf).groups() version = tmp[0] - bindingConf = ''' -#BINDING-%s-START -server -{ - listen %s; - server_name %s; - index index.php index.html index.htm default.php default.htm default.html; - root %s; - - include enable-php-%s.conf; - include %s/panel/vhost/rewrite/%s.conf; - #禁止访问的文件或目录 - location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) - { - return 404; - } - - #一键申请SSL证书验证目录相关设置 - location ~ \.well-known{ - allow all; - } - - location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$ - { - expires 30d; - error_log off; - access_log /dev/null; - } - location ~ .*\\.(js|css)?$ - { - expires 12h; - error_log off; - access_log /dev/null; - } - access_log %s.log; - error_log %s.error.log; -} -#BINDING-%s-END''' % (domain, port, domain, webdir, version, self.setupPath, siteInfo['name'], public.GetConfigValue('logs_path') + '/' + siteInfo['name'], public.GetConfigValue('logs_path') + '/' + siteInfo['name'], domain) - conf += bindingConf + source_dirbind_tpl = public.getRunDir() + '/data/tpl/nginx_dirbind.conf' + content = public.readFile(source_dirbind_tpl) + content = content.replace('{$PORT}', port) + content = content.replace('{$PHPVER}', version) + content = content.replace('{$DIRBIND}', domain) + content = content.replace('{$ROOT_DIR}', webdir) + content = content.replace('{$SERVER_MAIN}', siteInfo['name']) + content = content.replace('{$OR_REWRITE}', self.rewritePath) + content = content.replace('{$LOGPATH}', public.getLogsDir()) + + conf += "\r\n" + content shutil.copyfile(filename, '/tmp/backup.conf') public.writeFile(filename, conf) conf = public.readFile(filename) @@ -658,7 +680,7 @@ server return public.returnJson(False, 'ERROR:
' + isError.replace("\n", '
') + '
') public.M('binding').add('pid,domain,port,path,addtime', - (id, domain, port, dirName, public.getDate())) + (pid, domain, port, dirName, public.getDate())) public.restartWeb() msg = public.getInfo('网站[{1}]子目录[{2}]绑定到[{3}]', @@ -666,56 +688,68 @@ server public.writeLog('网站管理', msg) return public.returnJson(True, '添加成功!') - def addDomainApi(self): - isError = public.checkWebConfig() - if isError != True: - return public.returnJson(False, 'ERROR: 检测到配置文件有错误,请先排除后再操作

' + isError.replace("\n", '
') + '
') - - domain = request.form.get('domain', '').encode('utf-8') - webname = request.form.get('webname', '').encode('utf-8') - pid = request.form.get('id', '').encode('utf-8') - if len(domain) < 3: - return public.returnJson(False, '域名不能为空!') - domains = domain.split(',') - for domain in domains: - if domain == "": - continue - domain = domain.split(':') - # print domain - domain_name = self.toPunycode(domain[0]) - domain_port = '80' - - reg = "^([\w\-\*]{1,100}\.){1,4}([\w\-]{1,24}|[\w\-]{1,24}\.[\w\-]{1,24})$" - if not re.match(reg, domain_name): - return public.returnJson(False, '域名格式不正确!') - - if len(domain) == 2: - domain_port = domain[1] - if domain_port == "": - domain_port = "80" - - if not public.checkPort(domain_port): - return public.returnJson(False, '端口范围不合法!') - - opid = public.M('domain').where( - "name=? AND (port=? OR pid=?)", (domain, domain_port, pid)).getField('pid') - if opid: - if public.M('sites').where('id=?', (opid,)).count(): - return public.returnMsg(False, '指定域名已绑定过!') - public.M('domain').where('pid=?', (opid,)).delete() + def delDirBindApi(self): + mid = request.form.get('id', '').encode('utf-8') + binding = public.M('binding').where( + "id=?", (mid,)).field('id,pid,domain,path').find() + siteName = public.M('sites').where( + "id=?", (binding['pid'],)).getField('name') - if public.M('binding').where('domain=?', (domain,)).count(): - return public.returnMsg(False, '您添加的域名已存在!') + filename = self.getHostConf(siteName) + conf = public.readFile(filename) + if conf: + rep = "\s*.+BINDING-" + \ + binding['domain'] + \ + "-START(.|\n)+BINDING-" + binding['domain'] + "-END" + conf = re.sub(rep, '', conf) + public.writeFile(filename, conf) - self.nginxAddDomain(webname, domain_name, domain_port) + public.M('binding').where("id=?", (mid,)).delete() - public.restartWeb() - msg = public.getInfo('网站[{1}]添加域名[{2}]成功!', (webname, domain_name)) - public.writeLog('TYPE_SITE', msg) - public.M('domain').add('pid,name,port,addtime', - (pid, domain_name, domain_port, public.getDate())) + filename = self.getDirBindRewrite(siteName, binding['path']) + if os.path.exists(filename): + os.remove(filename) + public.restartWeb() + msg = public.getInfo('删除网站[{1}]子目录[{2}]绑定', + (siteName, binding['path'])) + public.writeLog('网站管理', msg) + return public.returnJson(True, '删除成功!') - return public.returnJson(True, '域名添加成功!') + # 取子目录Rewrite + def getDirBindRewriteApi(self): + mid = request.form.get('id', '').encode('utf-8') + add = request.form.get('add', '0').encode('utf-8') + find = public.M('binding').where( + "id=?", (mid,)).field('id,pid,domain,path').find() + site = public.M('sites').where( + "id=?", (find['pid'],)).field('id,name,path').find() + + filename = self.getDirBindRewrite(site['name'], find['path']) + if add == '1': + public.writeFile(filename, '') + file = self.getHostConf(site['name']) + conf = public.readFile(file) + domain = find['domain'] + rep = "\n#BINDING-" + domain + \ + "-START(.|\n)+BINDING-" + domain + "-END" + tmp = re.search(rep, conf).group() + dirConf = tmp.replace('rewrite/' + site['name'] + '.conf;', 'rewrite/' + site[ + 'name'] + '_' + find['path'] + '.conf;') + conf = conf.replace(tmp, dirConf) + public.writeFile(file, conf) + data = {} + data['rewrite_dir'] = self.rewritePath + data['status'] = False + if os.path.exists(filename): + data['status'] = True + data['data'] = public.readFile(filename) + data['rlist'] = [] + for ds in os.listdir(self.rewritePath): + if ds == 'list.txt': + continue + data['rlist'].append(ds[0:len(ds) - 5]) + data['filename'] = filename + return public.getJson(data) def delDomainApi(self): domain = request.form.get('domain', '').encode('utf-8') @@ -850,6 +884,9 @@ server def getRewriteConf(self, siteName): return self.rewritePath + '/' + siteName + '.conf' + def getDirBindRewrite(self, siteName, dirname): + return self.rewritePath + '/' + siteName + '_' + dirname + '.conf' + def getIndexConf(self): return public.getServerDir() + '/openresty/nginx/conf/nginx.conf' @@ -1106,7 +1143,7 @@ include enable-php-''' % (fix.strip().replace(',', '|'), domains.strip().replace public.execShell('chmod -R 755 ' + path) def nginxAddDomain(self, webname, domain, port): - file = self.vhostPath + '/' + webname + '.conf' + file = self.getHostConf(webname) conf = public.readFile(file) if not conf: return diff --git a/data/tpl/nginx_dirbind.conf b/data/tpl/nginx_dirbind.conf index 8aac2d3d1..ec40eb4f3 100755 --- a/data/tpl/nginx_dirbind.conf +++ b/data/tpl/nginx_dirbind.conf @@ -1,11 +1,13 @@ + +#BINDING-{$DIRBIND}-START server { listen {$PORT}; - server_name {$SERVER_NAME}; + server_name {$DIRBIND}; index index.php index.html index.htm default.php default.htm default.html; root {$ROOT_DIR}; - #SSL-START %s + #SSL-START #error_page 404/404.html; #SSL-END @@ -19,7 +21,7 @@ server #PHP-INFO-END #REWRITE-START - #include {$OR_REWRITE}/{$SERVER_NAME}.conf; + include {$OR_REWRITE}/{$SERVER_MAIN}.conf; #REWRITE-END #禁止访问的文件或目录 @@ -47,6 +49,7 @@ server access_log /dev/null; } - access_log {$LOGPATH}/{$SERVER_NAME}.log; - error_log {$LOGPATH}/{$SERVER_NAME}.error.log; -} \ No newline at end of file + access_log {$LOGPATH}/{$SERVER_MAIN}_{$DIRBIND}.log; + error_log {$LOGPATH}/{$SERVER_MAIN}_{$DIRBIND}.error.log; +} +#BINDING-{$DIRBIND}-END \ No newline at end of file diff --git a/route/static/app/site.js b/route/static/app/site.js index 735bb466d..e6b6f578f 100755 --- a/route/static/app/site.js +++ b/route/static/app/site.js @@ -1268,7 +1268,7 @@ function dirBinding(id){ var rdata = data['data']; var echoHtml = ''; for(var i=0;i"+rdata.binding[i].port+""+rdata.binding[i].path+""+lan.site.site_menu_4+" | "+lan.public.del+""; + echoHtml += ""+rdata.binding[i].domain+""+rdata.binding[i].port+""+rdata.binding[i].path+"伪静态 | 删除"; } var dirList = ''; @@ -1291,23 +1291,23 @@ function dirBinding(id){ } //子目录伪静态 -function SetDirRewrite(id){ - $.post('/site?action=GetDirRewrite&id='+id,function(rdata){ +function setDirRewrite(id){ + $.post('/site/get_dir_bind_rewrite','id='+id,function(rdata){ if(!rdata.status){ - var confirmObj = layer.confirm(lan.site.url_rewrite_alter,{icon:3,closeBtn:2},function(){ - $.post('/site?action=GetDirRewrite&id='+id+'&add=1',function(rdata){ + var confirmObj = layer.confirm('你真的要为这个子目录创建独立的伪静态规则吗?',{icon:3,closeBtn:2},function(){ + $.post('/site/get_dir_bind_rewrite','id='+id+'&add=1',function(rdata){ layer.close(confirmObj); - ShowRewrite(rdata); - }); + showRewrite(rdata); + },'json'); }); return; } - ShowRewrite(rdata); - }); + showRewrite(rdata); + },'json'); } //显示伪静态 -function ShowRewrite(rdata){ +function showRewrite(rdata){ var rList = ''; for(var i=0;i"+rdata.rlist[i]+""; @@ -1315,18 +1315,17 @@ function ShowRewrite(rdata){ var webBakHtml = "
\
\ \ - "+lan.site.rule_cov_tool+":"+lan.site.a_c_n+"\\
\ - \ + \
    \ -
  • "+lan.site.url_rw_help_1+"
  • \ -
  • "+lan.site.url_rw_help_2+"
  • \ +
  • 请选择您的应用,若设置伪静态后,网站无法正常访问,请尝试设置回default
  • \ +
  • 您可以对伪静态规则进行修改,修改完后保存即可。
  • \
\
"; layer.open({ type: 1, area: '500px', - title: lan.site.config_url, + title: '配置伪静态规则', closeBtn: 2, shift: 5, shadeClose: true, @@ -1335,9 +1334,9 @@ function ShowRewrite(rdata){ $("#myRewrite").change(function(){ var rewriteName = $(this).val(); - $.post('/files?action=GetFileBody','path=/www/server/panel/rewrite/'+getCookie('serverType')+'/'+rewriteName+'.conf',function(fileBody){ - $("#rewriteBody").val(fileBody.data); - }); + $.post('/files/get_body','path='+rdata['rewrite_dir']+'/'+rewriteName+'.conf',function(fileBody){ + $("#rewriteBody").val(fileBody.data.data); + },'json'); }); } @@ -1351,24 +1350,22 @@ function addDirBinding(id){ } var data = 'id='+id+'&domain='+domain+'&dirName='+dirName - $.post('site?action=AddDirBinding',data,function(rdata){ - DirBinding(id); + $.post('/site/add_dir_bind',data,function(rdata){ + dirBinding(id); layer.msg(rdata.msg,{icon:rdata.status?1:2}); - }); - + },'json'); } //删除子目录绑定 -function DelBinding(id,siteId){ +function delDirBind(id,siteId){ layer.confirm(lan.site.s_bin_del,{icon:3,closeBtn:2},function(){ - $.post('site?action=DelDirBinding','id='+id,function(rdata){ - DirBinding(siteId); + $.post('/site/del_dir_bind','id='+id,function(rdata){ + dirBinding(siteId); layer.msg(rdata.msg,{icon:rdata.status?1:2}); - }); + },'json'); }); } - //反向代理 function Proxy(siteName,type){ if(type == 1){