跳到主要内容

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

浏览文件

port_forwarding_test.go

280 行 · 10.0 KB历史下载
1package e2eqemu
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "net"
8 "net/http"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "runtime"
13 "time"
14
15 gvproxyclient "github.com/containers/gvisor-tap-vsock/pkg/client"
16 "github.com/containers/gvisor-tap-vsock/pkg/transport"
17 "github.com/containers/gvisor-tap-vsock/pkg/types"
18 "github.com/onsi/ginkgo"
19 "github.com/onsi/gomega"
20 log "github.com/sirupsen/logrus"
21)
22
23var _ = ginkgo.Describe("port forwarding", func() {
24 client := gvproxyclient.New(&http.Client{
25 Transport: &http.Transport{
26 DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
27 return net.Dial("unix", sock)
28 },
29 },
30 }, "http://base")
31
32 ginkgo.It("should reach a http server on the host", func() {
33 ln, err := net.Listen("tcp", "127.0.0.1:9090")
34 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
35 defer ln.Close()
36
37 mux := http.NewServeMux()
38 mux.HandleFunc("/", func(writer http.ResponseWriter, _ *http.Request) {
39 _, _ = writer.Write([]byte("Hello from the host"))
40 })
41 go func() {
42 s := &http.Server{
43 Handler: mux,
44 ReadTimeout: 10 * time.Second,
45 WriteTimeout: 10 * time.Second,
46 }
47 err := s.Serve(ln)
48 if err != nil {
49 log.Error(err)
50 }
51 }()
52
53 out, err := sshExec("curl http://host.containers.internal:9090")
54 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
55 gomega.Expect(string(out)).To(gomega.ContainSubstring("Hello from the host"))
56
57 out, err = sshExec("curl http://host.docker.internal:9090")
58 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
59 gomega.Expect(string(out)).To(gomega.ContainSubstring("Hello from the host"))
60 })
61
62 ginkgo.It("should reach a http server in the VM using dynamic port forwarding", func() {
63 _, err := net.Dial("tcp", "127.0.0.1:9090")
64 gomega.Expect(err).Should(gomega.HaveOccurred())
65 gomega.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))
66
67 gomega.Expect(client.Expose(&types.ExposeRequest{
68 Local: "127.0.0.1:9090",
69 Remote: "192.168.127.2:8080",
70 })).Should(gomega.Succeed())
71
72 gomega.Eventually(func(g gomega.Gomega) {
73 resp, err := http.Get("http://127.0.0.1:9090")
74 g.Expect(err).ShouldNot(gomega.HaveOccurred())
75 g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
76 }).Should(gomega.Succeed())
77
78 gomega.Expect(client.Unexpose(&types.UnexposeRequest{
79 Local: "127.0.0.1:9090",
80 })).Should(gomega.Succeed())
81
82 gomega.Eventually(func(g gomega.Gomega) {
83 _, err = net.Dial("tcp", "127.0.0.1:9090")
84 g.Expect(err).Should(gomega.HaveOccurred())
85 g.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))
86 }).Should(gomega.Succeed())
87 })
88
89 ginkgo.It("should reach a dns server in the VM using dynamic port forwarding", func() {
90 gomega.Expect(client.Expose(&types.ExposeRequest{
91 Local: ":1053",
92 Remote: "192.168.127.2:53",
93 Protocol: "udp",
94 })).Should(gomega.Succeed())
95
96 gomega.Eventually(func(g gomega.Gomega) {
97 cmd := exec.Command("nslookup", "-timeout=1", "-port=1053", "foobar", "127.0.0.1")
98 out, err := cmd.CombinedOutput()
99 g.Expect(err).ShouldNot(gomega.HaveOccurred())
100 g.Expect(string(out)).To(gomega.ContainSubstring("Address: 1.2.3.4"))
101 }).Should(gomega.Succeed())
102
103 gomega.Expect(client.Unexpose(&types.UnexposeRequest{
104 Local: ":1053",
105 Protocol: "udp",
106 })).Should(gomega.Succeed())
107 })
108
109 ginkgo.It("should reach a http server in the VM using the tunneling of the daemon", func() {
110 httpClient := &http.Client{
111 Transport: &http.Transport{
112 DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
113 conn, err := net.Dial("unix", sock)
114 if err != nil {
115 return nil, err
116 }
117 return conn, transport.Tunnel(conn, "192.168.127.2", 8080)
118 },
119 },
120 }
121
122 gomega.Eventually(func(g gomega.Gomega) {
123 resp, err := httpClient.Get("http://placeholder/")
124 g.Expect(err).ShouldNot(gomega.HaveOccurred())
125 g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
126 }).Should(gomega.Succeed())
127 })
128
129 ginkgo.It("should reach a http server in the VM using dynamic port forwarding configured within the VM", func() {
130 _, err := net.Dial("tcp", "127.0.0.1:9090")
131 gomega.Expect(err).Should(gomega.HaveOccurred())
132 gomega.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))
133
134 _, err = sshExec(`curl http://gateway.containers.internal/services/forwarder/expose -X POST -d'{"local":":9090", "remote":":8080"}'`)
135 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
136
137 gomega.Eventually(func(g gomega.Gomega) {
138 resp, err := http.Get("http://127.0.0.1:9090")
139 g.Expect(err).ShouldNot(gomega.HaveOccurred())
140 g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
141 }).Should(gomega.Succeed())
142
143 _, err = sshExec(`curl http://gateway.containers.internal/services/forwarder/unexpose -X POST -d'{"local":":9090"}'`)
144 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
145
146 gomega.Eventually(func(g gomega.Gomega) {
147 _, err = net.Dial("tcp", "127.0.0.1:9090")
148 g.Expect(err).Should(gomega.HaveOccurred())
149 g.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))
150 }).Should(gomega.Succeed())
151 })
152
153 ginkgo.It("should reach rootless podman API using unix socket forwarding over ssh", func() {
154 httpClient := &http.Client{
155 Transport: &http.Transport{
156 DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
157 return net.Dial("unix", forwardSock)
158 },
159 },
160 }
161
162 gomega.Eventually(func(g gomega.Gomega) {
163 resp, err := httpClient.Get("http://host/_ping")
164 g.Expect(err).ShouldNot(gomega.HaveOccurred())
165 g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
166 g.Expect(resp.ContentLength).To(gomega.Equal(int64(2)))
167
168 reply := make([]byte, resp.ContentLength)
169 _, err = io.ReadAtLeast(resp.Body, reply, len(reply))
170
171 g.Expect(err).ShouldNot(gomega.HaveOccurred())
172 g.Expect(string(reply)).To(gomega.Equal("OK"))
173 }).Should(gomega.Succeed())
174 })
175
176 ginkgo.It("should reach rootful podman API using unix socket forwarding over ssh", func() {
177 httpClient := &http.Client{
178 Transport: &http.Transport{
179 DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
180 return net.Dial("unix", forwardRootSock)
181 },
182 },
183 }
184
185 gomega.Eventually(func(g gomega.Gomega) {
186 resp, err := httpClient.Get("http://host/_ping")
187 g.Expect(err).ShouldNot(gomega.HaveOccurred())
188 g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
189 g.Expect(resp.ContentLength).To(gomega.Equal(int64(2)))
190
191 reply := make([]byte, resp.ContentLength)
192 _, err = io.ReadAtLeast(resp.Body, reply, len(reply))
193
194 g.Expect(err).ShouldNot(gomega.HaveOccurred())
195 g.Expect(string(reply)).To(gomega.Equal("OK"))
196 }).Should(gomega.Succeed())
197 })
198
199 ginkgo.It("should expose and reach an http service using unix to tcp forwarding", func() {
200 if runtime.GOOS == "windows" {
201 ginkgo.Skip("AF_UNIX not supported on Windows")
202 }
203
204 unix2tcpfwdsock, _ := filepath.Abs(filepath.Join(tmpDir, "podman-unix-to-unix-forwarding.sock"))
205
206 out, err := sshExec(`curl http://gateway.containers.internal/services/forwarder/expose -X POST -d'{"protocol":"unix","local":"` + unix2tcpfwdsock + `","remote":"tcp://192.168.127.2:8080"}'`)
207 gomega.Expect(string(out)).Should(gomega.Equal(""))
208 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
209
210 gomega.Eventually(func(g gomega.Gomega) {
211 sockfile, err := os.Stat(unix2tcpfwdsock)
212 g.Expect(err).ShouldNot(gomega.HaveOccurred())
213 g.Expect(sockfile.Mode().Type().String()).To(gomega.Equal(os.ModeSocket.String()))
214 }).Should(gomega.Succeed())
215
216 httpClient := &http.Client{
217 Transport: &http.Transport{
218 DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
219 return net.Dial("unix", unix2tcpfwdsock)
220 },
221 },
222 }
223
224 gomega.Eventually(func(g gomega.Gomega) {
225 resp, err := httpClient.Get("http://placeholder/")
226 g.Expect(err).ShouldNot(gomega.HaveOccurred())
227 g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
228 }).Should(gomega.Succeed())
229
230 gomega.Eventually(func(g gomega.Gomega) {
231 // exposing the port again should return an error
232 out, _ = sshExec(`curl http://gateway.containers.internal/services/forwarder/expose -X POST -d'{"protocol":"unix","local":"` + unix2tcpfwdsock + `","remote":"tcp://192.168.127.2:8080"}'`)
233 g.Expect(string(out)).Should(gomega.ContainSubstring("proxy already running"))
234
235 // unexpose the port
236 out, err = sshExec(`curl http://gateway.containers.internal/services/forwarder/unexpose -X POST -d'{"protocol":"unix","local":"` + unix2tcpfwdsock + `","remote":"tcp://192.168.127.2:8080"}'`)
237 g.Expect(err).ShouldNot(gomega.HaveOccurred())
238 g.Expect(string(out)).Should(gomega.Equal(""))
239 }).Should(gomega.Succeed())
240 })
241
242 ginkgo.It("should expose and reach rootless podman API using unix to unix forwarding over ssh", func() {
243 if runtime.GOOS == "windows" {
244 ginkgo.Skip("AF_UNIX not supported on Windows")
245 }
246
247 unix2unixfwdsock, _ := filepath.Abs(filepath.Join(tmpDir, "podman-unix-to-unix-forwarding.sock"))
248
249 remoteuri := fmt.Sprintf(`ssh-tunnel://root@%s:%d%s?key=%s`, "192.168.127.2", 22, podmanSock, privateKeyFile)
250 _, err := sshExec(`curl http://192.168.127.1/services/forwarder/expose -X POST -d'{"protocol":"unix","local":"` + unix2unixfwdsock + `","remote":"` + remoteuri + `"}'`)
251 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
252
253 gomega.Eventually(func(g gomega.Gomega) {
254 sockfile, err := os.Stat(unix2unixfwdsock)
255 g.Expect(err).ShouldNot(gomega.HaveOccurred())
256 g.Expect(sockfile.Mode().Type().String()).To(gomega.Equal(os.ModeSocket.String()))
257 }).Should(gomega.Succeed())
258
259 httpClient := &http.Client{
260 Transport: &http.Transport{
261 DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
262 return net.Dial("unix", unix2unixfwdsock)
263 },
264 },
265 }
266
267 gomega.Eventually(func(g gomega.Gomega) {
268 resp, err := httpClient.Get("http://host/_ping")
269 g.Expect(err).ShouldNot(gomega.HaveOccurred())
270 g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
271 g.Expect(resp.ContentLength).To(gomega.Equal(int64(2)))
272
273 reply := make([]byte, resp.ContentLength)
274 _, err = io.ReadAtLeast(resp.Body, reply, len(reply))
275
276 g.Expect(err).ShouldNot(gomega.HaveOccurred())
277 g.Expect(string(reply)).To(gomega.Equal("OK"))
278 }).Should(gomega.Succeed())
279 })
280})