1
0

client_bulk_reenable_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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_AppliedFieldReachesTrafficRowDespiteOtherFieldSkip(t *testing.T) {
  169. setupBulkDB(t)
  170. svc := &ClientService{}
  171. inboundSvc := &InboundService{}
  172. email := "mix2@x"
  173. c := model.Client{Email: email, ID: "11111111-1111-1111-1111-111111111111", SubID: email, Enable: true, TotalGB: 100, ExpiryTime: 0}
  174. ib := mkInbound(t, 52021, model.VLESS, clientsSettings(t, []model.Client{c}))
  175. if err := svc.SyncInbound(nil, ib.Id, []model.Client{c}); err != nil {
  176. t.Fatalf("seed linkage: %v", err)
  177. }
  178. mkTraffic(t, ib.Id, email, 0, 0, 100, 0, true)
  179. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 30, 50, ""); err != nil {
  180. t.Fatalf("BulkAdjust: %v", err)
  181. }
  182. if got := trafficOf(t, email).Total; got != 150 {
  183. t.Fatalf("client_traffics.total = %d, want 150: the applied quota adjustment must reach the enforcement row even though the unlimited-expiry field was skipped", got)
  184. }
  185. }
  186. func TestBulkAdjust_OverQuota_DaysOnly_StaysDisabled(t *testing.T) {
  187. setupBulkDB(t)
  188. svc := &ClientService{}
  189. inboundSvc := &InboundService{}
  190. now := time.Now().UnixMilli()
  191. email := "qd@x"
  192. ib := seedLocalDisabledClient(t, svc, 52005, "", email, 100, now-reenableDay, 60, 40)
  193. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 60, 0, ""); err != nil {
  194. t.Fatalf("BulkAdjust: %v", err)
  195. }
  196. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, false)
  197. }
  198. func TestBulkAdjust_NegativeReduction_DoesNotFlipEnable(t *testing.T) {
  199. setupBulkDB(t)
  200. svc := &ClientService{}
  201. inboundSvc := &InboundService{}
  202. now := time.Now().UnixMilli()
  203. email := "neg@x"
  204. c := model.Client{Email: email, ID: "11111111-1111-1111-1111-111111111111", SubID: email, Enable: true, ExpiryTime: now + 5*reenableDay}
  205. ib := mkInbound(t, 52006, model.VLESS, clientsSettings(t, []model.Client{c}))
  206. if err := svc.SyncInbound(nil, ib.Id, []model.Client{c}); err != nil {
  207. t.Fatalf("seed linkage: %v", err)
  208. }
  209. mkTraffic(t, ib.Id, email, 0, 0, 0, now+5*reenableDay, true)
  210. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, -10, 0, ""); err != nil {
  211. t.Fatalf("BulkAdjust: %v", err)
  212. }
  213. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, true)
  214. }
  215. func TestBulkAdjust_FlowOnly_NoEnableChange(t *testing.T) {
  216. setupBulkDB(t)
  217. svc := &ClientService{}
  218. inboundSvc := &InboundService{}
  219. now := time.Now().UnixMilli()
  220. email := "flow@x"
  221. ib := seedLocalDisabledClient(t, svc, 52007, realityStream, email, 0, now-reenableDay, 0, 0)
  222. if _, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 0, 0, "xtls-rprx-vision-udp443"); err != nil {
  223. t.Fatalf("BulkAdjust: %v", err)
  224. }
  225. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, false)
  226. if got := flowOf(t, svc, email); got != "xtls-rprx-vision-udp443" {
  227. t.Fatalf("%s: flow = %q, want xtls-rprx-vision-udp443", email, got)
  228. }
  229. }
  230. func TestBulkAdjust_UnlimitedExpiry_QuotaCleared_Reenables(t *testing.T) {
  231. setupBulkDB(t)
  232. svc := &ClientService{}
  233. inboundSvc := &InboundService{}
  234. email := "u@x"
  235. ib := seedLocalDisabledClient(t, svc, 52008, "", email, 100, 0, 100, 0)
  236. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 0, 200, "")
  237. if err != nil {
  238. t.Fatalf("BulkAdjust: %v", err)
  239. }
  240. if res.Adjusted != 1 {
  241. t.Fatalf("expected 1 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  242. }
  243. assertEnableEverywhere(t, svc, inboundSvc, ib.Id, email, true)
  244. if got := trafficOf(t, email).Total; got != 300 {
  245. t.Fatalf("%s: total = %d, want 300", email, got)
  246. }
  247. }
  248. func TestBulkAdjust_NodeInbound_ReenablesDBLocations(t *testing.T) {
  249. setupBulkDB(t)
  250. svc := &ClientService{}
  251. inboundSvc := &InboundService{}
  252. node := &model.Node{Name: "n5619", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Status: "offline"}
  253. if err := database.GetDB().Create(node).Error; err != nil {
  254. t.Fatalf("create node: %v", err)
  255. }
  256. now := time.Now().UnixMilli()
  257. email := "node@x"
  258. c := model.Client{Email: email, ID: "11111111-1111-1111-1111-111111111111", SubID: email, Enable: false, ExpiryTime: now - reenableDay}
  259. ib := &model.Inbound{
  260. Tag: "node-in-5619",
  261. Enable: true,
  262. Port: 52900,
  263. Protocol: model.VLESS,
  264. Settings: clientsSettings(t, []model.Client{c}),
  265. NodeID: &node.Id,
  266. }
  267. if err := database.GetDB().Create(ib).Error; err != nil {
  268. t.Fatalf("create node inbound: %v", err)
  269. }
  270. if err := svc.SyncInbound(nil, ib.Id, []model.Client{c}); err != nil {
  271. t.Fatalf("seed linkage: %v", err)
  272. }
  273. mkTraffic(t, ib.Id, email, 0, 0, 0, now-reenableDay, false)
  274. forceRecordDisabled(t, svc, email)
  275. res, _, err := svc.BulkAdjust(inboundSvc, []string{email}, 30, 0, "")
  276. if err != nil {
  277. t.Fatalf("BulkAdjust: %v", err)
  278. }
  279. if res.Adjusted != 1 {
  280. t.Fatalf("expected 1 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  281. }
  282. if got := trafficOf(t, email).Enable; !got {
  283. t.Fatalf("%s: client_traffics.enable = false, want true", email)
  284. }
  285. if got := recordEnableOf(t, svc, email); !got {
  286. t.Fatalf("%s: client_records.enable = false, want true", email)
  287. }
  288. if got := jsonClientEnable(t, inboundSvc, ib.Id, email); !got {
  289. t.Fatalf("%s: inbound JSON enable = false, want true", email)
  290. }
  291. }