clash_service_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package sub
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/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. // TestBuildProxy_VLESSRealityFieldsForClash locks the reality field mapping in
  38. // applySecurity (clash_service.go ~488): a regression that drops servername,
  39. // public-key, short-id, or client-fingerprint would hand mihomo a broken reality
  40. // proxy. The existing clash tests don't assert any of these.
  41. func TestBuildProxy_VLESSRealityFieldsForClash(t *testing.T) {
  42. svc := &SubClashService{SubService: &SubService{remarkModel: "-i"}}
  43. inbound := &model.Inbound{Listen: "203.0.113.1", Port: 443, Protocol: model.VLESS, Remark: "r", Settings: `{"encryption":"none"}`}
  44. client := model.Client{ID: "11111111-2222-4333-8444-555555555555"}
  45. stream := map[string]any{
  46. "network": "tcp",
  47. "security": "reality",
  48. "tcpSettings": map[string]any{"header": map[string]any{"type": "none"}},
  49. "realitySettings": map[string]any{"serverName": "reality.example.com", "publicKey": "PBKvalue", "shortId": "ab12", "fingerprint": "chrome"},
  50. }
  51. proxy := svc.buildProxy(svc.SubService, inbound, client, stream, "")
  52. if proxy == nil {
  53. t.Fatal("buildProxy returned nil for a valid reality stream")
  54. }
  55. if proxy["tls"] != true {
  56. t.Fatalf("tls = %v, want true", proxy["tls"])
  57. }
  58. if proxy["servername"] != "reality.example.com" {
  59. t.Fatalf("servername = %v, want reality.example.com", proxy["servername"])
  60. }
  61. if proxy["client-fingerprint"] != "chrome" {
  62. t.Fatalf("client-fingerprint = %v, want chrome", proxy["client-fingerprint"])
  63. }
  64. opts, _ := proxy["reality-opts"].(map[string]any)
  65. if opts == nil {
  66. t.Fatal("reality-opts missing")
  67. }
  68. if opts["public-key"] != "PBKvalue" {
  69. t.Fatalf("public-key = %v, want PBKvalue", opts["public-key"])
  70. }
  71. if opts["short-id"] != "ab12" {
  72. t.Fatalf("short-id = %v, want ab12", opts["short-id"])
  73. }
  74. }
  75. // TestApplyTransport_TCPHeader pins the tcp-header validation (clash_service.go ~359):
  76. // plain tcp and a "none" header are representable in clash; a non-none obfs header is
  77. // not, so applyTransport must reject it (returning false drops it from the YAML).
  78. func TestApplyTransport_TCPHeader(t *testing.T) {
  79. svc := &SubClashService{}
  80. if !svc.applyTransport(map[string]any{}, "tcp", map[string]any{}) {
  81. t.Fatal("plain tcp must be buildable")
  82. }
  83. noneStream := map[string]any{"tcpSettings": map[string]any{"header": map[string]any{"type": "none"}}}
  84. if !svc.applyTransport(map[string]any{}, "tcp", noneStream) {
  85. t.Fatal("tcp + header type none must be buildable")
  86. }
  87. httpStream := map[string]any{"tcpSettings": map[string]any{"header": map[string]any{"type": "http"}}}
  88. if svc.applyTransport(map[string]any{}, "tcp", httpStream) {
  89. t.Fatal("tcp + non-none (http) header is not representable in clash and must be rejected")
  90. }
  91. }
  92. func TestApplyTransport_XHTTP(t *testing.T) {
  93. svc := &SubClashService{}
  94. proxy := map[string]any{}
  95. stream := map[string]any{
  96. "xhttpSettings": map[string]any{
  97. "path": "/xh",
  98. "host": "example.com",
  99. "mode": "auto",
  100. },
  101. }
  102. if !svc.applyTransport(proxy, "xhttp", stream) {
  103. t.Fatalf("applyTransport returned false for xhttp (#4531: would drop the inbound and yield an empty Clash YAML)")
  104. }
  105. if proxy["network"] != "xhttp" {
  106. t.Fatalf("network = %v, want xhttp", proxy["network"])
  107. }
  108. opts, ok := proxy["xhttp-opts"].(map[string]any)
  109. if !ok {
  110. t.Fatalf("xhttp-opts missing or wrong type: %#v", proxy["xhttp-opts"])
  111. }
  112. want := map[string]any{"path": "/xh", "host": "example.com", "mode": "auto"}
  113. if !reflect.DeepEqual(opts, want) {
  114. t.Fatalf("xhttp-opts = %#v, want %#v", opts, want)
  115. }
  116. }
  117. func TestApplyTransport_XHTTP_HostFromHeaders(t *testing.T) {
  118. svc := &SubClashService{}
  119. proxy := map[string]any{}
  120. stream := map[string]any{
  121. "xhttpSettings": map[string]any{
  122. "path": "/xh",
  123. "headers": map[string]any{"Host": "via-header.example.com"},
  124. },
  125. }
  126. if !svc.applyTransport(proxy, "xhttp", stream) {
  127. t.Fatalf("applyTransport returned false for xhttp")
  128. }
  129. opts, _ := proxy["xhttp-opts"].(map[string]any)
  130. if opts["host"] != "via-header.example.com" {
  131. t.Fatalf("host should fall back to headers.Host, got %v", opts["host"])
  132. }
  133. }
  134. func TestApplyTransport_HTTPUpgrade(t *testing.T) {
  135. svc := &SubClashService{}
  136. proxy := map[string]any{}
  137. stream := map[string]any{
  138. "httpupgradeSettings": map[string]any{
  139. "path": "/hu",
  140. "host": "example.com",
  141. },
  142. }
  143. if !svc.applyTransport(proxy, "httpupgrade", stream) {
  144. t.Fatalf("applyTransport returned false for httpupgrade")
  145. }
  146. if proxy["network"] != "httpupgrade" {
  147. t.Fatalf("network = %v, want httpupgrade", proxy["network"])
  148. }
  149. opts, ok := proxy["http-upgrade-opts"].(map[string]any)
  150. if !ok {
  151. t.Fatalf("http-upgrade-opts missing: %#v", proxy["http-upgrade-opts"])
  152. }
  153. if opts["path"] != "/hu" {
  154. t.Fatalf("path = %v, want /hu", opts["path"])
  155. }
  156. headers, _ := opts["headers"].(map[string]any)
  157. if headers["Host"] != "example.com" {
  158. t.Fatalf("headers.Host = %v, want example.com", headers["Host"])
  159. }
  160. }
  161. func TestBuildProxy_VLESSPostQuantumEncryptionUsesMihomoEncryptionField(t *testing.T) {
  162. svc := &SubClashService{SubService: &SubService{remarkModel: "-i"}}
  163. encryption := "mlkem768x25519plus.native.0rtt.client"
  164. inbound := &model.Inbound{
  165. Listen: "203.0.113.1",
  166. Port: 443,
  167. Protocol: model.VLESS,
  168. Remark: "pq",
  169. Settings: `{"encryption":"` + encryption + `"}`,
  170. }
  171. client := model.Client{ID: "11111111-2222-4333-8444-555555555555"}
  172. stream := map[string]any{
  173. "network": "xhttp",
  174. "xhttpSettings": map[string]any{
  175. "path": "/",
  176. "mode": "auto",
  177. },
  178. "security": "reality",
  179. "realitySettings": map[string]any{
  180. "publicKey": "pub",
  181. "serverName": "example.com",
  182. "shortId": "abcd",
  183. },
  184. }
  185. proxy := svc.buildProxy(svc.SubService, inbound, client, stream, "")
  186. if proxy["encryption"] != encryption {
  187. t.Fatalf("encryption = %v, want %q", proxy["encryption"], encryption)
  188. }
  189. }
  190. func TestBuildProxy_VLESSFlowXhttpRealityVlessenc(t *testing.T) {
  191. svc := &SubClashService{SubService: &SubService{remarkModel: "-i"}}
  192. encryption := "mlkem768x25519plus.native.0rtt.client"
  193. inbound := &model.Inbound{
  194. Listen: "203.0.113.1",
  195. Port: 443,
  196. Protocol: model.VLESS,
  197. Remark: "pq-flow",
  198. Settings: `{"encryption":"` + encryption + `"}`,
  199. }
  200. client := model.Client{ID: "11111111-2222-4333-8444-555555555555", Flow: "xtls-rprx-vision"}
  201. stream := map[string]any{
  202. "network": "xhttp",
  203. "xhttpSettings": map[string]any{
  204. "path": "/",
  205. "mode": "auto",
  206. },
  207. "security": "reality",
  208. "realitySettings": map[string]any{
  209. "publicKey": "pub",
  210. "serverName": "example.com",
  211. "shortId": "abcd",
  212. },
  213. }
  214. proxy := svc.buildProxy(svc.SubService, inbound, client, stream, "")
  215. if proxy["flow"] != "xtls-rprx-vision" {
  216. t.Fatalf("xhttp+reality+vlessenc Clash proxy must carry the vision flow (#5232): %#v", proxy)
  217. }
  218. }
  219. func TestBuildProxy_VLESSFlowDroppedWithoutVisionSupport(t *testing.T) {
  220. svc := &SubClashService{SubService: &SubService{remarkModel: "-i"}}
  221. inbound := &model.Inbound{
  222. Listen: "203.0.113.1",
  223. Port: 443,
  224. Protocol: model.VLESS,
  225. Remark: "plain-flow",
  226. Settings: `{"encryption":"none"}`,
  227. }
  228. client := model.Client{ID: "11111111-2222-4333-8444-555555555555", Flow: "xtls-rprx-vision"}
  229. stream := map[string]any{
  230. "network": "tcp",
  231. "security": "none",
  232. "tcpSettings": map[string]any{
  233. "header": map[string]any{"type": "none"},
  234. },
  235. }
  236. proxy := svc.buildProxy(svc.SubService, inbound, client, stream, "")
  237. if _, ok := proxy["flow"]; ok {
  238. t.Fatalf("tcp without tls/reality must not carry a flow: %#v", proxy)
  239. }
  240. }
  241. func TestBuildProxy_VLESSNoneEncryptionOmittedForClash(t *testing.T) {
  242. svc := &SubClashService{SubService: &SubService{remarkModel: "-i"}}
  243. inbound := &model.Inbound{
  244. Listen: "203.0.113.1",
  245. Port: 443,
  246. Protocol: model.VLESS,
  247. Remark: "plain",
  248. Settings: `{"encryption":"none"}`,
  249. }
  250. client := model.Client{ID: "11111111-2222-4333-8444-555555555555"}
  251. stream := map[string]any{
  252. "network": "tcp",
  253. "security": "none",
  254. "tcpSettings": map[string]any{
  255. "header": map[string]any{"type": "none"},
  256. },
  257. }
  258. proxy := svc.buildProxy(svc.SubService, inbound, client, stream, "")
  259. if _, ok := proxy["encryption"]; ok {
  260. t.Fatalf("plain vless encryption should be omitted for mihomo: %#v", proxy)
  261. }
  262. // The rest of the proxy must still be well-formed — otherwise a mutant that
  263. // drops encryption *and* corrupts a core field passes the absence check alone.
  264. if proxy["type"] != "vless" {
  265. t.Fatalf("type = %v, want vless", proxy["type"])
  266. }
  267. if proxy["server"] != "203.0.113.1" {
  268. t.Fatalf("server = %v, want 203.0.113.1", proxy["server"])
  269. }
  270. if proxy["port"] != 443 {
  271. t.Fatalf("port = %v, want 443", proxy["port"])
  272. }
  273. if proxy["uuid"] != client.ID {
  274. t.Fatalf("uuid = %v, want %v", proxy["uuid"], client.ID)
  275. }
  276. }