waflog.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package admin
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. "math"
  9. "strings"
  10. "time"
  11. )
  12. type WafLogRepository interface {
  13. GetWafLog(ctx context.Context, id int64) (*model.WafLog, error)
  14. GetWafLogList(ctx context.Context, req adminApi.SearchWafLogParams) (*v1.PaginatedResponse[model.WafLog], error)
  15. AddWafLog(ctx context.Context, log *model.WafLog) error
  16. BatchAddWafLog(ctx context.Context, logs []*model.WafLog) error
  17. // 导出日志
  18. ExportWafLog(ctx context.Context, req adminApi.ExportWafLog) ([]model.WafLog, error)
  19. // 获取网关组记录
  20. GetWafLogGateWayIp(ctx context.Context, hostId int64, Uid int64,createdAt time.Time) (model.WafLog, error)
  21. }
  22. func NewWafLogRepository(
  23. repository *repository.Repository,
  24. ) WafLogRepository {
  25. return &wafLogRepository{
  26. Repository: repository,
  27. }
  28. }
  29. type wafLogRepository struct {
  30. *repository.Repository
  31. }
  32. func (r *wafLogRepository) GetWafLog(ctx context.Context, id int64) (*model.WafLog, error) {
  33. var res model.WafLog
  34. return &res, r.DBWithName(ctx,"admin").Where("id = ?", id).First(&res).Error
  35. }
  36. func (r *wafLogRepository) GetWafLogList(ctx context.Context, req adminApi.SearchWafLogParams) (*v1.PaginatedResponse[model.WafLog], error) {
  37. var res []model.WafLog
  38. var total int64
  39. query := r.DBWithName(ctx,"admin").Model(&model.WafLog{})
  40. if req.RequestIp != "" {
  41. trimmedName := strings.TrimSpace(req.RequestIp)
  42. // 使用 LIKE 进行模糊匹配
  43. query = query.Where("request_ip LIKE CONCAT('%', ?, '%')", trimmedName)
  44. }
  45. if req.Uid != 0 {
  46. query = query.Where("uid = ?", req.Uid)
  47. }
  48. if req.Api != "" {
  49. trimmedName := strings.TrimSpace(req.Api)
  50. // 使用 LIKE 进行模糊匹配
  51. query = query.Where("api LIKE CONCAT('%', ?, '%')", trimmedName)
  52. }
  53. if req.Name != "" {
  54. trimmedName := strings.TrimSpace(req.Name)
  55. // 使用 LIKE 进行模糊匹配
  56. query = query.Where("name LIKE CONCAT('%', ?, '%')", trimmedName)
  57. }
  58. if req.RuleId != 0 {
  59. query = query.Where("rule_id = ?", req.RuleId)
  60. }
  61. if req.HostId != 0 {
  62. query = query.Where("host_id = ?", req.HostId)
  63. }
  64. if req.Api != "" {
  65. trimmedName := strings.TrimSpace(req.Api)
  66. // 使用 LIKE 进行模糊匹配
  67. query = query.Where("api LIKE CONCAT('%', ?, '%')", trimmedName)
  68. }
  69. if req.UserAgent != "" {
  70. trimmedName := strings.TrimSpace(req.UserAgent)
  71. // 使用 LIKE 进行模糊匹配
  72. query = query.Where("user_agent LIKE CONCAT('%', ?, '%')", trimmedName)
  73. }
  74. if req.ApiName != "" {
  75. trimmedName := strings.TrimSpace(req.ApiName)
  76. // 使用 LIKE 进行模糊匹配
  77. query = query.Where("api_name LIKE CONCAT('%', ?, '%')", trimmedName)
  78. }
  79. if req.ApiType != "" {
  80. query = query.Where("api_type = ?", req.ApiType)
  81. }
  82. if req.Column != "" {
  83. query = query.Order(req.Column + " " + req.Order)
  84. }
  85. if err := query.Count(&total).Error; err != nil {
  86. // 如果连计数都失败了,直接返回错误
  87. return nil, err
  88. }
  89. page := req.Current
  90. pageSize := req.PageSize
  91. if page <= 0 {
  92. page = 1
  93. }
  94. if pageSize <= 0 {
  95. pageSize = 10 // 默认每页 10 条
  96. } else if pageSize > 100 {
  97. pageSize = 100 // 每页最多 100 条
  98. }
  99. // 计算 offset (偏移量)
  100. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  101. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  102. offset := (page - 1) * pageSize
  103. // 3. 执行最终的查询
  104. // 在所有条件都添加完毕后,再执行 .Find()
  105. result := query.Offset(offset).Limit(pageSize).Find(&res)
  106. if result.Error != nil {
  107. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  108. return nil, result.Error
  109. }
  110. return &v1.PaginatedResponse[model.WafLog]{
  111. Records: res,
  112. Page: page,
  113. PageSize: pageSize,
  114. Total: total,
  115. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  116. }, nil
  117. }
  118. func (r *wafLogRepository) AddWafLog(ctx context.Context, log *model.WafLog) error {
  119. return r.DBWithName(ctx,"admin").Create(log).Error
  120. }
  121. func (r *wafLogRepository) BatchAddWafLog(ctx context.Context, logs []*model.WafLog) error {
  122. if len(logs) == 0 {
  123. return nil
  124. }
  125. return r.DBWithName(ctx, "admin").CreateInBatches(logs, len(logs)).Error
  126. }
  127. func (r *wafLogRepository) ExportWafLog(ctx context.Context, req adminApi.ExportWafLog) ([]model.WafLog, error) {
  128. var res []model.WafLog
  129. query := r.DBWithName(ctx,"admin").Model(&model.WafLog{})
  130. if req.RequestIp != "" {
  131. trimmedName := strings.TrimSpace(req.RequestIp)
  132. // 使用 LIKE 进行模糊匹配
  133. query = query.Where("request_ip = ?", trimmedName)
  134. }
  135. if req.Uid != 0 {
  136. query = query.Where("uid = ?", req.Uid)
  137. }
  138. if req.Api != "" {
  139. trimmedName := strings.TrimSpace(req.Api)
  140. // 使用 LIKE 进行模糊匹配
  141. query = query.Where("api = ?", trimmedName)
  142. }
  143. if req.Name != "" {
  144. trimmedName := strings.TrimSpace(req.Name)
  145. // 使用 LIKE 进行模糊匹配
  146. query = query.Where("name = ?", trimmedName)
  147. }
  148. if req.RuleId != 0 {
  149. query = query.Where("rule_id = ?", req.RuleId)
  150. }
  151. if len(req.HostIds) > 0 {
  152. query = query.Where("host_id IN ?", req.HostIds)
  153. }
  154. if req.Api != "" {
  155. trimmedName := strings.TrimSpace(req.Api)
  156. // 使用 LIKE 进行模糊匹配
  157. query = query.Where("api = ?", trimmedName)
  158. }
  159. if req.UserAgent != "" {
  160. trimmedName := strings.TrimSpace(req.UserAgent)
  161. // 使用 LIKE 进行模糊匹配
  162. query = query.Where("user_agent = ?", trimmedName)
  163. }
  164. if len(req.ApiNames) > 0 {
  165. trimmedNames := make([]string, len(req.ApiNames))
  166. for _, apiName := range req.ApiNames {
  167. trimmedNames = append(trimmedNames, strings.TrimSpace(apiName))
  168. }
  169. // 使用 LIKE 进行模糊匹配
  170. query = query.Where("api_name IN ?", trimmedNames)
  171. }
  172. if len(req.ApiTypes) > 0 {
  173. query = query.Where("api_type IN ?", req.ApiTypes)
  174. }
  175. if req.StartTime != "" {
  176. trimmedName := strings.TrimSpace(req.StartTime)
  177. // 使用 LIKE 进行模糊匹配
  178. query = query.Where("create_at > ?", trimmedName)
  179. }
  180. if req.EndTime != "" {
  181. trimmedName := strings.TrimSpace(req.EndTime)
  182. // 使用 LIKE 进行模糊匹配
  183. query = query.Where("create_at < ?", trimmedName)
  184. }
  185. result := query.Find(&res)
  186. if result.Error != nil {
  187. return nil, result.Error
  188. }
  189. return res, nil
  190. }
  191. func (r *wafLogRepository) GetWafLogGateWayIp(ctx context.Context, hostId int64, Uid int64,createdAt time.Time) (model.WafLog, error) {
  192. var res model.WafLog
  193. return res, r.DBWithName(ctx,"admin").Where("host_id = ? and uid = ? and api_name = ? and created_at > ? ", hostId, Uid, "分配网关组", createdAt).First(&res).Error
  194. }