Forráskód Böngészése

feat(gatewayipadmin): 添加网关组 IP 功能

- 新增 sendIp 方法,用于向指定服务器发送 IP 请求
- 在添加和删除 IP 时,调用 sendIp 方法以启动或停止网关组 IP
- 更新配置文件,添加 addServerIp 配置项
- 修改服务初始化,增加 config 和 request 依赖
fusu 2 hete
szülő
commit
0a635a0d33

+ 1 - 1
cmd/server/wire/wire_gen.go

@@ -104,7 +104,7 @@ func NewWire(viperViper *viper.Viper, logger *log.Logger) (*app.App, func(), err
 	gateWayGroupIpService := service.NewGateWayGroupIpService(serviceService, gateWayGroupIpRepository, gatewayGroupRepository, requestService)
 	gateWayGroupIpHandler := handler.NewGateWayGroupIpHandler(handlerHandler, gateWayGroupIpService)
 	gatewayIpAdminRepository := admin.NewGatewayIpAdminRepository(repositoryRepository)
-	gatewayIpAdminService := admin2.NewGatewayIpAdminService(serviceService, gatewayIpAdminRepository)
+	gatewayIpAdminService := admin2.NewGatewayIpAdminService(serviceService, gatewayIpAdminRepository, viperViper, requestService)
 	gatewayIpAdminHandler := admin3.NewGatewayIpAdminHandler(handlerHandler, gatewayIpAdminService)
 	allowAndDenyIpHandler := waf3.NewAllowAndDenyIpHandler(handlerHandler, allowAndDenyIpService)
 	ccRepository := repository.NewCcRepository(repositoryRepository)

+ 7 - 0
config/local.yml

@@ -138,3 +138,10 @@ rabbitmq:
       routing_key: "whitelist.domain.*"   # 消费者监听的绑定键,能接收所有 domain 相关的任务
       consumer_count: 3
       prefetch_count: 1
+
+addServerIp:
+  宁波:
+    - 192.168.14.111
+    - 192.168.14.112
+  香港:
+    - 192.168.14.113

+ 7 - 1
config/prod.yml

@@ -145,4 +145,10 @@ rabbitmq:
       queue: "domain_whitelist_queue"
       routing_key: "whitelist.domain.*"   # 消费者监听的绑定键,能接收所有 domain 相关的任务
       consumer_count: 3
-      prefetch_count: 1
+      prefetch_count: 1
+
+#    addServerIp: 启动网关组IP
+addServerIp:
+  宁波:
+    - 110.42.14.216
+    - 110.42.14.217

+ 86 - 0
internal/service/admin/gatewayipadmin.go

@@ -2,11 +2,15 @@ package admin
 
 import (
 	"context"
+	"encoding/json"
+	"fmt"
 	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
 	v1admin "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/admin"
 	"github.com/go-nunu/nunu-layout-advanced/internal/service"
+	"github.com/hashicorp/go-multierror"
+	"github.com/spf13/viper"
 )
 
 type GatewayIpAdminService interface {
@@ -20,18 +24,53 @@ type GatewayIpAdminService interface {
 func NewGatewayIpAdminService(
     service *service.Service,
     gatewayIpAdminRepository admin.GatewayIpAdminRepository,
+	config *viper.Viper,
+	request service.RequestService,
 ) GatewayIpAdminService {
 	return &gatewayIpAdminService{
 		Service:        service,
 		gatewayIpAdminRepository: gatewayIpAdminRepository,
+		request: request,
+		config: config,
 	}
 }
 
 type gatewayIpAdminService struct {
 	*service.Service
 	gatewayIpAdminRepository admin.GatewayIpAdminRepository
+	config                    *viper.Viper
+	request                   service.RequestService
 }
 
+func (s *gatewayIpAdminService) sendIp(ctx context.Context, ip string, action string,nodeArea string) error  {
+
+
+	serverIps := s.config.GetStringSlice("addServerIp." + nodeArea)
+
+	for _, serverIp := range serverIps {
+
+		apiUrl := "http://" + serverIp + ":3075/" + action
+
+		formData := map[string]interface{}{
+			"ip": ip,
+		}
+		resBody, err := s.request.Request(ctx, formData, apiUrl, "", "")
+		if err != nil {
+			return err
+		}
+		var res v1.GeneralResponse[any]
+		err = json.Unmarshal(resBody, &res)
+		if err != nil {
+			return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
+		}
+		if res.Code != 0 {
+			return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
+		}
+	}
+
+	return nil
+
+}
 func (s *gatewayIpAdminService) GetGatewayIpAdmin(ctx context.Context, id int64) (*model.Gatewayip, error) {
 	return s.gatewayIpAdminRepository.GetGatewayIpAdmin(ctx, id)
 }
@@ -41,6 +80,16 @@ func (s *gatewayIpAdminService) GetGatewayGroupIpList(ctx context.Context,req v1
 }
 
 func (s *gatewayIpAdminService) AddGatewayIp(ctx context.Context,req model.Gatewayip) error {
+
+
+	// 启动网关组IP
+	if req.NodeArea != "" {
+		err := s.sendIp(ctx,req.Ip,"addIp",req.NodeArea)
+		if err != nil {
+			return err
+		}
+	}
+
 	return s.gatewayIpAdminRepository.AddGatewayIp(ctx,req)
 }
 
@@ -49,9 +98,46 @@ func (s *gatewayIpAdminService) EditGatewayIp(ctx context.Context,req model.Gate
 }
 
 func (s *gatewayIpAdminService) DeleteGatewayIp(ctx context.Context,id int64) error {
+	oldData, err := s.GetGatewayIpAdmin(ctx, id)
+	if err != nil {
+		return err
+	}
+	// 启动网关组IP
+	if oldData.NodeArea != "" {
+		err := s.sendIp(ctx,oldData.Ip,"deleteIp",oldData.NodeArea)
+		if err != nil {
+			return err
+		}
+	}
 	return s.gatewayIpAdminRepository.DeleteGatewayIp(ctx,id)
 }
 
 func (s *gatewayIpAdminService) DeleteGatewayIps(ctx context.Context, ids []int64) error {
+
+	var allErrors *multierror.Error
+
+	for _, id := range ids {
+		oldData, err := s.GetGatewayIpAdmin(ctx, id)
+		if err != nil {
+			return err
+		}
+
+		// 启动网关组IP
+		if oldData.NodeArea != "" {
+			err := s.sendIp(ctx,oldData.Ip,"deleteIp",oldData.NodeArea)
+			if err != nil {
+				allErrors = multierror.Append(allErrors, err)
+			}
+		}
+
+
+
+	}
+
+	if allErrors != nil {
+		return allErrors.ErrorOrNil()
+	}
+
+
 	return s.gatewayIpAdminRepository.DeleteGatewayIps(ctx,ids)
 }