waflog.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package admin
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. adminRep "github.com/go-nunu/nunu-layout-advanced/internal/repository/admin"
  9. "github.com/go-nunu/nunu-layout-advanced/internal/repository/api/waf"
  10. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  11. "github.com/go-nunu/nunu-layout-advanced/pkg/rabbitmq"
  12. amqp "github.com/rabbitmq/amqp091-go"
  13. "go.uber.org/zap"
  14. "strings"
  15. )
  16. type WafLogService interface {
  17. GetWafLog(ctx context.Context, id int64) (*model.WafLog, error)
  18. GetWafLogList(ctx context.Context) ([]model.WafLog, error)
  19. AddWafLog(ctx context.Context, req adminApi.WafLog) error
  20. PublishIpWafLogTask(ctx context.Context, req adminApi.WafLog)
  21. }
  22. func NewWafLogService(
  23. service *service.Service,
  24. wafLogRepository adminRep.WafLogRepository,
  25. globalLimitRepository waf.GlobalLimitRepository,
  26. mq *rabbitmq.RabbitMQ,
  27. ) WafLogService {
  28. return &wafLogService{
  29. Service: service,
  30. wafLogRepository: wafLogRepository,
  31. globalLimitRepository: globalLimitRepository,
  32. mq : mq,
  33. }
  34. }
  35. type wafLogService struct {
  36. *service.Service
  37. wafLogRepository adminRep.WafLogRepository
  38. globalLimitRepository waf.GlobalLimitRepository
  39. mq *rabbitmq.RabbitMQ
  40. }
  41. var ApiDescriptionMap = map[string]string{
  42. "/webForward/get": "获取web详情",
  43. "/webForward/getList" : "获取web列表",
  44. "/webForward/add" : "添加web",
  45. "/webForward/update" : "更新web",
  46. "/webForward/delete" : "删除web",
  47. "/tcpForward/add" : "添加tcp",
  48. "/tcpForward/update" : "更新tcp",
  49. "/tcpForward/delete" : "删除tcp",
  50. "/tcpForward/getList" : "获取tcp列表",
  51. "/tcpForward/get" : "获取tcp详情",
  52. "/udpForward/add" : "添加udp",
  53. "/udpForward/update" : "更新udp",
  54. "/udpForward/delete" : "删除udp",
  55. "/udpForward/getList" : "获取udp列表",
  56. "/udpForward/get" : "获取udp详情",
  57. "/globalLimit/add" : "添加实例",
  58. "/globalLimit/edit" : "编辑实例",
  59. "/globalLimit/delete" : "删除实例",
  60. "/allowAndDeny/get" : "获取黑白名单详情",
  61. "/allowAndDeny/getList" : "获取黑白名单列表",
  62. "/allowAndDeny/add" : "添加黑白名单",
  63. "/allowAndDeny/edit" : "编辑黑白名单",
  64. "/allowAndDeny/delete" : "删除黑白名单",
  65. "/cc/getList" : "获取CC列表",
  66. "/cc/editState" : "删除CC黑名单",
  67. "/ccIpList/getList" : "获取CC白名单列表",
  68. "/ccIpList/add" : "添加CC白名单",
  69. "/ccIpList/edit" : "编辑CC白名单",
  70. "/ccIpList/delete" : "删除CC白名单",
  71. }
  72. func (s *wafLogService) getFirstPathSegment(path string) (segment []string, ok bool) {
  73. // 1. 为了统一处理,先去掉路径最前面的 "/"
  74. // 这样 "/v1/admin" 会变成 "v1/admin",而 "v1/admin" 保持不变
  75. trimmedPath := strings.TrimPrefix(path, "/")
  76. // 如果去掉 "/" 后字符串为空(比如原路径是 "/" 或 ""),则无法提取
  77. if trimmedPath == "" {
  78. return nil, false
  79. }
  80. // 2. 使用 "/" 作为分隔符来切割字符串
  81. // "v1/admin/menus" 会被切割成一个字符串切片 (slice): ["v1", "admin", "menus"]
  82. parts := strings.Split(trimmedPath, "/")
  83. // 3. 只要切片不为空,第一个元素就是我们需要的值
  84. // len(parts) > 0 这个检查可以保证程序不会因为空切片而出错
  85. if len(parts) > 0 {
  86. return parts, true
  87. }
  88. return nil, false
  89. }
  90. func (s *wafLogService) GetWafLog(ctx context.Context, id int64) (*model.WafLog, error) {
  91. return s.wafLogRepository.GetWafLog(ctx, id)
  92. }
  93. func (s *wafLogService) GetWafLogList(ctx context.Context) ([]model.WafLog, error) {
  94. return s.wafLogRepository.GetWafLogList(ctx)
  95. }
  96. func (s *wafLogService) AddWafLog(ctx context.Context, req adminApi.WafLog) error {
  97. if req.Api != "" {
  98. api := strings.TrimPrefix(req.Api, "/v1")
  99. if _, ok := ApiDescriptionMap[api]; ok {
  100. req.ApiName = ApiDescriptionMap[api]
  101. }
  102. apiType, ok := s.getFirstPathSegment(req.Api)
  103. if ok {
  104. req.ApiType = apiType[len(apiType)-1]
  105. }
  106. }
  107. userInfo, err := s.globalLimitRepository.GetUserInfo(ctx, int64(req.Uid))
  108. if err != nil {
  109. return err
  110. }
  111. req.Name = userInfo.Username
  112. extraData, err := json.Marshal(req.ExtraData)
  113. if err != nil {
  114. return err
  115. }
  116. return s.wafLogRepository.AddWafLog(ctx, &model.WafLog{
  117. Uid: req.Uid,
  118. Name: req.Name,
  119. RequestIp: req.RequestIp,
  120. RuleId: req.RuleId,
  121. HostId: req.HostId,
  122. UserAgent: req.UserAgent,
  123. Api: req.Api,
  124. ApiType: req.ApiType,
  125. ApiName: req.ApiName,
  126. ExtraData: extraData,
  127. })
  128. }
  129. func (s *wafLogService) PublishIpWafLogTask(ctx context.Context, req adminApi.WafLog) {
  130. payload := &req
  131. // Serialize the message
  132. msgBody, err := json.Marshal(payload)
  133. if err != nil {
  134. s.Logger.Error("序列化 WafLog 任务消息失败", zap.Error(err), zap.Int("hostId", payload.HostId), zap.Int("uid", payload.Uid), zap.Any("req", req))
  135. return
  136. }
  137. // Get task configuration
  138. taskCfg, ok := s.mq.GetTaskConfig("waf_log")
  139. if !ok {
  140. s.Logger.Error("无法获取“waf_Log”任务配置")
  141. return
  142. }
  143. // Construct the routing key dynamically based on the action
  144. routingKey := fmt.Sprintf("wafLog.%s", "add")
  145. // Construct the amqp.Publishing message
  146. publishingMsg := amqp.Publishing{
  147. ContentType: "application/json",
  148. Body: msgBody,
  149. DeliveryMode: amqp.Persistent, // Persistent message
  150. }
  151. // Publish the message
  152. err = s.mq.PublishWithCh(taskCfg.Exchange, routingKey, publishingMsg)
  153. if err != nil {
  154. s.Logger.Error("发布 WafLog 任务消息失败", zap.Error(err), zap.Int("hostId", payload.HostId), zap.Int("uid", payload.Uid), zap.Any("req", req))
  155. } else {
  156. s.Logger.Info("已成功发布 WafLog 任务消息", zap.Int("hostId", payload.HostId), zap.Int("uid", payload.Uid), zap.Any("req", req))
  157. }
  158. }