1
0

clash_external_vless_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "net/http/httptest"
  6. "net/url"
  7. "testing"
  8. "github.com/goccy/go-yaml"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  11. )
  12. const clashExternalVlessLink = "vless://[email protected]:443?type=tcp&security=reality&sni=example.com&pbk=test-public-key&sid=ab12&fp=chrome&flow=xtls-rprx-vision"
  13. func TestClashExternalVlessEncryption(t *testing.T) {
  14. svc := NewSubClashService(false, "", &SubService{})
  15. for _, tc := range []struct {
  16. name string
  17. query string
  18. want string
  19. }{
  20. {"encrypted", "&encryption=" + url.QueryEscape(testMlkemEncryption), testMlkemEncryption},
  21. {"trimmed", "&encryption=" + url.QueryEscape(" \t"+testMlkemEncryption+" \n"), testMlkemEncryption},
  22. {"none", "&encryption=none", ""},
  23. {"trimmed none", "&encryption=%20none%20", ""},
  24. {"empty", "&encryption=", ""},
  25. {"whitespace", "&encryption=%20%09", ""},
  26. {"missing", "", ""},
  27. } {
  28. t.Run(tc.name, func(t *testing.T) {
  29. proxy := svc.clashProxyFromExternal(clashExternalVlessLink+tc.query+"#external", "external")
  30. if proxy == nil {
  31. t.Fatal("expected a VLESS proxy")
  32. }
  33. if got, exists := proxy["encryption"]; tc.want == "" {
  34. if exists {
  35. t.Errorf("plain VLESS encryption should be omitted, got %v", got)
  36. }
  37. } else if got != tc.want {
  38. t.Errorf("encryption = %v, want %q", got, tc.want)
  39. }
  40. for key, want := range map[string]any{
  41. "type": "vless", "uuid": "22222222-2222-4222-8222-222222222222",
  42. "server": "198.51.100.9", "port": 443, "network": "tcp",
  43. "flow": "xtls-rprx-vision", "tls": true, "servername": "example.com",
  44. } {
  45. if got := proxy[key]; got != want {
  46. t.Errorf("%s = %v, want %v", key, got, want)
  47. }
  48. }
  49. if _, exists := proxy["packet-encoding"]; exists {
  50. t.Error("VLESS encryption must not be exported as packet-encoding")
  51. }
  52. })
  53. }
  54. }
  55. // Regression for #6572: both pasted links and fetched subscriptions must keep
  56. // encryption when their nodes are merged with the client's local inbound.
  57. func TestClashMergedExternalVlessEncryption(t *testing.T) {
  58. link := clashExternalVlessLink + "&encryption=" + url.QueryEscape(testMlkemEncryption) + "#external"
  59. for _, source := range []string{"link", "subscription-plain", "subscription-base64"} {
  60. t.Run(source, func(t *testing.T) {
  61. seedSubDB(t)
  62. resetSubscriptionCache(t)
  63. seedSubInbound(t, "merged-vless", "local", 10001, 1, wsTLSStream)
  64. db := database.GetDB()
  65. var client model.ClientRecord
  66. if err := db.Where("email = ?", "local@e").First(&client).Error; err != nil {
  67. t.Fatal(err)
  68. }
  69. entry := model.ClientExternalLink{ClientId: client.Id, Kind: model.ExternalLinkKindLink, Value: link}
  70. if source != "link" {
  71. body := link + "\n"
  72. if source == "subscription-base64" {
  73. body = base64.StdEncoding.EncodeToString([]byte(body))
  74. }
  75. srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  76. _, _ = w.Write([]byte(body))
  77. }))
  78. defer srv.Close()
  79. previousClient := subscriptionHTTPClient
  80. subscriptionHTTPClient = srv.Client()
  81. t.Cleanup(func() { subscriptionHTTPClient = previousClient })
  82. entry.Kind = model.ExternalLinkKindSubscription
  83. entry.Value = srv.URL
  84. }
  85. if err := db.Create(&entry).Error; err != nil {
  86. t.Fatal(err)
  87. }
  88. w := fetchClashSub(t, clashSubRouter(t), "/clash/merged-vless?view=raw")
  89. if w.Code != http.StatusOK {
  90. t.Fatalf("status = %d, want 200: %s", w.Code, w.Body.String())
  91. }
  92. var config struct {
  93. Proxies []struct {
  94. UUID string `yaml:"uuid"`
  95. Encryption string `yaml:"encryption"`
  96. } `yaml:"proxies"`
  97. }
  98. if err := yaml.Unmarshal(w.Body.Bytes(), &config); err != nil {
  99. t.Fatalf("decode Clash YAML: %v", err)
  100. }
  101. if len(config.Proxies) != 2 {
  102. t.Fatalf("expected local and external proxies, got %#v", config.Proxies)
  103. }
  104. want := map[string]string{client.UUID: "", "22222222-2222-4222-8222-222222222222": testMlkemEncryption}
  105. for _, proxy := range config.Proxies {
  106. encryption, ok := want[proxy.UUID]
  107. if !ok || proxy.Encryption != encryption {
  108. t.Errorf("unexpected proxy: %#v", proxy)
  109. }
  110. delete(want, proxy.UUID)
  111. }
  112. if len(want) != 0 {
  113. t.Errorf("missing proxies: %v", want)
  114. }
  115. })
  116. }
  117. }