|
@@ -16,6 +16,7 @@ type UdpForWardingService interface {
|
|
AddUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
|
|
AddUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
|
|
EditUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
|
|
EditUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
|
|
DeleteUdpForwarding(ctx context.Context, Ids []int) error
|
|
DeleteUdpForwarding(ctx context.Context, Ids []int) error
|
|
|
|
+ GetUdpForwardingWafUdpAllIps(ctx context.Context, req v1.GetForwardingRequest) ([]v1.UdpForwardingDataRequest, error)
|
|
}
|
|
}
|
|
|
|
|
|
func NewUdpForWardingService(
|
|
func NewUdpForWardingService(
|
|
@@ -298,3 +299,82 @@ func (s *udpForWardingService) DeleteUdpForwarding(ctx context.Context, Ids []in
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func (s *udpForWardingService) GetUdpForwardingWafUdpAllIps(ctx context.Context, req v1.GetForwardingRequest) ([]v1.UdpForwardingDataRequest, error) {
|
|
|
|
+ type CombinedResult struct {
|
|
|
|
+ Id int
|
|
|
|
+ Forwarding *model.UdpForWarding
|
|
|
|
+ BackendRule *model.UdpForwardingRule
|
|
|
|
+ Err error // 如果此ID的处理出错,则携带错误
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ g,gCtx := errgroup.WithContext(ctx)
|
|
|
|
+ ids, err := s.udpForWardingRepository.GetUdpForwardingWafUdpAllIds(gCtx, req.HostId)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, fmt.Errorf("GetUdpForwardingWafUdpAllIds failed: %w", err)
|
|
|
|
+ }
|
|
|
|
+ if len(ids) == 0 {
|
|
|
|
+ return nil, nil
|
|
|
|
+ }
|
|
|
|
+ resChan := make(chan CombinedResult, len(ids))
|
|
|
|
+
|
|
|
|
+ for _, idVal := range ids {
|
|
|
|
+ currentID := idVal
|
|
|
|
+ g.Go(func() error {
|
|
|
|
+ var wf *model.UdpForWarding
|
|
|
|
+ var bk *model.UdpForwardingRule
|
|
|
|
+ var localErr error
|
|
|
|
+ wf, localErr = s.udpForWardingRepository.GetUdpForWarding(gCtx, int64(currentID))
|
|
|
|
+ if localErr != nil {
|
|
|
|
+ resChan <- CombinedResult{Id: currentID, Err: localErr}
|
|
|
|
+ return localErr
|
|
|
|
+ }
|
|
|
|
+ bk, localErr = s.udpForWardingRepository.GetUdpForwardingIpsByID(gCtx, currentID)
|
|
|
|
+ if localErr != nil {
|
|
|
|
+ resChan <- CombinedResult{Id: currentID, Err: localErr}
|
|
|
|
+ return localErr
|
|
|
|
+ }
|
|
|
|
+ resChan <- CombinedResult{Id: currentID, Forwarding: wf, BackendRule: bk}
|
|
|
|
+ return nil
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ groupErr := g.Wait()
|
|
|
|
+ close(resChan)
|
|
|
|
+ if groupErr != nil {
|
|
|
|
+ return nil, groupErr
|
|
|
|
+ }
|
|
|
|
+ res := make([]v1.UdpForwardingDataRequest, 0, len(ids))
|
|
|
|
+ for r := range resChan {
|
|
|
|
+ if r.Err != nil {
|
|
|
|
+ return nil, fmt.Errorf("received error from goroutine for ID %d: %w", r.Id, r.Err)
|
|
|
|
+ }
|
|
|
|
+ if r.Forwarding == nil {
|
|
|
|
+ return nil, fmt.Errorf("received nil forwarding from goroutine for ID %d", r.Id)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dataReq := v1.UdpForwardingDataRequest{
|
|
|
|
+ Id: r.Forwarding.Id,
|
|
|
|
+ Port: r.Forwarding.Port,
|
|
|
|
+ CcPacketCount: r.Forwarding.CcPacketCount,
|
|
|
|
+ CcPacketDuration: r.Forwarding.CcPacketDuration,
|
|
|
|
+ CcPacketBlockCount: r.Forwarding.CcPacketBlockCount,
|
|
|
|
+ CcPacketBlockDuration: r.Forwarding.CcPacketBlockDuration,
|
|
|
|
+ CcCount: r.Forwarding.CcCount,
|
|
|
|
+ CcDuration: r.Forwarding.CcDuration,
|
|
|
|
+ CcBlockCount: r.Forwarding.CcBlockCount,
|
|
|
|
+ CcBlockDuration: r.Forwarding.CcBlockDuration,
|
|
|
|
+ SessionTimeout: r.Forwarding.SessionTimeout,
|
|
|
|
+ Comment: r.Forwarding.Comment,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if r.BackendRule != nil {
|
|
|
|
+ dataReq.BackendList = r.BackendRule.BackendList
|
|
|
|
+ dataReq.AllowIpList = r.BackendRule.AllowIpList
|
|
|
|
+ dataReq.DenyIpList = r.BackendRule.DenyIpList
|
|
|
|
+ dataReq.AccessRule = r.BackendRule.AccessRule
|
|
|
|
+ }
|
|
|
|
+ res = append(res, dataReq)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return res, nil
|
|
|
|
+}
|