diff --git a/internal/apis/api_node.go b/internal/apis/api_node.go index d2afae57..e9ea8d02 100644 --- a/internal/apis/api_node.go +++ b/internal/apis/api_node.go @@ -65,6 +65,7 @@ func (this *APINode) listenRPC() error { pb.RegisterHTTPHeaderPolicyServiceServer(rpcServer, &services.HTTPHeaderPolicyService{}) pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{}) pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{}) + pb.RegisterHTTPAccessLogPolicyServiceServer(rpcServer, &services.HTTPAccessLogPolicyService{}) err = rpcServer.Serve(listener) if err != nil { return errors.New("[API]start rpc failed: " + err.Error()) diff --git a/internal/db/models/http_access_log_policy_dao.go b/internal/db/models/http_access_log_policy_dao.go new file mode 100644 index 00000000..849b792f --- /dev/null +++ b/internal/db/models/http_access_log_policy_dao.go @@ -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 +} diff --git a/internal/db/models/http_access_log_policy_dao_test.go b/internal/db/models/http_access_log_policy_dao_test.go new file mode 100644 index 00000000..97c24b56 --- /dev/null +++ b/internal/db/models/http_access_log_policy_dao_test.go @@ -0,0 +1,5 @@ +package models + +import ( + _ "github.com/go-sql-driver/mysql" +) diff --git a/internal/db/models/http_access_log_policy_model.go b/internal/db/models/http_access_log_policy_model.go new file mode 100644 index 00000000..b4fef975 --- /dev/null +++ b/internal/db/models/http_access_log_policy_model.go @@ -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{} +} diff --git a/internal/db/models/http_access_log_policy_model_ext.go b/internal/db/models/http_access_log_policy_model_ext.go new file mode 100644 index 00000000..2640e7f9 --- /dev/null +++ b/internal/db/models/http_access_log_policy_model_ext.go @@ -0,0 +1 @@ +package models diff --git a/internal/db/models/http_web_dao.go b/internal/db/models/http_web_dao.go index 556de3bd..9cba2f9c 100644 --- a/internal/db/models/http_web_dao.go +++ b/internal/db/models/http_web_dao.go @@ -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 更多配置 return config, nil @@ -265,6 +275,22 @@ func (this *HTTPWebDAO) UpdateWebShutdown(webId int64, shutdownJSON []byte) erro 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 { err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId) diff --git a/internal/db/models/http_web_model.go b/internal/db/models/http_web_model.go index f225bf51..a3186b48 100644 --- a/internal/db/models/http_web_model.go +++ b/internal/db/models/http_web_model.go @@ -16,13 +16,13 @@ type HTTPWeb struct { Pages string `field:"pages"` // 特殊页面 FirewallId uint32 `field:"firewallId"` // WAF ID CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID - AccessLogPolicyId uint32 `field:"accessLogPolicyId"` // 访问日志策略ID RedirectToHttps string `field:"redirectToHttps"` // 跳转到HTTPS设置 Indexes string `field:"indexes"` // 首页文件列表 MaxRequestBodySize string `field:"maxRequestBodySize"` // 最大允许的请求内容尺寸 StatPolicyId uint32 `field:"statPolicyId"` // 统计策略ID RequestHeaderPolicyId uint32 `field:"requestHeaderPolicyId"` // Request Header策略ID ResponseHeaderPolicyId uint32 `field:"responseHeaderPolicyId"` // Response Header策略 + AccessLog string `field:"accessLog"` // 访问日志配置 } type HTTPWebOperator struct { @@ -40,13 +40,13 @@ type HTTPWebOperator struct { Pages interface{} // 特殊页面 FirewallId interface{} // WAF ID CachePolicyId interface{} // 缓存策略ID - AccessLogPolicyId interface{} // 访问日志策略ID RedirectToHttps interface{} // 跳转到HTTPS设置 Indexes interface{} // 首页文件列表 MaxRequestBodySize interface{} // 最大允许的请求内容尺寸 StatPolicyId interface{} // 统计策略ID RequestHeaderPolicyId interface{} // Request Header策略ID ResponseHeaderPolicyId interface{} // Response Header策略 + AccessLog interface{} // 访问日志配置 } func NewHTTPWebOperator() *HTTPWebOperator { diff --git a/internal/rpc/services/service_http_access_log_policy.go b/internal/rpc/services/service_http_access_log_policy.go new file mode 100644 index 00000000..80b2a617 --- /dev/null +++ b/internal/rpc/services/service_http_access_log_policy.go @@ -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 +} diff --git a/internal/rpc/services/service_http_web.go b/internal/rpc/services/service_http_web.go index c9cb6ec1..76656f1f 100644 --- a/internal/rpc/services/service_http_web.go +++ b/internal/rpc/services/service_http_web.go @@ -162,3 +162,18 @@ func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.Upda } 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() +}