hot_diff_drops_users_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package xray
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  5. )
  6. func hotConfigWithClients(clients string) *Config {
  7. cfg := makeHotConfig()
  8. for i := range cfg.InboundConfigs {
  9. if cfg.InboundConfigs[i].Tag == "inbound-1080" {
  10. cfg.InboundConfigs[i].Settings = json_util.RawMessage(`{"clients":` + clients + `}`)
  11. }
  12. }
  13. return cfg
  14. }
  15. // diffInboundUsers refuses shadowsocks and hysteria, so their dropped clients
  16. // reach the guard through the inbound instead of through a per-user op.
  17. func TestHotDiffDropsUsersOnProtocolsItCannotDiff(t *testing.T) {
  18. for _, protocol := range []string{"shadowsocks", "hysteria"} {
  19. t.Run(protocol, func(t *testing.T) {
  20. withClients := func(clients string) *Config {
  21. cfg := makeHotConfig()
  22. ib := &cfg.InboundConfigs[1]
  23. ib.Protocol = protocol
  24. ib.Settings = json_util.RawMessage(`{"clients":` + clients + `}`)
  25. return cfg
  26. }
  27. both := `[{"email":"a@x","password":"pa"},{"email":"b@x","password":"pb"}]`
  28. onlyA := `[{"email":"a@x","password":"pa"}]`
  29. diff, ok := ComputeHotDiff(withClients(both), withClients(onlyA))
  30. if !ok {
  31. t.Fatal("a dropped client must stay API-applicable")
  32. }
  33. if !diff.DropsUsers() {
  34. t.Fatalf("DropsUsers = false for a dropped %s client (removed=%+v added=%+v dropped=%+v)",
  35. protocol, diff.RemovedUsers, diff.AddedUsers, diff.DroppedClients)
  36. }
  37. edited, ok := ComputeHotDiff(withClients(both), withClients(`[{"email":"a@x","password":"pa"},{"email":"b@x","password":"pb","level":1}]`))
  38. if !ok {
  39. t.Fatal("a client edit must stay API-applicable")
  40. }
  41. if edited.DropsUsers() {
  42. t.Fatal("an edited client is still served")
  43. }
  44. })
  45. }
  46. }
  47. // A disable or a delete takes the client out of the generated config; an edit
  48. // keeps the email and re-adds it. Only the first leaves sessions running.
  49. func TestHotDiffDropsUsers(t *testing.T) {
  50. cases := []struct {
  51. name string
  52. old string
  53. new string
  54. want bool
  55. }{
  56. {
  57. "client taken out of the config",
  58. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
  59. `[]`,
  60. true,
  61. },
  62. {
  63. "edited in place",
  64. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
  65. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","limitIp":5,"enable":true}]`,
  66. false,
  67. },
  68. {
  69. "added",
  70. `[]`,
  71. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
  72. false,
  73. },
  74. {
  75. "renamed",
  76. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
  77. `[{"email":"b@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
  78. true,
  79. },
  80. {
  81. "one dropped, one edited",
  82. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true},{"email":"b@x","id":"22222222-2222-2222-2222-222222222222","enable":true}]`,
  83. `[{"email":"b@x","id":"22222222-2222-2222-2222-222222222222","limitIp":5,"enable":true}]`,
  84. true,
  85. },
  86. {
  87. "unchanged",
  88. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
  89. `[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
  90. false,
  91. },
  92. }
  93. for _, tc := range cases {
  94. t.Run(tc.name, func(t *testing.T) {
  95. diff, ok := ComputeHotDiff(hotConfigWithClients(tc.old), hotConfigWithClients(tc.new))
  96. if !ok {
  97. t.Fatalf("diff of %s -> %s must be API-applicable", tc.old, tc.new)
  98. }
  99. if got := diff.DropsUsers(); got != tc.want {
  100. t.Fatalf("DropsUsers = %v, want %v (removed=%+v added=%+v)",
  101. got, tc.want, diff.RemovedUsers, diff.AddedUsers)
  102. }
  103. })
  104. }
  105. }