inbound_traffic_apply.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. "gorm.io/gorm"
  9. )
  10. type trafficLocalApplyAction uint8
  11. const (
  12. trafficAddUser trafficLocalApplyAction = iota + 1
  13. trafficRemoveUser
  14. trafficDisableInbound
  15. )
  16. type trafficLocalApplyPlan struct {
  17. action trafficLocalApplyAction
  18. inbound model.Inbound
  19. client map[string]any
  20. email string
  21. }
  22. type trafficMutationBatch struct {
  23. localPlans []trafficLocalApplyPlan
  24. remotePlans []trafficInboundUpdatePlan
  25. nodeIDs map[int]struct{}
  26. }
  27. type trafficInboundUpdatePlan struct{ oldInbound, newInbound model.Inbound }
  28. func newTrafficMutationBatch() *trafficMutationBatch {
  29. return &trafficMutationBatch{nodeIDs: make(map[int]struct{})}
  30. }
  31. func (b *trafficMutationBatch) addNode(nodeID int) {
  32. if nodeID > 0 {
  33. b.nodeIDs[nodeID] = struct{}{}
  34. }
  35. }
  36. func (b *trafficMutationBatch) markNodesTx(tx *gorm.DB) error {
  37. if b == nil {
  38. return nil
  39. }
  40. nodeSvc := NodeService{}
  41. for nodeID := range b.nodeIDs {
  42. if err := nodeSvc.MarkNodeDirtyTx(tx, nodeID); err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. }
  48. // applyTrafficRemotePlans is bounded like every per-client node push: the nodes are
  49. // already dirty, so an offline or slow one defers to the reconcile.
  50. func (s *InboundService) applyTrafficRemotePlans(plans []trafficInboundUpdatePlan) bool {
  51. ids := make([]int, len(plans))
  52. for i := range plans {
  53. ids[i] = plans[i].newInbound.Id
  54. }
  55. failed, panics := fanoutInboundResults(ids, inboundFanoutConcurrency, func(i int) bool {
  56. rt, push, _, err := s.nodePushPlan(&plans[i].newInbound)
  57. if err == nil && push {
  58. ctx, cancel := nodePushContext()
  59. err = rt.UpdateInbound(ctx, &plans[i].oldInbound, &plans[i].newInbound)
  60. cancel()
  61. }
  62. if err != nil {
  63. logger.Debug("traffic post-commit remote apply failed:", err)
  64. }
  65. return err != nil
  66. })
  67. needRestart := false
  68. for i := range failed {
  69. needRestart = needRestart || failed[i] || panics[i] != nil
  70. }
  71. return needRestart
  72. }
  73. func (s *InboundService) applyTrafficMutationBatch(b *trafficMutationBatch) bool {
  74. if b == nil {
  75. return false
  76. }
  77. needRestart := false
  78. for i := range b.localPlans {
  79. plan := &b.localPlans[i]
  80. if plan.inbound.Protocol == model.MTProto {
  81. s.applyLocalMtproto(plan.inbound.Id)
  82. continue
  83. }
  84. if plan.inbound.Protocol == model.AmneziaWG {
  85. s.applyLocalAmneziaWG(plan.inbound.Id)
  86. continue
  87. }
  88. if plan.inbound.Protocol == model.TUIC {
  89. s.applyLocalTuic(plan.inbound.Id)
  90. continue
  91. }
  92. rt, err := s.runtimeFor(&plan.inbound)
  93. if err == nil {
  94. switch plan.action {
  95. case trafficAddUser:
  96. err = rt.AddUser(context.Background(), &plan.inbound, plan.client)
  97. case trafficRemoveUser:
  98. err = rt.RemoveUser(context.Background(), &plan.inbound, plan.email)
  99. if err != nil && strings.Contains(err.Error(), "not found") {
  100. err = nil
  101. }
  102. case trafficDisableInbound:
  103. err = rt.DelInbound(context.Background(), &plan.inbound)
  104. if xray.IsMissingHandlerErr(err) {
  105. err = nil
  106. }
  107. }
  108. }
  109. if err != nil {
  110. logger.Debug("traffic post-commit runtime apply failed:", err)
  111. needRestart = true
  112. }
  113. }
  114. return needRestart
  115. }