node_client_expiry_sync_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  5. )
  6. // TestMergeActivationExpiry covers the pure reconciliation rule in isolation.
  7. func TestMergeActivationExpiry(t *testing.T) {
  8. const (
  9. dur = int64(-2592000000) // 30 days as a "start after first connect" duration
  10. early = int64(1000) // earliest absolute deadline (first connection)
  11. late = int64(2000) // a later absolute deadline
  12. )
  13. cases := []struct {
  14. name string
  15. existing, node int64
  16. want int64
  17. }{
  18. {"master unset takes node duration", 0, dur, dur},
  19. {"master unset takes node activation", 0, early, early},
  20. {"activation adopted over stored duration", dur, early, early},
  21. {"node still un-activated does not reset deadline", early, dur, early},
  22. {"node un-activated zero does not reset deadline", early, 0, early},
  23. {"node renewal extends the deadline forward", early, late, late},
  24. {"node positive adopted even if earlier", late, early, early},
  25. {"both un-activated keep node value", dur, dur, dur},
  26. }
  27. for _, c := range cases {
  28. t.Run(c.name, func(t *testing.T) {
  29. if got := mergeActivationExpiry(c.existing, c.node); got != c.want {
  30. t.Fatalf("mergeActivationExpiry(%d,%d) = %d, want %d", c.existing, c.node, got, c.want)
  31. }
  32. })
  33. }
  34. }
  35. // TestNodeFirstConnectExpiry_NotClobbered reproduces the multi-node bug: a
  36. // client is attached to inbounds on two nodes with a "start after first connect"
  37. // expiry. The client connects only on node 1, which activates an absolute
  38. // deadline; node 2 never sees a connection and keeps reporting the negative
  39. // duration. The shared per-email client_traffics row must hold the activated
  40. // deadline — a later node-2 sync must not reset it back to "not started".
  41. func TestNodeFirstConnectExpiry_NotClobbered(t *testing.T) {
  42. db := initTrafficTestDB(t)
  43. createNodeInbound(t, db, 1, "n1-in", 41001)
  44. createNodeInbound(t, db, 2, "n2-in", 41002)
  45. svc := &InboundService{}
  46. const email = "delayed"
  47. const duration = int64(-2592000000) // 30 days, not yet started
  48. // Both nodes start out reporting the un-activated negative duration.
  49. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 0, Down: 0, ExpiryTime: duration, Enable: true})
  50. syncNode(t, svc, 2, "n2-in", xray.ClientTraffic{Email: email, Up: 0, Down: 0, ExpiryTime: duration, Enable: true})
  51. if got := readTraffic(t, db, email).ExpiryTime; got != duration {
  52. t.Fatalf("before any connection: expiry = %d, want %d", got, duration)
  53. }
  54. // Client connects on node 1: it activates an absolute deadline.
  55. const activated = int64(1893456000000) // some absolute ms timestamp
  56. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 100, Down: 100, ExpiryTime: activated, Enable: true})
  57. if got := readTraffic(t, db, email).ExpiryTime; got != activated {
  58. t.Fatalf("after node 1 activation: expiry = %d, want %d", got, activated)
  59. }
  60. // Node 2 (no connection there) keeps reporting the negative duration. This
  61. // must NOT reset the activated deadline.
  62. syncNode(t, svc, 2, "n2-in", xray.ClientTraffic{Email: email, Up: 0, Down: 0, ExpiryTime: duration, Enable: true})
  63. if got := readTraffic(t, db, email).ExpiryTime; got != activated {
  64. t.Fatalf("node 2 clobbered the activated deadline: expiry = %d, want %d", got, activated)
  65. }
  66. // Subsequent node 1 syncs keep the same absolute deadline.
  67. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 200, Down: 200, ExpiryTime: activated, Enable: true})
  68. if got := readTraffic(t, db, email).ExpiryTime; got != activated {
  69. t.Fatalf("after further node 1 sync: expiry = %d, want %d", got, activated)
  70. }
  71. }
  72. // TestNodeFirstConnectExpiry_NotClobbered_WithSettings exercises the full
  73. // production sync path — snapshots carrying real settings JSON, which drives the
  74. // GetClients/SyncInbound branch inside setRemoteTrafficLocked — to prove that
  75. // branch does not re-derive the per-email client_traffics.expiry_time from the
  76. // node's (still negative) settings and undo the merge guard.
  77. func TestNodeFirstConnectExpiry_NotClobbered_WithSettings(t *testing.T) {
  78. db := initTrafficTestDB(t)
  79. createNodeInboundWithClient(t, db, 1, "n1-in", 41001, "delayed")
  80. createNodeInboundWithClient(t, db, 2, "n2-in", 41002, "delayed")
  81. svc := &InboundService{}
  82. const email = "delayed"
  83. const duration = int64(-2592000000)
  84. const activated = int64(1893456000000)
  85. negSettings := `{"clients":[{"email":"delayed","enable":true,"expiryTime":-2592000000}]}`
  86. actSettings := `{"clients":[{"email":"delayed","enable":true,"expiryTime":1893456000000}]}`
  87. // Both nodes start un-activated.
  88. syncNodeWithSettings(t, svc, 1, "n1-in", negSettings, xray.ClientTraffic{Email: email, ExpiryTime: duration, Enable: true})
  89. syncNodeWithSettings(t, svc, 2, "n2-in", negSettings, xray.ClientTraffic{Email: email, ExpiryTime: duration, Enable: true})
  90. // Node 1 activates (both its ClientStats and its settings now carry the
  91. // absolute deadline, like a real node after adjustTraffics).
  92. syncNodeWithSettings(t, svc, 1, "n1-in", actSettings, xray.ClientTraffic{Email: email, Up: 100, Down: 100, ExpiryTime: activated, Enable: true})
  93. if got := readTraffic(t, db, email).ExpiryTime; got != activated {
  94. t.Fatalf("after node 1 activation: expiry = %d, want %d", got, activated)
  95. }
  96. // Node 2 still reports the negative duration in BOTH ClientStats and
  97. // settings. Neither the merge nor SyncInbound may reset the deadline.
  98. syncNodeWithSettings(t, svc, 2, "n2-in", negSettings, xray.ClientTraffic{Email: email, ExpiryTime: duration, Enable: true})
  99. if got := readTraffic(t, db, email).ExpiryTime; got != activated {
  100. t.Fatalf("node 2 settings-sync clobbered the deadline: expiry = %d, want %d", got, activated)
  101. }
  102. }
  103. // TestNodeRenewExtendsExpiry guards against over-correcting: a node that renews
  104. // a client (traffic reset / auto-renew) legitimately moves the deadline FORWARD
  105. // to a later absolute timestamp, and that must still propagate to the master.
  106. // The guard only rejects un-activated (<= 0) values, never a positive one.
  107. func TestNodeRenewExtendsExpiry(t *testing.T) {
  108. db := initTrafficTestDB(t)
  109. createNodeInbound(t, db, 1, "n1-in", 41001)
  110. svc := &InboundService{}
  111. const email = "renewing"
  112. const first = int64(1893456000000)
  113. const renewed = first + int64(2592000000) // +30 days after auto-renew
  114. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 10, Down: 10, ExpiryTime: first, Enable: true})
  115. if got := readTraffic(t, db, email).ExpiryTime; got != first {
  116. t.Fatalf("after activation: expiry = %d, want %d", got, first)
  117. }
  118. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 20, Down: 20, ExpiryTime: renewed, Enable: true})
  119. if got := readTraffic(t, db, email).ExpiryTime; got != renewed {
  120. t.Fatalf("node renewal did not propagate: expiry = %d, want %d", got, renewed)
  121. }
  122. }