反向代理源站实现使用域名分组

This commit is contained in:
GoEdgeLab
2021-09-20 11:54:28 +08:00
parent 2f1e5bad43
commit 9f855e4477
8 changed files with 189 additions and 37 deletions

View File

@@ -54,22 +54,30 @@ func (this *IndexAction) RunGet(params struct {
primaryOriginMaps := []maps.Map{}
backupOriginMaps := []maps.Map{}
for _, originConfig := range reverseProxy.PrimaryOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
primaryOriginMaps = append(primaryOriginMaps, m)
}
for _, originConfig := range reverseProxy.BackupOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
backupOriginMaps = append(backupOriginMaps, m)
}

View File

@@ -56,6 +56,8 @@ func (this *AddPopupAction) RunPost(params struct {
MaxIdleConns int32
IdleTimeout int
DomainsJSON []byte
Description string
IsOn bool
@@ -121,6 +123,15 @@ func (this *AddPopupAction) RunPost(params struct {
return
}
var domains = []string{}
if len(params.DomainsJSON) > 0 {
err = json.Unmarshal(params.DomainsJSON, &domains)
if err != nil {
this.ErrorPage(err)
return
}
}
createResp, err := this.RPC().OriginRPC().CreateOrigin(this.AdminContext(), &pb.CreateOriginRequest{
Name: params.Name,
Addr: &pb.NetworkAddress{
@@ -136,6 +147,7 @@ func (this *AddPopupAction) RunPost(params struct {
IdleTimeoutJSON: idleTimeoutJSON,
MaxConns: params.MaxConns,
MaxIdleConns: params.MaxIdleConns,
Domains: domains,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -76,6 +76,10 @@ func (this *UpdatePopupAction) RunGet(params struct {
idleTimeout = types.Int(config.IdleTimeout.Count)
}
if len(config.Domains) == 0 {
config.Domains = []string{}
}
this.Data["origin"] = maps.Map{
"id": config.Id,
"protocol": config.Addr.Protocol,
@@ -89,6 +93,7 @@ func (this *UpdatePopupAction) RunGet(params struct {
"idleTimeout": idleTimeout,
"maxConns": config.MaxConns,
"maxIdleConns": config.MaxIdleConns,
"domains": config.Domains,
}
this.Show()
@@ -110,6 +115,8 @@ func (this *UpdatePopupAction) RunPost(params struct {
MaxIdleConns int32
IdleTimeout int
DomainsJSON []byte
Description string
IsOn bool
@@ -175,6 +182,15 @@ func (this *UpdatePopupAction) RunPost(params struct {
return
}
var domains = []string{}
if len(params.DomainsJSON) > 0 {
err = json.Unmarshal(params.DomainsJSON, &domains)
if err != nil {
this.ErrorPage(err)
return
}
}
_, err = this.RPC().OriginRPC().UpdateOrigin(this.AdminContext(), &pb.UpdateOriginRequest{
OriginId: params.OriginId,
Name: params.Name,
@@ -191,6 +207,7 @@ func (this *UpdatePopupAction) RunPost(params struct {
IdleTimeoutJSON: idleTimeoutJSON,
MaxConns: params.MaxConns,
MaxIdleConns: params.MaxIdleConns,
Domains: domains,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -53,22 +53,30 @@ func (this *IndexAction) RunGet(params struct {
primaryOriginMaps := []maps.Map{}
backupOriginMaps := []maps.Map{}
for _, originConfig := range reverseProxy.PrimaryOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
primaryOriginMaps = append(primaryOriginMaps, m)
}
for _, originConfig := range reverseProxy.BackupOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
backupOriginMaps = append(backupOriginMaps, m)
}

View File

@@ -0,0 +1,90 @@
// 域名列表
Vue.component("domains-box", {
props: ["v-domains"],
data: function () {
let domains = this.vDomains
if (domains == null) {
domains = []
}
return {
domains: domains,
isAdding: false,
addingDomain: ""
}
},
methods: {
add: function () {
this.isAdding = true
let that = this
setTimeout(function () {
that.$refs.addingDomain.focus()
}, 100)
},
confirm: function () {
let that = this
// 删除其中的空格
this.addingDomain = this.addingDomain.replace(/\s/g, "")
if (this.addingDomain.length == 0) {
teaweb.warn("请输入要添加的域名", function () {
that.$refs.addingDomain.focus()
})
return
}
// 基本校验
if (this.addingDomain[0] == "~") {
let expr = this.addingDomain.substring(1)
try {
new RegExp(expr)
} catch (e) {
teaweb.warn("正则表达式错误:" + e.message, function () {
that.$refs.addingDomain.focus()
})
return
}
}
this.domains.push(this.addingDomain)
this.cancel()
},
remove: function (index) {
this.domains.$remove(index)
},
cancel: function () {
this.isAdding = false
this.addingDomain = ""
}
},
template: `<div>
<input type="hidden" name="domainsJSON" :value="JSON.stringify(domains)"/>
<div v-if="domains.length > 0">
<span class="ui label small basic" v-for="(domain, index) in domains">
<span v-if="domain.length > 0 && domain[0] == '~'" class="grey" style="font-style: normal">[正则]</span>
<span v-if="domain.length > 0 && domain[0] == '.'" class="grey" style="font-style: normal">[后缀]</span>
<span v-if="domain.length > 0 && domain[0] == '*'" class="grey" style="font-style: normal">[泛域名]</span>
{{domain}}
&nbsp; <a href="" title="删除" @click.prevent="remove(index)"><i class="icon remove small"></i></a>
</span>
<div class="ui divider"></div>
</div>
<div v-if="isAdding">
<div class="ui fields">
<div class="ui field">
<input type="text" v-model="addingDomain" @keyup.enter="confirm()" @keypress.enter.prevent="1" ref="addingDomain" placeholder="*.xxx.com" size="30"/>
</div>
<div class="ui field">
<button class="ui button tiny" type="button" @click.prevent="confirm">确定</button>
&nbsp; <a href="" title="取消" @click.prevent="cancel"><i class="icon remove small"></i></a>
</div>
</div>
<p class="comment">支持普通域名(<code-label>example.com</code-label>)、泛域名(<code-label>*.example.com</code-label>)、域名后缀(以点号开头,如<code-label>.example.com</code-label>)和正则表达式(以波浪号开头,如<code-label>~.*.example.com</code-label>)。</p>
<div class="ui divider"></div>
</div>
<div style="margin-top: 0.5em" v-if="!isAdding">
<button class="ui button tiny" type="button" @click.prevent="add">+</button>
</div>
</div>`
})

View File

@@ -9,7 +9,7 @@ Vue.component("origin-list-box", {
methods: {
createPrimaryOrigin: function () {
teaweb.popup("/servers/server/settings/origins/addPopup?originType=primary&" + this.vParams, {
height: "24em",
height: "27em",
callback: function (resp) {
teaweb.success("保存成功", function () {
window.location.reload()
@@ -19,7 +19,7 @@ Vue.component("origin-list-box", {
},
createBackupOrigin: function () {
teaweb.popup("/servers/server/settings/origins/addPopup?originType=backup&" + this.vParams, {
height: "24em",
height: "27em",
callback: function (resp) {
teaweb.success("保存成功", function () {
window.location.reload()
@@ -29,7 +29,7 @@ Vue.component("origin-list-box", {
},
updateOrigin: function (originId, originType) {
teaweb.popup("/servers/server/settings/origins/updatePopup?originType=" + originType + "&" + this.vParams + "&originId=" + originId, {
height: "24em",
height: "27em",
callback: function (resp) {
teaweb.success("保存成功", function () {
window.location.reload()
@@ -89,6 +89,9 @@ Vue.component("origin-list-table", {
<div v-if="origin.name.length > 0" style="margin-top: 0.5em">
<tiny-basic-label>{{origin.name}}</tiny-basic-label>
</div>
<div v-if="origin.domains != null && origin.domains.length > 0">
<grey-label v-for="domain in origin.domains">{{domain}}</grey-label>
</div>
</td>
<td :class="{disabled:!origin.isOn}">{{origin.weight}}</td>
<td>

View File

@@ -35,6 +35,17 @@
源站服务器地址通常是一个IP或域名加端口<span v-if="serverType == 'httpProxy'">,不需要加 http:// 或 https://</span></p>
</td>
</tr>
<tr v-if="isHTTP">
<td>专属域名</td>
<td>
<domains-box></domains-box>
<p class="comment">需要指定域名生效的时候才需要添加。</p>
</td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>
<tbody v-show="moreOptionsVisible">
<tr>
<td>权重</td>
<td>
@@ -42,10 +53,6 @@
<p class="comment">数字越大,代表分配的请求比例越多。</p>
</td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>
<tbody v-show="moreOptionsVisible">
<tr>
<td>名称</td>
<td>

View File

@@ -36,6 +36,17 @@
<p class="comment"><span class="red" v-if="addrError.length > 0">{{addrError}}</span>源站服务器地址通常是一个IP或域名加端口<span v-if="serverType == 'httpProxy'">,不需要加 http:// 或 https://</span></p>
</td>
</tr>
<tr v-if="isHTTP">
<td>专属域名</td>
<td>
<domains-box :v-domains="origin.domains"></domains-box>
<p class="comment">需要指定域名生效的时候才需要添加。</p>
</td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>
<tbody v-show="moreOptionsVisible">
<tr>
<td>权重</td>
<td>
@@ -43,10 +54,6 @@
<p class="comment">数字越大,代表分配的请求比例越多。</p>
</td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>
<tbody v-show="moreOptionsVisible">
<tr>
<td>名称</td>
<td>