diff --git a/internal/sub/clash_service.go b/internal/sub/clash_service.go index 7ccb42692..dcc50772a 100644 --- a/internal/sub/clash_service.go +++ b/internal/sub/clash_service.go @@ -249,6 +249,10 @@ func legacyClashProxy(proxy map[string]any) map[string]any { if _, reality := proxy["reality-opts"]; reality { return nil } + // Legacy Clash would treat Mihomo's HTTPUpgrade options as ordinary WebSocket. + if opts, ok := proxy["ws-opts"].(map[string]any); ok && opts["v2ray-http-upgrade"] == true { + return nil + } var fields []string var cipher string @@ -1185,10 +1189,13 @@ func (s *SubClashService) applyTransport(proxy map[string]any, network string, s } } return true - case "ws": + case "ws", "httpupgrade": proxy["network"] = "ws" - ws, _ := stream["wsSettings"].(map[string]any) + ws, _ := stream[network+"Settings"].(map[string]any) wsOpts := map[string]any{} + if network == "httpupgrade" { + wsOpts["v2ray-http-upgrade"] = true + } if ws != nil { if path, ok := ws["path"].(string); ok && path != "" { wsOpts["path"] = path @@ -1220,28 +1227,6 @@ func (s *SubClashService) applyTransport(proxy map[string]any, network string, s proxy["grpc-opts"] = grpcOpts } return true - case "httpupgrade": - proxy["network"] = "httpupgrade" - hu, _ := stream["httpupgradeSettings"].(map[string]any) - opts := map[string]any{} - if hu != nil { - if path, ok := hu["path"].(string); ok && path != "" { - opts["path"] = path - } - host := "" - if v, ok := hu["host"].(string); ok && v != "" { - host = v - } else if headers, ok := hu["headers"].(map[string]any); ok { - host = searchHost(headers) - } - if host != "" { - opts["headers"] = map[string]any{"Host": host} - } - } - if len(opts) > 0 { - proxy["http-upgrade-opts"] = opts - } - return true case "xhttp": proxy["network"] = "xhttp" xhttp, _ := stream["xhttpSettings"].(map[string]any) diff --git a/internal/sub/clash_service_test.go b/internal/sub/clash_service_test.go index ae80a7fe8..6b4309859 100644 --- a/internal/sub/clash_service_test.go +++ b/internal/sub/clash_service_test.go @@ -117,6 +117,7 @@ func TestLegacyClashProxyCompatibility(t *testing.T) { {"name": "vl", "type": "vless"}, {"name": "hy", "type": "hysteria2"}, {"name": "xh", "type": "vmess", "cipher": "auto", "network": "xhttp"}, + {"name": "hu", "type": "vmess", "cipher": "auto", "network": "ws", "ws-opts": map[string]any{"v2ray-http-upgrade": true}}, {"name": "reality", "type": "vmess", "cipher": "auto", "network": "tcp", "reality-opts": map[string]any{}}, } { if got := legacyClashProxy(proxy); got != nil { @@ -421,27 +422,45 @@ func TestApplyTransport_HTTPUpgrade(t *testing.T) { proxy := map[string]any{} stream := map[string]any{ "httpupgradeSettings": map[string]any{ - "path": "/hu", - "host": "example.com", + "path": "/hu", + "host": "example.com", + "headers": map[string]any{"host": "ignored.example.com"}, }, } if !svc.applyTransport(proxy, "httpupgrade", stream) { t.Fatalf("applyTransport returned false for httpupgrade") } - if proxy["network"] != "httpupgrade" { - t.Fatalf("network = %v, want httpupgrade", proxy["network"]) + if proxy["network"] != "ws" { + t.Fatalf("network = %v, want ws", proxy["network"]) } - opts, ok := proxy["http-upgrade-opts"].(map[string]any) - if !ok { - t.Fatalf("http-upgrade-opts missing: %#v", proxy["http-upgrade-opts"]) + want := map[string]any{"path": "/hu", "headers": map[string]any{"Host": "example.com"}, "v2ray-http-upgrade": true} + if !reflect.DeepEqual(proxy["ws-opts"], want) { + t.Fatalf("ws-opts = %#v, want %#v", proxy["ws-opts"], want) } - if opts["path"] != "/hu" { - t.Fatalf("path = %v, want /hu", opts["path"]) + if _, exists := proxy["http-upgrade-opts"]; exists { + t.Fatalf("invalid http-upgrade-opts key: %#v", proxy) } - headers, _ := opts["headers"].(map[string]any) - if headers["Host"] != "example.com" { - t.Fatalf("headers.Host = %v, want example.com", headers["Host"]) +} + +func TestClashHTTPUpgradeForInboundAndExternalLink(t *testing.T) { + svc := &SubClashService{SubService: &SubService{}} + const id = "11111111-2222-4333-8444-555555555555" + stream := svc.streamData(`{"network":"httpupgrade","httpupgradeSettings":{"path":"/upgrade","headers":{"host":"host.example"}},"security":"tls","tlsSettings":{"serverName":"sni.example"}}`) + inbound := &model.Inbound{Protocol: model.VMESS, Listen: "203.0.113.1", Port: 443, Remark: "vmess"} + for _, tc := range []struct { + protocol string + proxy map[string]any + }{ + {"vmess", svc.buildProxy(svc.SubService, inbound, model.Client{ID: id}, stream, nil)}, + {"vless", svc.clashProxyFromExternal("vless://"+id+"@203.0.113.1:443?type=httpupgrade&host=host.example&path=%2Fupgrade&security=tls&sni=sni.example#vless", "vless")}, + } { + t.Run(tc.protocol, func(t *testing.T) { + want := map[string]any{"path": "/upgrade", "headers": map[string]any{"Host": "host.example"}, "v2ray-http-upgrade": true} + if tc.proxy["type"] != tc.protocol || tc.proxy["network"] != "ws" || tc.proxy["tls"] != true || tc.proxy["servername"] != "sni.example" || !reflect.DeepEqual(tc.proxy["ws-opts"], want) { + t.Fatalf("unexpected HTTPUpgrade proxy: %#v", tc.proxy) + } + }) } } diff --git a/internal/sub/mutation_audit_test.go b/internal/sub/mutation_audit_test.go index 8530c9235..99e77a276 100644 --- a/internal/sub/mutation_audit_test.go +++ b/internal/sub/mutation_audit_test.go @@ -478,24 +478,21 @@ func TestEnsureUniqueProxyNames_SuffixSequence(t *testing.T) { } } -// --- clash_service.go:422,447 — empty transport opts must NOT add the *-opts key --- - func TestApplyTransport_EmptyOptsOmitted(t *testing.T) { svc := &SubClashService{} - // httpupgrade with no path/host → opts empty → no http-upgrade-opts key (clash:422). - huProxy := map[string]any{} - if !svc.applyTransport(huProxy, "httpupgrade", map[string]any{"httpupgradeSettings": map[string]any{}}) { - t.Fatal("httpupgrade must still be buildable") - } - if huProxy["network"] != "httpupgrade" { - t.Fatalf("network = %v, want httpupgrade", huProxy["network"]) - } - if _, ok := huProxy["http-upgrade-opts"]; ok { - t.Fatalf("empty opts must not set http-upgrade-opts: %#v", huProxy["http-upgrade-opts"]) + // HTTPUpgrade still needs the mode flag without a configured Host or path. + for _, stream := range []map[string]any{nil, {"httpupgradeSettings": map[string]any{}}} { + huProxy := map[string]any{} + if !svc.applyTransport(huProxy, "httpupgrade", stream) || huProxy["network"] != "ws" { + t.Fatalf("HTTPUpgrade must use ws even without options: %#v", huProxy) + } + if opts, ok := huProxy["ws-opts"].(map[string]any); !ok || len(opts) != 1 || opts["v2ray-http-upgrade"] != true { + t.Fatalf("HTTPUpgrade must always set the mode flag: %#v", huProxy) + } } - // xhttp with no path/host/mode → opts empty → no xhttp-opts key (clash:447). + // xhttp with no path, Host or mode requires no options block. xhProxy := map[string]any{} if !svc.applyTransport(xhProxy, "xhttp", map[string]any{"xhttpSettings": map[string]any{}}) { t.Fatal("xhttp must still be buildable")