subClashService_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package sub
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/database/model"
  6. )
  7. func TestEnsureUniqueProxyNames(t *testing.T) {
  8. proxies := []map[string]any{
  9. {"name": "", "type": "vless", "server": "a.com", "port": 443},
  10. {"name": "", "type": "vmess", "server": "b.com", "port": 8443},
  11. {"name": "node"},
  12. {"name": "node"},
  13. {"name": ""},
  14. }
  15. ensureUniqueProxyNames(proxies)
  16. seen := map[string]bool{}
  17. for i, p := range proxies {
  18. name, _ := p["name"].(string)
  19. if name == "" {
  20. t.Fatalf("proxy %d still has an empty name (mihomo would reject the config, #4641)", i)
  21. }
  22. if seen[name] {
  23. t.Fatalf("proxy %d has duplicate name %q (mihomo rejects the whole config, #4641)", i, name)
  24. }
  25. seen[name] = true
  26. }
  27. if got := proxies[0]["name"]; got != "vless-a.com-443" {
  28. t.Errorf("empty name fallback = %q, want vless-a.com-443", got)
  29. }
  30. if proxies[2]["name"] == proxies[3]["name"] {
  31. t.Errorf("duplicate %q was not disambiguated", proxies[2]["name"])
  32. }
  33. if got := proxies[4]["name"]; got != "proxy-5" {
  34. t.Errorf("typeless empty name fallback = %q, want proxy-5", got)
  35. }
  36. }
  37. func TestApplyTransport_XHTTP(t *testing.T) {
  38. svc := &SubClashService{}
  39. proxy := map[string]any{}
  40. stream := map[string]any{
  41. "xhttpSettings": map[string]any{
  42. "path": "/xh",
  43. "host": "example.com",
  44. "mode": "auto",
  45. },
  46. }
  47. if !svc.applyTransport(proxy, "xhttp", stream) {
  48. t.Fatalf("applyTransport returned false for xhttp (#4531: would drop the inbound and yield an empty Clash YAML)")
  49. }
  50. if proxy["network"] != "xhttp" {
  51. t.Fatalf("network = %v, want xhttp", proxy["network"])
  52. }
  53. opts, ok := proxy["xhttp-opts"].(map[string]any)
  54. if !ok {
  55. t.Fatalf("xhttp-opts missing or wrong type: %#v", proxy["xhttp-opts"])
  56. }
  57. want := map[string]any{"path": "/xh", "host": "example.com", "mode": "auto"}
  58. if !reflect.DeepEqual(opts, want) {
  59. t.Fatalf("xhttp-opts = %#v, want %#v", opts, want)
  60. }
  61. }
  62. func TestApplyTransport_XHTTP_HostFromHeaders(t *testing.T) {
  63. svc := &SubClashService{}
  64. proxy := map[string]any{}
  65. stream := map[string]any{
  66. "xhttpSettings": map[string]any{
  67. "path": "/xh",
  68. "headers": map[string]any{"Host": "via-header.example.com"},
  69. },
  70. }
  71. if !svc.applyTransport(proxy, "xhttp", stream) {
  72. t.Fatalf("applyTransport returned false for xhttp")
  73. }
  74. opts, _ := proxy["xhttp-opts"].(map[string]any)
  75. if opts["host"] != "via-header.example.com" {
  76. t.Fatalf("host should fall back to headers.Host, got %v", opts["host"])
  77. }
  78. }
  79. func TestApplyTransport_HTTPUpgrade(t *testing.T) {
  80. svc := &SubClashService{}
  81. proxy := map[string]any{}
  82. stream := map[string]any{
  83. "httpupgradeSettings": map[string]any{
  84. "path": "/hu",
  85. "host": "example.com",
  86. },
  87. }
  88. if !svc.applyTransport(proxy, "httpupgrade", stream) {
  89. t.Fatalf("applyTransport returned false for httpupgrade")
  90. }
  91. if proxy["network"] != "httpupgrade" {
  92. t.Fatalf("network = %v, want httpupgrade", proxy["network"])
  93. }
  94. opts, ok := proxy["http-upgrade-opts"].(map[string]any)
  95. if !ok {
  96. t.Fatalf("http-upgrade-opts missing: %#v", proxy["http-upgrade-opts"])
  97. }
  98. if opts["path"] != "/hu" {
  99. t.Fatalf("path = %v, want /hu", opts["path"])
  100. }
  101. headers, _ := opts["headers"].(map[string]any)
  102. if headers["Host"] != "example.com" {
  103. t.Fatalf("headers.Host = %v, want example.com", headers["Host"])
  104. }
  105. }
  106. func TestBuildProxy_VLESSPostQuantumEncryptionUsesMihomoEncryptionField(t *testing.T) {
  107. svc := &SubClashService{SubService: &SubService{remarkModel: "-i"}}
  108. encryption := "mlkem768x25519plus.native.0rtt.client"
  109. inbound := &model.Inbound{
  110. Listen: "203.0.113.1",
  111. Port: 443,
  112. Protocol: model.VLESS,
  113. Remark: "pq",
  114. Settings: `{"encryption":"` + encryption + `"}`,
  115. }
  116. client := model.Client{ID: "11111111-2222-4333-8444-555555555555"}
  117. stream := map[string]any{
  118. "network": "xhttp",
  119. "xhttpSettings": map[string]any{
  120. "path": "/",
  121. "mode": "auto",
  122. },
  123. "security": "reality",
  124. "realitySettings": map[string]any{
  125. "publicKey": "pub",
  126. "serverName": "example.com",
  127. "shortId": "abcd",
  128. },
  129. }
  130. proxy := svc.buildProxy(inbound, client, stream, "")
  131. if proxy["encryption"] != encryption {
  132. t.Fatalf("encryption = %v, want %q", proxy["encryption"], encryption)
  133. }
  134. }
  135. func TestBuildProxy_VLESSNoneEncryptionOmittedForClash(t *testing.T) {
  136. svc := &SubClashService{SubService: &SubService{remarkModel: "-i"}}
  137. inbound := &model.Inbound{
  138. Listen: "203.0.113.1",
  139. Port: 443,
  140. Protocol: model.VLESS,
  141. Remark: "plain",
  142. Settings: `{"encryption":"none"}`,
  143. }
  144. client := model.Client{ID: "11111111-2222-4333-8444-555555555555"}
  145. stream := map[string]any{
  146. "network": "tcp",
  147. "security": "none",
  148. "tcpSettings": map[string]any{
  149. "header": map[string]any{"type": "none"},
  150. },
  151. }
  152. proxy := svc.buildProxy(inbound, client, stream, "")
  153. if _, ok := proxy["encryption"]; ok {
  154. t.Fatalf("plain vless encryption should be omitted for mihomo: %#v", proxy)
  155. }
  156. }