| 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 | } |