aidedtcp.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package waf
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "maps"
  7. "net"
  8. "strconv"
  9. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  10. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  11. "github.com/go-nunu/nunu-layout-advanced/internal/repository/api/waf"
  12. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  13. "github.com/go-nunu/nunu-layout-advanced/internal/service/api/flexCdn"
  14. )
  15. // AidedTcpService TCP转发辅助服务接口
  16. type AidedTcpService interface {
  17. // 验证相关
  18. ValidateAddRequest(ctx context.Context, req *v1.TcpForwardingRequest, require RequireResponse) error
  19. ValidateEditRequest(ctx context.Context, req *v1.TcpForwardingRequest, require RequireResponse, oldData *model.Tcpforwarding) error
  20. ValidateDeletePermission(oldData *model.Tcpforwarding, hostId int) error
  21. // CDN操作相关
  22. CreateCdnWebsite(ctx context.Context, formData v1.WebsiteSend) (int64, error)
  23. UpdateCdnConfiguration(ctx context.Context, req *v1.TcpForwardingRequest, oldData *model.Tcpforwarding, require RequireResponse, formData v1.WebsiteSend) error
  24. DeleteCdnServer(ctx context.Context, cdnWebId int) error
  25. // 源站操作相关
  26. AddOriginsToWebsite(ctx context.Context, req *v1.TcpForwardingRequest, tcpId int64) (map[string]int64, error)
  27. UpdateOriginServers(ctx context.Context, req *v1.TcpForwardingRequest, oldData *model.Tcpforwarding, ipData *model.TcpForwardingRule) error
  28. // 代理协议配置
  29. ConfigureProxyProtocol(ctx context.Context, req *v1.TcpForwardingRequest, tcpId int64) error
  30. // 异步任务处理
  31. ProcessAsyncTasks(req *v1.TcpForwardingRequest)
  32. ProcessIpWhitelistChanges(ctx context.Context, req *v1.TcpForwardingRequest, ipData *model.TcpForwardingRule) error
  33. ProcessDeleteIpWhitelist(ctx context.Context, id int) error
  34. // 数据准备和配置
  35. PrepareWafData(ctx context.Context, req *v1.TcpForwardingRequest) (RequireResponse, v1.WebsiteSend, error)
  36. BuildTcpListenConfig(gatewayIps []string, portStr string) ([]byte, error)
  37. // 模型构建
  38. BuildTcpForwardingModel(req *v1.TcpForwardingDataRequest, ruleId int, require RequireResponse) *model.Tcpforwarding
  39. BuildTcpRuleModel(reqData *v1.TcpForwardingDataRequest, require RequireResponse, localDbId int, cdnOriginIds map[string]int64) *model.TcpForwardingRule
  40. // 数据库操作
  41. SaveToDatabase(ctx context.Context, req *v1.TcpForwardingRequest, require RequireResponse, tcpId int64, cdnOriginIds map[string]int64) (int, error)
  42. UpdateDatabaseRecords(ctx context.Context, req *v1.TcpForwardingRequest, oldData *model.Tcpforwarding, require RequireResponse, ipData *model.TcpForwardingRule) error
  43. CleanupDatabaseRecords(ctx context.Context, id int) error
  44. // 工具函数
  45. ExtractIpsFromBackends(backends []string) []string
  46. }
  47. type aidedTcpService struct {
  48. *service.Service
  49. wafformatter WafFormatterService
  50. cdn flexCdn.CdnService
  51. proxy flexCdn.ProxyService
  52. globalRep waf.GlobalLimitRepository
  53. tcpRepository waf.TcpforwardingRepository
  54. }
  55. func NewAidedTcpService(
  56. service *service.Service,
  57. wafformatter WafFormatterService,
  58. cdn flexCdn.CdnService,
  59. proxy flexCdn.ProxyService,
  60. globalRep waf.GlobalLimitRepository,
  61. tcpRepository waf.TcpforwardingRepository,
  62. ) AidedTcpService {
  63. return &aidedTcpService{
  64. Service: service,
  65. wafformatter: wafformatter,
  66. cdn: cdn,
  67. proxy: proxy,
  68. globalRep: globalRep,
  69. tcpRepository: tcpRepository,
  70. }
  71. }
  72. // ValidateAddRequest 验证添加 TCP 转发请求的合法性
  73. // 该函数验证以下内容:
  74. // 1. 验证 WAF 端口数量限制(防止超出配额)
  75. // 2. 验证端口号是否已被占用(确保端口唯一性)
  76. func (s *aidedTcpService) ValidateAddRequest(ctx context.Context, req *v1.TcpForwardingRequest, require RequireResponse) error {
  77. // 验证端口数量限制
  78. if err := s.wafformatter.validateWafPortCount(ctx, require.HostId); err != nil {
  79. return fmt.Errorf("端口数量验证失败: %w", err)
  80. }
  81. // 验证端口占用情况
  82. if err := s.wafformatter.VerifyPort(ctx, "tcp", int64(req.TcpForwardingData.Id), req.TcpForwardingData.Port, int64(require.HostId), ""); err != nil {
  83. return fmt.Errorf("端口 %d 已被占用或不合法: %w", req.TcpForwardingData.Port, err)
  84. }
  85. return nil
  86. }
  87. // ValidateEditRequest 验证编辑 TCP 转发请求的合法性
  88. // 该函数仅在端口发生变更时才验证端口冲突,提高性能并避免不必要的检查
  89. func (s *aidedTcpService) ValidateEditRequest(ctx context.Context, req *v1.TcpForwardingRequest, require RequireResponse, oldData *model.Tcpforwarding) error {
  90. // 只有端口发生变更时才需要验证端口冲突
  91. if oldData.Port != req.TcpForwardingData.Port {
  92. if err := s.wafformatter.VerifyPort(ctx, "tcp", int64(req.TcpForwardingData.Id), req.TcpForwardingData.Port, int64(require.HostId), ""); err != nil {
  93. return fmt.Errorf("新端口 %d 已被占用或不合法: %w", req.TcpForwardingData.Port, err)
  94. }
  95. }
  96. return nil
  97. }
  98. // ValidateDeletePermission 验证删除 TCP 转发配置的权限
  99. // 该函数确保用户只能删除属于自己主机的配置,防止越权操作
  100. func (s *aidedTcpService) ValidateDeletePermission(oldData *model.Tcpforwarding, hostId int) error {
  101. if oldData == nil {
  102. return fmt.Errorf("TCP转发配置数据不存在")
  103. }
  104. if oldData.HostId != hostId {
  105. return fmt.Errorf("用户权限不足,无法删除不属于自己主机的配置(主机ID: %d)", hostId)
  106. }
  107. return nil
  108. }
  109. // CreateCdnWebsite 在 CDN 系统中创建 TCP 代理网站
  110. // 该函数调用 CDN 服务接口创建一个新的 TCP 代理网站,返回网站ID用于后续配置
  111. func (s *aidedTcpService) CreateCdnWebsite(ctx context.Context, formData v1.WebsiteSend) (int64, error) {
  112. tcpId, err := s.cdn.CreateWebsite(ctx, formData)
  113. if err != nil {
  114. return 0, fmt.Errorf("在CDN系统中创建 TCP 代理网站失败: %w", err)
  115. }
  116. if tcpId <= 0 {
  117. return 0, fmt.Errorf("CDN系统返回了无效的网站ID: %d", tcpId)
  118. }
  119. return tcpId, nil
  120. }
  121. // UpdateCdnConfiguration 更新 CDN 系统中的 TCP 代理配置
  122. // 该函数根据变更内容智能更新:
  123. // 1. 端口变更 - 更新网站监听配置
  124. // 2. 名称变更 - 更新网站基本信息
  125. // 3. 代理协议变更 - 开启/关闭 Proxy Protocol
  126. func (s *aidedTcpService) UpdateCdnConfiguration(ctx context.Context, req *v1.TcpForwardingRequest, oldData *model.Tcpforwarding, require RequireResponse, formData v1.WebsiteSend) error {
  127. // 更新网站端口
  128. if oldData.Port != req.TcpForwardingData.Port {
  129. if err := s.cdn.EditServerType(ctx, v1.EditWebsite{
  130. Id: int64(oldData.CdnWebId),
  131. TypeJSON: formData.TcpJSON,
  132. }, "tcp"); err != nil {
  133. return fmt.Errorf("更新网站端口失败: %w", err)
  134. }
  135. }
  136. // 更新网站名称
  137. if oldData.Comment != req.TcpForwardingData.Comment {
  138. nodeId, err := s.globalRep.GetNodeId(ctx, oldData.CdnWebId)
  139. if err != nil {
  140. return fmt.Errorf("获取节点ID失败: %w", err)
  141. }
  142. if err := s.cdn.EditServerBasic(ctx, int64(oldData.CdnWebId), require.Tag, nodeId); err != nil {
  143. return fmt.Errorf("更新网站名称失败: %w", err)
  144. }
  145. }
  146. // 更新代理协议
  147. if oldData.Proxy != req.TcpForwardingData.Proxy {
  148. if err := s.proxy.EditProxy(ctx, int64(oldData.CdnWebId), v1.ProxyProtocolJSON{
  149. IsOn: req.TcpForwardingData.Proxy,
  150. Version: 1,
  151. }); err != nil {
  152. return fmt.Errorf("更新代理协议失败: %w", err)
  153. }
  154. }
  155. return nil
  156. }
  157. // DeleteCdnServer 从 CDN 系统中删除 TCP 代理网站
  158. // 该函数删除 CDN 中的网站配置,释放端口资源和相关配置
  159. func (s *aidedTcpService) DeleteCdnServer(ctx context.Context, cdnWebId int) error {
  160. if err := s.cdn.DelServer(ctx, int64(cdnWebId)); err != nil {
  161. return fmt.Errorf("删除CDN服务器失败: %w", err)
  162. }
  163. return nil
  164. }
  165. // AddOriginsToWebsite 为 TCP 代理网站添加后端源站
  166. // 该函数执行以下操作:
  167. // 1. 批量创建后端服务器的源站记录
  168. // 2. 将源站关联到 TCP 代理网站
  169. // 3. 返回后端地址与源站ID的映射关系
  170. func (s *aidedTcpService) AddOriginsToWebsite(ctx context.Context, req *v1.TcpForwardingRequest, tcpId int64) (map[string]int64, error) {
  171. cdnOriginIds := make(map[string]int64)
  172. // 批量创建源站
  173. for _, backend := range req.TcpForwardingData.BackendList {
  174. id, err := s.wafformatter.AddOrigin(ctx, v1.WebJson{
  175. ApiType: "tcp",
  176. BackendList: backend,
  177. Comment: req.TcpForwardingData.Comment,
  178. })
  179. if err != nil {
  180. return nil, fmt.Errorf("添加源站失败 %s: %w", backend, err)
  181. }
  182. cdnOriginIds[backend] = id
  183. }
  184. // 批量关联源站到网站
  185. for _, originId := range cdnOriginIds {
  186. if err := s.cdn.AddServerOrigin(ctx, tcpId, originId); err != nil {
  187. return nil, fmt.Errorf("关联源站到网站失败: %w", err)
  188. }
  189. }
  190. return cdnOriginIds, nil
  191. }
  192. // UpdateOriginServers 更新 TCP 代理的后端源站配置
  193. // 该函数智能对比新旧后端列表,只处理变更的部分:
  194. // 1. 添加新增的后端服务器
  195. // 2. 删除不再需要的后端服务器
  196. // 3. 更新源站ID映射关系
  197. func (s *aidedTcpService) UpdateOriginServers(ctx context.Context, req *v1.TcpForwardingRequest, oldData *model.Tcpforwarding, ipData *model.TcpForwardingRule) error {
  198. addOrigins, delOrigins := s.wafformatter.findIpDifferences(ipData.BackendList, req.TcpForwardingData.BackendList)
  199. // 添加新源站
  200. addedIds := make(map[string]int64)
  201. for _, origin := range addOrigins {
  202. id, err := s.wafformatter.AddOrigin(ctx, v1.WebJson{
  203. ApiType: "tcp",
  204. BackendList: origin,
  205. Comment: req.TcpForwardingData.Comment,
  206. })
  207. if err != nil {
  208. return fmt.Errorf("添加源站失败 %s: %w", origin, err)
  209. }
  210. addedIds[origin] = id
  211. }
  212. // 关联新源站到网站
  213. for _, originId := range addedIds {
  214. if err := s.cdn.AddServerOrigin(ctx, int64(oldData.CdnWebId), originId); err != nil {
  215. return fmt.Errorf("关联源站到网站失败: %w", err)
  216. }
  217. }
  218. // 更新源站ID映射
  219. maps.Copy(ipData.CdnOriginIds, addedIds)
  220. // 删除不需要的源站
  221. for backend, originId := range ipData.CdnOriginIds {
  222. for _, delOrigin := range delOrigins {
  223. if backend == delOrigin {
  224. if err := s.cdn.DelServerOrigin(ctx, int64(oldData.CdnWebId), originId); err != nil {
  225. return fmt.Errorf("删除源站失败: %w", err)
  226. }
  227. delete(ipData.CdnOriginIds, backend)
  228. break
  229. }
  230. }
  231. }
  232. return nil
  233. }
  234. // ConfigureProxyProtocol 配置 TCP 代理的 Proxy Protocol 协议
  235. // 该函数根据请求参数决定是否开启 Proxy Protocol,用于传递客户端真实IP
  236. func (s *aidedTcpService) ConfigureProxyProtocol(ctx context.Context, req *v1.TcpForwardingRequest, tcpId int64) error {
  237. // 如果不需要开启 Proxy Protocol,直接返回
  238. if !req.TcpForwardingData.Proxy {
  239. return nil
  240. }
  241. // 配置 Proxy Protocol v1
  242. if err := s.proxy.EditProxy(ctx, tcpId, v1.ProxyProtocolJSON{
  243. IsOn: true,
  244. Version: 1,
  245. }); err != nil {
  246. return fmt.Errorf("开启 Proxy Protocol 失败: %w", err)
  247. }
  248. return nil
  249. }
  250. // ProcessAsyncTasks 处理 TCP 转发相关的异步任务
  251. // 该函数主要处理新增后端服务器时的IP白名单添加任务,确保后端服务器能正常访问
  252. func (s *aidedTcpService) ProcessAsyncTasks(req *v1.TcpForwardingRequest) {
  253. // 检查是否有后端服务器需要处理
  254. if req == nil || len(req.TcpForwardingData.BackendList) == 0 {
  255. return
  256. }
  257. // 提取后端 IP 地址
  258. ips := s.ExtractIpsFromBackends(req.TcpForwardingData.BackendList)
  259. if len(ips) > 0 {
  260. // 异步添加到白名单
  261. go s.wafformatter.PublishIpWhitelistTask(ips, "add", "", "white")
  262. }
  263. }
  264. // ProcessIpWhitelistChanges 处理 TCP 转发编辑时的IP白名单变更
  265. // 该函数对比新旧后端列表,智能处理IP白名单:
  266. // 1. 将新增的后端 IP 加入白名单
  267. // 2. 将不再使用的后端 IP 从白名单移除
  268. func (s *aidedTcpService) ProcessIpWhitelistChanges(ctx context.Context, req *v1.TcpForwardingRequest, ipData *model.TcpForwardingRule) error {
  269. addedIps, removedIps, err := s.wafformatter.WashEditWafIp(ctx, req.TcpForwardingData.BackendList, ipData.BackendList)
  270. if err != nil {
  271. return fmt.Errorf("处理IP变更失败: %w", err)
  272. }
  273. // 处理新增IP
  274. if len(addedIps) > 0 {
  275. go s.wafformatter.PublishIpWhitelistTask(addedIps, "add", "", "white")
  276. }
  277. // 处理移除IP
  278. if len(removedIps) > 0 {
  279. ipsToDelist, err := s.wafformatter.WashDelIps(ctx, removedIps)
  280. if err != nil {
  281. return fmt.Errorf("处理移除IP失败: %w", err)
  282. }
  283. if len(ipsToDelist) > 0 {
  284. go s.wafformatter.PublishIpWhitelistTask(ipsToDelist, "del", "0", "white")
  285. }
  286. }
  287. return nil
  288. }
  289. // ProcessDeleteIpWhitelist 处理 TCP 转发删除时的IP白名单清理
  290. // 该函数在删除 TCP 转发配置时,智能清理不再需要的后端 IP 白名单条目
  291. func (s *aidedTcpService) ProcessDeleteIpWhitelist(ctx context.Context, id int) error {
  292. ipData, err := s.tcpRepository.GetTcpForwardingIpsByID(ctx, id)
  293. if err != nil {
  294. return fmt.Errorf("获取IP数据失败: %w", err)
  295. }
  296. if ipData == nil || len(ipData.BackendList) == 0 {
  297. return nil
  298. }
  299. ips, err := s.wafformatter.WashDeleteWafIp(ctx, ipData.BackendList)
  300. if err != nil {
  301. return fmt.Errorf("处理删除IP失败: %w", err)
  302. }
  303. if len(ips) > 0 {
  304. ipsToDelist, err := s.wafformatter.WashDelIps(ctx, ips)
  305. if err != nil {
  306. return fmt.Errorf("清理IP列表失败: %w", err)
  307. }
  308. if len(ipsToDelist) > 0 {
  309. go s.wafformatter.PublishIpWhitelistTask(ipsToDelist, "del", "0", "white")
  310. }
  311. }
  312. return nil
  313. }
  314. // ExtractIpsFromBackends 从后端服务器地址列表中提取纯IP地址
  315. // 该函数解析 "IP:端口" 格式的后端地址,提取出纯IP地址用于白名单处理
  316. func (s *aidedTcpService) ExtractIpsFromBackends(backends []string) []string {
  317. var ips []string
  318. for _, backend := range backends {
  319. // 跳过空字符串
  320. if backend == "" {
  321. continue
  322. }
  323. // 解析 "IP:端口" 格式
  324. if ip, _, err := net.SplitHostPort(backend); err == nil && ip != "" {
  325. ips = append(ips, ip)
  326. }
  327. }
  328. return ips
  329. }
  330. // PrepareWafData 准备WAF配置数据
  331. // 该函数的作用是根据请求参数准备创建CDN网站所需的所有配置数据,包括:
  332. // 1. 获取全局配置信息(用户、主机、网关等)
  333. // 2. 构建 TCP 代理的监听配置 JSON
  334. // 3. 组装 CDN 创建网站的表单数据
  335. func (s *aidedTcpService) PrepareWafData(ctx context.Context, req *v1.TcpForwardingRequest) (RequireResponse, v1.WebsiteSend, error) {
  336. // 获取全局配置信息,包括用户信息、网关IP等
  337. require, err := s.wafformatter.Require(ctx, v1.GlobalRequire{
  338. HostId: req.HostId,
  339. Uid: req.Uid,
  340. Comment: req.TcpForwardingData.Comment,
  341. })
  342. if err != nil {
  343. return RequireResponse{}, v1.WebsiteSend{}, fmt.Errorf("获取全局配置信息失败: %w", err)
  344. }
  345. // 验证实例配置是否完整
  346. if require.Uid == 0 {
  347. return RequireResponse{}, v1.WebsiteSend{}, fmt.Errorf("请先配置实例,确保用户信息和网关配置正确")
  348. }
  349. // 构建 TCP 监听配置
  350. tcpConfig, err := s.BuildTcpListenConfig(require.GatewayIps, req.TcpForwardingData.Port)
  351. if err != nil {
  352. return RequireResponse{}, v1.WebsiteSend{}, fmt.Errorf("构建 TCP 监听配置失败: %w", err)
  353. }
  354. // 组装 CDN 创建网站的表单数据
  355. formData := v1.WebsiteSend{
  356. UserId: int64(require.CdnUid),
  357. Type: "tcpProxy",
  358. Name: require.Tag,
  359. Description: req.TcpForwardingData.Comment,
  360. TcpJSON: tcpConfig,
  361. ServerGroupIds: []int64{int64(require.GroupId)},
  362. NodeClusterId: 2, // 默认节点集群ID
  363. }
  364. return require, formData, nil
  365. }
  366. // BuildTcpListenConfig 构建 TCP 监听配置 JSON
  367. // 该函数将网关IP列表和端口转换为 CDN 所需的 JSON 配置格式
  368. func (s *aidedTcpService) BuildTcpListenConfig(gatewayIps []string, portStr string) ([]byte, error) {
  369. if len(gatewayIps) == 0 {
  370. return nil, fmt.Errorf("网关IP列表不能为空")
  371. }
  372. // 验证端口字符串不为空
  373. if portStr == "" {
  374. return nil, fmt.Errorf("端口号不能为空")
  375. }
  376. // 转换端口字符串为整数并验证范围
  377. port, err := strconv.Atoi(portStr)
  378. if err != nil {
  379. return nil, fmt.Errorf("端口号格式不合法: %s", portStr)
  380. }
  381. if port <= 0 || port > 65535 {
  382. return nil, fmt.Errorf("端口号不合法: %d,必须在 1-65535 范围内", port)
  383. }
  384. // 构建监听配置
  385. var listenConfigs []v1.Listen
  386. for _, gatewayIp := range gatewayIps {
  387. if gatewayIp == "" {
  388. continue // 跳过空的IP
  389. }
  390. listenConfigs = append(listenConfigs, v1.Listen{
  391. Protocol: "tcp",
  392. Host: gatewayIp,
  393. Port: portStr,
  394. })
  395. }
  396. if len(listenConfigs) == 0 {
  397. return nil, fmt.Errorf("没有有效的网关IP配置")
  398. }
  399. // 组装最终的 JSON 配置
  400. tcpJSON := v1.TypeJSON{
  401. IsOn: true,
  402. Listen: listenConfigs,
  403. }
  404. byteData, err := json.Marshal(tcpJSON)
  405. if err != nil {
  406. return nil, fmt.Errorf("序列化 TCP 配置失败: %w", err)
  407. }
  408. return byteData, nil
  409. }
  410. // BuildTcpForwardingModel 构建 TCP 转发主记录模型
  411. // 该函数将请求数据转换为数据库模型,包含主机、CDN网站、端口等信息
  412. func (s *aidedTcpService) BuildTcpForwardingModel(req *v1.TcpForwardingDataRequest, ruleId int, require RequireResponse) *model.Tcpforwarding {
  413. return &model.Tcpforwarding{
  414. HostId: require.HostId,
  415. CdnWebId: ruleId,
  416. Port: req.Port,
  417. Comment: req.Comment,
  418. Proxy: req.Proxy,
  419. }
  420. }
  421. // BuildTcpRuleModel 构建 TCP 转发规则记录模型
  422. // 该函数构建包含后端服务器列表和 CDN 源站ID映射的规则记录
  423. func (s *aidedTcpService) BuildTcpRuleModel(reqData *v1.TcpForwardingDataRequest, require RequireResponse, localDbId int, cdnOriginIds map[string]int64) *model.TcpForwardingRule {
  424. return &model.TcpForwardingRule{
  425. Uid: require.Uid,
  426. HostId: require.HostId,
  427. TcpId: localDbId,
  428. CdnOriginIds: cdnOriginIds,
  429. BackendList: reqData.BackendList,
  430. }
  431. }
  432. // SaveToDatabase 保存 TCP 转发配置到数据库
  433. // 该函数分别保存主记录(基本信息)和规则记录(后端服务器和源站ID映射)
  434. func (s *aidedTcpService) SaveToDatabase(ctx context.Context, req *v1.TcpForwardingRequest, require RequireResponse, tcpId int64, cdnOriginIds map[string]int64) (int, error) {
  435. // 保存主记录
  436. tcpModel := s.BuildTcpForwardingModel(&req.TcpForwardingData, int(tcpId), require)
  437. id, err := s.tcpRepository.AddTcpforwarding(ctx, tcpModel)
  438. if err != nil {
  439. return 0, fmt.Errorf("保存TCP转发记录失败: %w", err)
  440. }
  441. // 保存规则记录
  442. tcpRuleModel := s.BuildTcpRuleModel(&req.TcpForwardingData, require, id, cdnOriginIds)
  443. if _, err := s.tcpRepository.AddTcpforwardingIps(ctx, *tcpRuleModel); err != nil {
  444. return 0, fmt.Errorf("保存TCP转发规则失败: %w", err)
  445. }
  446. return id, nil
  447. }
  448. // UpdateDatabaseRecords 更新数据库记录
  449. // 该函数更新 TCP 转发的主记录和规则记录,同步最新的配置变更
  450. func (s *aidedTcpService) UpdateDatabaseRecords(ctx context.Context, req *v1.TcpForwardingRequest, oldData *model.Tcpforwarding, require RequireResponse, ipData *model.TcpForwardingRule) error {
  451. // 更新主记录
  452. tcpModel := s.BuildTcpForwardingModel(&req.TcpForwardingData, oldData.CdnWebId, require)
  453. tcpModel.Id = req.TcpForwardingData.Id
  454. if err := s.tcpRepository.EditTcpforwarding(ctx, tcpModel); err != nil {
  455. return fmt.Errorf("更新TCP转发记录失败: %w", err)
  456. }
  457. // 更新规则记录
  458. tcpRuleModel := s.BuildTcpRuleModel(&req.TcpForwardingData, require, req.TcpForwardingData.Id, ipData.CdnOriginIds)
  459. if err := s.tcpRepository.EditTcpforwardingIps(ctx, *tcpRuleModel); err != nil {
  460. return fmt.Errorf("更新TCP转发规则失败: %w", err)
  461. }
  462. return nil
  463. }
  464. // CleanupDatabaseRecords 清理数据库记录
  465. // 该函数删除 TCP 转发相关的所有数据库记录,包括主记录和规则记录
  466. func (s *aidedTcpService) CleanupDatabaseRecords(ctx context.Context, id int) error {
  467. if err := s.tcpRepository.DeleteTcpforwarding(ctx, int64(id)); err != nil {
  468. return fmt.Errorf("删除TCP转发主记录失败: %w", err)
  469. }
  470. if err := s.tcpRepository.DeleteTcpForwardingIpsById(ctx, id); err != nil {
  471. return fmt.Errorf("删除TCP转发规则记录失败: %w", err)
  472. }
  473. return nil
  474. }