inbound_settings_clients_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  7. )
  8. func TestParseInboundSettingsClientsIgnoresProtocolScalarFields(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. settings string
  12. want string
  13. }{
  14. {
  15. name: "vless scalar settings",
  16. settings: `{
  17. "clients": [{"email": "[email protected]", "id": "11111111-1111-1111-1111-111111111111", "limitIp": 2}],
  18. "decryption": "none",
  19. "encryption": "none",
  20. "fallbacks": []
  21. }`,
  22. want: "[email protected]",
  23. },
  24. {
  25. name: "hysteria scalar settings",
  26. settings: `{
  27. "clients": [{"email": "[email protected]", "password": "secret"}],
  28. "version": 2,
  29. "ignoreClientBandwidth": false
  30. }`,
  31. want: "[email protected]",
  32. },
  33. }
  34. for _, tt := range tests {
  35. t.Run(tt.name, func(t *testing.T) {
  36. clients, err := ParseInboundSettingsClients(tt.settings)
  37. if err != nil {
  38. t.Fatalf("ParseInboundSettingsClients: %v", err)
  39. }
  40. if len(clients) != 1 || clients[0].Email != tt.want {
  41. t.Fatalf("clients = %+v, want one client with email %s", clients, tt.want)
  42. }
  43. })
  44. }
  45. }
  46. func TestParseInboundSettingsClientsRejectsEmptyOrNullSettings(t *testing.T) {
  47. for _, settings := range []string{"", " ", "null", " \n null \t "} {
  48. t.Run(settings, func(t *testing.T) {
  49. clients, err := ParseInboundSettingsClients(settings)
  50. if err == nil {
  51. t.Fatalf("ParseInboundSettingsClients(%q) error = nil, want error", settings)
  52. }
  53. if clients != nil {
  54. t.Fatalf("clients = %+v, want nil", clients)
  55. }
  56. })
  57. }
  58. }
  59. func TestGetClientsIgnoresProtocolScalarFields(t *testing.T) {
  60. inbound := &model.Inbound{
  61. Settings: `{
  62. "clients": [{"email": "[email protected]", "id": "11111111-1111-1111-1111-111111111111"}],
  63. "decryption": "none",
  64. "encryption": "none",
  65. "fallbacks": []
  66. }`,
  67. }
  68. clients, err := (&InboundService{}).GetClients(inbound)
  69. if err != nil {
  70. t.Fatalf("GetClients: %v", err)
  71. }
  72. if len(clients) != 1 || clients[0].Email != "[email protected]" {
  73. t.Fatalf("clients = %+v, want [email protected]", clients)
  74. }
  75. }
  76. func TestSearchClientTrafficIgnoresProtocolScalarFields(t *testing.T) {
  77. setupBulkDB(t)
  78. db := database.GetDB()
  79. client := model.Client{
  80. Email: "[email protected]",
  81. ID: "11111111-1111-1111-1111-111111111111",
  82. SubID: "sub-alice",
  83. Enable: true,
  84. }
  85. inbound := &model.Inbound{
  86. UserId: 1,
  87. Tag: "vless-scalar",
  88. Enable: true,
  89. Port: 43001,
  90. Protocol: model.VLESS,
  91. Settings: `{
  92. "clients": [{"email": "[email protected]", "id": "11111111-1111-1111-1111-111111111111", "subId": "sub-alice", "enable": true}],
  93. "decryption": "none",
  94. "encryption": "none",
  95. "fallbacks": []
  96. }`,
  97. }
  98. if err := db.Create(inbound).Error; err != nil {
  99. t.Fatalf("create inbound: %v", err)
  100. }
  101. if err := db.Create(&model.ClientRecord{Email: client.Email, Enable: true, SubID: client.SubID}).Error; err != nil {
  102. t.Fatalf("create client record: %v", err)
  103. }
  104. if err := db.Create(&xray.ClientTraffic{InboundId: inbound.Id, Email: client.Email, Enable: true}).Error; err != nil {
  105. t.Fatalf("create client traffic: %v", err)
  106. }
  107. traffic, err := (&InboundService{}).SearchClientTraffic(client.ID)
  108. if err != nil {
  109. t.Fatalf("SearchClientTraffic: %v", err)
  110. }
  111. if traffic.Email != client.Email || traffic.InboundId != inbound.Id {
  112. t.Fatalf("traffic = %+v, want email %s inbound %d", traffic, client.Email, inbound.Id)
  113. }
  114. }