跳到主要内容

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

浏览文件

server.go

154 行 · 4.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package portfwdserver
5
6import (
7 "context"
8 "errors"
9 "io"
10 "net"
11 "sync"
12 "time"
13
14 "github.com/inetaf/tcpproxy"
15 "github.com/sirupsen/logrus"
16
17 "github.com/lima-vm/lima/v2/pkg/guestagent/api"
18)
19
20type TunnelServer struct{}
21
22func NewTunnelServer() *TunnelServer {
23 return &TunnelServer{}
24}
25
26func (s *TunnelServer) Start(stream api.GuestService_TunnelServer) error {
27 ctx := stream.Context()
28 // Receive the handshake message to start tunnel
29 in, err := stream.Recv()
30 if err != nil {
31 if errors.Is(err, io.EOF) {
32 return nil
33 }
34 return err
35 }
36
37 // We simply forward data form GRPC stream to net.Conn for both tcp and udp. So simple proxy is sufficient
38 var dialer net.Dialer
39 conn, err := dialer.DialContext(ctx, in.Protocol, in.GuestAddr)
40 if err != nil {
41 return err
42 }
43 // The tunnel is unusable once this function returns, so close the dialed
44 // connection here as well. This both releases the FD even if the proxy
45 // goroutine is still blocked reading from the guest, and unblocks that
46 // read so the goroutine can finish.
47 defer conn.Close()
48 rw := &GRPCServerRW{stream: stream, id: in.Id, closeCh: make(chan any)}
49 go func() {
50 <-ctx.Done()
51 rw.Close()
52 }()
53
54 proxy := tcpproxy.DialProxy{DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
55 return conn, nil
56 }}
57 go proxy.HandleConn(rw)
58
59 // The stream will be closed when this function returns.
60 // Wait here until rw.Close() or rw.CloseWrite() is called. A half-close
61 // from the host does not end the tunnel; see CloseRead below.
62 <-rw.closeCh
63 logrus.Debugf("closed GRPCServerRW for id: %s", in.Id)
64
65 return nil
66}
67
68type GRPCServerRW struct {
69 id string
70 stream api.GuestService_TunnelServer
71 closeCh chan any
72 // closeOnce guards closeCh. Close and CloseWrite may be called in any
73 // order and multiple times (e.g., tcpproxy calls CloseWrite from a copy
74 // direction and then Close), so they must be idempotent and must never
75 // block; otherwise the proxy goroutine gets stuck and never closes the
76 // dialed guest connection, leaking one FD per forwarded connection.
77 closeOnce sync.Once
78
79 // rxBuf holds bytes received from the stream that did not fit in the
80 // buffer passed to the previous Read call.
81 rxBuf []byte
82}
83
84var _ net.Conn = (*GRPCServerRW)(nil)
85
86func (g *GRPCServerRW) Write(p []byte) (n int, err error) {
87 err = g.stream.Send(&api.TunnelMessage{Id: g.id, Data: p})
88 return len(p), err
89}
90
91func (g *GRPCServerRW) Read(p []byte) (n int, err error) {
92 if len(g.rxBuf) == 0 {
93 in, err := g.stream.Recv()
94 if err != nil {
95 return 0, err
96 }
97 g.rxBuf = in.Data
98 }
99 n = copy(p, g.rxBuf)
100 g.rxBuf = g.rxBuf[n:]
101 return n, nil
102}
103
104func (g *GRPCServerRW) Close() error {
105 logrus.Debugf("closing GRPCServerRW for id: %s", g.id)
106 g.closeOnce.Do(func() { close(g.closeCh) })
107 return nil
108}
109
110// By adding CloseRead and CloseWrite methods, GRPCServerRW can work with
111// other than containers/gvisor-tap-vsock/pkg/tcpproxy, e.g., inetaf/tcpproxy, bicopy.Bicopy.
112
113// CloseRead is called once the host stops sending, which happens both when it
114// shuts down its write side and when it closes the connection outright. Both
115// reach us as io.EOF from stream.Recv, and only the latter also cancels the
116// stream context. Ending the tunnel here would therefore turn every half-close
117// into a full close and discard the response the host is still waiting for.
118// The proxy shuts down the write side of the guest connection by itself, so
119// there is nothing left to do: the tunnel lives on until the guest service
120// closes (CloseWrite) or the host cancels the stream (Close).
121func (g *GRPCServerRW) CloseRead() error {
122 logrus.Debugf("closing read GRPCServerRW for id: %s", g.id)
123 return nil
124}
125
126// CloseWrite is called once the guest service closes its side. The tunnel
127// protocol cannot signal a half-close towards the host, so this ends the whole
128// tunnel; the host then sees the stream end and shuts down the write side of
129// its own connection.
130func (g *GRPCServerRW) CloseWrite() error {
131 logrus.Debugf("closing write GRPCServerRW for id: %s", g.id)
132 g.closeOnce.Do(func() { close(g.closeCh) })
133 return nil
134}
135
136func (g *GRPCServerRW) LocalAddr() net.Addr {
137 return &net.UnixAddr{Name: "grpc", Net: "unixpacket"}
138}
139
140func (g *GRPCServerRW) RemoteAddr() net.Addr {
141 return &net.UnixAddr{Name: "grpc", Net: "unixpacket"}
142}
143
144func (g *GRPCServerRW) SetDeadline(_ time.Time) error {
145 return nil
146}
147
148func (g *GRPCServerRW) SetReadDeadline(_ time.Time) error {
149 return nil
150}
151
152func (g *GRPCServerRW) SetWriteDeadline(_ time.Time) error {
153 return nil
154}