webforwarding.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  9. "github.com/go-nunu/nunu-layout-advanced/pkg/rabbitmq"
  10. "golang.org/x/sync/errgroup"
  11. "net"
  12. "sort"
  13. "strconv"
  14. "strings"
  15. )
  16. type WebForwardingService interface {
  17. GetWebForwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.WebForwardingDataRequest, error)
  18. GetWebForwardingWafWebAllIps(ctx context.Context, req v1.GetForwardingRequest) ([]v1.WebForwardingDataRequest, error)
  19. AddWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error
  20. EditWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error
  21. DeleteWebForwarding(ctx context.Context, Ids []int) error
  22. }
  23. func NewWebForwardingService(
  24. service *Service,
  25. required RequiredService,
  26. webForwardingRepository repository.WebForwardingRepository,
  27. crawler CrawlerService,
  28. parser ParserService,
  29. wafformatter WafFormatterService,
  30. aoDun AoDunService,
  31. mq *rabbitmq.RabbitMQ,
  32. gatewayGroupIpRep repository.GateWayGroupIpRepository,
  33. gatewayGroupRep repository.GatewayGroupRepository,
  34. ) WebForwardingService {
  35. return &webForwardingService{
  36. Service: service,
  37. webForwardingRepository: webForwardingRepository,
  38. required: required,
  39. parser: parser,
  40. crawler: crawler,
  41. wafformatter: wafformatter,
  42. aoDun: aoDun,
  43. mq: mq,
  44. gatewayGroupIpRep: gatewayGroupIpRep,
  45. gatewayGroupRep: gatewayGroupRep,
  46. }
  47. }
  48. type webForwardingService struct {
  49. *Service
  50. webForwardingRepository repository.WebForwardingRepository
  51. required RequiredService
  52. parser ParserService
  53. crawler CrawlerService
  54. wafformatter WafFormatterService
  55. aoDun AoDunService
  56. mq *rabbitmq.RabbitMQ
  57. gatewayGroupIpRep repository.GateWayGroupIpRepository
  58. gatewayGroupRep repository.GatewayGroupRepository
  59. }
  60. func (s *webForwardingService) require(ctx context.Context,req v1.GlobalRequire) (v1.GlobalRequire, error) {
  61. var err error
  62. var res v1.GlobalRequire
  63. g, gCtx := errgroup.WithContext(ctx)
  64. //g.Go(func() error {
  65. // result, e := s.wafformatter.require(gCtx, req, "web")
  66. // if e != nil {
  67. // return e
  68. // }
  69. // res = result
  70. // return nil
  71. //})
  72. g.Go(func() error {
  73. e := s.wafformatter.validateWafDomainCount(gCtx, req)
  74. if e != nil {
  75. return e
  76. }
  77. return nil
  78. })
  79. if err = g.Wait(); err != nil {
  80. return v1.GlobalRequire{}, err
  81. }
  82. return res, nil
  83. }
  84. func (s *webForwardingService) GetWebForwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.WebForwardingDataRequest, error) {
  85. var webForwarding model.WebForwarding
  86. var backend model.WebForwardingRule
  87. g, gCtx := errgroup.WithContext(ctx)
  88. g.Go(func() error {
  89. res, e := s.webForwardingRepository.GetWebForwarding(gCtx, int64(req.Id))
  90. if e != nil {
  91. // 直接返回错误,errgroup 会捕获它
  92. return fmt.Errorf("GetWebForwarding failed: %w", e)
  93. }
  94. if res != nil {
  95. webForwarding = *res
  96. }
  97. return nil
  98. })
  99. g.Go(func() error {
  100. res, e := s.webForwardingRepository.GetWebForwardingIpsByID(ctx, req.Id)
  101. if e != nil {
  102. return fmt.Errorf("GetWebForwardingByID failed: %w", e)
  103. }
  104. if res != nil {
  105. backend = *res
  106. }
  107. return nil
  108. })
  109. if err := g.Wait(); err != nil {
  110. return v1.WebForwardingDataRequest{}, err
  111. }
  112. return v1.WebForwardingDataRequest{
  113. Id: webForwarding.Id,
  114. WafWebId: webForwarding.WafWebId,
  115. Tag: webForwarding.Tag,
  116. Port: webForwarding.Port,
  117. Domain: webForwarding.Domain,
  118. CustomHost: webForwarding.CustomHost,
  119. WafWebLimitId: webForwarding.WebLimitRuleId,
  120. WafGatewayGroupId: webForwarding.WafGatewayGroupId,
  121. CcCount: webForwarding.CcCount,
  122. CcDuration: webForwarding.CcDuration,
  123. CcBlockCount: webForwarding.CcBlockCount,
  124. CcBlockDuration: webForwarding.CcBlockDuration,
  125. Cc4xxCount: webForwarding.Cc4xxCount,
  126. Cc4xxDuration: webForwarding.Cc4xxDuration,
  127. Cc4xxBlockCount: webForwarding.Cc4xxBlockCount,
  128. Cc4xxBlockDuration: webForwarding.Cc4xxBlockDuration,
  129. Cc5xxCount: webForwarding.Cc5xxCount,
  130. Cc5xxDuration: webForwarding.Cc5xxDuration,
  131. Cc5xxBlockCount: webForwarding.Cc5xxBlockCount,
  132. Cc5xxBlockDuration: webForwarding.Cc5xxBlockDuration,
  133. IsHttps: webForwarding.IsHttps,
  134. Comment: webForwarding.Comment,
  135. BackendList: backend.BackendList,
  136. AllowIpList: backend.AllowIpList,
  137. DenyIpList: backend.DenyIpList,
  138. AccessRule: backend.AccessRule,
  139. HttpsKey: webForwarding.HttpsKey,
  140. HttpsCert: webForwarding.HttpsCert,
  141. }, nil
  142. }
  143. // buildWafFormData 辅助函数,用于构建通用的 formData
  144. func (s *webForwardingService) buildWafFormData(req *v1.WebForwardingDataSend, require v1.GlobalRequire) map[string]interface{} {
  145. // 将BackendList序列化为JSON字符串
  146. backendJSON, err := json.MarshalIndent(req.BackendList, "", " ")
  147. var backendStr interface{}
  148. if err != nil {
  149. // 如果序列化失败,使用空数组
  150. backendStr = "[]"
  151. } else {
  152. // 成功序列化后,使用JSON字符串
  153. backendStr = string(backendJSON)
  154. }
  155. return map[string]interface{}{
  156. "waf_web_id": req.WafWebId,
  157. "port": req.Port,
  158. "domain": req.Domain,
  159. "custom_host": req.CustomHost,
  160. "cc_count": req.CcCount,
  161. "cc_duration": req.CcDuration,
  162. "cc_block_count": req.CcBlockCount,
  163. "cc_block_duration": req.CcBlockDuration,
  164. "cc_4xx_count": req.Cc4xxCount,
  165. "cc_4xx_duration": req.Cc4xxDuration,
  166. "cc_4xx_block_count": req.Cc4xxBlockCount,
  167. "cc_4xx_block_duration": req.Cc4xxBlockDuration,
  168. "cc_5xx_count": req.Cc5xxCount,
  169. "cc_5xx_duration": req.Cc5xxDuration,
  170. "cc_5xx_block_count": req.Cc5xxBlockCount,
  171. "cc_5xx_block_duration": req.Cc5xxBlockDuration,
  172. "backend": backendStr,
  173. "allow_ip_list": req.AllowIpList,
  174. "deny_ip_list": req.DenyIpList,
  175. "access_rule": req.AccessRule,
  176. "is_https": req.IsHttps,
  177. "comment": req.Comment,
  178. "https_cert": req.HttpsCert,
  179. "https_key": req.HttpsKey,
  180. }
  181. }
  182. // buildWebForwardingModel 辅助函数,用于构建通用的 WebForwarding 模型
  183. // ruleId 是从 WAF 系统获取的 ID
  184. func (s *webForwardingService) buildWebForwardingModel(req *v1.WebForwardingDataRequest,ruleId int, require v1.GlobalRequire) *model.WebForwarding {
  185. return &model.WebForwarding{
  186. HostId: require.HostId,
  187. WafWebId: ruleId,
  188. Port: req.Port,
  189. Domain: req.Domain,
  190. CustomHost: req.CustomHost,
  191. CcCount: req.CcCount,
  192. CcDuration: req.CcDuration,
  193. CcBlockCount: req.CcBlockCount,
  194. CcBlockDuration: req.CcBlockDuration,
  195. Cc4xxCount: req.Cc4xxCount,
  196. Cc4xxDuration: req.Cc4xxDuration,
  197. Cc4xxBlockCount: req.Cc4xxBlockCount,
  198. Cc4xxBlockDuration: req.Cc4xxBlockDuration,
  199. Cc5xxCount: req.Cc5xxCount,
  200. Cc5xxDuration: req.Cc5xxDuration,
  201. Cc5xxBlockCount: req.Cc5xxBlockCount,
  202. Cc5xxBlockDuration: req.Cc5xxBlockDuration,
  203. IsHttps: req.IsHttps,
  204. Comment: req.Comment,
  205. HttpsCert: req.HttpsCert,
  206. HttpsKey: req.HttpsKey,
  207. }
  208. }
  209. func (s *webForwardingService) buildWebRuleModel(reqData *v1.WebForwardingDataRequest, require v1.GlobalRequire, localDbId int) *model.WebForwardingRule {
  210. return &model.WebForwardingRule{
  211. Uid: require.Uid,
  212. HostId: require.HostId,
  213. WebId: localDbId, // 关联到本地数据库的主记录 ID
  214. BackendList: reqData.BackendList,
  215. AllowIpList: reqData.AllowIpList,
  216. DenyIpList: reqData.DenyIpList,
  217. AccessRule: reqData.AccessRule,
  218. }
  219. }
  220. func (s *webForwardingService) prepareWafData(ctx context.Context, req *v1.WebForwardingRequest) (v1.GlobalRequire, map[string]interface{}, error) {
  221. // 1. 获取必要的全局信息
  222. require, err := s.require(ctx, v1.GlobalRequire{
  223. HostId: req.HostId,
  224. Uid: req.Uid,
  225. Comment: req.WebForwardingData.Comment,
  226. Domain: req.WebForwardingData.Domain,
  227. })
  228. if err != nil {
  229. return v1.GlobalRequire{}, nil, err
  230. }
  231. // 2. 将字符串切片拼接成字符串,用于 WAF API
  232. allowIpListStr := strings.Join(req.WebForwardingData.AllowIpList, "\n")
  233. denyIpListStr := strings.Join(req.WebForwardingData.DenyIpList, "\n")
  234. PortInt, err := strconv.Atoi(req.WebForwardingData.Port)
  235. if err != nil {
  236. return v1.GlobalRequire{}, nil, err
  237. }
  238. // 3. 创建用于构建 WAF 表单的数据结构
  239. formDataBase := v1.WebForwardingDataSend{
  240. WafWebId: req.WebForwardingData.WafWebId,
  241. Port: PortInt,
  242. Domain: req.WebForwardingData.Domain,
  243. CustomHost: req.WebForwardingData.CustomHost,
  244. CcCount: req.WebForwardingData.CcCount,
  245. CcDuration: req.WebForwardingData.CcDuration,
  246. CcBlockCount: req.WebForwardingData.CcBlockCount,
  247. CcBlockDuration: req.WebForwardingData.CcBlockDuration,
  248. Cc4xxCount: req.WebForwardingData.Cc4xxCount,
  249. Cc4xxDuration: req.WebForwardingData.Cc4xxDuration,
  250. Cc4xxBlockCount: req.WebForwardingData.Cc4xxBlockCount,
  251. Cc4xxBlockDuration: req.WebForwardingData.Cc4xxBlockDuration,
  252. Cc5xxCount: req.WebForwardingData.Cc5xxCount,
  253. Cc5xxDuration: req.WebForwardingData.Cc5xxDuration,
  254. Cc5xxBlockCount: req.WebForwardingData.Cc5xxBlockCount,
  255. Cc5xxBlockDuration: req.WebForwardingData.Cc5xxBlockDuration,
  256. IsHttps: req.WebForwardingData.IsHttps,
  257. BackendList: req.WebForwardingData.BackendList,
  258. AllowIpList: allowIpListStr,
  259. DenyIpList: denyIpListStr,
  260. AccessRule: req.WebForwardingData.AccessRule,
  261. Comment: req.WebForwardingData.Comment,
  262. HttpsCert: req.WebForwardingData.HttpsCert,
  263. HttpsKey: req.WebForwardingData.HttpsKey,
  264. }
  265. // 4. 构建 WAF 表单数据映射
  266. formData := s.buildWafFormData(&formDataBase, require)
  267. return require, formData, nil
  268. }
  269. func (s *webForwardingService) AddWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error {
  270. //require, formData, err := s.prepareWafData(ctx, req)
  271. //if err != nil {
  272. // return err
  273. //}
  274. //err = s.wafformatter.validateWafPortCount(ctx, require.HostId)
  275. //if err != nil {
  276. // return err
  277. //}
  278. //
  279. //
  280. //gatewayIps, firstIp, err := s.wafformatter.GetIp(ctx, require.WafGatewayGroupId)
  281. //if err != nil {
  282. // return err
  283. //}
  284. //if req.WebForwardingData.Domain != "" {
  285. // // 异步任务:将域名添加到白名单
  286. // doMain, err := s.wafformatter.ConvertToWildcardDomain(ctx, req.WebForwardingData.Domain)
  287. // if err != nil {
  288. // return err
  289. // }
  290. // go s.wafformatter.PublishDomainWhitelistTask(doMain,firstIp, "add")
  291. //
  292. //}
  293. //
  294. //// IP过白
  295. //var ips []string
  296. //if req.WebForwardingData.BackendList != nil {
  297. // for _, v := range req.WebForwardingData.BackendList {
  298. // ip, _, err := net.SplitHostPort(v.Addr)
  299. // if err != nil {
  300. // return err
  301. // }
  302. // ips = append(ips,ip)
  303. // }
  304. // go s.wafformatter.PublishIpWhitelistTask(ips, "add","")
  305. //}
  306. //var accessRuleIps []string
  307. //if len(req.WebForwardingData.AllowIpList) > 0 {
  308. // for _, v := range gatewayIps {
  309. // for _, ip := range req.WebForwardingData.AllowIpList {
  310. // if net.ParseIP(ip) != nil{
  311. // accessRuleIps = append(accessRuleIps, ip)
  312. // }
  313. // }
  314. // go s.wafformatter.PublishIpWhitelistTask(accessRuleIps, "add",v)
  315. // }
  316. //}
  317. //
  318. //
  319. //
  320. //webModel := s.buildWebForwardingModel(&req.WebForwardingData, wafWebId, require)
  321. //
  322. //id, err := s.webForwardingRepository.AddWebForwarding(ctx, webModel)
  323. //if err != nil {
  324. // return err
  325. //}
  326. //webRuleModel := s.buildWebRuleModel(&req.WebForwardingData, require, id)
  327. //
  328. //
  329. //if _, err = s.webForwardingRepository.AddWebForwardingIps(ctx, *webRuleModel); err != nil {
  330. // return err
  331. //}
  332. return nil
  333. }
  334. func (s *webForwardingService) EditWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error {
  335. //WafWebId, err := s.webForwardingRepository.GetWebForwardingWafWebIdById(ctx, req.WebForwardingData.Id)
  336. //if err != nil {
  337. // return err
  338. //}
  339. //req.WebForwardingData.WafWebId = WafWebId
  340. //require, formData, err := s.prepareWafData(ctx, req)
  341. //if err != nil {
  342. // return err
  343. //}
  344. //_, err = s.wafformatter.sendFormData(ctx, "admin/info/waf_web/edit?&__goadmin_edit_pk="+strconv.Itoa(req.WebForwardingData.WafWebId), "admin/edit/waf_web", formData)
  345. //if err != nil {
  346. // return err
  347. //}
  348. //
  349. //// 将域名添加到白名单
  350. //webData, err := s.webForwardingRepository.GetWebForwarding(ctx, int64(req.WebForwardingData.Id))
  351. //if err != nil {
  352. // return err
  353. //}
  354. //gatewayIps, firstIp, err := s.wafformatter.GetIp(ctx, webData.WafGatewayGroupId)
  355. //if err != nil {
  356. // return err
  357. //}
  358. //// 异步任务:将域名添加到白名单
  359. //if webData.Domain != req.WebForwardingData.Domain {
  360. //
  361. // doMain, err := s.wafformatter.ConvertToWildcardDomain(ctx, req.WebForwardingData.Domain)
  362. // if err != nil {
  363. // return err
  364. // }
  365. // oldDomain, err := s.wafformatter.ConvertToWildcardDomain(ctx, webData.Domain)
  366. // if err != nil {
  367. // return err
  368. // }
  369. // go s.wafformatter.PublishDomainWhitelistTask(oldDomain, firstIp, "del")
  370. // go s.wafformatter.PublishDomainWhitelistTask(doMain, firstIp, "add")
  371. //}
  372. //
  373. //// IP过白
  374. //ipData, err := s.webForwardingRepository.GetWebForwardingIpsByID(ctx, req.WebForwardingData.Id)
  375. //if err != nil {
  376. // return err
  377. //}
  378. //var oldIps []string
  379. //var oldAllowIps []string
  380. //var newIps []string
  381. //var newAllowIps []string
  382. //for _, v := range ipData.BackendList {
  383. // ip, _, err := net.SplitHostPort(v.Addr)
  384. // if err != nil {
  385. // return err
  386. // }
  387. // oldIps = append(oldIps, ip)
  388. //
  389. //}
  390. //for _, v := range req.WebForwardingData.BackendList {
  391. // ip, _, err := net.SplitHostPort(v.Addr)
  392. // if err != nil {
  393. // return err
  394. // }
  395. // newIps = append(newIps, ip)
  396. //}
  397. //addedIps, removedIps := s.wafformatter.findIpDifferences(oldIps, newIps)
  398. //if len(addedIps) > 0 {
  399. // go s.wafformatter.PublishIpWhitelistTask(addedIps, "add","")
  400. //}
  401. //if len(removedIps) > 0 {
  402. // go s.wafformatter.PublishIpWhitelistTask(removedIps, "del","")
  403. //}
  404. //if len(ipData.AllowIpList) > 0 {
  405. // for _, v := range ipData.AllowIpList {
  406. // if net.ParseIP(v) != nil{
  407. // oldAllowIps = append(oldAllowIps, v)
  408. // }
  409. // }
  410. //}
  411. //
  412. //if len(req.WebForwardingData.AllowIpList) > 0 {
  413. // for _, v := range req.WebForwardingData.AllowIpList {
  414. // if net.ParseIP(v) != nil{
  415. // newAllowIps = append(newAllowIps, v)
  416. // }
  417. // }
  418. //}
  419. //addedAllowIps, removedAllowIps := s.wafformatter.findIpDifferences(oldAllowIps, newAllowIps)
  420. //for _, v := range gatewayIps {
  421. // if len(addedAllowIps) > 0 {
  422. // go s.wafformatter.PublishIpWhitelistTask(addedAllowIps, "add",v)
  423. // }
  424. // if len(removedAllowIps) > 0 {
  425. // go s.wafformatter.PublishIpWhitelistTask(removedAllowIps, "del",v)
  426. // }
  427. //}
  428. //
  429. //
  430. //
  431. //
  432. //
  433. //
  434. //
  435. //webModel := s.buildWebForwardingModel(&req.WebForwardingData, req.WebForwardingData.WafWebId, require)
  436. //webModel.Id = req.WebForwardingData.Id
  437. //if err = s.webForwardingRepository.EditWebForwarding(ctx, webModel); err != nil {
  438. // return err
  439. //}
  440. //webRuleModel := s.buildWebRuleModel(&req.WebForwardingData, require, req.WebForwardingData.Id)
  441. //if err = s.webForwardingRepository.EditWebForwardingIps(ctx, *webRuleModel); err != nil {
  442. // return err
  443. //}
  444. return nil
  445. }
  446. func (s *webForwardingService) DeleteWebForwarding(ctx context.Context, Ids []int) error {
  447. for _, Id := range Ids {
  448. wafWebId, err := s.webForwardingRepository.GetWebForwardingWafWebIdById(ctx, Id)
  449. if err != nil {
  450. return err
  451. }
  452. _, err = s.crawler.DeleteRule(ctx, wafWebId, "admin/delete/waf_web?page=1&__pageSize=10&__sort=waf_web_id&__sort_type=desc")
  453. if err != nil {
  454. return err
  455. }
  456. webData, err := s.webForwardingRepository.GetWebForwarding(ctx, int64(Id))
  457. if err != nil {
  458. return err
  459. }
  460. _, firstIp, err := s.wafformatter.GetIp(ctx, webData.WafGatewayGroupId)
  461. if err != nil {
  462. return err
  463. }
  464. // 异步任务:将域名添加到白名单
  465. if webData.Domain != "" {
  466. doMain, err := s.wafformatter.ConvertToWildcardDomain(ctx, webData.Domain)
  467. if err != nil {
  468. return err
  469. }
  470. go s.wafformatter.PublishDomainWhitelistTask(doMain,firstIp, "del")
  471. }
  472. // IP过白
  473. ipData, err := s.webForwardingRepository.GetWebForwardingIpsByID(ctx, Id)
  474. if err != nil {
  475. return err
  476. }
  477. var ips []string
  478. if len(ipData.BackendList) > 0 {
  479. for _, v := range ipData.BackendList {
  480. ip, _, err := net.SplitHostPort(v.Addr)
  481. if err != nil {
  482. return err
  483. }
  484. ips = append(ips, ip)
  485. }
  486. }
  487. if len(ipData.AllowIpList) > 0 {
  488. ips = append(ips, ipData.AllowIpList...)
  489. }
  490. if len(ips) > 0 {
  491. go s.wafformatter.PublishIpWhitelistTask(ips, "del","")
  492. }
  493. if err = s.webForwardingRepository.DeleteWebForwarding(ctx, int64(Id)); err != nil {
  494. return err
  495. }
  496. if err = s.webForwardingRepository.DeleteWebForwardingIpsById(ctx, Id); err != nil {
  497. return err
  498. }
  499. }
  500. return nil
  501. }
  502. func (s *webForwardingService) GetWebForwardingWafWebAllIps(ctx context.Context, req v1.GetForwardingRequest) ([]v1.WebForwardingDataRequest, error) {
  503. type CombinedResult struct {
  504. Id int
  505. Forwarding *model.WebForwarding
  506. BackendRule *model.WebForwardingRule
  507. Err error // 如果此ID的处理出错,则携带错误
  508. }
  509. g, gCtx := errgroup.WithContext(ctx)
  510. ids, err := s.webForwardingRepository.GetWebForwardingWafWebAllIds(gCtx, req.HostId)
  511. if err != nil {
  512. return nil, fmt.Errorf("GetWebForwardingWafWebAllIds failed: %w", err)
  513. }
  514. if len(ids) == 0 {
  515. return nil, nil // 没有ID,直接返回空切片
  516. }
  517. // 创建一个通道来接收每个ID的处理结果
  518. // 通道缓冲区大小设为ID数量,这样发送者不会因为接收者慢而阻塞(在所有goroutine都启动后)
  519. resultsChan := make(chan CombinedResult, len(ids))
  520. for _, idVal := range ids {
  521. currentID := idVal // 捕获循环变量
  522. g.Go(func() error {
  523. var wf *model.WebForwarding
  524. var bk *model.WebForwardingRule
  525. var localErr error
  526. // 1. 获取 WebForwarding 信息
  527. wf, localErr = s.webForwardingRepository.GetWebForwarding(gCtx, int64(currentID))
  528. if localErr != nil {
  529. // 发送错误到通道,并由 errgroup 捕获
  530. // errgroup 会处理第一个非nil错误,并取消其他 goroutine
  531. resultsChan <- CombinedResult{Id: currentID, Err: fmt.Errorf("GetWebForwarding for id %d failed: %w", currentID, localErr)}
  532. return localErr // 返回错误给 errgroup
  533. }
  534. if wf == nil { // 正常情况下,如果没错误,wf不应为nil,但防御性检查
  535. localErr = fmt.Errorf("GetWebForwarding for id %d returned nil data without error", currentID)
  536. resultsChan <- CombinedResult{Id: currentID, Err: localErr}
  537. return localErr
  538. }
  539. // 2. 获取 Backend IP 信息
  540. // 注意:这里我们允许 GetWebForwardingIpsByID 可能返回 nil 数据(例如没有规则)而不是错误
  541. // 如果它也可能返回错误,则处理方式与上面类似
  542. bk, localErr = s.webForwardingRepository.GetWebForwardingIpsByID(gCtx, currentID)
  543. if localErr != nil {
  544. // 如果获取IP信息失败是一个致命错误,则也应返回错误
  545. // 如果允许部分成功(比如有WebForwarding但没有IP信息),则可以不将此视为errgroup的错误
  546. // 这里假设它也是一个需要errgroup捕获的错误
  547. resultsChan <- CombinedResult{Id: currentID, Forwarding: wf, Err: fmt.Errorf("GetWebForwardingIpsByID for id %d failed: %w", currentID, localErr)}
  548. return localErr // 返回错误给 errgroup
  549. }
  550. // bk 可能是 nil 如果没有错误且没有规则,这取决于业务逻辑
  551. // 发送成功的结果到通道
  552. resultsChan <- CombinedResult{Id: currentID, Forwarding: wf, BackendRule: bk}
  553. return nil // 此goroutine成功
  554. })
  555. }
  556. // 等待所有goroutine完成
  557. groupErr := g.Wait()
  558. // 关闭通道,表示所有发送者都已完成
  559. // 这一步很重要,这样下面的 range 循环才能正常结束
  560. close(resultsChan)
  561. // 如果 errgroup 捕获到任何错误,优先返回该错误
  562. if groupErr != nil {
  563. // 虽然errgroup已经出错了,但通道中可能已经有一些结果(来自出错前成功或出错的goroutine)
  564. // 我们需要排空通道以避免goroutine泄漏(如果它们在发送时阻塞)
  565. // 但由于我们优先返回groupErr,这些结果将被丢弃。
  566. // 在这种设计下,通常任何一个子任务失败都会导致整个操作失败。
  567. return nil, groupErr
  568. }
  569. // 如果没有错误,收集所有成功的结果
  570. finalResults := make([]v1.WebForwardingDataRequest, 0, len(ids))
  571. for res := range resultsChan {
  572. // 再次检查通道中的错误,尽管 errgroup 应该已经捕获了
  573. // 但这是一种更细致的错误处理,以防万一有goroutine在errgroup.Wait()前发送了错误但未被errgroup捕获
  574. // (理论上,如果goroutine返回了错误,errgroup会处理)
  575. // 主要目的是处理 res.forwarding 为 nil 的情况 (如果上面允许不返回错误)
  576. if res.Err != nil {
  577. // 如果到这里还有错误,说明逻辑可能有问题,或者我们决定忽略某些类型的子错误
  578. // 在此示例中,因为 g.Wait() 没有错误,所以这里的 res.err 应该是nil
  579. // 如果不是,那么可能是goroutine在return nil前发送了带有错误的res。
  580. // 严格来说,如果errgroup没有错误,这里res.err也应该是nil
  581. // 但以防万一,我们可以记录日志
  582. return nil, fmt.Errorf("received error from goroutine for ID %d: %w", res.Id, res.Err)
  583. }
  584. if res.Forwarding == nil {
  585. return nil, fmt.Errorf("received nil forwarding from goroutine for ID %d", res.Id)
  586. }
  587. dataReq := v1.WebForwardingDataRequest{
  588. Id: res.Forwarding.Id,
  589. Port: res.Forwarding.Port,
  590. Domain: res.Forwarding.Domain,
  591. CustomHost: res.Forwarding.CustomHost,
  592. CcCount: res.Forwarding.CcCount,
  593. CcDuration: res.Forwarding.CcDuration,
  594. CcBlockCount: res.Forwarding.CcBlockCount,
  595. CcBlockDuration: res.Forwarding.CcBlockDuration,
  596. Cc4xxCount: res.Forwarding.Cc4xxCount,
  597. Cc4xxDuration: res.Forwarding.Cc4xxDuration,
  598. Cc4xxBlockCount: res.Forwarding.Cc4xxBlockCount,
  599. Cc4xxBlockDuration: res.Forwarding.Cc4xxBlockDuration,
  600. Cc5xxCount: res.Forwarding.Cc5xxCount,
  601. Cc5xxDuration: res.Forwarding.Cc5xxDuration,
  602. Cc5xxBlockCount: res.Forwarding.Cc5xxBlockCount,
  603. Cc5xxBlockDuration: res.Forwarding.Cc5xxBlockDuration,
  604. IsHttps: res.Forwarding.IsHttps,
  605. Comment: res.Forwarding.Comment,
  606. HttpsKey: res.Forwarding.HttpsKey,
  607. HttpsCert: res.Forwarding.HttpsCert,
  608. }
  609. if res.BackendRule != nil { // 只有当 BackendRule 存在时才填充相关字段
  610. dataReq.BackendList = res.BackendRule.BackendList
  611. dataReq.AllowIpList = res.BackendRule.AllowIpList
  612. dataReq.DenyIpList = res.BackendRule.DenyIpList
  613. dataReq.AccessRule = res.BackendRule.AccessRule
  614. }
  615. finalResults = append(finalResults, dataReq)
  616. }
  617. sort.Slice(finalResults, func(i, j int) bool {
  618. return finalResults[i].Id > finalResults[j].Id
  619. })
  620. return finalResults, nil
  621. }