内容压缩支持brotli和deflate

This commit is contained in:
GoEdgeLab
2021-09-29 20:12:27 +08:00
parent 84dd0d662b
commit 2a8846b4be
23 changed files with 486 additions and 322 deletions

View File

@@ -1,6 +1,7 @@
package waf
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
@@ -37,6 +38,7 @@ func (this *UpdateAction) RunGet(params struct {
StatusCode: http.StatusForbidden,
Body: "Blocked By WAF",
URL: "",
Timeout: 60,
}
}
@@ -87,7 +89,14 @@ func (this *UpdateAction) RunPost(params struct {
Field("name", params.Name).
Require("请输入策略名称")
_, err := this.RPC().HTTPFirewallPolicyRPC().UpdateHTTPFirewallPolicy(this.AdminContext(), &pb.UpdateHTTPFirewallPolicyRequest{
// 校验JSON
var blockOptions = &firewallconfigs.HTTPFirewallBlockAction{}
err := json.Unmarshal(params.BlockOptionsJSON, blockOptions)
if err != nil {
this.Fail("拦截动作参数校验失败:" + err.Error())
}
_, err = this.RPC().HTTPFirewallPolicyRPC().UpdateHTTPFirewallPolicy(this.AdminContext(), &pb.UpdateHTTPFirewallPolicyRequest{
HttpFirewallPolicyId: params.FirewallPolicyId,
IsOn: params.IsOn,
Name: params.Name,

View File

@@ -0,0 +1,71 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package compression
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"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("compression")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// WebId
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["compressionConfig"] = webConfig.Compression
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
CompressionJSON []byte
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo("修改Web %d 的压缩设置", params.WebId)
// 校验配置
var compressionConfig = &serverconfigs.HTTPCompressionConfig{}
err := json.Unmarshal(params.CompressionJSON, compressionConfig)
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
err = compressionConfig.Init()
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebCompression(this.AdminContext(), &pb.UpdateHTTPWebCompressionRequest{
WebId: params.WebId,
CompressionJSON: params.CompressionJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -1,4 +1,4 @@
package gzip
package compression
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
@@ -12,7 +12,7 @@ func init() {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/gzip").
Prefix("/servers/server/settings/compression").
GetPost("", new(IndexAction)).
EndAll()
})

View File

@@ -1,153 +0,0 @@
package gzip
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"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"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("gzip")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
gzipId := int64(0)
if webConfig.GzipRef != nil {
gzipId = webConfig.GzipRef.GzipId
}
gzipConfig := &serverconfigs.HTTPGzipConfig{
Id: 0,
IsOn: true,
}
if gzipId > 0 {
resp, err := this.RPC().HTTPGzipRPC().FindEnabledHTTPGzipConfig(this.AdminContext(), &pb.FindEnabledGzipConfigRequest{HttpGzipId: gzipId})
if err != nil {
this.ErrorPage(err)
return
}
err = json.Unmarshal(resp.HttpGzipJSON, gzipConfig)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["gzipConfig"] = gzipConfig
if webConfig.GzipRef == nil {
webConfig.GzipRef = &serverconfigs.HTTPGzipRef{
IsPrior: false,
IsOn: false,
GzipId: 0,
}
}
this.Data["gzipRef"] = webConfig.GzipRef
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
GzipId int64
Level int
MinLength string
MaxLength string
CondsJSON []byte
GzipRefJSON []byte
Must *actions.Must
}) {
// 日志
defer this.CreateLog(oplogs.LevelInfo, "修改Web %d 的GZip配置", params.WebId)
if params.Level < 0 || params.Level > 9 {
this.Fail("请选择正确的压缩级别")
}
minLength := &pb.SizeCapacity{Count: -1}
if len(params.MinLength) > 0 {
err := json.Unmarshal([]byte(params.MinLength), minLength)
if err != nil {
this.ErrorPage(err)
return
}
}
maxLength := &pb.SizeCapacity{Count: -1}
if len(params.MaxLength) > 0 {
err := json.Unmarshal([]byte(params.MaxLength), maxLength)
if err != nil {
this.ErrorPage(err)
return
}
}
gzipRef := &serverconfigs.HTTPGzipRef{}
err := json.Unmarshal(params.GzipRefJSON, gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
if params.GzipId > 0 {
_, err := this.RPC().HTTPGzipRPC().UpdateHTTPGzip(this.AdminContext(), &pb.UpdateHTTPGzipRequest{
HttpGzipId: params.GzipId,
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
CondsJSON: params.CondsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
} else {
resp, err := this.RPC().HTTPGzipRPC().CreateHTTPGzip(this.AdminContext(), &pb.CreateHTTPGzipRequest{
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
CondsJSON: params.CondsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
gzipRef.GzipId = resp.HttpGzipId
}
gzipRefJSON, err := json.Marshal(gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{
WebId: params.WebId,
GzipJSON: gzipRefJSON,
})
if err != nil {
this.ErrorPage(err)
}
this.Success()
}

View File

@@ -0,0 +1,69 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package compression
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"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
LocationId int64
}) {
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithLocationId(this.AdminContext(), params.LocationId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["compressionConfig"] = webConfig.Compression
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
CompressionJSON []byte
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo("修改Web %d 的压缩设置", params.WebId)
// 校验配置
var compressionConfig = &serverconfigs.HTTPCompressionConfig{}
err := json.Unmarshal(params.CompressionJSON, compressionConfig)
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
err = compressionConfig.Init()
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebCompression(this.AdminContext(), &pb.UpdateHTTPWebCompressionRequest{
WebId: params.WebId,
CompressionJSON: params.CompressionJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -1,4 +1,4 @@
package gzip
package compression
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
@@ -14,8 +14,8 @@ func init() {
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Helper(locationutils.NewLocationHelper()).
Helper(serverutils.NewServerHelper()).
Data("tinyMenuItem", "gzip").
Prefix("/servers/server/settings/locations/gzip").
Data("tinyMenuItem", "compression").
Prefix("/servers/server/settings/locations/compression").
GetPost("", new(IndexAction)).
EndAll()
})

View File

@@ -1,150 +0,0 @@
package gzip
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"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("gzip")
}
func (this *IndexAction) RunGet(params struct {
LocationId int64
}) {
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithLocationId(this.AdminContext(), params.LocationId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
gzipId := int64(0)
if webConfig.GzipRef != nil {
gzipId = webConfig.GzipRef.GzipId
}
gzipConfig := &serverconfigs.HTTPGzipConfig{
Id: 0,
IsOn: true,
}
if gzipId > 0 {
resp, err := this.RPC().HTTPGzipRPC().FindEnabledHTTPGzipConfig(this.AdminContext(), &pb.FindEnabledGzipConfigRequest{HttpGzipId: gzipId})
if err != nil {
this.ErrorPage(err)
return
}
err = json.Unmarshal(resp.HttpGzipJSON, gzipConfig)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["gzipConfig"] = gzipConfig
if webConfig.GzipRef == nil {
webConfig.GzipRef = &serverconfigs.HTTPGzipRef{
IsPrior: false,
IsOn: false,
GzipId: 0,
}
}
this.Data["gzipRef"] = webConfig.GzipRef
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
GzipRefJSON []byte
GzipId int64
Level int
MinLength string
MaxLength string
Must *actions.Must
}) {
defer this.CreateLogInfo("修改Web %d 的Gzip设置", params.WebId)
if params.Level < 0 || params.Level > 9 {
this.Fail("请选择正确的压缩级别")
}
minLength := &pb.SizeCapacity{Count: -1}
if len(params.MinLength) > 0 {
err := json.Unmarshal([]byte(params.MinLength), minLength)
if err != nil {
this.ErrorPage(err)
return
}
}
maxLength := &pb.SizeCapacity{Count: -1}
if len(params.MaxLength) > 0 {
err := json.Unmarshal([]byte(params.MaxLength), maxLength)
if err != nil {
this.ErrorPage(err)
return
}
}
gzipRef := &serverconfigs.HTTPGzipRef{}
err := json.Unmarshal(params.GzipRefJSON, gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
gzipRef.GzipId = params.GzipId
if params.GzipId > 0 {
_, err := this.RPC().HTTPGzipRPC().UpdateHTTPGzip(this.AdminContext(), &pb.UpdateHTTPGzipRequest{
HttpGzipId: params.GzipId,
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
})
if err != nil {
this.ErrorPage(err)
return
}
} else {
resp, err := this.RPC().HTTPGzipRPC().CreateHTTPGzip(this.AdminContext(), &pb.CreateHTTPGzipRequest{
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
})
if err != nil {
this.ErrorPage(err)
return
}
gzipId := resp.HttpGzipId
gzipRef.GzipId = gzipId
}
gzipRefJSON, err := json.Marshal(gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{
WebId: params.WebId,
GzipJSON: gzipRefJSON,
})
if err != nil {
this.ErrorPage(err)
}
this.Success()
}

View File

@@ -127,10 +127,10 @@ func (this *LocationHelper) createMenus(serverIdString string, locationIdString
"isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.StatRef != nil && locationConfig.Web.StatRef.IsPrior,
})
menuItems = append(menuItems, maps.Map{
"name": "Gzip压缩",
"url": "/servers/server/settings/locations/gzip?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "gzip",
"isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.GzipRef != nil && locationConfig.Web.GzipRef.IsPrior,
"name": "内容压缩",
"url": "/servers/server/settings/locations/compression?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "compression",
"isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.Compression != nil && locationConfig.Web.Compression.IsPrior,
})
menuItems = append(menuItems, maps.Map{
"name": "特殊页面",

View File

@@ -299,10 +299,10 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"isOn": serverConfig.Web != nil && serverConfig.Web.StatRef != nil && serverConfig.Web.StatRef.IsOn,
})
menuItems = append(menuItems, maps.Map{
"name": "Gzip压缩",
"url": "/servers/server/settings/gzip?serverId=" + serverIdString,
"isActive": secondMenuItem == "gzip",
"isOn": serverConfig.Web != nil && serverConfig.Web.GzipRef != nil && serverConfig.Web.GzipRef.IsOn,
"name": "内容压缩",
"url": "/servers/server/settings/compression?serverId=" + serverIdString,
"isActive": secondMenuItem == "compression",
"isOn": serverConfig.Web != nil && serverConfig.Web.Compression != nil && serverConfig.Web.Compression.IsOn,
})
menuItems = append(menuItems, maps.Map{
"name": "特殊页面",

View File

@@ -59,10 +59,10 @@ import (
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/accessLog"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/cache"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/charset"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/compression"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/conds"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/dns"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/fastcgi"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/gzip"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/headers"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/http"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/https"
@@ -71,8 +71,8 @@ import (
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/accessLog"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/cache"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/charset"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/compression"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/fastcgi"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/gzip"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/headers"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/http"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/location"