1
0

node_push_payload_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. )
  9. func payloadClientEmails(t *testing.T, settings string) []string {
  10. t.Helper()
  11. var parsed map[string]any
  12. if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
  13. t.Fatalf("settings not valid json: %v", err)
  14. }
  15. raw, _ := parsed["clients"].([]any)
  16. out := make([]string, 0, len(raw))
  17. for _, c := range raw {
  18. cm, ok := c.(map[string]any)
  19. if !ok {
  20. continue
  21. }
  22. if email, _ := cm["email"].(string); email != "" {
  23. out = append(out, email)
  24. }
  25. }
  26. return out
  27. }
  28. // Stripping disabled clients from a node push made the node delete the row and
  29. // the master mirror that back: every depleted client destroyed, not disabled.
  30. func TestNodePushKeepsDisabledClientsLocalPushStripsThem(t *testing.T) {
  31. setupBulkDB(t)
  32. svc := &InboundService{}
  33. db := database.GetDB()
  34. clients := []model.Client{
  35. {Email: "alive@x", ID: "11111111-1111-1111-1111-111111111111", Enable: true},
  36. {Email: "quota@x", ID: "22222222-2222-2222-2222-222222222222", Enable: true},
  37. }
  38. ib := mkInbound(t, 30301, model.VLESS, clientsSettings(t, clients))
  39. for _, c := range clients {
  40. row := &xray.ClientTraffic{InboundId: ib.Id, Email: c.Email, Enable: true}
  41. if err := db.Create(row).Error; err != nil {
  42. t.Fatalf("seed traffic %s: %v", c.Email, err)
  43. }
  44. }
  45. if err := db.Model(xray.ClientTraffic{}).
  46. Where("email = ?", "quota@x").
  47. Update("enable", false).Error; err != nil {
  48. t.Fatalf("deplete quota@x: %v", err)
  49. }
  50. t.Run("node payload keeps the depletion-disabled client", func(t *testing.T) {
  51. built, err := svc.buildInboundForNodePush(db, ib)
  52. if err != nil {
  53. t.Fatalf("buildInboundForNodePush: %v", err)
  54. }
  55. got := payloadClientEmails(t, built.Settings)
  56. if len(got) != 2 {
  57. t.Fatalf("node push dropped a client: got %v, want both alive@x and quota@x", got)
  58. }
  59. })
  60. t.Run("local xray payload still strips it", func(t *testing.T) {
  61. built, err := svc.buildInboundForLocalRuntime(db, ib)
  62. if err != nil {
  63. t.Fatalf("buildInboundForLocalRuntime: %v", err)
  64. }
  65. got := payloadClientEmails(t, built.Settings)
  66. if len(got) != 1 || got[0] != "alive@x" {
  67. t.Fatalf("local runtime payload = %v, want only alive@x", got)
  68. }
  69. })
  70. }