inbound_autorenew_shadowsocks_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  9. )
  10. // TestAPIUserFromClientCarriesShadowsocksCipher pins what the renew path must
  11. // hand the runtime API. A shadowsocks client object holds no cipher of its own,
  12. // and xray's legacy and 2022 inbounds take different account types that they
  13. // cast without checking — an account built for the wrong one panics the core.
  14. func TestAPIUserFromClientCarriesShadowsocksCipher(t *testing.T) {
  15. client := map[string]any{"email": "a@x", "password": "pw", "method": "aes-256-gcm"}
  16. user := apiUserFromClient(client, "aes-256-gcm")
  17. if got := user["cipher"]; got != "aes-256-gcm" {
  18. t.Fatalf("cipher = %v, want the inbound method", got)
  19. }
  20. if _, polluted := client["cipher"]; polluted {
  21. t.Fatal("the stored client object was mutated; the API-only cipher would be persisted into the inbound settings")
  22. }
  23. user["email"] = "changed@x"
  24. if client["email"] != "a@x" {
  25. t.Fatal("the API user shares storage with the stored client object")
  26. }
  27. }
  28. func TestAPIUserFromClientWithoutCipher(t *testing.T) {
  29. client := map[string]any{"email": "a@x", "id": "11111111-1111-1111-1111-111111111111"}
  30. user := apiUserFromClient(client, "")
  31. if _, ok := user["cipher"]; ok {
  32. t.Fatal("a non-shadowsocks client must not gain a cipher key")
  33. }
  34. if user["id"] != client["id"] {
  35. t.Fatalf("id = %v, want it copied from the client", user["id"])
  36. }
  37. }
  38. // TestAutoRenewShadowsocksKeepsSettingsClean renews a shadowsocks client and
  39. // checks the inbound settings the panel writes back: the cipher the API needs
  40. // must not leak into a stored client object, where it would end up in the
  41. // generated xray config as a per-user key.
  42. func TestAutoRenewShadowsocksKeepsSettingsClean(t *testing.T) {
  43. setupBulkDB(t)
  44. svc := &InboundService{}
  45. db := database.GetDB()
  46. past := time.Now().Add(-48 * time.Hour).UnixMilli()
  47. clients := []model.Client{
  48. {Email: "ss@x", Password: "pw", Enable: false, Reset: 30, ExpiryTime: past},
  49. }
  50. settings := map[string]any{
  51. "method": "aes-256-gcm",
  52. "network": "tcp,udp",
  53. "clients": []map[string]any{
  54. {"email": "ss@x", "password": "pw", "method": "aes-256-gcm", "enable": false, "expiryTime": past, "reset": 30},
  55. },
  56. }
  57. raw, err := json.MarshalIndent(settings, "", " ")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. ib := mkInbound(t, 30011, model.Shadowsocks, string(raw))
  62. if err := svc.clientService.SyncInbound(nil, ib.Id, clients); err != nil {
  63. t.Fatalf("SyncInbound: %v", err)
  64. }
  65. if err := db.Create(&[]xray.ClientTraffic{
  66. {InboundId: ib.Id, Email: "ss@x", Enable: false, Up: 10, Down: 20, Reset: 30, ExpiryTime: past},
  67. }).Error; err != nil {
  68. t.Fatalf("seed client_traffics: %v", err)
  69. }
  70. if _, count, err := svc.autoRenewClients(db); err != nil {
  71. t.Fatalf("autoRenewClients: %v", err)
  72. } else if count != 1 {
  73. t.Fatalf("renewed count = %d, want 1", count)
  74. }
  75. var stored model.Inbound
  76. if err := db.Where("id = ?", ib.Id).First(&stored).Error; err != nil {
  77. t.Fatalf("read inbound: %v", err)
  78. }
  79. var parsed map[string]any
  80. if err := json.Unmarshal([]byte(stored.Settings), &parsed); err != nil {
  81. t.Fatalf("unmarshal stored settings: %v", err)
  82. }
  83. storedClients, _ := parsed["clients"].([]any)
  84. if len(storedClients) != 1 {
  85. t.Fatalf("stored clients = %d, want 1", len(storedClients))
  86. }
  87. client, _ := storedClients[0].(map[string]any)
  88. if _, polluted := client["cipher"]; polluted {
  89. t.Fatalf("the renewed client was persisted with an API-only cipher key: %+v", client)
  90. }
  91. if client["method"] != "aes-256-gcm" {
  92. t.Fatalf("the client's method was lost: %+v", client)
  93. }
  94. if enabled, _ := client["enable"].(bool); !enabled {
  95. t.Fatalf("the renewed client was not re-enabled: %+v", client)
  96. }
  97. }