|
@@ -3,11 +3,15 @@ package admin
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
+ "fmt"
|
|
|
|
+ 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/model"
|
|
adminRep "github.com/go-nunu/nunu-layout-advanced/internal/repository/admin"
|
|
adminRep "github.com/go-nunu/nunu-layout-advanced/internal/repository/admin"
|
|
- adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
|
|
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/repository/api/waf"
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/repository/api/waf"
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/service"
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/service"
|
|
|
|
+ "github.com/go-nunu/nunu-layout-advanced/pkg/rabbitmq"
|
|
|
|
+ amqp "github.com/rabbitmq/amqp091-go"
|
|
|
|
+ "go.uber.org/zap"
|
|
"strings"
|
|
"strings"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -15,16 +19,19 @@ type WafLogService interface {
|
|
GetWafLog(ctx context.Context, id int64) (*model.WafLog, error)
|
|
GetWafLog(ctx context.Context, id int64) (*model.WafLog, error)
|
|
GetWafLogList(ctx context.Context) ([]model.WafLog, error)
|
|
GetWafLogList(ctx context.Context) ([]model.WafLog, error)
|
|
AddWafLog(ctx context.Context, req adminApi.WafLog) error
|
|
AddWafLog(ctx context.Context, req adminApi.WafLog) error
|
|
|
|
+ PublishIpWafLogTask(ctx context.Context, req adminApi.WafLog)
|
|
}
|
|
}
|
|
func NewWafLogService(
|
|
func NewWafLogService(
|
|
service *service.Service,
|
|
service *service.Service,
|
|
wafLogRepository adminRep.WafLogRepository,
|
|
wafLogRepository adminRep.WafLogRepository,
|
|
globalLimitRepository waf.GlobalLimitRepository,
|
|
globalLimitRepository waf.GlobalLimitRepository,
|
|
|
|
+ mq *rabbitmq.RabbitMQ,
|
|
) WafLogService {
|
|
) WafLogService {
|
|
return &wafLogService{
|
|
return &wafLogService{
|
|
Service: service,
|
|
Service: service,
|
|
wafLogRepository: wafLogRepository,
|
|
wafLogRepository: wafLogRepository,
|
|
globalLimitRepository: globalLimitRepository,
|
|
globalLimitRepository: globalLimitRepository,
|
|
|
|
+ mq : mq,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -32,6 +39,7 @@ type wafLogService struct {
|
|
*service.Service
|
|
*service.Service
|
|
wafLogRepository adminRep.WafLogRepository
|
|
wafLogRepository adminRep.WafLogRepository
|
|
globalLimitRepository waf.GlobalLimitRepository
|
|
globalLimitRepository waf.GlobalLimitRepository
|
|
|
|
+ mq *rabbitmq.RabbitMQ
|
|
}
|
|
}
|
|
|
|
|
|
var ApiDescriptionMap = map[string]string{
|
|
var ApiDescriptionMap = map[string]string{
|
|
@@ -141,3 +149,40 @@ func (s *wafLogService) AddWafLog(ctx context.Context, req adminApi.WafLog) erro
|
|
|
|
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func (s *wafLogService) PublishIpWafLogTask(ctx context.Context, req adminApi.WafLog) {
|
|
|
|
+
|
|
|
|
+ payload := &req
|
|
|
|
+
|
|
|
|
+ // Serialize the message
|
|
|
|
+ msgBody, err := json.Marshal(payload)
|
|
|
|
+ if err != nil {
|
|
|
|
+ s.Logger.Error("序列化 WafLog 任务消息失败", zap.Error(err), zap.Int("hostId", payload.HostId), zap.Int("uid", payload.Uid), zap.Any("req", req))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Get task configuration
|
|
|
|
+ taskCfg, ok := s.mq.GetTaskConfig("waf_log")
|
|
|
|
+ if !ok {
|
|
|
|
+ s.Logger.Error("无法获取“waf_Log”任务配置")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Construct the routing key dynamically based on the action
|
|
|
|
+ routingKey := fmt.Sprintf("wafLog.%s", "add")
|
|
|
|
+
|
|
|
|
+ // Construct the amqp.Publishing message
|
|
|
|
+ publishingMsg := amqp.Publishing{
|
|
|
|
+ ContentType: "application/json",
|
|
|
|
+ Body: msgBody,
|
|
|
|
+ DeliveryMode: amqp.Persistent, // Persistent message
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Publish the message
|
|
|
|
+ err = s.mq.PublishWithCh(taskCfg.Exchange, routingKey, publishingMsg)
|
|
|
|
+ if err != nil {
|
|
|
|
+ s.Logger.Error("发布 WafLog 任务消息失败", zap.Error(err), zap.Int("hostId", payload.HostId), zap.Int("uid", payload.Uid), zap.Any("req", req))
|
|
|
|
+ } else {
|
|
|
|
+ s.Logger.Info("已成功发布 WafLog 任务消息", zap.Int("hostId", payload.HostId), zap.Int("uid", payload.Uid), zap.Any("req", req))
|
|
|
|
+ }
|
|
|
|
+}
|