| 1 | package managedproxy |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "go.yaml.in/yaml/v3" |
| 12 | ) |
| 13 | |
| 14 | func TestHysteria2SubscriptionAliasesPreserveTransportAndPin(t *testing.T) { |
| 15 | profile := t.TempDir() |
| 16 | s, _ := Load(profile) |
| 17 | pin := strings.Repeat("ab", 32) |
| 18 | data := `proxies: |
| 19 | - {name: ordinary, type: socks5, server: 127.0.0.1, port: 1080} |
| 20 | - {name: hopping, type: hysteria2, server: example.com, port: 35000, password: test-password, mport: 35000-39000, pinSHA256: "` + pin + `"} |
| 21 | ` |
| 22 | if err := s.Import("feedback", "", []byte(data), "feedback", 0, ""); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if s.Count() != 2 { |
| 26 | t.Fatalf("imported %d nodes, want 2", s.Count()) |
| 27 | } |
| 28 | s.Selected = s.Sources[0].Nodes[1].ID |
| 29 | runtime, err := s.Runtime(profile) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | var config struct{ Proxies []map[string]any } |
| 34 | if err := yaml.Unmarshal(runtime, &config); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | hy2 := config.Proxies[1] |
| 38 | if hy2["ports"] != "35000-39000" || hy2["fingerprint"] != pin || hy2["mport"] != nil || hy2["pinSHA256"] != nil { |
| 39 | t.Fatalf("aliases did not preserve transport and certificate pin: %v", hy2) |
| 40 | } |
| 41 | if os.Getenv("CAGE_PROXY_INTEGRATION") == "1" { |
| 42 | if err := Validate(context.Background(), profile, s); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestPartialSubscriptionImportReportsSkippedNodesAndDependencies(t *testing.T) { |
| 49 | profile := t.TempDir() |
| 50 | s, _ := Load(profile) |
| 51 | data := `proxies: |
| 52 | - {name: good, type: socks5, server: 127.0.0.1, port: 1080} |
| 53 | - {name: unsupported, type: hysteria2, server: example.com, port: 443, password: secret-password, mport: secret-value} |
| 54 | - {name: dependent, type: socks5, server: example.com, port: 1080, dialer-proxy: unsupported} |
| 55 | - {name: unsafe, type: http, server: example.com, port: 80, ws-opts: {private-key: /private/secret-key}} |
| 56 | - {name: cycle-a, type: http, server: example.com, port: 80, dialer-proxy: cycle-b} |
| 57 | - {name: cycle-b, type: http, server: example.com, port: 80, dialer-proxy: cycle-a} |
| 58 | ` |
| 59 | if err := s.Import("partial", "", []byte(data), "partial", 0, ""); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if s.Count() != 1 || s.Sources[0].Nodes[0].Name != "good" { |
| 63 | t.Fatal("failed nodes or broken chains survived the import") |
| 64 | } |
| 65 | if err := s.Save(profile); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | loaded, err := Load(profile) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | b, _ := json.Marshal(loaded.Sources[0]) |
| 73 | var report struct { |
| 74 | Skipped []struct{ Name, Reason string } |
| 75 | } |
| 76 | if err := json.Unmarshal(b, &report); err != nil || len(report.Skipped) != 5 { |
| 77 | t.Fatalf("missing durable import report: %s, %v", b, err) |
| 78 | } |
| 79 | issues, _ := json.Marshal(report.Skipped) |
| 80 | if !strings.Contains(string(issues), "mport") || !strings.Contains(string(issues), "dependent") { |
| 81 | t.Fatal("skipped nodes have no useful diagnosis", string(issues)) |
| 82 | } |
| 83 | for _, secret := range []string{"secret-password", "secret-value", "/private/secret-key"} { |
| 84 | if strings.Contains(string(issues), secret) { |
| 85 | t.Fatal("import diagnosis leaked a field value") |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestHysteria2InvalidOrConflictingAliasesSkipOnlyThatNode(t *testing.T) { |
| 91 | pin := strings.Repeat("ab", 32) |
| 92 | for _, fields := range []string{ |
| 93 | `mport: 35000-39000, ports: 45000-49000`, |
| 94 | `MPORT: 35000-39000, PORTS: 45000-49000`, |
| 95 | `mport: 35000-39000, MPORT: 35000-39000`, |
| 96 | `pinSHA256: "` + pin + `", fingerprint: "` + strings.Repeat("cd", 32) + `"`, |
| 97 | `PINSHA256: "` + pin + `", FINGERPRINT: "` + strings.Repeat("cd", 32) + `"`, |
| 98 | `pinSHA256: invalid-private-pin`, |
| 99 | `pinSHA256: ""`, |
| 100 | `mport: ""`, |
| 101 | `mport: 0-65536`, |
| 102 | `mport: {nested: secret}`, |
| 103 | } { |
| 104 | s, _ := Load(t.TempDir()) |
| 105 | data := `proxies: |
| 106 | - {name: good, type: http, server: 127.0.0.1, port: 8080} |
| 107 | - {name: bad, type: hysteria2, server: example.com, port: 443, password: secret, ` + fields + `} |
| 108 | ` |
| 109 | if err := s.Import("test", "", []byte(data), "test", 0, ""); err != nil || s.Count() != 1 { |
| 110 | t.Fatalf("fields %s: count=%d err=%v", fields, s.Count(), err) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestHysteria2EquivalentAliasesAndExplicitHopInterval(t *testing.T) { |
| 116 | s, _ := Load(t.TempDir()) |
| 117 | upper := strings.TrimSuffix(strings.Repeat("AB:", 32), ":") |
| 118 | data := `payload: |
| 119 | - {name: hopping, type: hysteria2, server: example.com, password: secret, mport: "443/5000-6000", ports: "443,5000-6000", hop-interval: 45, pinSHA256: "` + upper + `", fingerprint: "` + strings.Repeat("ab", 32) + `"} |
| 120 | - {name: single, type: hysteria2, server: example.com, password: secret, MPORT: 35000, PINSHA256: "` + upper + `"} |
| 121 | ` |
| 122 | if err := s.Import("equivalent", "", []byte(data), "test", 0, ""); err != nil || s.Count() != 2 { |
| 123 | t.Fatalf("equivalent aliases were rejected: count=%d err=%v", s.Count(), err) |
| 124 | } |
| 125 | first, second := s.Sources[0].Nodes[0].Config, s.Sources[0].Nodes[1].Config |
| 126 | if first["ports"] != "443,5000-6000" || first["hop-interval"] != 45 || second["ports"] != "35000" || second["fingerprint"] != strings.Repeat("ab", 32) { |
| 127 | t.Fatal("normalization changed port hopping semantics") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestAllSkippedImportPreservesLastGoodSourceAndReportsWithoutValues(t *testing.T) { |
| 132 | s, _ := Load(t.TempDir()) |
| 133 | if err := s.Import("test", "", []byte(testNodes), "test", 0, ""); err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | before, _ := json.Marshal(s) |
| 137 | data := `proxies: |
| 138 | - {name: first, type: hysteria2, server: example.com, password: private-password, pinSHA256: private-pin} |
| 139 | - {name: second, type: http, server: example.com, port: 80, INTERFACE_NAME: private-field} |
| 140 | ` |
| 141 | err := s.Import("test", "", []byte(data), "test", 0, "") |
| 142 | if err == nil || !strings.Contains(err.Error(), "2 个") || !strings.Contains(err.Error(), "first") || !strings.Contains(err.Error(), "pinSHA256") || !strings.Contains(err.Error(), "网络接口") { |
| 143 | t.Fatal("all-skipped import did not identify the failed nodes", err) |
| 144 | } |
| 145 | if strings.Contains(err.Error(), "private-") { |
| 146 | t.Fatal("all-skipped error leaked credentials or field values") |
| 147 | } |
| 148 | after, _ := json.Marshal(s) |
| 149 | if !reflect.DeepEqual(before, after) { |
| 150 | t.Fatal("all-skipped import replaced the last good source") |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func TestRefreshRetainsSelectedExitAndTransitWhenSkipped(t *testing.T) { |
| 155 | for _, explicit := range []bool{false, true} { |
| 156 | for _, bad := range []string{"entry", "exit"} { |
| 157 | s, _ := Load(t.TempDir()) |
| 158 | data := `proxies: |
| 159 | - {name: entry, type: http, server: 127.0.0.1, port: 8080} |
| 160 | - {name: exit, type: http, server: 127.0.0.1, port: 8081, dialer-proxy: entry} |
| 161 | - {name: spare, type: http, server: 127.0.0.1, port: 8082} |
| 162 | ` |
| 163 | if err := s.Import("test", "https://example.com/sub", []byte(data), "test", 24, ""); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | s.Selected = s.Sources[0].Nodes[1].ID |
| 167 | if explicit { |
| 168 | s.Chain = []string{s.Sources[0].Nodes[0].ID, s.Selected} |
| 169 | } |
| 170 | before, _ := json.Marshal(s) |
| 171 | broken := strings.Replace(data, "name: "+bad+",", "name: "+bad+", INTERFACE_NAME: secret,", 1) |
| 172 | if err := s.Import("test", "https://example.com/sub", []byte(broken), "test", 24, ""); err == nil { |
| 173 | t.Fatal("refresh accepted a missing active exit or transit") |
| 174 | } |
| 175 | after, _ := json.Marshal(s) |
| 176 | if !reflect.DeepEqual(before, after) { |
| 177 | t.Fatal("failed refresh changed saved nodes, metadata or route") |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |