|
@@ -1,13 +1,14 @@
|
|
|
package admin
|
|
|
|
|
|
import (
|
|
|
- "context"
|
|
|
+ "context"
|
|
|
v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
|
|
|
adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
|
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/model"
|
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/repository"
|
|
|
"math"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type WafLogRepository interface {
|
|
@@ -15,6 +16,10 @@ type WafLogRepository interface {
|
|
|
GetWafLogList(ctx context.Context, req adminApi.SearchWafLogParams) (*v1.PaginatedResponse[model.WafLog], error)
|
|
|
AddWafLog(ctx context.Context, log *model.WafLog) error
|
|
|
BatchAddWafLog(ctx context.Context, logs []*model.WafLog) error
|
|
|
+ // 导出日志
|
|
|
+ ExportWafLog(ctx context.Context, req adminApi.ExportWafLog) ([]model.WafLog, error)
|
|
|
+ // 获取网关组记录
|
|
|
+ GetWafLogGateWayIp(ctx context.Context, hostId int64, Uid int64,createdAt time.Time) (model.WafLog, error)
|
|
|
}
|
|
|
|
|
|
func NewWafLogRepository(
|
|
@@ -148,3 +153,89 @@ func (r *wafLogRepository) BatchAddWafLog(ctx context.Context, logs []*model.Waf
|
|
|
}
|
|
|
return r.DBWithName(ctx, "admin").CreateInBatches(logs, len(logs)).Error
|
|
|
}
|
|
|
+
|
|
|
+func (r *wafLogRepository) ExportWafLog(ctx context.Context, req adminApi.ExportWafLog) ([]model.WafLog, error) {
|
|
|
+ var res []model.WafLog
|
|
|
+ query := r.DBWithName(ctx,"admin").Model(&model.WafLog{})
|
|
|
+ if req.RequestIp != "" {
|
|
|
+ trimmedName := strings.TrimSpace(req.RequestIp)
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("request_ip = ?", trimmedName)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if req.Uid != 0 {
|
|
|
+ query = query.Where("uid = ?", req.Uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.Api != "" {
|
|
|
+ trimmedName := strings.TrimSpace(req.Api)
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("api = ?", trimmedName)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.Name != "" {
|
|
|
+ trimmedName := strings.TrimSpace(req.Name)
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("name = ?", trimmedName)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if req.RuleId != 0 {
|
|
|
+ query = query.Where("rule_id = ?", req.RuleId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(req.HostIds) > 0 {
|
|
|
+ query = query.Where("host_id IN ?", req.HostIds)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.Api != "" {
|
|
|
+ trimmedName := strings.TrimSpace(req.Api)
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("api = ?", trimmedName)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.UserAgent != "" {
|
|
|
+ trimmedName := strings.TrimSpace(req.UserAgent)
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("user_agent = ?", trimmedName)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(req.ApiNames) > 0 {
|
|
|
+ trimmedNames := make([]string, len(req.ApiNames))
|
|
|
+ for _, apiName := range req.ApiNames {
|
|
|
+ trimmedNames = append(trimmedNames, strings.TrimSpace(apiName))
|
|
|
+ }
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("api_name IN ?", trimmedNames)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(req.ApiTypes) > 0 {
|
|
|
+ query = query.Where("api_type IN ?", req.ApiTypes)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if req.StartTime != "" {
|
|
|
+ trimmedName := strings.TrimSpace(req.StartTime)
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("create_at > ?", trimmedName)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.EndTime != "" {
|
|
|
+ trimmedName := strings.TrimSpace(req.EndTime)
|
|
|
+ // 使用 LIKE 进行模糊匹配
|
|
|
+ query = query.Where("create_at < ?", trimmedName)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ result := query.Find(&res)
|
|
|
+ if result.Error != nil {
|
|
|
+ return nil, result.Error
|
|
|
+ }
|
|
|
+ return res, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (r *wafLogRepository) GetWafLogGateWayIp(ctx context.Context, hostId int64, Uid int64,createdAt time.Time) (model.WafLog, error) {
|
|
|
+ var res model.WafLog
|
|
|
+ return res, r.DBWithName(ctx,"admin").Where("host_id = ? and uid = ? and api_name = ? and created_at > ? ", hostId, Uid, "分配网关组", createdAt).First(&res).Error
|
|
|
+}
|