|
@@ -7,6 +7,7 @@ import (
|
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/model"
|
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/repository"
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
+ "net"
|
|
|
"sort"
|
|
|
"strconv"
|
|
|
"strings"
|
|
@@ -222,6 +223,20 @@ func (s *tcpforwardingService) AddTcpForwarding(ctx context.Context, req *v1.Tcp
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ // 异步任务:将IP添加到白名单
|
|
|
+ var ips []string
|
|
|
+ if req.TcpForwardingData.BackendList != nil {
|
|
|
+ for _, v := range req.TcpForwardingData.BackendList {
|
|
|
+ ip, _, err := net.SplitHostPort(v)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ ips = append(ips, ip)
|
|
|
+ }
|
|
|
+ ips = append(ips, req.TcpForwardingData.AllowIpList...)
|
|
|
+ go s.wafformatter.PublishIpWhitelistTask(ips, "add")
|
|
|
+ }
|
|
|
+
|
|
|
tcpModel := s.buildTcpForwardingModel(&req.TcpForwardingData, wafTcpId, require)
|
|
|
|
|
|
id, err := s.tcpforwardingRepository.AddTcpforwarding(ctx, tcpModel)
|
|
@@ -249,6 +264,43 @@ func (s *tcpforwardingService) EditTcpForwarding(ctx context.Context, req *v1.Tc
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+ // 异步任务:将IP添加到白名单
|
|
|
+ var oldIps []string
|
|
|
+ var newIps []string
|
|
|
+ ipData, err := s.tcpforwardingRepository.GetTcpForwardingIpsByID(ctx, req.TcpForwardingData.Id)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ for _, v := range ipData.BackendList {
|
|
|
+ ip, _, err := net.SplitHostPort(v)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ oldIps = append(oldIps, ip)
|
|
|
+ }
|
|
|
+ if ipData.AllowIpList != nil {
|
|
|
+ oldIps = append(oldIps, ipData.AllowIpList...)
|
|
|
+ }
|
|
|
+ if req.TcpForwardingData.BackendList != nil {
|
|
|
+ for _, v := range req.TcpForwardingData.BackendList {
|
|
|
+ ip, _, err := net.SplitHostPort(v)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ newIps = append(newIps, ip)
|
|
|
+ }
|
|
|
+ newIps = append(newIps, req.TcpForwardingData.AllowIpList...)
|
|
|
+ }
|
|
|
+ addedIps, removedIps := s.wafformatter.findIpDifferences(oldIps, newIps)
|
|
|
+
|
|
|
+ if len(addedIps) > 0 {
|
|
|
+ go s.wafformatter.PublishIpWhitelistTask(addedIps, "add")
|
|
|
+ }
|
|
|
+ if len(removedIps) > 0 {
|
|
|
+ go s.wafformatter.PublishIpWhitelistTask(removedIps, "del")
|
|
|
+ }
|
|
|
+
|
|
|
tcpModel := s.buildTcpForwardingModel(&req.TcpForwardingData, req.TcpForwardingData.WafTcpId, require)
|
|
|
tcpModel.Id = req.TcpForwardingData.Id
|
|
|
if err = s.tcpforwardingRepository.EditTcpforwarding(ctx, tcpModel); err != nil {
|
|
@@ -267,11 +319,35 @@ func (s *tcpforwardingService) DeleteTcpForwarding(ctx context.Context, req v1.D
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
_, err = s.crawler.DeleteRule(ctx, wafTcpId, "admin/delete/waf_tcp?page=1&__pageSize=1000000&__sort=waf_tcp_id&__sort_type=desc")
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ // 删除白名单
|
|
|
+ var ips []string
|
|
|
+ ipData, err := s.tcpforwardingRepository.GetTcpForwardingIpsByID(ctx, Id)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if ipData.BackendList != nil {
|
|
|
+ for _, v := range ipData.BackendList {
|
|
|
+ ip, _, err := net.SplitHostPort(v)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ ips = append(ips, ip)
|
|
|
+ }
|
|
|
+ ips = append(ips, ipData.AllowIpList...)
|
|
|
+ }
|
|
|
+ if len(ips) > 0 {
|
|
|
+ go s.wafformatter.PublishIpWhitelistTask(ips, "del")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
if err = s.tcpforwardingRepository.DeleteTcpforwarding(ctx, int64(Id)); err != nil {
|
|
|
return err
|
|
|
}
|