路径规则、重写规则、URL跳转规则均支持匹配条件;URL跳转规则增加排序、是否启用功能

This commit is contained in:
GoEdgeLab
2021-06-09 21:45:26 +08:00
parent 40f0cc3a17
commit 2f0c0015a0
16 changed files with 275 additions and 63 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
"regexp"
"strings"
@@ -48,6 +49,7 @@ func (this *CreateAction) RunPost(params struct {
IsBreak bool
IsCaseInsensitive bool
IsReverse bool
CondsJSON []byte
Must *actions.Must
}) {
@@ -63,6 +65,20 @@ func (this *CreateAction) RunPost(params struct {
}
}
// 校验匹配条件
if len(params.CondsJSON) > 0 {
conds := &shared.HTTPRequestCondsConfig{}
err := json.Unmarshal(params.CondsJSON, conds)
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
err = conds.Init()
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
}
// 自动加上前缀斜杠
if params.PatternType == serverconfigs.HTTPLocationPatternTypePrefix ||
params.PatternType == serverconfigs.HTTPLocationPatternTypeExact {
@@ -79,6 +95,7 @@ func (this *CreateAction) RunPost(params struct {
Description: params.Description,
Pattern: resultPattern,
IsBreak: params.IsBreak,
CondsJSON: params.CondsJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -1,9 +1,11 @@
package location
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
"regexp"
"strings"
@@ -28,6 +30,7 @@ func (this *IndexAction) RunGet(params struct {
this.Data["type"] = locationConfig.PatternType()
this.Data["isReverse"] = locationConfig.IsReverse()
this.Data["isCaseInsensitive"] = locationConfig.IsCaseInsensitive()
this.Data["conds"] = locationConfig.Conds
this.Show()
}
@@ -45,6 +48,8 @@ func (this *IndexAction) RunPost(params struct {
IsReverse bool
IsOn bool
CondsJSON []byte
Must *actions.Must
}) {
defer this.CreateLogInfo("修改路径规则 %d 设置", params.LocationId)
@@ -67,6 +72,20 @@ func (this *IndexAction) RunPost(params struct {
params.Pattern = "/" + strings.TrimLeft(params.Pattern, "/")
}
// 校验匹配条件
if len(params.CondsJSON) > 0 {
conds := &shared.HTTPRequestCondsConfig{}
err := json.Unmarshal(params.CondsJSON, conds)
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
err = conds.Init()
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
}
location := &serverconfigs.HTTPLocationConfig{}
location.SetPattern(params.Pattern, params.PatternType, params.IsCaseInsensitive, params.IsReverse)
resultPattern := location.Pattern
@@ -78,6 +97,7 @@ func (this *IndexAction) RunPost(params struct {
Pattern: resultPattern,
IsBreak: params.IsBreak,
IsOn: params.IsOn,
CondsJSON: params.CondsJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -1,8 +1,10 @@
package redirects
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"net/url"
@@ -30,8 +32,9 @@ func (this *CreatePopupAction) RunPost(params struct {
MatchPrefix bool
MatchRegexp bool
KeepRequestURI bool
Status int
CondsJSON []byte
IsOn bool
Must *actions.Must
CSRF *actionutils.CSRF
@@ -80,6 +83,21 @@ func (this *CreatePopupAction) RunPost(params struct {
Field("status", params.Status).
Gte(0, "请选择正确的跳转状态码")
// 校验匹配条件
var conds *shared.HTTPRequestCondsConfig
if len(params.CondsJSON) > 0 {
conds = &shared.HTTPRequestCondsConfig{}
err := json.Unmarshal(params.CondsJSON, conds)
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
err = conds.Init()
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
}
this.Data["redirect"] = maps.Map{
"status": params.Status,
"beforeURL": params.BeforeURL,
@@ -87,7 +105,8 @@ func (this *CreatePopupAction) RunPost(params struct {
"matchPrefix": params.MatchPrefix,
"matchRegexp": params.MatchRegexp,
"keepRequestURI": params.KeepRequestURI,
"isOn": true,
"conds": conds,
"isOn": params.IsOn,
}
this.Success()

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
"regexp"
@@ -37,6 +38,8 @@ func (this *CreatePopupAction) RunPost(params struct {
IsBreak bool
IsOn bool
CondsJSON []byte
Must *actions.Must
}) {
params.Must.
@@ -54,6 +57,20 @@ func (this *CreatePopupAction) RunPost(params struct {
Field("replace", params.Replace).
Require("请输入目标URL")
// 校验匹配条件
if len(params.CondsJSON) > 0 {
conds := &shared.HTTPRequestCondsConfig{}
err := json.Unmarshal(params.CondsJSON, conds)
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
err = conds.Init()
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
}
// web配置
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithId(this.AdminContext(), params.WebId)
if err != nil {
@@ -71,6 +88,7 @@ func (this *CreatePopupAction) RunPost(params struct {
WithQuery: params.WithQuery,
IsBreak: params.IsBreak,
IsOn: params.IsOn,
CondsJSON: params.CondsJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -1,9 +1,11 @@
package rewrite
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
"regexp"
@@ -56,6 +58,7 @@ func (this *UpdatePopupAction) RunPost(params struct {
WithQuery bool
IsBreak bool
IsOn bool
CondsJSON []byte
Must *actions.Must
}) {
@@ -76,6 +79,20 @@ func (this *UpdatePopupAction) RunPost(params struct {
Field("replace", params.Replace).
Require("请输入目标URL")
// 校验匹配条件
if len(params.CondsJSON) > 0 {
conds := &shared.HTTPRequestCondsConfig{}
err := json.Unmarshal(params.CondsJSON, conds)
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
err = conds.Init()
if err != nil {
this.Fail("匹配条件校验失败:" + err.Error())
}
}
// 修改
_, err := this.RPC().HTTPRewriteRuleRPC().UpdateHTTPRewriteRule(this.AdminContext(), &pb.UpdateHTTPRewriteRuleRequest{
RewriteRuleId: params.RewriteRuleId,
@@ -87,6 +104,7 @@ func (this *UpdatePopupAction) RunPost(params struct {
WithQuery: params.WithQuery,
IsBreak: params.IsBreak,
IsOn: params.IsOn,
CondsJSON: params.CondsJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -1,11 +1,31 @@
Vue.component("http-host-redirect-box", {
props: ["v-redirects"],
mounted: function () {
let that = this
sortTable(function (ids) {
let newRedirects = []
ids.forEach(function (id) {
that.redirects.forEach(function (redirect) {
if (redirect.id == id) {
newRedirects.push(redirect)
}
})
})
that.updateRedirects(newRedirects)
})
},
data: function () {
let redirects = this.vRedirects
if (redirects == null) {
redirects = []
}
let id = 0
redirects.forEach(function (v) {
id++
v.id = id
})
return {
redirects: redirects,
statusOptions: [
@@ -14,7 +34,8 @@ Vue.component("http-host-redirect-box", {
{"code": 302, "text": "Found"},
{"code": 303, "text": "See Other"},
{"code": 307, "text": "Temporary Redirect"}
]
],
id: id
}
},
methods: {
@@ -23,9 +44,13 @@ Vue.component("http-host-redirect-box", {
window.UPDATING_REDIRECT = null
teaweb.popup("/servers/server/settings/redirects/createPopup", {
height: "22em",
width: "50em",
height: "30em",
callback: function (resp) {
that.id++
resp.data.redirect.id = that.id
that.redirects.push(resp.data.redirect)
that.change()
}
})
},
@@ -34,31 +59,84 @@ Vue.component("http-host-redirect-box", {
window.UPDATING_REDIRECT = redirect
teaweb.popup("/servers/server/settings/redirects/createPopup", {
height: "22em",
width: "50em",
height: "30em",
callback: function (resp) {
resp.data.redirect.id = redirect.id
Vue.set(that.redirects, index, resp.data.redirect)
that.change()
}
})
},
remove: function (index) {
this.redirects.$remove(index)
this.change()
},
change: function () {
let that = this
setTimeout(function (){
that.$emit("change", that.redirects)
}, 100)
},
updateRedirects: function (newRedirects) {
this.redirects = newRedirects
this.change()
}
},
template: `<div>
<input type="hidden" name="hostRedirectsJSON" :value="JSON.stringify(redirects)"/>
<first-menu>
<menu-item @click.prevent="add">[创建]</menu-item>
</first-menu>
<div class="margin"></div>
<!-- TODO 将来支持排序并支持isOn切换 -->
<div v-if="redirects.length > 0">
<div v-for="(redirect, index) in redirects" class="ui label basic small" style="margin-bottom: 0.5em;margin-top: 0.5em">
<span v-if="redirect.status > 0" class="small grey">[{{redirect.status}}]</span>
<span v-if="redirect.matchPrefix" class="small grey">[匹配前缀]</span>
<span v-if="redirect.matchRegexp" class="small grey">[正则匹配]</span>
{{redirect.beforeURL}} -&gt; {{redirect.afterURL}} <a href="" @click.prevent="update(index, redirect)" title="修改"><i class="icon pencil small"></i></a> &nbsp; <a href="" @click.prevent="remove(index)" title="删除"><i class="icon remove"></i></a>
<p class="comment" v-if="redirects.length == 0">暂时还没有URL跳转规则。</p>
<div v-show="redirects.length > 0">
<table class="ui table celled selectable" id="sortable-table">
<thead>
<tr>
<th style="width: 1em"></th>
<th>跳转前URL</th>
<th style="width: 1em"></th>
<th>跳转后URL</th>
<th>匹配模式</th>
<th>HTTP状态码</th>
<th class="two wide">状态</th>
<th class="two op">操作</th>
</tr>
</thead>
<tbody v-for="(redirect, index) in redirects" :key="redirect.id" :v-id="redirect.id">
<tr>
<td style="text-align: center;"><i class="icon bars handle grey"></i> </td>
<td>
{{redirect.beforeURL}}
<div style="margin-top: 0.5em" v-if="redirect.conds != null && redirect.conds.groups != null && redirect.conds.groups.length > 0">
<span class="ui label text basic tiny">匹配条件</span>
</div>
<div class="ui divider"></div>
</div>
<div>
<button type="button" class="ui button tiny" @click.prevent="add">+</button>
</td>
<td nowrap="">-&gt;</td>
<td>{{redirect.afterURL}}</td>
<td>
<span v-if="redirect.matchPrefix">匹配前缀</span>
<span v-if="redirect.matchRegexp">正则匹配</span>
<span v-if="!redirect.matchPrefix && !redirect.matchRegexp">精准匹配</span>
</td>
<td>
<span v-if="redirect.status > 0">{{redirect.status}}</span>
<span v-else class="disabled">默认</span>
</td>
<td><label-on :v-is-on="redirect.isOn"></label-on></td>
<td>
<a href="" @click.prevent="update(index, redirect)">修改</a> &nbsp;
<a href="" @click.prevent="remove(index)">删除</a>
</td>
</tr>
</tbody>
</table>
<p class="comment" v-if="redirects.length > 1">所有规则匹配顺序为从上到下,可以拖动左侧的<i class="icon bars"></i>排序。</p>
</div>
<div class="margin"></div>
</div>`
})

View File

@@ -40,6 +40,12 @@
<p class="comment">可以用来说明此规则用途。。</p>
</td>
</tr>
<tr>
<td>匹配条件</td>
<td>
<http-request-conds-box></http-request-conds-box>
</td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>

View File

@@ -42,6 +42,12 @@
<p class="comment">可以用来说明此规则用途。。</p>
</td>
</tr>
<tr>
<td>匹配条件</td>
<td>
<http-request-conds-box :v-conds="conds"></http-request-conds-box>
</td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>

View File

@@ -4,7 +4,9 @@ Tea.context(function () {
teaweb.popup("/servers/server/settings/rewrite/createPopup?webId=" + this.webId, {
height: "26em",
callback: function () {
window.location.reload()
teaweb.success("保存成功", function () {
teaweb.reload()
})
}
})
}

View File

@@ -52,6 +52,14 @@
</select>
</td>
</tr>
<tr>
<td>匹配条件</td>
<td><http-request-conds-box :v-conds="redirect.conds" @change="changeConds"></http-request-conds-box></td>
</tr>
<tr>
<td>是否启用</td>
<td><checkbox name="isOn" value="1" v-model="redirect.isOn"></checkbox></td>
</tr>
</table>
<submit-btn></submit-btn>

View File

@@ -10,7 +10,9 @@ Tea.context(function () {
afterURL: "",
matchPrefix: false,
matchRegexp: false,
keepRequestURI: false
keepRequestURI: false,
conds: null,
isOn: true
}
}
@@ -38,4 +40,8 @@ Tea.context(function () {
}
})
})
this.changeConds = function (conds) {
this.redirect.conds = conds
}
})

View File

@@ -4,14 +4,7 @@
<div class="right-box">
<form class="ui form" method="post" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="webId" :value="webId"/>
<table class="ui table selectable definition">
<tr>
<td class="title">URL跳转设置</td>
<td>
<http-host-redirect-box :v-redirects="redirects"></http-host-redirect-box>
</td>
</tr>
</table>
<http-host-redirect-box :v-redirects="redirects" @change="change"></http-host-redirect-box>
<submit-btn></submit-btn>
</form>
</div>

View File

@@ -1,3 +1,14 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
this.change = function (values) {
this.$post("$")
.params({
webId: this.webId,
hostRedirectsJSON: JSON.stringify(values)
})
.success(function () {
NotifyReloadSuccess("保存成功")()
})
}
})

View File

@@ -28,6 +28,10 @@
<p class="comment">隐式表示不在客户端显示重写后的URL显式表示在客户端跳转URL将会显示重写后的URL。</p>
</td>
</tr>
<tr>
<td>匹配条件</td>
<td><http-request-conds-box></http-request-conds-box></td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>

View File

@@ -4,7 +4,9 @@ Tea.context(function () {
teaweb.popup("/servers/server/settings/rewrite/createPopup?webId=" + this.webId, {
height: "26em",
callback: function () {
window.location.reload()
teaweb.success("保存成功", function () {
teaweb.reload()
})
}
})
}

View File

@@ -29,6 +29,10 @@
<p class="comment">隐式表示不在客户端显示重写后的URL显式表示在客户端跳转URL将会显示重写后的URL。</p>
</td>
</tr>
<tr>
<td>匹配条件</td>
<td><http-request-conds-box :v-conds="rewriteRule.conds"></http-request-conds-box></td>
</tr>
<tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td>
</tr>