实现访问日志配置

This commit is contained in:
GoEdgeLab
2020-09-20 11:56:30 +08:00
parent 8a04239edd
commit ea7499ff5d
9 changed files with 241 additions and 2 deletions

View File

@@ -65,6 +65,7 @@ func (this *APINode) listenRPC() error {
pb.RegisterHTTPHeaderPolicyServiceServer(rpcServer, &services.HTTPHeaderPolicyService{}) pb.RegisterHTTPHeaderPolicyServiceServer(rpcServer, &services.HTTPHeaderPolicyService{})
pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{}) pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{})
pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{}) pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{})
pb.RegisterHTTPAccessLogPolicyServiceServer(rpcServer, &services.HTTPAccessLogPolicyService{})
err = rpcServer.Serve(listener) err = rpcServer.Serve(listener)
if err != nil { if err != nil {
return errors.New("[API]start rpc failed: " + err.Error()) return errors.New("[API]start rpc failed: " + err.Error())

View File

@@ -0,0 +1,118 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
HTTPAccessLogPolicyStateEnabled = 1 // 已启用
HTTPAccessLogPolicyStateDisabled = 0 // 已禁用
)
type HTTPAccessLogPolicyDAO dbs.DAO
func NewHTTPAccessLogPolicyDAO() *HTTPAccessLogPolicyDAO {
return dbs.NewDAO(&HTTPAccessLogPolicyDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeHTTPAccessLogPolicies",
Model: new(HTTPAccessLogPolicy),
PkName: "id",
},
}).(*HTTPAccessLogPolicyDAO)
}
var SharedHTTPAccessLogPolicyDAO = NewHTTPAccessLogPolicyDAO()
// 启用条目
func (this *HTTPAccessLogPolicyDAO) EnableHTTPAccessLogPolicy(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", HTTPAccessLogPolicyStateEnabled).
Update()
return err
}
// 禁用条目
func (this *HTTPAccessLogPolicyDAO) DisableHTTPAccessLogPolicy(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", HTTPAccessLogPolicyStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *HTTPAccessLogPolicyDAO) FindEnabledHTTPAccessLogPolicy(id int64) (*HTTPAccessLogPolicy, error) {
result, err := this.Query().
Pk(id).
Attr("state", HTTPAccessLogPolicyStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*HTTPAccessLogPolicy), err
}
// 根据主键查找名称
func (this *HTTPAccessLogPolicyDAO) FindHTTPAccessLogPolicyName(id int64) (string, error) {
return this.Query().
Pk(id).
Result("name").
FindStringCol("")
}
// 查找所有可用策略信息
func (this *HTTPAccessLogPolicyDAO) FindAllEnabledAccessLogPolicies() (result []*HTTPAccessLogPolicy, err error) {
_, err = this.Query().
State(HTTPAccessLogPolicyStateEnabled).
DescPk().
Slice(&result).
FindAll()
return
}
// 组合配置
func (this *HTTPAccessLogPolicyDAO) ComposeAccessLogPolicyConfig(policyId int64) (*serverconfigs.HTTPAccessLogStoragePolicy, error) {
policy, err := this.FindEnabledHTTPAccessLogPolicy(policyId)
if err != nil {
return nil, err
}
if policy == nil {
return nil, nil
}
config := &serverconfigs.HTTPAccessLogStoragePolicy{}
config.Id = int64(policy.Id)
config.IsOn = policy.IsOn == 1
config.Name = policy.Name
config.Type = policy.Type
// 选项
if IsNotNull(policy.Options) {
m := map[string]interface{}{}
err = json.Unmarshal([]byte(policy.Options), &m)
if err != nil {
return nil, err
}
config.Options = m
}
// 条件
if IsNotNull(policy.Conds) {
// TODO 需要用更全面的条件管理器来代替RequestCond
conds := []*shared.RequestCond{}
err = json.Unmarshal([]byte(policy.Conds), &conds)
if err != nil {
return nil, err
}
config.Conds = conds
}
return config, nil
}

View File

@@ -0,0 +1,5 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,34 @@
package models
// 访问日志策略
type HTTPAccessLogPolicy struct {
Id uint32 `field:"id"` // ID
TemplateId uint32 `field:"templateId"` // 模版ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间
Name string `field:"name"` // 名称
IsOn uint8 `field:"isOn"` // 是否启用
Type string `field:"type"` // 存储类型
Options string `field:"options"` // 存储选项
Conds string `field:"conds"` // 请求条件
}
type HTTPAccessLogPolicyOperator struct {
Id interface{} // ID
TemplateId interface{} // 模版ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
State interface{} // 状态
CreatedAt interface{} // 创建时间
Name interface{} // 名称
IsOn interface{} // 是否启用
Type interface{} // 存储类型
Options interface{} // 存储选项
Conds interface{} // 请求条件
}
func NewHTTPAccessLogPolicyOperator() *HTTPAccessLogPolicyOperator {
return &HTTPAccessLogPolicyOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -136,6 +136,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
} }
} }
// 访问日志
if IsNotNull(web.AccessLog) {
accessLogConfig := &serverconfigs.HTTPAccessLogConfig{}
err = json.Unmarshal([]byte(web.AccessLog), accessLogConfig)
if err != nil {
return nil, err
}
config.AccessLog = accessLogConfig
}
// TODO 更多配置 // TODO 更多配置
return config, nil return config, nil
@@ -265,6 +275,22 @@ func (this *HTTPWebDAO) UpdateWebShutdown(webId int64, shutdownJSON []byte) erro
return this.NotifyUpdating(webId) return this.NotifyUpdating(webId)
} }
// 更改访问日志策略
func (this *HTTPWebDAO) UpdateWebAccessLogConfig(webId int64, accessLogJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
}
op := NewHTTPWebOperator()
op.Id = webId
op.AccessLog = JSONBytes(accessLogJSON)
_, err := this.Save(op)
if err != nil {
return err
}
return this.NotifyUpdating(webId)
}
// 通知更新 // 通知更新
func (this *HTTPWebDAO) NotifyUpdating(webId int64) error { func (this *HTTPWebDAO) NotifyUpdating(webId int64) error {
err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId) err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId)

View File

@@ -16,13 +16,13 @@ type HTTPWeb struct {
Pages string `field:"pages"` // 特殊页面 Pages string `field:"pages"` // 特殊页面
FirewallId uint32 `field:"firewallId"` // WAF ID FirewallId uint32 `field:"firewallId"` // WAF ID
CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID
AccessLogPolicyId uint32 `field:"accessLogPolicyId"` // 访问日志策略ID
RedirectToHttps string `field:"redirectToHttps"` // 跳转到HTTPS设置 RedirectToHttps string `field:"redirectToHttps"` // 跳转到HTTPS设置
Indexes string `field:"indexes"` // 首页文件列表 Indexes string `field:"indexes"` // 首页文件列表
MaxRequestBodySize string `field:"maxRequestBodySize"` // 最大允许的请求内容尺寸 MaxRequestBodySize string `field:"maxRequestBodySize"` // 最大允许的请求内容尺寸
StatPolicyId uint32 `field:"statPolicyId"` // 统计策略ID StatPolicyId uint32 `field:"statPolicyId"` // 统计策略ID
RequestHeaderPolicyId uint32 `field:"requestHeaderPolicyId"` // Request Header策略ID RequestHeaderPolicyId uint32 `field:"requestHeaderPolicyId"` // Request Header策略ID
ResponseHeaderPolicyId uint32 `field:"responseHeaderPolicyId"` // Response Header策略 ResponseHeaderPolicyId uint32 `field:"responseHeaderPolicyId"` // Response Header策略
AccessLog string `field:"accessLog"` // 访问日志配置
} }
type HTTPWebOperator struct { type HTTPWebOperator struct {
@@ -40,13 +40,13 @@ type HTTPWebOperator struct {
Pages interface{} // 特殊页面 Pages interface{} // 特殊页面
FirewallId interface{} // WAF ID FirewallId interface{} // WAF ID
CachePolicyId interface{} // 缓存策略ID CachePolicyId interface{} // 缓存策略ID
AccessLogPolicyId interface{} // 访问日志策略ID
RedirectToHttps interface{} // 跳转到HTTPS设置 RedirectToHttps interface{} // 跳转到HTTPS设置
Indexes interface{} // 首页文件列表 Indexes interface{} // 首页文件列表
MaxRequestBodySize interface{} // 最大允许的请求内容尺寸 MaxRequestBodySize interface{} // 最大允许的请求内容尺寸
StatPolicyId interface{} // 统计策略ID StatPolicyId interface{} // 统计策略ID
RequestHeaderPolicyId interface{} // Request Header策略ID RequestHeaderPolicyId interface{} // Request Header策略ID
ResponseHeaderPolicyId interface{} // Response Header策略 ResponseHeaderPolicyId interface{} // Response Header策略
AccessLog interface{} // 访问日志配置
} }
func NewHTTPWebOperator() *HTTPWebOperator { func NewHTTPWebOperator() *HTTPWebOperator {

View File

@@ -0,0 +1,39 @@
package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type HTTPAccessLogPolicyService struct {
}
// 获取所有可用策略
func (this *HTTPAccessLogPolicyService) FindAllEnabledHTTPAccessLogPolicies(ctx context.Context, req *pb.FindAllEnabledHTTPAccessLogPoliciesRequest) (*pb.FindAllEnabledHTTPAccessLogPoliciesResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAccessLogPolicies()
if err != nil {
return nil, err
}
result := []*pb.HTTPAccessLogPolicy{}
for _, policy := range policies {
result = append(result, &pb.HTTPAccessLogPolicy{
Id: int64(policy.Id),
Name: policy.Name,
IsOn: policy.IsOn == 1,
Type: policy.Name,
OptionsJSON: []byte(policy.Options),
CondsJSON: []byte(policy.Conds),
})
}
return &pb.FindAllEnabledHTTPAccessLogPoliciesResponse{AccessLogPolicies: result}, nil
}

View File

@@ -162,3 +162,18 @@ func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.Upda
} }
return rpcutils.RPCUpdateSuccess() return rpcutils.RPCUpdateSuccess()
} }
// 更改访问日志配置
func (this *HTTPWebService) UpdateHTTPAccessLog(ctx context.Context, req *pb.UpdateHTTPAccessLogRequest) (*pb.RPCUpdateSuccess, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebAccessLogConfig(req.WebId, req.AccessLogJSON)
if err != nil {
return nil, err
}
return rpcutils.RPCUpdateSuccess()
}