feta: openresty proxy

pull/117/head
soxft 3 years ago
parent 4a6c881018
commit d29f86d51e
  1. 131
      class/core/site_api.py
  2. 163
      route/static/app/site.js

@ -3,6 +3,7 @@
import time
import os
import sys
from urllib.parse import urlparse
import mw
import re
import json
@ -1344,14 +1345,130 @@ class site_api:
# 读取 网站 反向代理列表
def getProxyListApi(self):
siteName = request.form.get('siteName', '')
conf_path = self.getHostConf(siteName)
old_conf = mw.readFile(conf_path)
_siteName = request.form.get('siteName', '')
data_path = self.getProxytDataPath(_siteName)
data_content = mw.readFile(data_path)
# not exists
if data_content == False:
mw.execShell("mkdir {}/{}".format(self.proxyPath, _siteName))
return mw.returnJson(True, "", {"result": [], "count": 0})
try:
data = json.loads(data_content)
except:
mw.execShell("rm -rf {}/{}".format(self.proxyPath, _siteName))
return mw.returnJson(True, "", {"result": [], "count": 0})
return mw.returnJson(True, "ok", {"result": data, "count": len(data)})
# 设置 网站 反向代理列表
def setProxyApi(self):
_siteName = request.form.get('siteName', '')
_from = request.form.get('from', '')
_to = request.form.get('to', '')
_host = request.form.get('host', '')
if _siteName == "" or _from == "" or _to == "" or _host == "":
return mw.returnJson(False, "必填项不能为空")
data_path = self.getProxytDataPath(_siteName)
data_content = mw.readFile(
data_path) if os.path.exists(data_path) else ""
data = json.loads(data_content) if data_content != "" else []
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, "错误的目标地址!")
# get host from url
try:
if _host == "$host":
host_tmp = urlparse(_to)
_host = host_tmp.netloc
except:
return mw.returnJson(False, "错误的目标地址")
tpl = """
#PROXY-START/
location ~* \.(gif|png|jpg|css|js|woff|woff2)$
{
proxy_pass {to};
proxy_set_header Host {host};
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
expires 12h;
}
location ~* ^{from}(.*)$ {
proxy_pass {to};
proxy_set_header Host {host};
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
proxy_ignore_headers Set-Cookie Cache-Control expires;
add_header Cache-Control no-cache;
}
#PROXY-END/
"""
# replace
if _from[0] != '/':
_from = '/' + _from
tpl = tpl.replace("{from}", _from, 999)
tpl = tpl.replace("{to}", _to)
tpl = tpl.replace("{host}", _host, 999)
_id = mw.md5("{}+{}+{}".format(_from, _to, _siteName))
for item in data:
if item["id"] == _id:
return mw.returnJson(False, "已存在该规则!")
if item["from"] == _from:
return mw.returnJson(False, "已存在该规则!")
data.append({
"from": _from,
"to": _to,
"host": _host,
"id": _id
})
mw.writeFile(data_path, json.dumps(data))
mw.writeFile(
"{}/{}.conf".format(self.getProxyPath(_siteName), _id), tpl)
mw.restartWeb()
return mw.returnJson(True, "ok", {
"conf_content": old_conf
"hash": _id
})
def delProxyApi(self):
_siteName = request.form.get("siteName", '')
_id = request.form.get("id", '')
if _id == '' or _siteName == '':
return mw.returnJson(False, "必填项不能为空!")
try:
data_path = self.getProxytDataPath(_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.getProxyPath(_siteName), _id))
except:
return mw.returnJson(False, "删除失败!")
return mw.returnJson(True, "删除成功!")
def getSiteTypesApi(self):
# 取网站分类
data = mw.M("site_types").field("id,name").order("id asc").select()
@ -1507,13 +1624,13 @@ class site_api:
return "{}/{}/data.json".format(self.redirectPath, siteName)
def getRedirectPath(self, siteName):
return "{}/{}/".format(self.redirectPath, siteName)
return "{}/{}".format(self.redirectPath, siteName)
def getProxytDataPath(self, siteName):
return "{}/{}/data.json".format(self.proxyPath, siteName)
def getProxyDataPath(self, siteName):
return "{}/{}/data.json".format(self.proxyPath, siteName)
def getProxyPath(self, siteName):
return "{}/{}".format(self.proxyPath, siteName)
def getDirBindRewrite(self, siteName, dirname):
return self.rewritePath + '/' + siteName + '_' + dirname + '.conf'

@ -1366,41 +1366,6 @@ function delDirBind(id,siteId){
});
}
//反向代理
function toProxy(siteName, type, obj) {
if (type == 1) {
return;
}
var body = '<div id="redirect_list" class="bt_table">\
<div style="padding-bottom: 10px">\
<button type="button" title="添加反向代理" class="btn btn-success btn-sm mr5" onclick="toProxy(\''+siteName+'\',1)" ><span>添加反向代理</span></button>\
</div>\
<div class="divtable" style="max-height:200px;">\
<table class="table table-hover" >\
<thead style="position: relative;z-index: 1;">\
<tr>\
<th><span data-index="1"><span>代理目录</span></span></th>\
<th><span data-index="2"><span>目标地址</span></span></th>\
<th><span data-index="3"><span>操作</span></span></th>\
</tr>\
</thead>\
<tbody id="md-301-body">\
</tbody>\
</table>\
</div>\
</div>';
$("#webedit-con").html(body);
var loading = layer.load()
$.post("/site/get_proxy_list", {
siteName: siteName
},function (res) {
var res = JSON.parse(res)
console.log(res)
})
}
//开启缓存
function openCache(siteName){
var loadT = layer.msg(lan.site.the_msg,{icon:16,time:0,shade: [0.3, '#000']});
@ -1623,6 +1588,134 @@ function to301(siteName, type, obj){
});
}
//反向代理
function toProxy(siteName, type, obj) {
// 设置 页面展示
if(type == 1) {
var proxy_form = layer.open({
type: 1,
skin: 'demo-class',
area: '650px',
title: "创建反向代理",
closeBtn: 2,
shift: 5,
shadeClose: false,
content: "<form id='form_redirect' class='divtable pd20' style='padding-bottom: 60px'>" +
"<div class='line'>"+
"<span class='tname'>目标URL</span>" +
"<div class='info-r ml0'>" +
"<input name='to' class='bt-input-text mr5' type='text' style='width:200px''>" +
"</div>" +
"</div>" +
"<div class='line'>" +
"<span class='tname'>代理目录</span>" +
"<div class='info-r ml0'>" +
"<input name='from' value='/' placeholder='/' class='bt-input-text mr5' type='text' style='width:200px;float: left;margin-right:0px''>" +
"<span class='tname' style='width:90px'>发送域名</span>" +
"<input name='host' value='$host' class='bt-input-text mr5' type='text' style='width:200px'>" +
"</div>" +
"</div>" +
"<div class='bt-form-submit-btn'><button type='button' class='btn btn-sm btn-danger btn-colse-proxy'>关闭</button><button type='button' class='btn btn-sm btn-success btn-submit-proxy'>提交</button></div>" +
"</form>"
});
setTimeout(function() {
$('.btn-colse-proxy').click(function() {
layer.close(proxy_form);
});
$('.btn-submit-proxy').click(function() {
var to = $('[name="to"]').val();
var from = $('[name="from"]').val();
var host = $('[name="host"]').val();
$.post('/site/set_proxy', {
siteName: siteName,
from: from,
to: to,
host: host,
}, function(res) {
res = JSON.parse(res);
if (res.status) {
layer.close(proxy_form);
toProxy(siteName)
} else {
layer.msg(res.msg, {
icon: 2
});
}
});
});
}, 100);
}
if (type == 2) {
$.post('/site/del_proxy', {
siteName: siteName,
id: obj,
}, function(res) {
res = JSON.parse(res);
if (res.status == true) {
layer.msg('删除成功', {
time: 1000,
icon: 1
});
toProxy(siteName)
} else {
layer.msg(res.msg, {
time: 1000,
icon: 2
});
}
});
return
}
var body = '<div id="proxy_list" class="bt_table">\
<div style="padding-bottom: 10px">\
<button type="button" title="添加反向代理" class="btn btn-success btn-sm mr5" onclick="toProxy(\''+siteName+'\',1)" ><span>添加反向代理</span></button>\
</div>\
<div class="divtable" style="max-height:200px;">\
<table class="table table-hover" >\
<thead style="position: relative;z-index: 1;">\
<tr>\
<th><span data-index="1"><span>代理目录</span></span></th>\
<th><span data-index="2"><span>目标地址</span></span></th>\
<th><span data-index="3"><span>操作</span></span></th>\
</tr>\
</thead>\
<tbody id="md-301-body">\
</tbody>\
</table>\
</div>\
</div>';
$("#webedit-con").html(body);
var loadT = layer.msg(lan.site.the_msg,{icon:16,time:0,shade: [0.3, '#000']});
$.post("/site/get_proxy_list", {
siteName: siteName
},function (response) {
layer.close(loadT);
$("#md-301-loading").remove();
let res = JSON.parse(response);
if (res.status === true) {
let data = res.data.result;
data.forEach(function(item){
let tmp = '<tr>\
<td><span data-index="1"><span>'+item.from+'</span></span></td>\
<td><span data-index="2"><span>'+item.to+'</span></span></td>\
<td><span data-index="4" onclick="toProxy(\''+siteName+'\', 2, \''+ item.id +'\')" class="btlink">删除</span></td>\
</tr>';
$("#md-301-body").append(tmp);
})
} else {
layer.msg(res.msg, {icon:2});
}
})
}
//文件验证
// function file_check(){
// $(".check_message").html('<div style="margin-left:100px">\

Loading…
Cancel
Save