|
@@ -6,7 +6,9 @@ import (
|
|
|
"fmt"
|
|
|
v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
|
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/model"
|
|
|
+ "github.com/go-nunu/nunu-layout-advanced/internal/repository"
|
|
|
"maps"
|
|
|
+ "slices"
|
|
|
"sort"
|
|
|
"strconv"
|
|
|
"strings"
|
|
@@ -25,17 +27,23 @@ type FormatterService interface {
|
|
|
func NewFormatterService(
|
|
|
service *Service,
|
|
|
gameShieldPublicIpService GameShieldPublicIpService,
|
|
|
+ gameShieldBackendRepository repository.GameShieldBackendRepository,
|
|
|
+ hostService HostService,
|
|
|
|
|
|
) FormatterService {
|
|
|
return &formatterService{
|
|
|
- Service: service,
|
|
|
- gameShieldPublicIpService: gameShieldPublicIpService,
|
|
|
+ Service: service,
|
|
|
+ gameShieldPublicIpService: gameShieldPublicIpService,
|
|
|
+ gameShieldBackendRepository: gameShieldBackendRepository,
|
|
|
+ hostService: hostService,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
type formatterService struct {
|
|
|
*Service
|
|
|
- gameShieldPublicIpService GameShieldPublicIpService
|
|
|
+ gameShieldPublicIpService GameShieldPublicIpService
|
|
|
+ gameShieldBackendRepository repository.GameShieldBackendRepository
|
|
|
+ hostService HostService
|
|
|
}
|
|
|
|
|
|
func (service *formatterService) FormatBackendData(ctx context.Context, req *v1.GameShieldBackendArrayRequest, oldFormat map[string]v1.SendGameShieldBackend, keyCounter int) (string, error) {
|
|
@@ -117,84 +125,126 @@ func (service *formatterService) OldFormat(ctx context.Context, req *[]model.Gam
|
|
|
}
|
|
|
|
|
|
func (service *formatterService) TidyFormatBackendData(ctx context.Context, req *v1.GameShieldBackendArrayRequest, keyCounter int) (map[string]v1.SendGameShieldBackend, error) {
|
|
|
+ // 初始化输出映射
|
|
|
output := make(map[string]v1.SendGameShieldBackend)
|
|
|
+
|
|
|
+ // 获取所需基础数据
|
|
|
userIp, err := service.gameShieldPublicIpService.GetUserIp(ctx, req.Uid)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+
|
|
|
+ oldCount, err := service.gameShieldBackendRepository.GetGameShieldBackendConfigCountByHostId(ctx, req.HostId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ oldMachineIp, err := service.gameShieldBackendRepository.GetGameShieldBackendSourceMachineIpByHostId(ctx, req.HostId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ configCount, err := service.hostService.GetGameShieldConfig(ctx, req.HostId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历请求中的所有项目
|
|
|
for _, item := range req.Items {
|
|
|
- // 提取必要字段
|
|
|
- sourceIP := item.SourceMachineIP // 假设结构体中有这个字段
|
|
|
+ // 检查并验证源机器IP
|
|
|
+ sourceIP := item.SourceMachineIP
|
|
|
if sourceIP == "" {
|
|
|
- return nil, fmt.Errorf("没有有效源IP的配置") // 跳过没有有效源IP的配置
|
|
|
+ return nil, fmt.Errorf("没有有效源IP的配置")
|
|
|
+ }
|
|
|
+ // 检查源机器IP是否为新增
|
|
|
+ if !slices.Contains(oldMachineIp, sourceIP) {
|
|
|
+ oldCount.SourceMachinesCount++
|
|
|
+ if oldCount.SourceMachinesCount > configCount.SourceMachinesCount {
|
|
|
+ return nil, fmt.Errorf("超出最大源机数量")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- protocol := item.Protocol // 假设结构体中有这个字段
|
|
|
+ // 验证协议
|
|
|
+ protocol := item.Protocol
|
|
|
if protocol == "" {
|
|
|
- return nil, fmt.Errorf("没有有效协议的配置") // 跳过没有有效协议的配置
|
|
|
+ return nil, fmt.Errorf("没有有效协议的配置")
|
|
|
}
|
|
|
- // 获取端口数组
|
|
|
+
|
|
|
+ // 获取并验证端口配置
|
|
|
conPorts := service.FormatPort(ctx, item.ConnectPort)
|
|
|
sdkPorts := service.FormatPort(ctx, item.SdkPort)
|
|
|
|
|
|
+ // 验证端口数量匹配
|
|
|
+ if len(sdkPorts) > 0 && len(conPorts) != len(sdkPorts) {
|
|
|
+ return nil, fmt.Errorf("端口数量不匹配")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证规则条目数量
|
|
|
+ oldCount.RuleEntriesCount += int64(len(conPorts))
|
|
|
+ if oldCount.RuleEntriesCount > configCount.RuleEntriesCount {
|
|
|
+ return nil, fmt.Errorf("超出最大规则数量")
|
|
|
+ }
|
|
|
+
|
|
|
// 处理每一对端口
|
|
|
for i := 0; i < len(conPorts); i++ {
|
|
|
keyCounter++
|
|
|
key := fmt.Sprintf("key%d", keyCounter)
|
|
|
|
|
|
- // 使用数组中的具体端口
|
|
|
- addr := fmt.Sprintf("%s:%d", sourceIP, conPorts[i])
|
|
|
-
|
|
|
+ // 构建基本的后端配置项
|
|
|
itemMap := v1.SendGameShieldBackend{
|
|
|
- Addr: []string{addr},
|
|
|
+ Addr: []string{fmt.Sprintf("%s:%d", sourceIP, conPorts[i])},
|
|
|
Protocol: protocol,
|
|
|
+ Type: item.Type,
|
|
|
}
|
|
|
|
|
|
- //// 设置主机名(如果存在)
|
|
|
- //if item.Host != "" {
|
|
|
- // itemMap["host"] = item.Host
|
|
|
- //}
|
|
|
-
|
|
|
- // 根据协议设置不同属性
|
|
|
+ // 根据协议类型设置属性
|
|
|
if protocol != "udp" {
|
|
|
+ // 非UDP协议的配置
|
|
|
if item.Checked == "agent" {
|
|
|
itemMap.AgentAddr = fmt.Sprintf("%s:%s", sourceIP, "23350")
|
|
|
}
|
|
|
itemMap.ProxyAddr = userIp + ":32353"
|
|
|
-
|
|
|
} else {
|
|
|
+ // UDP协议的配置
|
|
|
itemMap.ProxyAddr = ""
|
|
|
itemMap.UdpSessionTimeout = "300s"
|
|
|
}
|
|
|
- itemMap.Type = item.Type
|
|
|
- if item.Type != "pc" {
|
|
|
- itemMap.SdkIp = ""
|
|
|
- } else {
|
|
|
+
|
|
|
+ // 根据设备类型设置SDK IP
|
|
|
+ if item.Type == "pc" {
|
|
|
itemMap.SdkIp = item.SdkIp
|
|
|
+ } else {
|
|
|
+ itemMap.SdkIp = ""
|
|
|
}
|
|
|
|
|
|
- if item.MaxBandwidth == "1" {
|
|
|
+ // 处理最大带宽设置
|
|
|
+ if item.MaxBandwidth == 1 {
|
|
|
+ oldCount.MaxBandwidthCount++
|
|
|
+ if oldCount.MaxBandwidthCount > configCount.MaxBandwidthCount {
|
|
|
+ return nil, fmt.Errorf("超出最大带宽数量")
|
|
|
+ }
|
|
|
itemMap.MaxBandwidth = "50m"
|
|
|
} else {
|
|
|
itemMap.MaxBandwidth = ""
|
|
|
}
|
|
|
- // 设置SDK端口 - 使用数组中的具体端口
|
|
|
- if len(sdkPorts) != 0 {
|
|
|
- if sdkPorts[i] <= 1024 {
|
|
|
- if item.Type == "mobile" {
|
|
|
- return nil, fmt.Errorf("移动端不支持SSH端口")
|
|
|
- }
|
|
|
+
|
|
|
+ // 设置SDK端口(如果存在)
|
|
|
+ if len(sdkPorts) > 0 {
|
|
|
+ sdkPort := sdkPorts[i]
|
|
|
+ // 检查移动端的SSH端口限制
|
|
|
+ if sdkPort <= 1024 && item.Type == "mobile" {
|
|
|
+ return nil, fmt.Errorf("移动端不支持SSH端口")
|
|
|
}
|
|
|
- itemMap.SdkPort = sdkPorts[i]
|
|
|
+ itemMap.SdkPort = sdkPort
|
|
|
}
|
|
|
|
|
|
+ // 将配置项添加到输出映射
|
|
|
output[key] = itemMap
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return output, nil
|
|
|
}
|
|
|
-
|
|
|
func (service *formatterService) Sort(ctx context.Context, mapData map[string]v1.SendGameShieldBackend) (map[string]v1.SendGameShieldBackend, error) {
|
|
|
var keys []int
|
|
|
for key := range mapData {
|