修复旧 WebSocket 节点路径和请求头导入
为 VMess、VLESS、Trojan 的显式 WebSocket 传输转换旧字段,检查重复和冲突并兼容已保存来源。保留字段安全限制,补充真实随附核心握手回归及导入、持久化、运行配置验证。
为 VMess、VLESS、Trojan 的显式 WebSocket 传输转换旧字段,检查重复和冲突并兼容已保存来源。保留字段安全限制,补充真实随附核心握手回归及导入、持久化、运行配置验证。
| @@ -26,6 +26,8 @@ keywords: Clash Mihomo YAML SOCKS5 住宅 IP DNS DoH 暂停 断网 代理链 订 | ||
| 26 | 26 | |
| 27 | 27 | 普通节点参数保留并交给 Cage 随附的 Mihomo 核心校验。例如,从 FlClash 复制的 VLESS 节点可以保留 `xudp: true`,无需手动改成其他字段。内核可能忽略未知字段;配置校验通过后,仍需应用节点并检查实际出口。客机公网 UDP 的阻断规则不受这些协议选项影响。 |
| 28 | 28 | |
| 29 | 新增:VMess、VLESS 和 Trojan 的 WebSocket 节点可以导入旧字段 `ws-path`、`ws-headers`。节点须明确设置 `network: ws`,Cage 会将路径和请求头转换为 `ws-opts.path`、`ws-opts.headers`,已保存的旧节点在应用代理时也会转换。新旧字段可同时存在,路径须一致,请求头按名称合并并忽略名称的字母大小写。相同请求头的值冲突、字段写法重复或旧字段用于其他传输方式时,来源会显示具体原因,需要修改该节点后重新导入。 | |
| 30 | 新增: | |
| 29 | 31 | Hysteria2 节点可使用 `mport` 指定端口跳跃范围、`pinSHA256` 指定证书指纹,导入时会转换为核心支持的字段并保留功能。若节点使用不支持的协议、引用本机文件或网卡、管理字段重复、别名参数冲突或代理链不可用,会跳过该节点,并在来源下显示导入数量、跳过数量和原因。点击「查看原因」可展开具体节点;依赖失败节点的代理链也会跳过。若所有节点均被跳过,导入失败;更新影响当前出口或中转节点时保留原配置,请先应用其他节点再更新。其余节点还须整体通过核心配置校验,校验失败时原配置保持不变,可在设置的「日志与诊断」中导出诊断包查看校验记录。 |
| 30 | 32 | |
| 31 | 33 | 同一个订阅地址可能按请求的客户端身份返回不同格式。Cage 下载时使用 Clash 客户端身份,服务商返回 YAML 时可以导入;若返回 Base64 或分享链接列表,仍需向服务商获取 Clash/Mihomo YAML。 |
| @@ -5,10 +5,13 @@ import ( | ||
| 5 | 5 | "encoding/hex" |
| 6 | 6 | "errors" |
| 7 | 7 | "fmt" |
| 8 | 新增: "maps" | |
| 9 | 新增: "net/http" | |
| 8 | 10 | "strconv" |
| 9 | 11 | "strings" |
| 10 | 12 | |
| 11 | 13 | "go.yaml.in/yaml/v3" |
| 14 | 新增: "golang.org/x/net/http/httpguts" | |
| 12 | 15 | ) |
| 13 | 16 | |
| 14 | 17 | // NodeIssue contains display metadata only, never node credentials or field values. |
| @@ -85,10 +88,12 @@ func parseNodes(data []byte, sourceID string) ([]Node, []NodeIssue, error) { | ||
| 85 | 88 | issue.Reason = "节点协议暂不支持" |
| 86 | 89 | case server == "" || len(server) > 253: |
| 87 | 90 | issue.Reason = "节点服务器缺失或过长" |
| 88 | 删除: case unsafeOption(p): | |
| 89 | 删除: issue.Reason = "节点不能引用本机文件、网络接口或额外路由" | |
| 90 | 91 | default: |
| 91 | 删除: if typ == "hysteria2" { | |
| 92 | 新增: if err := normalizeWebSocket(p); err != nil { | |
| 93 | 新增: issue.Reason = err.Error() | |
| 94 | 新增: } else if unsafeOption(p) { | |
| 95 | 新增: issue.Reason = "节点不能引用本机文件、网络接口或额外路由" | |
| 96 | 新增: } else if typ == "hysteria2" { | |
| 92 | 97 | if err := normalizeHysteria2(p); err != nil { |
| 93 | 98 | issue.Reason = err.Error() |
| 94 | 99 | } |
| @@ -221,6 +226,93 @@ func normalizeHysteria2(p map[string]any) error { | ||
| 221 | 226 | return nil |
| 222 | 227 | } |
| 223 | 228 | |
| 229 | 新增:// Legacy WebSocket fields are accepted by the core but ignored by its transport. | |
| 230 | 新增:// Normalize only nodes carrying aliases, leaving other protocol options alone. | |
| 231 | 新增:func normalizeWebSocket(p map[string]any) error { | |
| 232 | 新增: legacy := false | |
| 233 | 新增: for key := range p { | |
| 234 | 新增: if optionKey(key) == "ws-path" || optionKey(key) == "ws-headers" { | |
| 235 | 新增: legacy = true | |
| 236 | 新增: } | |
| 237 | 新增: } | |
| 238 | 新增: if !legacy { | |
| 239 | 新增: return nil | |
| 240 | 新增: } | |
| 241 | 新增: if err := normalizeImportKeys(p, "ws-path", "ws-headers", "ws-opts", "network"); err != nil { | |
| 242 | 新增: return err | |
| 243 | 新增: } | |
| 244 | 新增: if p["type"] != "vmess" && p["type"] != "vless" && p["type"] != "trojan" { | |
| 245 | 新增: return errors.New("旧 WebSocket 字段仅适用于 VMess、VLESS 或 Trojan 节点") | |
| 246 | 新增: } | |
| 247 | 新增: if p["network"] != "ws" { | |
| 248 | 新增: return errors.New("旧 WebSocket 字段需要显式设置 network: ws,请核对节点传输方式") | |
| 249 | 新增: } | |
| 250 | 新增: opts := map[string]any{} | |
| 251 | 新增: if raw, exists := p["ws-opts"]; exists { | |
| 252 | 新增: current, ok := raw.(map[string]any) | |
| 253 | 新增: if !ok { | |
| 254 | 新增: return errors.New("WebSocket 字段 ws-opts 无效,未忽略旧字段") | |
| 255 | 新增: } | |
| 256 | 新增: opts = maps.Clone(current) | |
| 257 | 新增: } | |
| 258 | 新增: if err := normalizeImportKeys(opts, "path", "headers"); err != nil { | |
| 259 | 新增: return err | |
| 260 | 新增: } | |
| 261 | 新增: if raw, exists := p["ws-path"]; exists { | |
| 262 | 新增: path, ok := raw.(string) | |
| 263 | 新增: if !ok { | |
| 264 | 新增: return errors.New("WebSocket 字段 ws-path 无效,未忽略该字段") | |
| 265 | 新增: } | |
| 266 | 新增: if current, exists := opts["path"]; exists && current != path { | |
| 267 | 新增: return errors.New("WebSocket 字段 ws-path 与 ws-opts.path 冲突") | |
| 268 | 新增: } | |
| 269 | 新增: opts["path"] = path | |
| 270 | 新增: delete(p, "ws-path") | |
| 271 | 新增: } | |
| 272 | 新增: if raw, exists := p["ws-headers"]; exists { | |
| 273 | 新增: headers, err := webSocketHeaders(raw) | |
| 274 | 新增: if err != nil { | |
| 275 | 新增: return err | |
| 276 | 新增: } | |
| 277 | 新增: if current, exists := opts["headers"]; exists { | |
| 278 | 新增: modern, err := webSocketHeaders(current) | |
| 279 | 新增: if err != nil { | |
| 280 | 新增: return err | |
| 281 | 新增: } | |
| 282 | 新增: for key, value := range modern { | |
| 283 | 新增: if previous, exists := headers[key]; exists && previous != value { | |
| 284 | 新增: return errors.New("WebSocket 字段 ws-headers 与 ws-opts.headers 冲突") | |
| 285 | 新增: } | |
| 286 | 新增: headers[key] = value | |
| 287 | 新增: } | |
| 288 | 新增: } | |
| 289 | 新增: opts["headers"] = headers | |
| 290 | 新增: delete(p, "ws-headers") | |
| 291 | 新增: } | |
| 292 | 新增: p["ws-opts"] = opts | |
| 293 | 新增: return nil | |
| 294 | 新增:} | |
| 295 | 新增: | |
| 296 | 新增:func webSocketHeaders(raw any) (map[string]any, error) { | |
| 297 | 新增: input, ok := raw.(map[string]any) | |
| 298 | 新增: if !ok { | |
| 299 | 新增: return nil, errors.New("WebSocket 请求头必须为字符串映射,未忽略该字段") | |
| 300 | 新增: } | |
| 301 | 新增: headers := make(map[string]any, len(input)) | |
| 302 | 新增: for key, rawValue := range input { | |
| 303 | 新增: value, ok := rawValue.(string) | |
| 304 | 新增: if !ok || !httpguts.ValidHeaderFieldName(key) || !httpguts.ValidHeaderFieldValue(value) { | |
| 305 | 新增: return nil, errors.New("WebSocket 请求头无效,未忽略该字段") | |
| 306 | 新增: } | |
| 307 | 新增: key = http.CanonicalHeaderKey(key) | |
| 308 | 新增: if previous, exists := headers[key]; exists && previous != value { | |
| 309 | 新增: return nil, errors.New("WebSocket 请求头存在大小写不同的冲突写法") | |
| 310 | 新增: } | |
| 311 | 新增: headers[key] = value | |
| 312 | 新增: } | |
| 313 | 新增: return headers, nil | |
| 314 | 新增:} | |
| 315 | 新增: | |
| 224 | 316 | func hysteria2Ports(raw any) (string, error) { |
| 225 | 317 | var ports string |
| 226 | 318 | switch v := raw.(type) { |
| @@ -283,6 +283,10 @@ func (s Store) Runtime(profile string) ([]byte, error) { | ||
| 283 | 283 | for key, value := range n.Config { |
| 284 | 284 | config[key] = value |
| 285 | 285 | } |
| 286 | 新增: // Older stores may contain legacy headers accepted before normalization. | |
| 287 | 新增: if err := normalizeWebSocket(config); err != nil { | |
| 288 | 新增: return nil, fmt.Errorf("节点 %q:%w", n.Name, err) | |
| 289 | 新增: } | |
| 286 | 290 | if via, overridden := dialers[n.ID]; overridden { |
| 287 | 291 | delete(config, "dialer-proxy") |
| 288 | 292 | if via != "" { |
| @@ -0,0 +1,148 @@ | ||
| 1 | 新增:package managedproxy | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "context" | |
| 5 | 新增: "fmt" | |
| 6 | 新增: "net" | |
| 7 | 新增: "net/http" | |
| 8 | 新增: "net/http/httptest" | |
| 9 | 新增: "net/url" | |
| 10 | 新增: "os" | |
| 11 | 新增: "os/exec" | |
| 12 | 新增: "path/filepath" | |
| 13 | 新增: "testing" | |
| 14 | 新增: "time" | |
| 15 | 新增: | |
| 16 | 新增: "github.com/gorilla/websocket" | |
| 17 | 新增:) | |
| 18 | 新增: | |
| 19 | 新增:// Configuration validation alone accepts legacy fields that the transport ignores. | |
| 20 | 新增:// Observe a real local upgrade after importing, saving and reloading each protocol. | |
| 21 | 新增:func TestCoreWebSocketAliasesReachHandshake(t *testing.T) { | |
| 22 | 新增: if os.Getenv("CAGE_PROXY_INTEGRATION") != "1" { | |
| 23 | 新增: t.Skip("requires a test binary beside the bundled Mihomo") | |
| 24 | 新增: } | |
| 25 | 新增: for _, protocol := range []string{"vmess", "vless", "trojan"} { | |
| 26 | 新增: for _, pathField := range []string{"WS_PATH", "ws-opts.path"} { | |
| 27 | 新增: t.Run(protocol+"/"+pathField, func(t *testing.T) { | |
| 28 | 新增: type observation struct { | |
| 29 | 新增: path, host, token, underscore, modern string | |
| 30 | 新增: upgraded bool | |
| 31 | 新增: } | |
| 32 | 新增: requests := make(chan observation, 8) | |
| 33 | 新增: upgrader := websocket.Upgrader{} | |
| 34 | 新增: upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { | |
| 35 | 新增: observed := observation{ | |
| 36 | 新增: path: request.URL.RequestURI(), host: request.Host, | |
| 37 | 新增: token: request.Header.Get("X-Legacy"), underscore: request.Header.Get("X_Underscore"), | |
| 38 | 新增: modern: request.Header.Get("X-Modern"), | |
| 39 | 新增: } | |
| 40 | 新增: connection, err := upgrader.Upgrade(w, request, nil) | |
| 41 | 新增: if err == nil { | |
| 42 | 新增: observed.upgraded = true | |
| 43 | 新增: _ = connection.Close() | |
| 44 | 新增: } | |
| 45 | 新增: select { | |
| 46 | 新增: case requests <- observed: | |
| 47 | 新增: default: | |
| 48 | 新增: } | |
| 49 | 新增: })) | |
| 50 | 新增: t.Cleanup(upstream.Close) | |
| 51 | 新增: _, port, err := net.SplitHostPort(upstream.Listener.Addr().String()) | |
| 52 | 新增: if err != nil { | |
| 53 | 新增: t.Fatal(err) | |
| 54 | 新增: } | |
| 55 | 新增: profile := t.TempDir() | |
| 56 | 新增: store, err := Load(profile) | |
| 57 | 新增: if err != nil { | |
| 58 | 新增: t.Fatal(err) | |
| 59 | 新增: } | |
| 60 | 新增: pathOptions := `WS_PATH: "/legacy?source=cage", ws-opts: {headers: {HOST: edge.example, X-Modern: retained}}` | |
| 61 | 新增: if pathField == "ws-opts.path" { | |
| 62 | 新增: pathOptions = `ws-opts: {path: "/legacy?source=cage", headers: {X-Modern: retained}}` | |
| 63 | 新增: } | |
| 64 | 新增: data := fmt.Sprintf(`proxies: [{name: local, type: %s, server: 127.0.0.1, port: %s, uuid: 00000000-0000-4000-8000-000000000000, password: local-test-password, alterId: 0, cipher: auto, tls: true, skip-cert-verify: true, network: ws, WS_HEADERS: {host: edge.example, X-Legacy: local-token, X_Underscore: preserved}, %s}]`, protocol, port, pathOptions) | |
| 65 | 新增: if err := store.Import("local", "", []byte(data), "local", 0, ""); err != nil { | |
| 66 | 新增: t.Fatal(err) | |
| 67 | 新增: } | |
| 68 | 新增: store.Selected = store.Sources[0].Nodes[0].ID | |
| 69 | 新增: if err := store.Save(profile); err != nil { | |
| 70 | 新增: t.Fatal(err) | |
| 71 | 新增: } | |
| 72 | 新增: store, err = Load(profile) | |
| 73 | 新增: if err != nil { | |
| 74 | 新增: t.Fatal(err) | |
| 75 | 新增: } | |
| 76 | 新增: if err := Validate(context.Background(), profile, store); err != nil { | |
| 77 | 新增: t.Fatal(err) | |
| 78 | 新增: } | |
| 79 | 新增: store.Port = 0 | |
| 80 | 新增: runtime, err := store.Runtime(profile) | |
| 81 | 新增: if err != nil { | |
| 82 | 新增: t.Fatal(err) | |
| 83 | 新增: } | |
| 84 | 新增: interfaces, err := net.Interfaces() | |
| 85 | 新增: if err != nil { | |
| 86 | 新增: t.Fatal(err) | |
| 87 | 新增: } | |
| 88 | 新增: loopback := "" | |
| 89 | 新增: for _, iface := range interfaces { | |
| 90 | 新增: if iface.Flags&net.FlagLoopback != 0 { | |
| 91 | 新增: loopback = iface.Name | |
| 92 | 新增: break | |
| 93 | 新增: } | |
| 94 | 新增: } | |
| 95 | 新增: runtime, err = BindInterface(runtime, loopback) | |
| 96 | 新增: if err != nil { | |
| 97 | 新增: t.Fatal(err) | |
| 98 | 新增: } | |
| 99 | 新增: config := filepath.Join(Dir(profile), "config.yaml") | |
| 100 | 新增: if err := Write(config, runtime); err != nil { | |
| 101 | 新增: t.Fatal(err) | |
| 102 | 新增: } | |
| 103 | 新增: if err := os.MkdirAll(filepath.Dir(Socket(profile)), 0700); err != nil { | |
| 104 | 新增: t.Fatal(err) | |
| 105 | 新增: } | |
| 106 | 新增: t.Cleanup(func() { _ = os.RemoveAll(filepath.Dir(Socket(profile))) }) | |
| 107 | 新增: core, err := CorePath() | |
| 108 | 新增: if err != nil { | |
| 109 | 新增: t.Fatal(err) | |
| 110 | 新增: } | |
| 111 | 新增: log, err := os.Create(filepath.Join(profile, "core.log")) | |
| 112 | 新增: if err != nil { | |
| 113 | 新增: t.Fatal(err) | |
| 114 | 新增: } | |
| 115 | 新增: command := exec.Command(core, "-d", Dir(profile), "-f", config) | |
| 116 | 新增: command.Env, command.Stdout, command.Stderr = coreEnvironment(Dir(profile)), log, log | |
| 117 | 新增: if err := command.Start(); err != nil { | |
| 118 | 新增: _ = log.Close() | |
| 119 | 新增: t.Fatal(err) | |
| 120 | 新增: } | |
| 121 | 新增: t.Cleanup(func() { _ = command.Process.Kill(); _ = command.Wait(); _ = log.Close() }) | |
| 122 | 新增: ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | |
| 123 | 新增: defer cancel() | |
| 124 | 新增: ticker := time.NewTicker(20 * time.Millisecond) | |
| 125 | 新增: defer ticker.Stop() | |
| 126 | 新增: for !Running(ctx, profile, store) { | |
| 127 | 新增: select { | |
| 128 | 新增: case <-ctx.Done(): | |
| 129 | 新增: t.Fatal("generated core did not become ready") | |
| 130 | 新增: case <-ticker.C: | |
| 131 | 新增: } | |
| 132 | 新增: } | |
| 133 | 新增: // The local server closes after the WebSocket upgrade. The protocol tunnel | |
| 134 | 新增: // then fails, while the observed HTTP request proves transport parameters. | |
| 135 | 新增: _ = API(ctx, profile, "GET", "/proxies/"+store.Selected+"/delay?timeout=2000&url="+url.QueryEscape("http://127.0.0.1:1/"), nil, nil) | |
| 136 | 新增: select { | |
| 137 | 新增: case observed := <-requests: | |
| 138 | 新增: if observed.path != "/legacy?source=cage" || observed.host != "edge.example" || observed.token != "local-token" || observed.underscore != "preserved" || observed.modern != "retained" || !observed.upgraded { | |
| 139 | 新增: t.Fatalf("imported WebSocket parameters did not reach the server: %+v", observed) | |
| 140 | 新增: } | |
| 141 | 新增: t.Logf("%s WebSocket upgrade preserved path, Host and both header formats", protocol) | |
| 142 | 新增: case <-ctx.Done(): | |
| 143 | 新增: t.Fatal("core never reached local WebSocket server") | |
| 144 | 新增: } | |
| 145 | 新增: }) | |
| 146 | 新增: } | |
| 147 | 新增: } | |
| 148 | 新增:} | |
| @@ -0,0 +1,182 @@ | ||
| 1 | 新增:package managedproxy | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "encoding/json" | |
| 5 | 新增: "fmt" | |
| 6 | 新增: "reflect" | |
| 7 | 新增: "strings" | |
| 8 | 新增: "testing" | |
| 9 | 新增: | |
| 10 | 新增: "go.yaml.in/yaml/v3" | |
| 11 | 新增:) | |
| 12 | 新增: | |
| 13 | 新增:func TestWebSocketAliasesSurviveImportStoreAndRuntime(t *testing.T) { | |
| 14 | 新增: for _, protocol := range []string{"vmess", "vless", "trojan"} { | |
| 15 | 新增: for _, tc := range []struct { | |
| 16 | 新增: name, fields, path string | |
| 17 | 新增: headers map[string]any | |
| 18 | 新增: }{ | |
| 19 | 新增: {"legacy", `ws-path: "/legacy?token=example", ws-headers: {host: edge.example, X-Token: example, X_Underscore: preserved}`, "/legacy?token=example", map[string]any{"Host": "edge.example", "X-Token": "example", "X_underscore": "preserved"}}, | |
| 20 | 新增: {"variants", `WS_PATH: /variants, WS_HEADERS: {HOST: edge.example}, WS_OPTS: {max-early-data: 64}`, "/variants", map[string]any{"Host": "edge.example"}}, | |
| 21 | 新增: {"equivalent", `ws-path: /same, ws-headers: {host: edge.example, HOST: edge.example}, ws-opts: {PATH: /same, HEADERS: {Host: edge.example}}`, "/same", map[string]any{"Host": "edge.example"}}, | |
| 22 | 新增: {"merged-headers", `ws-path: /merged, ws-headers: {host: edge.example, X-Legacy: old}, ws-opts: {headers: {HOST: edge.example, X-Modern: new}}`, "/merged", map[string]any{"Host": "edge.example", "X-Legacy": "old", "X-Modern": "new"}}, | |
| 23 | 新增: {"path-only", `ws-path: /path`, "/path", nil}, | |
| 24 | 新增: {"headers-only", `ws-headers: {Host: edge.example}, ws-opts: {path: /modern}`, "/modern", map[string]any{"Host": "edge.example"}}, | |
| 25 | 新增: {"empty-defaults", `ws-path: "", ws-headers: {}`, "", map[string]any{}}, | |
| 26 | 新增: } { | |
| 27 | 新增: t.Run(protocol+"/"+tc.name, func(t *testing.T) { | |
| 28 | 新增: profile := t.TempDir() | |
| 29 | 新增: store, err := Load(profile) | |
| 30 | 新增: if err != nil { | |
| 31 | 新增: t.Fatal(err) | |
| 32 | 新增: } | |
| 33 | 新增: data := fmt.Sprintf(`proxies: [{name: local, type: %s, server: 127.0.0.1, port: 443, network: ws, %s}]`, protocol, tc.fields) | |
| 34 | 新增: if err := store.Import("websocket", "", []byte(data), "websocket", 0, ""); err != nil { | |
| 35 | 新增: t.Fatal(err) | |
| 36 | 新增: } | |
| 37 | 新增: store.Selected = store.Sources[0].Nodes[0].ID | |
| 38 | 新增: if err := store.Save(profile); err != nil { | |
| 39 | 新增: t.Fatal(err) | |
| 40 | 新增: } | |
| 41 | 新增: loaded, err := Load(profile) | |
| 42 | 新增: if err != nil { | |
| 43 | 新增: t.Fatal(err) | |
| 44 | 新增: } | |
| 45 | 新增: runtime, err := loaded.Runtime(profile) | |
| 46 | 新增: if err != nil { | |
| 47 | 新增: t.Fatal(err) | |
| 48 | 新增: } | |
| 49 | 新增: var generated struct{ Proxies []map[string]any } | |
| 50 | 新增: if err := yaml.Unmarshal(runtime, &generated); err != nil { | |
| 51 | 新增: t.Fatal(err) | |
| 52 | 新增: } | |
| 53 | 新增: for _, config := range []map[string]any{loaded.Sources[0].Nodes[0].Config, generated.Proxies[0]} { | |
| 54 | 新增: for key := range config { | |
| 55 | 新增: if optionKey(key) == "ws-path" || optionKey(key) == "ws-headers" { | |
| 56 | 新增: t.Fatal("runtime retained an ignored WebSocket alias") | |
| 57 | 新增: } | |
| 58 | 新增: } | |
| 59 | 新增: opts := config["ws-opts"].(map[string]any) | |
| 60 | 新增: headers, _ := opts["headers"].(map[string]any) | |
| 61 | 新增: if opts["path"] != tc.path || !reflect.DeepEqual(headers, tc.headers) { | |
| 62 | 新增: t.Fatalf("WebSocket transport changed across import, store or runtime: %#v", opts) | |
| 63 | 新增: } | |
| 64 | 新增: if config["udp"] != false || config["network"] != "ws" { | |
| 65 | 新增: t.Fatal("conversion changed Cage UDP policy or transport") | |
| 66 | 新增: } | |
| 67 | 新增: } | |
| 68 | 新增: if tc.name == "variants" && generated.Proxies[0]["ws-opts"].(map[string]any)["max-early-data"] != 64 { | |
| 69 | 新增: t.Fatal("conversion lost unrelated WebSocket settings") | |
| 70 | 新增: } | |
| 71 | 新增: }) | |
| 72 | 新增: } | |
| 73 | 新增: } | |
| 74 | 新增:} | |
| 75 | 新增: | |
| 76 | 新增:func TestWebSocketInvalidAliasesSkipNodeAndDependentWithoutLeakingValues(t *testing.T) { | |
| 77 | 新增: for _, fields := range []string{ | |
| 78 | 新增: `ws-path: /private-path, WS_PATH: /private-path`, | |
| 79 | 新增: `ws-headers: {}, WS_HEADERS: {}`, | |
| 80 | 新增: `ws-path: /private-path, NETWORK: ws`, | |
| 81 | 新增: `ws-path: /private-path, ws-opts: {}, WS_OPTS: {}`, | |
| 82 | 新增: `ws-path: /private-path, ws-opts: {path: /private-path, PATH: /private-path}`, | |
| 83 | 新增: `ws-headers: {}, ws-opts: {headers: {}, HEADERS: {}}`, | |
| 84 | 新增: `ws-path: /private-path, ws-opts: {path: /other-private-path}`, | |
| 85 | 新增: `ws-path: 12`, | |
| 86 | 新增: `ws-path: {private-key: private-value}`, | |
| 87 | 新增: `ws-path: /private-path, ws-opts: private-value`, | |
| 88 | 新增: `ws-path: /private-path, ws-opts: {path: [private-value]}`, | |
| 89 | 新增: `ws-headers: private-value`, | |
| 90 | 新增: `ws-headers: null`, | |
| 91 | 新增: `ws-headers: {Host: 123}`, | |
| 92 | 新增: `ws-headers: {Host: [private-value]}`, | |
| 93 | 新增: `ws-headers: {host: private-value, HOST: different-private-value}`, | |
| 94 | 新增: `ws-headers: {Host: private-value}, ws-opts: {headers: {host: different-private-value}}`, | |
| 95 | 新增: `ws-headers: {Host: private-value}, ws-opts: {headers: private-value}`, | |
| 96 | 新增: `ws-headers: {"invalid header": private-value}`, | |
| 97 | 新增: `ws-headers: {Host: "private-value\r\nInjected: private-value"}`, | |
| 98 | 新增: `ws-path: /private-path, ws-opts: {PRIVATE_KEY: /private/key}`, | |
| 99 | 新增: `ws-path: /private-path, future-options: {CA_PATH: /private/ca}`, | |
| 100 | 新增: `ws-headers: {certificate: /private/certificate}`, | |
| 101 | 新增: } { | |
| 102 | 新增: t.Run(fields, func(t *testing.T) { | |
| 103 | 新增: store, _ := Load(t.TempDir()) | |
| 104 | 新增: data := `proxies: | |
| 105 | 新增: - {name: good, type: http, server: 127.0.0.1, port: 8080} | |
| 106 | 新增: - {name: bad, type: vless, server: example.com, port: 443, network: ws, ` + fields + `} | |
| 107 | 新增: - {name: dependent, type: socks5, server: example.com, port: 1080, dialer-proxy: bad} | |
| 108 | 新增:` | |
| 109 | 新增: if err := store.Import("websocket", "", []byte(data), "websocket", 0, ""); err != nil { | |
| 110 | 新增: t.Fatal(err) | |
| 111 | 新增: } | |
| 112 | 新增: if store.Count() != 1 || store.Sources[0].Nodes[0].Name != "good" || len(store.Sources[0].Skipped) != 2 { | |
| 113 | 新增: t.Fatal("invalid WebSocket node or dependent survived import") | |
| 114 | 新增: } | |
| 115 | 新增: report, _ := json.Marshal(store.Sources[0].Skipped) | |
| 116 | 新增: if strings.Contains(string(report), "private-") || strings.Contains(string(report), "/private/") { | |
| 117 | 新增: t.Fatal("import report leaked a field value") | |
| 118 | 新增: } | |
| 119 | 新增: }) | |
| 120 | 新增: } | |
| 121 | 新增:} | |
| 122 | 新增: | |
| 123 | 新增:func TestWebSocketAliasesRequireAnApplicableTransport(t *testing.T) { | |
| 124 | 新增: for _, fields := range []string{ | |
| 125 | 新增: `type: http, network: ws`, `type: ss, network: ws`, | |
| 126 | 新增: `type: vless, network: tcp`, `type: vmess, network: grpc`, `type: trojan`, | |
| 127 | 新增: } { | |
| 128 | 新增: if _, err := Parse([]byte(`proxies: [{name: example, server: example.com, port: 443, ws-headers: {Host: example.com}, `+fields+`}]`), "test"); err == nil { | |
| 129 | 新增: t.Fatalf("ignored fields survived incompatible transport: %s", fields) | |
| 130 | 新增: } | |
| 131 | 新增: } | |
| 132 | 新增: data := `proxies: [{name: example, type: http, server: example.com, port: 8080, network: grpc, ws-opts: {PATH: /retained, headers: {x_custom: example}}}]` | |
| 133 | 新增: nodes, err := Parse([]byte(data), "test") | |
| 134 | 新增: if err != nil { | |
| 135 | 新增: t.Fatal(err) | |
| 136 | 新增: } | |
| 137 | 新增: opts := nodes[0].Config["ws-opts"].(map[string]any) | |
| 138 | 新增: if opts["PATH"] != "/retained" || opts["headers"].(map[string]any)["x_custom"] != "example" { | |
| 139 | 新增: t.Fatal("normalization rewrote unrelated protocol data") | |
| 140 | 新增: } | |
| 141 | 新增:} | |
| 142 | 新增: | |
| 143 | 新增:func TestWebSocketRuntimeNormalizesOlderStoresWithoutMutatingThem(t *testing.T) { | |
| 144 | 新增: profile := t.TempDir() | |
| 145 | 新增: store, _ := Load(profile) | |
| 146 | 新增: if err := store.Import("websocket", "", []byte(`proxies: [{name: old, type: vless, server: example.com, port: 443, network: ws}]`), "test", 0, ""); err != nil { | |
| 147 | 新增: t.Fatal(err) | |
| 148 | 新增: } | |
| 149 | 新增: store.Selected = store.Sources[0].Nodes[0].ID | |
| 150 | 新增: config := store.Sources[0].Nodes[0].Config | |
| 151 | 新增: config["ws-path"] = "/stored" | |
| 152 | 新增: config["ws-headers"] = map[string]any{"host": "edge.example"} | |
| 153 | 新增: config["ws-opts"] = map[string]any{"PATH": "/stored", "headers": map[string]any{"X-Modern": "retained"}} | |
| 154 | 新增: if err := store.Save(profile); err != nil { | |
| 155 | 新增: t.Fatal(err) | |
| 156 | 新增: } | |
| 157 | 新增: loaded, err := Load(profile) | |
| 158 | 新增: if err != nil { | |
| 159 | 新增: t.Fatal(err) | |
| 160 | 新增: } | |
| 161 | 新增: before, _ := json.Marshal(loaded) | |
| 162 | 新增: runtime, err := loaded.Runtime(profile) | |
| 163 | 新增: if err != nil { | |
| 164 | 新增: t.Fatal(err) | |
| 165 | 新增: } | |
| 166 | 新增: var generated struct{ Proxies []map[string]any } | |
| 167 | 新增: if err := yaml.Unmarshal(runtime, &generated); err != nil { | |
| 168 | 新增: t.Fatal(err) | |
| 169 | 新增: } | |
| 170 | 新增: opts := generated.Proxies[0]["ws-opts"].(map[string]any) | |
| 171 | 新增: if opts["path"] != "/stored" || !reflect.DeepEqual(opts["headers"], map[string]any{"Host": "edge.example", "X-Modern": "retained"}) { | |
| 172 | 新增: t.Fatal("older persisted aliases were not converted") | |
| 173 | 新增: } | |
| 174 | 新增: after, _ := json.Marshal(loaded) | |
| 175 | 新增: if string(before) != string(after) { | |
| 176 | 新增: t.Fatal("runtime generation mutated stored source") | |
| 177 | 新增: } | |
| 178 | 新增: loaded.Sources[0].Nodes[0].Config["ws-path"] = "/conflict" | |
| 179 | 新增: if _, err := loaded.Runtime(profile); err == nil { | |
| 180 | 新增: t.Fatal("ambiguous older store silently selected one path") | |
| 181 | 新增: } | |
| 182 | 新增:} | |