diff --git a/internal/web/actions/default/servers/server/settings/locations/create.go b/internal/web/actions/default/servers/server/settings/locations/create.go index 8486e0cc..b86d1fdb 100644 --- a/internal/web/actions/default/servers/server/settings/locations/create.go +++ b/internal/web/actions/default/servers/server/settings/locations/create.go @@ -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) diff --git a/internal/web/actions/default/servers/server/settings/locations/location/index.go b/internal/web/actions/default/servers/server/settings/locations/location/index.go index d702f2a8..510c7628 100644 --- a/internal/web/actions/default/servers/server/settings/locations/location/index.go +++ b/internal/web/actions/default/servers/server/settings/locations/location/index.go @@ -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) diff --git a/internal/web/actions/default/servers/server/settings/redirects/createPopup.go b/internal/web/actions/default/servers/server/settings/redirects/createPopup.go index e1d61c98..1d6d1693 100644 --- a/internal/web/actions/default/servers/server/settings/redirects/createPopup.go +++ b/internal/web/actions/default/servers/server/settings/redirects/createPopup.go @@ -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 + 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() diff --git a/internal/web/actions/default/servers/server/settings/rewrite/createPopup.go b/internal/web/actions/default/servers/server/settings/rewrite/createPopup.go index 6b40af15..d7072a61 100644 --- a/internal/web/actions/default/servers/server/settings/rewrite/createPopup.go +++ b/internal/web/actions/default/servers/server/settings/rewrite/createPopup.go @@ -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) diff --git a/internal/web/actions/default/servers/server/settings/rewrite/updatePopup.go b/internal/web/actions/default/servers/server/settings/rewrite/updatePopup.go index bb037974..0b543977 100644 --- a/internal/web/actions/default/servers/server/settings/rewrite/updatePopup.go +++ b/internal/web/actions/default/servers/server/settings/rewrite/updatePopup.go @@ -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) diff --git a/web/public/js/components/server/http-host-redirect-box.js b/web/public/js/components/server/http-host-redirect-box.js index fa87b195..10763933 100644 --- a/web/public/js/components/server/http-host-redirect-box.js +++ b/web/public/js/components/server/http-host-redirect-box.js @@ -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: `
暂时还没有URL跳转规则。
+ +可以用来说明此规则用途。。
+可以用来说明此规则用途。。
+选中表示匹配规则中的路径中的英文字母不区分大小写。
-选中表示匹配所有不符合规则的路径。
-选中表示匹配规则中的路径中的英文字母不区分大小写。
+选中表示匹配所有不符合规则的路径。
+隐式表示不在客户端显示重写后的URL;显式表示在客户端跳转URL,将会显示重写后的URL。
+隐式表示不在客户端显示重写后的URL;显式表示在客户端跳转URL,将会显示重写后的URL。
+