inbound_traffic_apply.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. func (s *InboundService) applyTrafficMutationBatch(b *trafficMutationBatch) bool {
  49. if b == nil {
  50. return false
  51. }
  52. needRestart := false
  53. for i := range b.remotePlans {
  54. plan := &b.remotePlans[i]
  55. rt, err := s.runtimeFor(&plan.newInbound)
  56. if err == nil {
  57. err = rt.UpdateInbound(context.Background(), &plan.oldInbound, &plan.newInbound)
  58. }
  59. if err != nil {
  60. logger.Debug("traffic post-commit remote apply failed:", err)
  61. needRestart = true
  62. }
  63. }
  64. for i := range b.localPlans {
  65. plan := &b.localPlans[i]
  66. if plan.inbound.Protocol == model.MTProto {
  67. s.applyLocalMtproto(plan.inbound.Id)
  68. continue
  69. }
  70. rt, err := s.runtimeFor(&plan.inbound)
  71. if err == nil {
  72. switch plan.action {
  73. case trafficAddUser:
  74. err = rt.AddUser(context.Background(), &plan.inbound, plan.client)
  75. case trafficRemoveUser:
  76. err = rt.RemoveUser(context.Background(), &plan.inbound, plan.email)
  77. if err != nil && strings.Contains(err.Error(), "not found") {
  78. err = nil
  79. }
  80. case trafficDisableInbound:
  81. err = rt.DelInbound(context.Background(), &plan.inbound)
  82. if xray.IsMissingHandlerErr(err) {
  83. err = nil
  84. }
  85. }
  86. }
  87. if err != nil {
  88. logger.Debug("traffic post-commit runtime apply failed:", err)
  89. needRestart = true
  90. }
  91. }
  92. return needRestart
  93. }