tcpforwarding.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. "golang.org/x/sync/errgroup"
  9. "net"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. )
  14. type TcpforwardingService interface {
  15. GetTcpforwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.TcpForwardingDataRequest, error)
  16. AddTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest) error
  17. EditTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest) error
  18. DeleteTcpForwarding(ctx context.Context, req v1.DeleteTcpForwardingRequest) error
  19. GetTcpForwardingAllIpsByHostId(ctx context.Context, req v1.GetForwardingRequest) ([]v1.TcpForwardingDataRequest, error)
  20. }
  21. func NewTcpforwardingService(
  22. service *Service,
  23. tcpforwardingRepository repository.TcpforwardingRepository,
  24. parser ParserService,
  25. required RequiredService,
  26. crawler CrawlerService,
  27. globalRep repository.GlobalLimitRepository,
  28. hostRep repository.HostRepository,
  29. wafformatter WafFormatterService,
  30. ) TcpforwardingService {
  31. return &tcpforwardingService{
  32. Service: service,
  33. tcpforwardingRepository: tcpforwardingRepository,
  34. parser: parser,
  35. required: required,
  36. crawler: crawler,
  37. globalRep: globalRep,
  38. hostRep: hostRep,
  39. wafformatter: wafformatter,
  40. }
  41. }
  42. type tcpforwardingService struct {
  43. *Service
  44. tcpforwardingRepository repository.TcpforwardingRepository
  45. parser ParserService
  46. required RequiredService
  47. crawler CrawlerService
  48. globalRep repository.GlobalLimitRepository
  49. hostRep repository.HostRepository
  50. wafformatter WafFormatterService
  51. }
  52. func (s *tcpforwardingService) GetTcpforwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.TcpForwardingDataRequest, error) {
  53. var tcpForwarding model.Tcpforwarding
  54. var backend model.TcpForwardingRule
  55. var err error
  56. g, gCtx := errgroup.WithContext(ctx)
  57. g.Go(func() error {
  58. res, e := s.tcpforwardingRepository.GetTcpforwarding(gCtx, int64(req.Id))
  59. if e != nil {
  60. return fmt.Errorf("GetTcpforwarding failed: %w", e)
  61. }
  62. if res != nil {
  63. tcpForwarding = *res
  64. }
  65. return nil
  66. })
  67. g.Go(func() error {
  68. res, e := s.tcpforwardingRepository.GetTcpForwardingIpsByID(gCtx, req.Id)
  69. if e != nil {
  70. return fmt.Errorf("GetTcpforwardingIps failed: %w", e)
  71. }
  72. if res != nil {
  73. backend = *res
  74. }
  75. return nil
  76. })
  77. if err = g.Wait(); err != nil {
  78. return v1.TcpForwardingDataRequest{}, err
  79. }
  80. return v1.TcpForwardingDataRequest{
  81. Id: tcpForwarding.Id,
  82. WafTcpId: tcpForwarding.WafTcpId,
  83. Tag: tcpForwarding.Tag,
  84. Port: tcpForwarding.Port,
  85. Comment: tcpForwarding.Comment,
  86. WafGatewayGroupId: tcpForwarding.WafGatewayGroupId,
  87. WafTcpLimitRuleId: tcpForwarding.TcpLimitRuleId,
  88. CcCount: tcpForwarding.CcCount,
  89. CcDuration: tcpForwarding.CcDuration,
  90. CcBlockCount: tcpForwarding.CcBlockCount,
  91. CcBlockDuration: tcpForwarding.CcBlockDuration,
  92. BackendProtocol: tcpForwarding.BackendProtocol,
  93. BackendTimeout: tcpForwarding.BackendTimeout,
  94. BackendList: backend.BackendList,
  95. AllowIpList: backend.AllowIpList,
  96. DenyIpList: backend.DenyIpList,
  97. AccessRule: backend.AccessRule,
  98. }, nil
  99. }
  100. func (s *tcpforwardingService) require(ctx context.Context,req v1.GlobalRequire) (v1.GlobalRequire, error) {
  101. res, err := s.wafformatter.require(ctx, req, "tcp")
  102. if err != nil {
  103. return v1.GlobalRequire{}, err
  104. }
  105. return res, nil
  106. }
  107. func (s *tcpforwardingService) buildWafFormData(req *v1.TcpForwardingDataSend, require v1.GlobalRequire) map[string]interface{} {
  108. return map[string]interface{}{
  109. "waf_tcp_id": req.WafTcpId,
  110. "tag": require.Tag,
  111. "port": req.Port,
  112. "waf_gateway_group_id": require.WafGatewayGroupId,
  113. "waf_tcp_limit_id": require.LimitRuleId,
  114. "cc_count": req.CcCount,
  115. "cc_duration": req.CcDuration,
  116. "cc_block_count": req.CcBlockCount,
  117. "cc_block_duration": req.CcBlockDuration,
  118. "backend_protocol": req.BackendProtocol,
  119. "backend_timeout": req.BackendTimeout,
  120. "comment": req.Comment,
  121. "backend_list": req.BackendList,
  122. "allow_ip_list": req.AllowIpList,
  123. "deny_ip_list": req.DenyIpList,
  124. "access_rule": req.AccessRule,
  125. }
  126. }
  127. func (s *tcpforwardingService) buildTcpForwardingModel(req *v1.TcpForwardingDataRequest, ruleId int, require v1.GlobalRequire) *model.Tcpforwarding {
  128. return &model.Tcpforwarding{
  129. HostId: require.HostId,
  130. WafTcpId: ruleId,
  131. Port: req.Port,
  132. Tag: require.Tag,
  133. Comment: req.Comment,
  134. TcpLimitRuleId: require.LimitRuleId,
  135. WafGatewayGroupId: require.WafGatewayGroupId,
  136. CcCount: req.CcCount,
  137. CcDuration: req.CcDuration,
  138. CcBlockCount: req.CcBlockCount,
  139. CcBlockDuration: req.CcBlockDuration,
  140. BackendProtocol: req.BackendProtocol,
  141. BackendTimeout: req.BackendTimeout,
  142. }
  143. }
  144. func (s *tcpforwardingService) buildTcpRuleModel(reqData *v1.TcpForwardingDataRequest, require v1.GlobalRequire, localDbId int) *model.TcpForwardingRule {
  145. return &model.TcpForwardingRule{
  146. Uid: require.Uid,
  147. HostId: require.HostId,
  148. TcpId: localDbId, // 关联到本地数据库的主记录 ID
  149. BackendList: reqData.BackendList,
  150. AllowIpList: reqData.AllowIpList,
  151. DenyIpList: reqData.DenyIpList,
  152. AccessRule: reqData.AccessRule,
  153. }
  154. }
  155. func (s *tcpforwardingService) prepareWafData(ctx context.Context, req *v1.TcpForwardingRequest) (v1.GlobalRequire, map[string]interface{}, error) {
  156. // 1. 获取必要的全局信息
  157. require, err := s.require(ctx, v1.GlobalRequire{
  158. HostId: req.HostId,
  159. Uid: req.Uid,
  160. Comment: req.TcpForwardingData.Comment,
  161. })
  162. if err != nil {
  163. return v1.GlobalRequire{}, nil, err
  164. }
  165. if require.WafGatewayGroupId == 0 || require.LimitRuleId == 0 {
  166. return v1.GlobalRequire{}, nil, fmt.Errorf("请先配置实例")
  167. }
  168. // 2. 将字符串切片拼接成字符串,用于 WAF API
  169. backendListStr := strings.Join(req.TcpForwardingData.BackendList, "\n")
  170. allowIpListStr := strings.Join(req.TcpForwardingData.AllowIpList, "\n")
  171. denyIpListStr := strings.Join(req.TcpForwardingData.DenyIpList, "\n")
  172. // 3. 创建用于构建 WAF 表单的数据结构
  173. formDataBase := v1.TcpForwardingDataSend{
  174. Tag: require.Tag,
  175. WafTcpId: req.TcpForwardingData.WafTcpId,
  176. WafGatewayGroupId: require.WafGatewayGroupId,
  177. WafTcpLimitRuleId: require.LimitRuleId,
  178. Port: req.TcpForwardingData.Port,
  179. CcCount: req.TcpForwardingData.CcCount,
  180. CcDuration: req.TcpForwardingData.CcDuration,
  181. CcBlockCount: req.TcpForwardingData.CcBlockCount,
  182. CcBlockDuration: req.TcpForwardingData.CcBlockDuration,
  183. BackendProtocol: req.TcpForwardingData.BackendProtocol,
  184. BackendTimeout: req.TcpForwardingData.BackendTimeout,
  185. BackendList: backendListStr,
  186. AllowIpList: allowIpListStr,
  187. DenyIpList: denyIpListStr,
  188. AccessRule: req.TcpForwardingData.AccessRule,
  189. Comment: req.TcpForwardingData.Comment,
  190. }
  191. // 4. 构建 WAF 表单数据映射
  192. formData := s.buildWafFormData(&formDataBase, require)
  193. return require, formData, nil
  194. }
  195. func (s *tcpforwardingService) AddTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest) error {
  196. require, formData, err := s.prepareWafData(ctx, req)
  197. if err != nil {
  198. return err
  199. }
  200. err = s.wafformatter.validateWafPortCount(ctx, require.HostId)
  201. if err != nil {
  202. return err
  203. }
  204. wafTcpId, err := s.wafformatter.sendFormData(ctx, "admin/info/waf_tcp/new", "admin/new/waf_tcp", formData)
  205. if err != nil {
  206. return err
  207. }
  208. gatewayIps, _, err := s.wafformatter.GetIp(ctx, require.WafGatewayGroupId)
  209. if err != nil {
  210. return err
  211. }
  212. // 异步任务:将IP添加到白名单
  213. var ips []string
  214. if req.TcpForwardingData.BackendList != nil {
  215. for _, v := range req.TcpForwardingData.BackendList {
  216. ip, _, err := net.SplitHostPort(v)
  217. if err != nil {
  218. return err
  219. }
  220. ips = append(ips, ip)
  221. }
  222. go s.wafformatter.PublishIpWhitelistTask(ips, "add","")
  223. }
  224. var accessRuleIps []string
  225. if req.TcpForwardingData.AllowIpList != nil {
  226. for _, v := range gatewayIps {
  227. for _, ip := range req.TcpForwardingData.AllowIpList {
  228. if net.ParseIP(ip) != nil{
  229. accessRuleIps = append(accessRuleIps, ip)
  230. }
  231. }
  232. go s.wafformatter.PublishIpWhitelistTask(accessRuleIps, "add",v)
  233. }
  234. }
  235. tcpModel := s.buildTcpForwardingModel(&req.TcpForwardingData, wafTcpId, require)
  236. id, err := s.tcpforwardingRepository.AddTcpforwarding(ctx, tcpModel)
  237. if err != nil {
  238. return err
  239. }
  240. TcpRuleModel := s.buildTcpRuleModel(&req.TcpForwardingData, require, id)
  241. if _, err = s.tcpforwardingRepository.AddTcpforwardingIps(ctx, *TcpRuleModel); err != nil {
  242. return err
  243. }
  244. return nil
  245. }
  246. func (s *tcpforwardingService) EditTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest) error {
  247. WafTcpId, err := s.tcpforwardingRepository.GetTcpforwardingWafTcpIdById(ctx, req.TcpForwardingData.Id)
  248. if err != nil {
  249. return err
  250. }
  251. req.TcpForwardingData.WafTcpId = WafTcpId
  252. require, formData, err := s.prepareWafData(ctx, req)
  253. if err != nil {
  254. return err
  255. }
  256. _, err = s.wafformatter.sendFormData(ctx, "admin/info/waf_tcp/edit?&__goadmin_edit_pk="+strconv.Itoa(req.TcpForwardingData.WafTcpId), "admin/edit/waf_tcp", formData)
  257. if err != nil {
  258. return err
  259. }
  260. gatewayIps, _, err := s.wafformatter.GetIp(ctx, require.WafGatewayGroupId)
  261. if err != nil {
  262. return err
  263. }
  264. // 异步任务:将IP添加到白名单
  265. ipData, err := s.tcpforwardingRepository.GetTcpForwardingIpsByID(ctx, req.TcpForwardingData.Id)
  266. if err != nil {
  267. return err
  268. }
  269. addedIps, removedIps, addedAllowIps, removedAllowIps, err := s.wafformatter.WashEditWafIp(ctx,req.TcpForwardingData.BackendList,req.TcpForwardingData.AllowIpList,ipData.BackendList,ipData.AllowIpList)
  270. if err != nil {
  271. return err
  272. }
  273. if len(addedIps) > 0 {
  274. go s.wafformatter.PublishIpWhitelistTask(addedIps, "add","")
  275. }
  276. if len(removedIps) > 0 {
  277. go s.wafformatter.PublishIpWhitelistTask(removedIps, "del","")
  278. }
  279. if len(addedAllowIps) > 0 {
  280. for _, v := range gatewayIps {
  281. go s.wafformatter.PublishIpWhitelistTask(addedAllowIps, "add",v)
  282. }
  283. }
  284. if len(removedAllowIps) > 0 {
  285. for _, v := range gatewayIps {
  286. go s.wafformatter.PublishIpWhitelistTask(removedAllowIps, "del",v)
  287. }
  288. }
  289. tcpModel := s.buildTcpForwardingModel(&req.TcpForwardingData, req.TcpForwardingData.WafTcpId, require)
  290. tcpModel.Id = req.TcpForwardingData.Id
  291. if err = s.tcpforwardingRepository.EditTcpforwarding(ctx, tcpModel); err != nil {
  292. return err
  293. }
  294. TcpRuleModel := s.buildTcpRuleModel(&req.TcpForwardingData, require, req.TcpForwardingData.Id)
  295. if err = s.tcpforwardingRepository.EditTcpforwardingIps(ctx, *TcpRuleModel); err != nil {
  296. return err
  297. }
  298. return nil
  299. }
  300. func (s *tcpforwardingService) DeleteTcpForwarding(ctx context.Context, req v1.DeleteTcpForwardingRequest) error {
  301. for _, Id := range req.Ids {
  302. wafTcpId, err := s.tcpforwardingRepository.GetTcpforwardingWafTcpIdById(ctx, Id)
  303. if err != nil {
  304. return err
  305. }
  306. _, err = s.crawler.DeleteRule(ctx, wafTcpId, "admin/delete/waf_tcp?page=1&__pageSize=1000000&__sort=waf_tcp_id&__sort_type=desc")
  307. if err != nil {
  308. return err
  309. }
  310. // 删除白名单
  311. var ips []string
  312. ipData, err := s.tcpforwardingRepository.GetTcpForwardingIpsByID(ctx, Id)
  313. if err != nil {
  314. return err
  315. }
  316. ips, err = s.wafformatter.WashDeleteWafIp(ctx, ipData.BackendList, ipData.AllowIpList)
  317. if err != nil {
  318. return err
  319. }
  320. if len(ips) > 0 {
  321. go s.wafformatter.PublishIpWhitelistTask(ips, "del","")
  322. }
  323. if err = s.tcpforwardingRepository.DeleteTcpforwarding(ctx, int64(Id)); err != nil {
  324. return err
  325. }
  326. if err = s.tcpforwardingRepository.DeleteTcpForwardingIpsById(ctx, Id); err != nil {
  327. return err
  328. }
  329. }
  330. return nil
  331. }
  332. func (s *tcpforwardingService) GetTcpForwardingAllIpsByHostId(ctx context.Context, req v1.GetForwardingRequest) ([]v1.TcpForwardingDataRequest, error) {
  333. type CombinedResult struct {
  334. Id int
  335. Forwarding *model.Tcpforwarding
  336. BackendRule *model.TcpForwardingRule
  337. Err error // 如果此ID的处理出错,则携带错误
  338. }
  339. g,gCtx := errgroup.WithContext(ctx)
  340. ids, err := s.tcpforwardingRepository.GetTcpForwardingAllIdsByID(gCtx, req.HostId)
  341. if err != nil {
  342. return nil, fmt.Errorf("GetTcpForwardingAllIds failed: %w", err)
  343. }
  344. if len(ids) == 0 {
  345. return nil, nil
  346. }
  347. resChan := make(chan CombinedResult, len(ids))
  348. g.Go(func() error {
  349. for _, idVal := range ids {
  350. currentID := idVal
  351. g.Go(func() error {
  352. var wf *model.Tcpforwarding
  353. var bk *model.TcpForwardingRule
  354. var localErr error
  355. wf, localErr = s.tcpforwardingRepository.GetTcpforwarding(gCtx, int64(currentID))
  356. if localErr != nil {
  357. resChan <- CombinedResult{Id: currentID, Err: localErr}
  358. return localErr
  359. }
  360. bk, localErr = s.tcpforwardingRepository.GetTcpForwardingIpsByID(gCtx, currentID)
  361. if localErr != nil {
  362. resChan <- CombinedResult{Id: currentID, Err: localErr}
  363. return localErr
  364. }
  365. resChan <- CombinedResult{Id: currentID, Forwarding: wf, BackendRule: bk}
  366. return nil
  367. })
  368. }
  369. return nil
  370. })
  371. groupErr := g.Wait()
  372. close(resChan)
  373. if groupErr != nil {
  374. return nil, groupErr
  375. }
  376. res := make([]v1.TcpForwardingDataRequest, 0, len(ids))
  377. for r := range resChan {
  378. if r.Err != nil {
  379. return nil, fmt.Errorf("received error from goroutine for ID %d: %w", r.Id, r.Err)
  380. }
  381. if r.Forwarding == nil {
  382. return nil,fmt.Errorf("received nil forwarding from goroutine for ID %d", r.Id)
  383. }
  384. dataReq := v1.TcpForwardingDataRequest{
  385. Id: r.Forwarding.Id,
  386. Port: r.Forwarding.Port,
  387. CcCount: r.Forwarding.CcCount,
  388. CcDuration: r.Forwarding.CcDuration,
  389. CcBlockCount: r.Forwarding.CcBlockCount,
  390. CcBlockDuration: r.Forwarding.CcBlockDuration,
  391. BackendProtocol: r.Forwarding.BackendProtocol,
  392. BackendTimeout: r.Forwarding.BackendTimeout,
  393. Comment: r.Forwarding.Comment,
  394. }
  395. if r.BackendRule != nil {
  396. dataReq.BackendList = r.BackendRule.BackendList
  397. dataReq.AllowIpList = r.BackendRule.AllowIpList
  398. dataReq.DenyIpList = r.BackendRule.DenyIpList
  399. dataReq.AccessRule = r.BackendRule.AccessRule
  400. }
  401. res = append(res, dataReq)
  402. }
  403. sort.Slice(res, func(i, j int) bool {
  404. return res[i].Id > res[j].Id
  405. })
  406. return res, nil
  407. }