实现重写规则管理

This commit is contained in:
GoEdgeLab
2020-09-28 16:25:26 +08:00
parent 1353ad9e6d
commit f459632dd6
37 changed files with 935 additions and 66 deletions

View File

@@ -58,6 +58,7 @@ func (this *IndexAction) RunGet(params struct {
func (this *IndexAction) RunPost(params struct {
ServerId int64
IsOn bool
Addresses string
WebId int64
@@ -82,10 +83,9 @@ func (this *IndexAction) RunPost(params struct {
this.ErrorPage(err)
return
}
} else {
httpConfig.IsOn = true
}
httpConfig.IsOn = params.IsOn
httpConfig.Listen = addresses
configData, err := json.Marshal(httpConfig)
if err != nil {

View File

@@ -48,6 +48,7 @@ func (this *IndexAction) RunGet(params struct {
func (this *IndexAction) RunPost(params struct {
ServerId int64
IsOn bool
Addresses string
Must *actions.Must
@@ -69,10 +70,9 @@ func (this *IndexAction) RunPost(params struct {
this.ErrorPage(err)
return
}
} else {
httpsConfig.IsOn = true
}
httpsConfig.IsOn = params.IsOn
httpsConfig.Listen = addresses
configData, err := json.Marshal(httpsConfig)
if err != nil {

View File

@@ -63,6 +63,7 @@ func (this *IndexAction) RunGet(params struct {
"type": server.Type,
"name": server.Name,
"description": server.Description,
"isOn": server.IsOn,
}
serverType := serverconfigs.FindServerType(server.Type)
@@ -83,6 +84,7 @@ func (this *IndexAction) RunPost(params struct {
Name string
Description string
ClusterId int64
IsOn bool
Must *actions.Must
}) {
@@ -99,6 +101,7 @@ func (this *IndexAction) RunPost(params struct {
Name: params.Name,
Description: params.Description,
ClusterId: params.ClusterId,
IsOn: params.IsOn,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -11,6 +11,7 @@ import (
"strings"
)
// 创建路径规则
type CreateAction struct {
actionutils.ParentAction
}
@@ -22,6 +23,7 @@ func (this *CreateAction) Init() {
func (this *CreateAction) RunGet(params struct {
ServerId int64
ParentId int64 // 父节点
}) {
webConfig, err := webutils.FindWebConfigWithServerId(this.Parent(), params.ServerId)
if err != nil {

View File

@@ -15,6 +15,7 @@ func init() {
Get("", new(IndexAction)).
GetPost("/create", new(CreateAction)).
Post("/delete", new(DeleteAction)).
Post("/sort", new(SortAction)).
EndAll()
})
}

View File

@@ -79,15 +79,16 @@ func (this *LocationHelper) createMenus(serverIdString string, locationIdString
"isActive": secondMenuItem == "reverseProxy",
"isOn": locationConfig != nil && locationConfig.ReverseProxyRef != nil && locationConfig.ReverseProxyRef.IsPrior,
})
menuItems = append(menuItems, maps.Map{
"name": "-",
"url": "",
"isActive": false,
})
menuItems = append(menuItems, maps.Map{
"name": "重写规则",
"url": "/servers/server/settings/locations/rewrite?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "rewrite",
})
menuItems = append(menuItems, maps.Map{
"name": "访问控制",
"url": "/servers/server/settings/locations/access?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "access",
"isOn": locationConfig != nil && locationConfig.Web != nil && len(locationConfig.Web.RewriteRefs) > 0,
})
menuItems = append(menuItems, maps.Map{
"name": "WAF",
@@ -102,9 +103,9 @@ func (this *LocationHelper) createMenus(serverIdString string, locationIdString
"isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.CacheRef != nil && locationConfig.Web.CacheRef.IsPrior,
})
menuItems = append(menuItems, maps.Map{
"name": "-",
"url": "",
"isActive": false,
"name": "访问控制",
"url": "/servers/server/settings/locations/access?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "access",
})
menuItems = append(menuItems, maps.Map{
"name": "字符编码",

View File

@@ -2,6 +2,7 @@ package rewrite
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
)
type IndexAction struct {
@@ -14,7 +15,18 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct {
LocationId int64
}) {
// TODO
webConfig, err := webutils.FindWebConfigWithLocationId(this.Parent(), params.LocationId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
if len(webConfig.RewriteRules) == 0 {
this.Data["rewriteRules"] = []interface{}{}
} else {
this.Data["rewriteRules"] = webConfig.RewriteRules
}
this.Show()
}

View File

@@ -0,0 +1,58 @@
package locations
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
type SortAction struct {
actionutils.ParentAction
}
func (this *SortAction) RunPost(params struct {
WebId int64
LocationIds []int64
}) {
if len(params.LocationIds) == 0 {
this.Success()
}
webConfig, err := webutils.FindWebConfigWithId(this.Parent(), params.WebId)
if err != nil {
this.ErrorPage(err)
return
}
if webConfig == nil {
this.Success()
}
refMap := map[int64]*serverconfigs.HTTPLocationRef{}
for _, ref := range webConfig.LocationRefs {
refMap[ref.LocationId] = ref
}
newRefs := []*serverconfigs.HTTPLocationRef{}
for _, locationId := range params.LocationIds {
ref, ok := refMap[locationId]
if ok {
newRefs = append(newRefs, ref)
}
}
newRefsJSON, err := json.Marshal(newRefs)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebLocations(this.AdminContext(), &pb.UpdateHTTPWebLocationsRequest{
WebId: params.WebId,
LocationsJSON: newRefsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,100 @@
package rewrite
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
"regexp"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
}
func (this *CreatePopupAction) RunGet(params struct {
WebId int64
}) {
this.Data["webId"] = params.WebId
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
WebId int64
Pattern string
Replace string
Mode string
RedirectStatus int
ProxyHost string
IsBreak bool
IsOn bool
Must *actions.Must
}) {
params.Must.
Field("pattern", params.Pattern).
Require("请输入匹配规则").
Expect(func() (message string, success bool) {
_, err := regexp.Compile(params.Pattern)
if err != nil {
return "匹配规则错误:" + err.Error(), false
}
return "", true
})
params.Must.
Field("replace", params.Replace).
Require("请输入目标URL")
// web配置
webConfig, err := webutils.FindWebConfigWithId(this.Parent(), params.WebId)
if err != nil {
this.ErrorPage(err)
return
}
// 创建
createResp, err := this.RPC().HTTPRewriteRuleRPC().CreateHTTPRewriteRule(this.AdminContext(), &pb.CreateHTTPRewriteRuleRequest{
Pattern: params.Pattern,
Replace: params.Replace,
Mode: params.Mode,
RedirectStatus: types.Int32(params.RedirectStatus),
ProxyHost: params.ProxyHost,
IsBreak: params.IsBreak,
IsOn: params.IsOn,
})
if err != nil {
this.ErrorPage(err)
return
}
ref := &serverconfigs.HTTPRewriteRef{
IsOn: true,
RewriteRuleId: createResp.RewriteRuleId,
}
webConfig.RewriteRefs = append(webConfig.RewriteRefs, ref)
refsJSON, err := json.Marshal(webConfig.RewriteRefs)
if err != nil {
this.ErrorPage(err)
return
}
// 设置Web中的重写规则
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRewriteRules(this.AdminContext(), &pb.UpdateHTTPWebRewriteRulesRequest{
WebId: params.WebId,
RewriteRulesJSON: refsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,48 @@
package rewrite
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
WebId int64
RewriteRuleId int64
}) {
webConfig, err := webutils.FindWebConfigWithId(this.Parent(), params.WebId)
if err != nil {
this.ErrorPage(err)
return
}
refs := []*serverconfigs.HTTPRewriteRef{}
for _, ref := range webConfig.RewriteRefs {
if ref.RewriteRuleId == params.RewriteRuleId {
continue
}
refs = append(refs, ref)
}
refsJSON, err := json.Marshal(refs)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRewriteRules(this.AdminContext(), &pb.UpdateHTTPWebRewriteRulesRequest{
WebId: params.WebId,
RewriteRulesJSON: refsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -2,6 +2,7 @@ package rewrite
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
)
type IndexAction struct {
@@ -16,7 +17,18 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// TODO
webConfig, err := webutils.FindWebConfigWithServerId(this.Parent(), params.ServerId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
if len(webConfig.RewriteRules) == 0 {
this.Data["rewriteRules"] = []interface{}{}
} else {
this.Data["rewriteRules"] = webConfig.RewriteRules
}
this.Show()
}

View File

@@ -13,6 +13,10 @@ func init() {
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/rewrite").
Get("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)).
GetPost("/updatePopup", new(UpdatePopupAction)).
Post("/delete", new(DeleteAction)).
Post("/sort", new(SortAction)).
EndAll()
})
}

View File

@@ -0,0 +1,50 @@
package rewrite
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
type SortAction struct {
actionutils.ParentAction
}
func (this *SortAction) RunPost(params struct {
WebId int64
RewriteRuleIds []int64
}) {
webConfig, err := webutils.FindWebConfigWithId(this.Parent(), params.WebId)
if err != nil {
this.ErrorPage(err)
return
}
refsMap := map[int64]*serverconfigs.HTTPRewriteRef{}
for _, ref := range webConfig.RewriteRefs {
refsMap[ref.RewriteRuleId] = ref
}
newRefs := []*serverconfigs.HTTPRewriteRef{}
for _, rewriteRuleId := range params.RewriteRuleIds {
ref, ok := refsMap[rewriteRuleId]
if ok {
newRefs = append(newRefs, ref)
}
}
refsJSON, err := json.Marshal(newRefs)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRewriteRules(this.AdminContext(), &pb.UpdateHTTPWebRewriteRulesRequest{
WebId: params.WebId,
RewriteRulesJSON: refsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,93 @@
package rewrite
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
"regexp"
)
type UpdatePopupAction struct {
actionutils.ParentAction
}
func (this *UpdatePopupAction) Init() {
}
func (this *UpdatePopupAction) RunGet(params struct {
WebId int64
RewriteRuleId int64
}) {
this.Data["webId"] = params.WebId
webConfig, err := webutils.FindWebConfigWithId(this.Parent(), params.WebId)
if err != nil {
this.ErrorPage(err)
return
}
isFound := false
for _, rewriteRule := range webConfig.RewriteRules {
if rewriteRule.Id == params.RewriteRuleId {
this.Data["rewriteRule"] = rewriteRule
isFound = true
break
}
}
if !isFound {
this.WriteString("找不到要修改的重写规则")
return
}
this.Show()
}
func (this *UpdatePopupAction) RunPost(params struct {
WebId int64
RewriteRuleId int64
Pattern string
Replace string
Mode string
RedirectStatus int
ProxyHost string
IsBreak bool
IsOn bool
Must *actions.Must
}) {
params.Must.
Field("pattern", params.Pattern).
Require("请输入匹配规则").
Expect(func() (message string, success bool) {
_, err := regexp.Compile(params.Pattern)
if err != nil {
return "匹配规则错误:" + err.Error(), false
}
return "", true
})
params.Must.
Field("replace", params.Replace).
Require("请输入目标URL")
// 修改
_, err := this.RPC().HTTPRewriteRuleRPC().UpdateHTTPRewriteRule(this.AdminContext(), &pb.UpdateHTTPRewriteRuleRequest{
RewriteRuleId: params.RewriteRuleId,
Pattern: params.Pattern,
Replace: params.Replace,
Mode: params.Mode,
RedirectStatus: types.Int32(params.RedirectStatus),
ProxyHost: params.ProxyHost,
IsBreak: params.IsBreak,
IsOn: params.IsOn,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -159,12 +159,14 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"url": "/servers/server/settings/http?serverId=" + serverIdString,
"isActive": secondMenuItem == "http",
"isOn": (serverConfig.HTTP != nil && serverConfig.HTTP.IsOn && len(serverConfig.HTTP.Listen) > 0) || (serverConfig.Web != nil && serverConfig.Web.RedirectToHttps != nil && serverConfig.Web.RedirectToHttps.IsOn),
"isOff": serverConfig.HTTP != nil && !serverConfig.HTTP.IsOn,
})
menuItems = append(menuItems, maps.Map{
"name": "HTTPS",
"url": "/servers/server/settings/https?serverId=" + serverIdString,
"isActive": secondMenuItem == "https",
"isOn": serverConfig.HTTPS != nil && serverConfig.HTTPS.IsOn && len(serverConfig.HTTPS.Listen) > 0,
"isOff": serverConfig.HTTPS != nil && !serverConfig.HTTPS.IsOn,
})
menuItems = append(menuItems, maps.Map{
"name": "Web设置",
@@ -178,6 +180,11 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"isActive": secondMenuItem == "reverseProxy",
"isOn": serverConfig.ReverseProxyRef != nil && serverConfig.ReverseProxyRef.IsOn,
})
menuItems = append(menuItems, maps.Map{
"name": "-",
"url": "",
"isActive": false,
})
menuItems = append(menuItems, maps.Map{
"name": "路径规则",
"url": "/servers/server/settings/locations?serverId=" + serverIdString,
@@ -188,11 +195,7 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"name": "重写规则",
"url": "/servers/server/settings/rewrite?serverId=" + serverIdString,
"isActive": secondMenuItem == "rewrite",
})
menuItems = append(menuItems, maps.Map{
"name": "访问控制",
"url": "/servers/server/settings/access?serverId=" + serverIdString,
"isActive": secondMenuItem == "access",
"isOn": serverConfig.Web != nil && len(serverConfig.Web.RewriteRefs) > 0,
})
menuItems = append(menuItems, maps.Map{
"name": "WAF",
@@ -207,9 +210,9 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"isOn": serverConfig.Web != nil && serverConfig.Web.CacheRef != nil && serverConfig.Web.CacheRef.IsOn,
})
menuItems = append(menuItems, maps.Map{
"name": "-",
"url": "",
"isActive": false,
"name": "访问控制",
"url": "/servers/server/settings/access?serverId=" + serverIdString,
"isActive": secondMenuItem == "access",
})
menuItems = append(menuItems, maps.Map{
"name": "字符编码",