|
@@ -6,7 +6,6 @@ import (
|
|
v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
|
|
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/model"
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/repository"
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/repository"
|
|
- "github.com/spf13/cast"
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/spf13/viper"
|
|
"golang.org/x/sync/errgroup"
|
|
"golang.org/x/sync/errgroup"
|
|
"strconv"
|
|
"strconv"
|
|
@@ -36,6 +35,7 @@ func NewGlobalLimitService(
|
|
gateWayGroup GatewayGroupService,
|
|
gateWayGroup GatewayGroupService,
|
|
hostRep repository.HostRepository,
|
|
hostRep repository.HostRepository,
|
|
gateWayGroupRep repository.GatewayGroupRepository,
|
|
gateWayGroupRep repository.GatewayGroupRepository,
|
|
|
|
+ cdnService CdnService,
|
|
) GlobalLimitService {
|
|
) GlobalLimitService {
|
|
return &globalLimitService{
|
|
return &globalLimitService{
|
|
Service: service,
|
|
Service: service,
|
|
@@ -52,6 +52,7 @@ func NewGlobalLimitService(
|
|
gateWayGroup: gateWayGroup,
|
|
gateWayGroup: gateWayGroup,
|
|
hostRep: hostRep,
|
|
hostRep: hostRep,
|
|
gateWayGroupRep: gateWayGroupRep,
|
|
gateWayGroupRep: gateWayGroupRep,
|
|
|
|
+ cdnService: cdnService,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -70,6 +71,41 @@ type globalLimitService struct {
|
|
gateWayGroup GatewayGroupService
|
|
gateWayGroup GatewayGroupService
|
|
hostRep repository.HostRepository
|
|
hostRep repository.HostRepository
|
|
gateWayGroupRep repository.GatewayGroupRepository
|
|
gateWayGroupRep repository.GatewayGroupRepository
|
|
|
|
+ cdnService CdnService
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *globalLimitService) GetCdnUserId(ctx context.Context, uid int64) (int64, error) {
|
|
|
|
+ data, err := s.globalLimitRepository.GetGlobalLimitFirst(ctx, uid)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return 0, err
|
|
|
|
+ }
|
|
|
|
+ if data != nil && data.CdnUid != 0 {
|
|
|
|
+ return int64(data.CdnUid), nil
|
|
|
|
+ }
|
|
|
|
+ userInfo,err := s.globalLimitRepository.GetUserInfo(ctx, uid)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return 0, err
|
|
|
|
+ }
|
|
|
|
+ userId, err := s.cdnService.AddUser(ctx, v1.User{
|
|
|
|
+ Username: userInfo.Username,
|
|
|
|
+ Email: userInfo.Email,
|
|
|
|
+ Fullname: userInfo.Username,
|
|
|
|
+ Mobile: userInfo.PhoneNumber,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ return 0, err
|
|
|
|
+ }
|
|
|
|
+ return userId, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *globalLimitService) AddGroupId(ctx context.Context,groupName string) (int64, error) {
|
|
|
|
+ groupId, err := s.cdnService.CreateGroup(ctx, v1.Group{
|
|
|
|
+ Name: groupName,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ return 0, err
|
|
|
|
+ }
|
|
|
|
+ return groupId, nil
|
|
}
|
|
}
|
|
|
|
|
|
func (s *globalLimitService) GlobalLimitRequire(ctx context.Context, req v1.GlobalLimitRequest) (res v1.GlobalLimitRequireResponse, err error) {
|
|
func (s *globalLimitService) GlobalLimitRequire(ctx context.Context, req v1.GlobalLimitRequest) (res v1.GlobalLimitRequireResponse, err error) {
|
|
@@ -116,124 +152,66 @@ func (s *globalLimitService) AddGlobalLimit(ctx context.Context, req v1.GlobalLi
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- gatewayGroupId, err := s.gateWayGroupRep.GetGatewayGroupWhereHostIdNull(ctx, require.Operator, require.IpCount)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- formData := map[string]interface{}{
|
|
|
|
- "tag": require.GlobalLimitName,
|
|
|
|
- "bps": require.Bps,
|
|
|
|
- "max_bytes_month": require.MaxBytesMonth,
|
|
|
|
- "expired_at": require.ExpiredAt,
|
|
|
|
- }
|
|
|
|
- respBody, err := s.required.SendForm(ctx, "admin/info/waf_common_limit/new", "admin/new/waf_common_limit", formData)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- ruleIdBase, err := s.parser.GetRuleIdByColumnName(ctx, respBody, require.GlobalLimitName)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- if ruleIdBase == "" {
|
|
|
|
- res, err := s.parser.ParseAlert(string(respBody))
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- return fmt.Errorf(res)
|
|
|
|
- }
|
|
|
|
- ruleId, err := cast.ToIntE(ruleIdBase)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- var tcpLimitRuleId, udpLimitRuleId, webLimitRuleId int
|
|
|
|
|
|
|
|
g, gCtx := errgroup.WithContext(ctx)
|
|
g, gCtx := errgroup.WithContext(ctx)
|
|
-
|
|
|
|
- // 启动tcpLimit调用 - 使用独立的请求参数副本
|
|
|
|
|
|
+ var gatewayGroupId int
|
|
|
|
+ var userId int64
|
|
|
|
+ var groupId int64
|
|
g.Go(func() error {
|
|
g.Go(func() error {
|
|
- tcpLimitReq := &v1.GeneralLimitRequireRequest{
|
|
|
|
- Tag: require.GlobalLimitName,
|
|
|
|
- HostId: req.HostId,
|
|
|
|
- RuleId: ruleId,
|
|
|
|
- Uid: req.Uid,
|
|
|
|
- }
|
|
|
|
- result, e := s.tcpLimit.AddTcpLimit(gCtx, tcpLimitReq)
|
|
|
|
|
|
+ gatewayGroupId, e := s.gateWayGroupRep.GetGatewayGroupWhereHostIdNull(gCtx, require.Operator, require.IpCount)
|
|
if e != nil {
|
|
if e != nil {
|
|
- return fmt.Errorf("tcpLimit调用失败: %w", e)
|
|
|
|
|
|
+ return e
|
|
}
|
|
}
|
|
- if result != 0 {
|
|
|
|
- tcpLimitRuleId = result
|
|
|
|
- return nil
|
|
|
|
|
|
+ if gatewayGroupId == 0 {
|
|
|
|
+ return fmt.Errorf("获取网关组失败")
|
|
}
|
|
}
|
|
- return fmt.Errorf("tcpLimit调用失败,Id为 %d", result)
|
|
|
|
|
|
+ return nil
|
|
})
|
|
})
|
|
|
|
|
|
- // 启动udpLimit调用 - 使用独立的请求参数副本
|
|
|
|
|
|
+
|
|
|
|
+
|
|
g.Go(func() error {
|
|
g.Go(func() error {
|
|
- udpLimitReq := &v1.GeneralLimitRequireRequest{
|
|
|
|
- Tag: require.GlobalLimitName,
|
|
|
|
- HostId: req.HostId,
|
|
|
|
- RuleId: ruleId,
|
|
|
|
- Uid: req.Uid,
|
|
|
|
- }
|
|
|
|
- result, e := s.udpLimit.AddUdpLimit(gCtx, udpLimitReq)
|
|
|
|
|
|
+ res, e := s.GetCdnUserId(gCtx, int64(req.Uid))
|
|
if e != nil {
|
|
if e != nil {
|
|
- return fmt.Errorf("udpLimit调用失败: %w", e)
|
|
|
|
|
|
+ return e
|
|
}
|
|
}
|
|
- if result != 0 {
|
|
|
|
- udpLimitRuleId = result
|
|
|
|
- return nil
|
|
|
|
|
|
+ if res == 0 {
|
|
|
|
+ return fmt.Errorf("获取cdn用户失败")
|
|
}
|
|
}
|
|
- return fmt.Errorf("udpLimit调用失败,Id为 %d", result)
|
|
|
|
|
|
+ userId = res
|
|
|
|
+ return nil
|
|
})
|
|
})
|
|
|
|
|
|
-
|
|
|
|
- // 启动webLimit调用 - 使用独立的请求参数副本
|
|
|
|
g.Go(func() error {
|
|
g.Go(func() error {
|
|
- webLimitReq := &v1.GeneralLimitRequireRequest{
|
|
|
|
- Tag: require.GlobalLimitName,
|
|
|
|
- HostId: req.HostId,
|
|
|
|
- RuleId: ruleId,
|
|
|
|
- Uid: req.Uid,
|
|
|
|
- }
|
|
|
|
- result, e := s.webLimit.AddWebLimit(gCtx, webLimitReq)
|
|
|
|
|
|
+ res, e := s.AddGroupId(gCtx, require.GlobalLimitName)
|
|
if e != nil {
|
|
if e != nil {
|
|
- return fmt.Errorf("webLimit调用失败: %w", e)
|
|
|
|
|
|
+ return e
|
|
}
|
|
}
|
|
- if result != 0 {
|
|
|
|
- webLimitRuleId = result
|
|
|
|
- return nil
|
|
|
|
|
|
+ if res == 0 {
|
|
|
|
+ return fmt.Errorf("创建规则分组失败")
|
|
}
|
|
}
|
|
- return fmt.Errorf("webLimit调用失败,Id为 %d", result)
|
|
|
|
|
|
+ return nil
|
|
})
|
|
})
|
|
|
|
|
|
- if err := g.Wait(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- t, err := time.Parse("2006-01-02 15:04:05", require.ExpiredAt)
|
|
|
|
- if err != nil {
|
|
|
|
|
|
+ if err = g.Wait(); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- expiredAt := t.Unix()
|
|
|
|
|
|
+
|
|
|
|
+ ruleId, err := s.cdnService.BindPlan(ctx, v1.Plan{
|
|
|
|
+
|
|
|
|
+ })
|
|
|
|
+
|
|
err = s.globalLimitRepository.AddGlobalLimit(ctx, &model.GlobalLimit{
|
|
err = s.globalLimitRepository.AddGlobalLimit(ctx, &model.GlobalLimit{
|
|
- HostId: req.HostId,
|
|
|
|
- RuleId: cast.ToInt(ruleId),
|
|
|
|
- Uid: req.Uid,
|
|
|
|
- GlobalLimitName: require.GlobalLimitName,
|
|
|
|
- Comment: req.Comment,
|
|
|
|
- TcpLimitRuleId: tcpLimitRuleId,
|
|
|
|
- UdpLimitRuleId: udpLimitRuleId,
|
|
|
|
- WebLimitRuleId: webLimitRuleId,
|
|
|
|
- GatewayGroupId: gatewayGroupId,
|
|
|
|
- ExpiredAt: expiredAt,
|
|
|
|
|
|
+ HostId: req.HostId,
|
|
|
|
+ Uid: req.Uid,
|
|
|
|
+ RuleId: ,
|
|
|
|
+ CdnUid: int(userId),
|
|
|
|
+ Comment: req.Comment,
|
|
|
|
+ ExpiredAt: require.ExpiredAt,
|
|
})
|
|
})
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- err = s.gateWayGroupRep.EditGatewayGroup(ctx, &model.GatewayGroup{
|
|
|
|
- RuleId: gatewayGroupId,
|
|
|
|
- HostId: req.HostId,
|
|
|
|
- })
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|