1
0

client_bulk_reenable_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. const reenableDay = int64(24 * 60 * 60 * 1000)
  9. func recordEnableOf(t *testing.T, svc *ClientService, email string) bool {
  10. t.Helper()
  11. rec, err := svc.GetRecordByEmail(nil, email)
  12. if err != nil {
  13. t.Fatalf("GetRecordByEmail(%q): %v", email, err)
  14. }
  15. return rec.Enable
  16. }
  17. func forceRecordDisabled(t *testing.T, svc *ClientService, email string) {
  18. t.Helper()
  19. if err := database.GetDB().Model(&model.ClientRecord{}).
  20. Where("email = ?", email).
  21. UpdateColumn("enable", false).Error; err != nil {
  22. t.Fatalf("force record disabled %q: %v", email, err)
  23. }
  24. if recordEnableOf(t, svc, email) {
  25. t.Fatalf("setup: record %q should start disabled", email)
  26. }
  27. }
  28. func jsonClientEnable(t *testing.T, inboundSvc *InboundService, inboundId int, email string) bool {
  29. t.Helper()
  30. ib, err := inboundSvc.GetInbound(inboundId)
  31. if err != nil {
  32. t.Fatalf("GetInbound(%d): %v", inboundId, err)
  33. }
  34. clients, err := inboundSvc.GetClients(ib)
  35. if err != nil {
  36. t.Fatalf("GetClients(%d): %v", inboundId, err)
  37. }
  38. for _, c := range clients {
  39. if c.Email == email {
  40. return c.Enable
  41. }
  42. }
  43. t.Fatalf("client %q not found in inbound %d settings JSON", email, inboundId)
  44. return false
  45. }
  46. func assertEnableEverywhere(t *testing.T, svc *ClientService, inboundSvc *InboundService, inboundId int, email string, want bool) {
  47. t.Helper()
  48. if got := trafficOf(t, email).Enable; got != want {
  49. t.Fatalf("%s: client_traffics.enable = %v, want %v", email, got, want)
  50. }
  51. if got := recordEnableOf(t, svc, email); got != want {
  52. t.Fatalf("%s: client_records.enable = %v, want %v", email, got, want)
  53. }
  54. if got := jsonClientEnable(t, inboundSvc, inboundId, email); got != want {
  55. t.Fatalf("%s: inbound JSON enable = %v, want %v", email, got, want)
  56. }
  57. }
  58. func seedLocalDisabledClient(t *testing.T, svc *ClientService, port int, stream, email string, total, expiry, up, down int64) *model.Inbound {
  59. t.Helper()
  60. c := model.Client{
  61. Email: email,
  62. ID: "11111111-1111-1111-1111-111111111111",
  63. SubID: email,
  64. Enable: false,
  65. TotalGB: total,
  66. ExpiryTime: expiry,
  67. }
  68. var ib *model.Inbound
  69. if stream == "" {
  70. ib = mkInbound(t, port, model.VLESS, clientsSettings(t, []model.Client{c}))
  71. } else {
  72. ib = mkInboundStream(t, port, model.VLESS, clientsSettings(t, []model.Client{c}), stream)
  73. }
  74. if err := svc.SyncInbound(nil, ib.Id, []model.Client{c}); err != nil {
  75. t.Fatalf("seed linkage: %v", err)
  76. }
  77. mkTraffic(t, ib.Id, email, up, down, total, expiry, false)
  78. forceRecordDisabled(t, svc, email)
  79. return ib
  80. }
  81. func TestBulkAdjust_ReenablesExpiredThenExtended_AllThreeLocations(t *testing.T) {
  82. setupBulkDB(t)
  83. svc := &ClientService{}
  84. inboundSvc := &InboundService{}
  85. now := time.Now().UnixMilli()
  86. email := "exp@x"
  87. ib := seedLocalDisabledClient(t, svc, 52001, "", email, 0, now-reenableDay, 0, 0)
  88. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 30, 0, "")
  89. if err != nil {
  90. t.Fatalf("BulkAdjust: %v", err)
  91. }
  92. if res.Adjusted != 1 {
  93. t.Fatalf("expected 1 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  94. }
  95. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, true)
  96. if got := trafficOf(t, email).ExpiryTime; got != now-reenableDay+30*reenableDay {
  97. t.Fatalf("%s: expiry = %d, want %d", email, got, now-reenableDay+30*reenableDay)
  98. }
  99. }
  100. func TestBulkAdjust_DoesNotReenable_ManuallyDisabledNotDepleted(t *testing.T) {
  101. setupBulkDB(t)
  102. svc := &ClientService{}
  103. inboundSvc := &InboundService{}
  104. now := time.Now().UnixMilli()
  105. email := "man@x"
  106. ib := seedLocalDisabledClient(t, svc, 52002, "", email, 0, now+30*reenableDay, 0, 0)
  107. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 30, 0, "")
  108. if err != nil {
  109. t.Fatalf("BulkAdjust: %v", err)
  110. }
  111. if res.Adjusted != 1 {
  112. t.Fatalf("expected 1 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  113. }
  114. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, false)
  115. }
  116. func TestBulkAdjust_StaysDisabled_ExtensionTooSmall(t *testing.T) {
  117. setupBulkDB(t)
  118. svc := &ClientService{}
  119. inboundSvc := &InboundService{}
  120. now := time.Now().UnixMilli()
  121. email := "sml@x"
  122. ib := seedLocalDisabledClient(t, svc, 52003, "", email, 0, now-10*reenableDay, 0, 0)
  123. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 5, 0, ""); err != nil {
  124. t.Fatalf("BulkAdjust: %v", err)
  125. }
  126. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, false)
  127. }
  128. func TestBulkAdjust_ReenablesOverQuota_WhenAddBytesClearsQuota(t *testing.T) {
  129. setupBulkDB(t)
  130. svc := &ClientService{}
  131. inboundSvc := &InboundService{}
  132. email := "q@x"
  133. ib := seedLocalDisabledClient(t, svc, 52004, "", email, 100, 0, 60, 40)
  134. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 0, 200, "")
  135. if err != nil {
  136. t.Fatalf("BulkAdjust: %v", err)
  137. }
  138. if res.Adjusted != 1 {
  139. t.Fatalf("expected 1 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  140. }
  141. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, true)
  142. if got := trafficOf(t, email).Total; got != 300 {
  143. t.Fatalf("%s: total = %d, want 300", email, got)
  144. }
  145. }
  146. func TestBulkAdjust_QuotaReductionBelowZeroSkipsInsteadOfUnlimited(t *testing.T) {
  147. setupBulkDB(t)
  148. svc := &ClientService{}
  149. inboundSvc := &InboundService{}
  150. email := "qr@x"
  151. c := model.Client{Email: email, ID: "11111111-1111-1111-1111-111111111111", SubID: email, Enable: true, TotalGB: 10}
  152. ib := mkInbound(t, 52020, model.VLESS, clientsSettings(t, []model.Client{c}))
  153. if err := svc.SyncInbound(nil, ib.Id, []model.Client{c}); err != nil {
  154. t.Fatalf("seed linkage: %v", err)
  155. }
  156. mkTraffic(t, ib.Id, email, 0, 0, 10, 0, true)
  157. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 0, -20, "")
  158. if err != nil {
  159. t.Fatalf("BulkAdjust: %v", err)
  160. }
  161. if res.Adjusted != 0 {
  162. t.Fatalf("over-reduction should not adjust, got Adjusted=%d skipped=%v", res.Adjusted, res.Skipped)
  163. }
  164. if got := trafficOf(t, email).Total; got != 10 {
  165. t.Fatalf("quota reduced past zero was written as %d (0 means unlimited); want it left at 10", got)
  166. }
  167. }
  168. func TestBulkAdjust_OverQuota_DaysOnly_StaysDisabled(t *testing.T) {
  169. setupBulkDB(t)
  170. svc := &ClientService{}
  171. inboundSvc := &InboundService{}
  172. now := time.Now().UnixMilli()
  173. email := "qd@x"
  174. ib := seedLocalDisabledClient(t, svc, 52005, "", email, 100, now-reenableDay, 60, 40)
  175. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 60, 0, ""); err != nil {
  176. t.Fatalf("BulkAdjust: %v", err)
  177. }
  178. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, false)
  179. }
  180. func TestBulkAdjust_NegativeReduction_DoesNotFlipEnable(t *testing.T) {
  181. setupBulkDB(t)
  182. svc := &ClientService{}
  183. inboundSvc := &InboundService{}
  184. now := time.Now().UnixMilli()
  185. email := "neg@x"
  186. c := model.Client{Email: email, ID: "11111111-1111-1111-1111-111111111111", SubID: email, Enable: true, ExpiryTime: now + 5*reenableDay}
  187. ib := mkInbound(t, 52006, model.VLESS, clientsSettings(t, []model.Client{c}))
  188. if err := svc.SyncInbound(nil, ib.Id, []model.Client{c}); err != nil {
  189. t.Fatalf("seed linkage: %v", err)
  190. }
  191. mkTraffic(t, ib.Id, email, 0, 0, 0, now+5*reenableDay, true)
  192. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, -10, 0, ""); err != nil {
  193. t.Fatalf("BulkAdjust: %v", err)
  194. }
  195. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, true)
  196. }
  197. func TestBulkAdjust_FlowOnly_NoEnableChange(t *testing.T) {
  198. setupBulkDB(t)
  199. svc := &ClientService{}
  200. inboundSvc := &InboundService{}
  201. now := time.Now().UnixMilli()
  202. email := "flow@x"
  203. ib := seedLocalDisabledClient(t, svc, 52007, realityStream, email, 0, now-reenableDay, 0, 0)
  204. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 0, 0, "xtls-rprx-vision-udp443"); err != nil {
  205. t.Fatalf("BulkAdjust: %v", err)
  206. }
  207. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, false)
  208. if got := flowOf(t, svc, email); got != "xtls-rprx-vision-udp443" {
  209. t.Fatalf("%s: flow = %q, want xtls-rprx-vision-udp443", email, got)
  210. }
  211. }
  212. func TestBulkAdjust_UnlimitedExpiry_QuotaCleared_Reenables(t *testing.T) {
  213. setupBulkDB(t)
  214. svc := &ClientService{}
  215. inboundSvc := &InboundService{}
  216. email := "u@x"
  217. ib := seedLocalDisabledClient(t, svc, 52008, "", email, 100, 0, 100, 0)
  218. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 0, 200, "")
  219. if err != nil {
  220. t.Fatalf("BulkAdjust: %v", err)
  221. }
  222. if res.Adjusted != 1 {
  223. t.Fatalf("expected 1 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  224. }
  225. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, true)
  226. if got := trafficOf(t, email).Total; got != 300 {
  227. t.Fatalf("%s: total = %d, want 300", email, got)
  228. }
  229. }
  230. func TestBulkAdjust_NodeInbound_ReenablesDBLocations(t *testing.T) {
  231. setupBulkDB(t)
  232. svc := &ClientService{}
  233. inboundSvc := &InboundService{}
  234. node := &model.Node{Name: "n5619", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Status: "offline"}
  235. if err := database.GetDB().Create(node).Error; err != nil {
  236. t.Fatalf("create node: %v", err)
  237. }
  238. now := time.Now().UnixMilli()
  239. email := "node@x"
  240. c := model.Client{Email: email, ID: "11111111-1111-1111-1111-111111111111", SubID: email, Enable: false, ExpiryTime: now - reenableDay}
  241. ib := &model.Inbound{
  242. Tag: "node-in-5619",
  243. Enable: true,
  244. Port: 52900,
  245. Protocol: model.VLESS,
  246. Settings: clientsSettings(t, []model.Client{c}),
  247. NodeID: &node.Id,
  248. }
  249. if err := database.GetDB().Create(ib).Error; err != nil {
  250. t.Fatalf("create node inbound: %v", err)
  251. }
  252. if err := svc.SyncInbound(nil, ib.Id, []model.Client{c}); err != nil {
  253. t.Fatalf("seed linkage: %v", err)
  254. }
  255. mkTraffic(t, ib.Id, email, 0, 0, 0, now-reenableDay, false)
  256. forceRecordDisabled(t, svc, email)
  257. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 30, 0, "")
  258. if err != nil {
  259. t.Fatalf("BulkAdjust: %v", err)
  260. }
  261. if res.Adjusted != 1 {
  262. t.Fatalf("expected 1 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  263. }
  264. if got := trafficOf(t, email).Enable; !got {
  265. t.Fatalf("%s: client_traffics.enable = false, want true", email)
  266. }
  267. if got := recordEnableOf(t, svc, email); !got {
  268. t.Fatalf("%s: client_records.enable = false, want true", email)
  269. }
  270. if got := jsonClientEnable(t, inboundSvc, ib.Id, email); !got {
  271. t.Fatalf("%s: inbound JSON enable = false, want true", email)
  272. }
  273. }