跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

proxy_import_test.go

144 行 · 6.0 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "os"
8 "reflect"
9 "strings"
10 "testing"
11 "time"
12
13 "github.com/sylvinus/agent-vm/internal/managedproxy"
14)
15
16func TestProxyImportAPIReportsPartialSuccessAndProtectsRefresh(t *testing.T) {
17 s := testServer(t)
18 cfg, _ := loadConfig(s.configPath)
19 cfg.ProxyRevision = ""
20 if err := writePrivate(s.configPath, cfg); err != nil {
21 t.Fatal(err)
22 }
23 oldValidate, oldFetch := validateProxyCore, fetchProxySource
24 t.Cleanup(func() { validateProxyCore, fetchProxySource = oldValidate, oldFetch })
25 validateProxyCore = func(_ context.Context, profile string, p managedproxy.Store) error {
26 _, err := p.Runtime(profile)
27 return err
28 }
29 pin := strings.Repeat("ab", 32)
30 content := `proxies:
31 - {name: hopping, type: hysteria2, server: example.com, port: 35000, password: private-node-password, mport: 35000-39000, pinSHA256: "` + pin + `"}
32 - {name: unsupported, type: socks5, server: example.com, port: 1080, INTERFACE_NAME: private-field-value}
33`
34 mutate := func(action string, input proxyInput) Job {
35 t.Helper()
36 body, _ := json.Marshal(input)
37 w := request(s, "POST", "/api/proxy/"+action, s.origin, string(body), true)
38 if w.Code != http.StatusOK {
39 t.Fatal(w.Code, w.Body.String())
40 }
41 deadline := time.Now().Add(3 * time.Second)
42 for time.Now().Before(deadline) {
43 job := s.jobs.snapshot()
44 if !job.Running {
45 return job
46 }
47 time.Sleep(10 * time.Millisecond)
48 }
49 t.Fatal("proxy import job did not finish")
50 return Job{}
51 }
52 if job := mutate("import", proxyInput{Name: "feedback", Content: content, Interval: 24}); job.Error != "" {
53 t.Fatal(job.Error)
54 }
55 p, _ := managedproxy.Load(s.profile)
56 src := p.Sources[1]
57 if len(src.Nodes) != 1 || len(src.Skipped) != 1 || src.Nodes[0].Config["fingerprint"] != pin {
58 t.Fatal("API import lost the valid node, certificate pin or skipped-node report")
59 }
60 w := request(s, "GET", "/api/proxy", s.origin, "", true)
61 if w.Code != http.StatusOK {
62 t.Fatal(w.Code, w.Body.String())
63 }
64 var view struct {
65 Sources []struct {
66 Skipped []managedproxy.NodeIssue `json:"skipped"`
67 }
68 }
69 if err := json.Unmarshal(w.Body.Bytes(), &view); err != nil || len(view.Sources[1].Skipped) != 1 || view.Sources[1].Skipped[0].Index != 2 {
70 t.Fatal("status lost node import diagnostics", err, w.Body.String())
71 }
72 for _, secret := range []string{"private-node-password", "private-field-value", pin, p.Auth} {
73 if strings.Contains(w.Body.String(), secret) {
74 t.Fatal("status leaked subscription credentials or field values")
75 }
76 }
77 // A later successful refresh replaces the report. An unsuccessful refresh
78 // must keep that last successful source and its metadata.
79 p.Sources[1].URL = "https://example.com/sub?token=private-subscription"
80 p.Selected = src.Nodes[0].ID
81 p.Chain = []string{p.Selected}
82 if err := p.Save(s.profile); err != nil {
83 t.Fatal(err)
84 }
85 response := strings.Split(content, " - {name: unsupported")[0]
86 fetchProxySource = func(context.Context, *http.Client, string) ([]byte, string, error) {
87 return []byte(response), "", nil
88 }
89 if job := mutate("refresh", proxyInput{ID: src.ID, HostDownload: true}); job.Error != "" {
90 t.Fatal(job.Error)
91 }
92 before, _ := managedproxy.Load(s.profile)
93 if len(before.Sources[1].Skipped) != 0 {
94 t.Fatal("successful refresh retained stale skipped-node report")
95 }
96 response = strings.Replace(content, "mport: 35000-39000", "mport: invalid", 1) + " - {name: spare, type: http, server: example.com, port: 8080}\n"
97 if job := mutate("refresh", proxyInput{ID: src.ID, HostDownload: true}); !strings.Contains(job.Error, "hopping") || !strings.Contains(job.Error, "mport") || strings.Contains(job.Error, "private-") {
98 t.Fatal("refresh did not identify the skipped active exit safely", job.Error)
99 }
100 after, _ := managedproxy.Load(s.profile)
101 if !reflect.DeepEqual(after.Sources[1].Nodes, before.Sources[1].Nodes) || after.Selected != before.Selected || !reflect.DeepEqual(after.Chain, before.Chain) || !after.Sources[1].Updated.Equal(before.Sources[1].Updated) || after.Sources[1].Error == "" {
102 t.Fatal("failed refresh changed the last working nodes or route")
103 }
104}
105
106func TestProxyImportAndRefreshUseBundledCoreValidation(t *testing.T) {
107 if os.Getenv("CAGE_PROXY_INTEGRATION") != "1" {
108 t.Skip("requires a test binary beside the bundled Mihomo")
109 }
110 s := testServer(t)
111 cfg, _ := loadConfig(s.configPath)
112 cfg.ProxyRevision = ""
113 if err := writePrivate(s.configPath, cfg); err != nil {
114 t.Fatal(err)
115 }
116 content := `proxies: [{name: feedback, type: vless, server: example.com, port: 443, uuid: 00000000-0000-4000-8000-000000000000, xudp: true, future-option: private-field-value}]`
117 if err := s.changeProxy("import", proxyInput{Name: "feedback", Content: content}, nil); err != nil {
118 t.Fatal(err)
119 }
120 before, err := managedproxy.Load(s.profile)
121 if err != nil || len(before.Sources) != 2 || before.Sources[1].Nodes[0].Config["xudp"] != true {
122 t.Fatal("core-compatible node was not saved", err)
123 }
124 src := &before.Sources[1]
125 src.URL = "https://example.com/sub?token=private-subscription"
126 before.Selected = src.Nodes[0].ID
127 before.Chain = []string{before.Selected}
128 if err := before.Save(s.profile); err != nil {
129 t.Fatal(err)
130 }
131 oldFetch := fetchProxySource
132 t.Cleanup(func() { fetchProxySource = oldFetch })
133 fetchProxySource = func(context.Context, *http.Client, string) ([]byte, string, error) {
134 return []byte(strings.Replace(content, "xudp: true", "xudp: {invalid: private-value}", 1)), "", nil
135 }
136 err = s.changeProxy("refresh", proxyInput{ID: src.ID, HostDownload: true}, nil)
137 if err == nil || !strings.Contains(err.Error(), "Mihomo") || strings.Contains(err.Error(), "private-") {
138 t.Fatal("core did not safely reject the invalid refresh", err)
139 }
140 after, err := managedproxy.Load(s.profile)
141 if err != nil || !reflect.DeepEqual(after.Sources[1].Nodes, src.Nodes) || after.Selected != before.Selected || !reflect.DeepEqual(after.Chain, before.Chain) || !after.Sources[1].Updated.Equal(src.Updated) || after.Sources[1].Error == "" {
142 t.Fatal("core rejection changed the last good source or active route", err)
143 }
144}