跳到主要内容

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

浏览文件

client.go

154 行 · 4.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package portfwd
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "net"
11 "sync/atomic"
12 "time"
13
14 "github.com/containers/gvisor-tap-vsock/pkg/services/forwarder"
15 "github.com/inetaf/tcpproxy"
16 "github.com/sirupsen/logrus"
17
18 "github.com/lima-vm/lima/v2/pkg/guestagent/api"
19 guestagentclient "github.com/lima-vm/lima/v2/pkg/guestagent/api/client"
20)
21
22func HandleTCPConnection(_ context.Context, dialContext func(ctx context.Context, network string, addr string) (net.Conn, error), conn net.Conn, guestAddr string) {
23 proxy := tcpproxy.DialProxy{Addr: guestAddr, DialContext: dialContext}
24 proxy.HandleConn(conn)
25 logrus.Debugf("tcp proxy for guestAddr: %s closed", guestAddr)
26}
27
28func HandleUDPConnection(ctx context.Context, dialContext func(ctx context.Context, network string, addr string) (net.Conn, error), conn net.PacketConn, guestAddr string) {
29 proxy, err := forwarder.NewUDPProxy(conn, func() (net.Conn, error) {
30 return dialContext(ctx, "udp", guestAddr)
31 })
32 if err != nil {
33 logrus.WithError(err).Error("error in udp tunnel proxy")
34 return
35 }
36
37 defer func() {
38 err := proxy.Close()
39 if err != nil {
40 logrus.WithError(err).Error("error in closing udp tunnel proxy")
41 }
42 }()
43 proxy.Run()
44 logrus.Debugf("udp proxy for guestAddr: %s closed", guestAddr)
45}
46
47func DialContextToGRPCTunnel(client *guestagentclient.GuestAgentClient) func(ctx context.Context, network, addr string) (net.Conn, error) {
48 // gvisor-tap-vsock's UDPProxy demultiplexes client connections internally based on their source address.
49 // It calls this dialer function only when it receives a datagram from a new, unrecognized client.
50 // For each new client, we must return a new net.Conn, which in our case is a new gRPC stream.
51 // The atomic counter ensures that each stream has a unique ID to distinguish them on the server side.
52 var connectionCounter atomic.Uint32
53 return func(_ context.Context, network, addr string) (net.Conn, error) {
54 // Passed context.Context is used for timeout on initiate connection, not for the lifetime of the connection.
55 // Use a dedicated context so CloseRead can unblock a goroutine waiting in stream.Recv().
56 streamCtx, cancel := context.WithCancel(context.Background())
57 stream, err := client.Tunnel(streamCtx)
58 if err != nil {
59 cancel()
60 return nil, fmt.Errorf("could not open tunnel for addr: %s error:%w", addr, err)
61 }
62 // Handshake message to start tunnel
63 id := fmt.Sprintf("%s-%s-%d", network, addr, connectionCounter.Add(1))
64 if err := stream.Send(&api.TunnelMessage{Id: id, Protocol: network, GuestAddr: addr}); err != nil {
65 cancel()
66 return nil, fmt.Errorf("could not start tunnel for id: %s addr: %s error:%w", id, addr, err)
67 }
68 rw := &GrpcClientRW{stream: stream, cancel: cancel, id: id, addr: addr, protocol: network}
69 return rw, nil
70 }
71}
72
73type GrpcClientRW struct {
74 id string
75 addr string
76
77 protocol string
78 stream api.GuestService_TunnelClient
79 cancel context.CancelFunc
80
81 // rxBuf holds bytes received from the stream that did not fit in the
82 // buffer passed to the previous Read call.
83 rxBuf []byte
84}
85
86var _ net.Conn = (*GrpcClientRW)(nil)
87
88func (g *GrpcClientRW) Write(p []byte) (n int, err error) {
89 err = g.stream.Send(&api.TunnelMessage{
90 Id: g.id,
91 GuestAddr: g.addr,
92 Data: p,
93 Protocol: g.protocol,
94 })
95 if err != nil {
96 return 0, err
97 }
98 return len(p), nil
99}
100
101func (g *GrpcClientRW) Read(p []byte) (n int, err error) {
102 if len(g.rxBuf) == 0 {
103 in, err := g.stream.Recv()
104 if err != nil {
105 return 0, err
106 }
107 g.rxBuf = in.Data
108 }
109 n = copy(p, g.rxBuf)
110 g.rxBuf = g.rxBuf[n:]
111 return n, nil
112}
113
114func (g *GrpcClientRW) Close() error {
115 logrus.Debugf("closing GrpcClientRW for id: %s", g.id)
116 err := g.stream.CloseSend()
117 g.cancel()
118 return err
119}
120
121func (g *GrpcClientRW) CloseRead() error {
122 logrus.Debugf("closing read GrpcClientRW for id: %s", g.id)
123 g.cancel()
124 return nil
125}
126
127func (g *GrpcClientRW) CloseWrite() error {
128 logrus.Debugf("closing write GrpcClientRW for id: %s", g.id)
129 err := g.stream.CloseSend()
130 if errors.Is(err, context.Canceled) {
131 return nil
132 }
133 return err
134}
135
136func (g *GrpcClientRW) LocalAddr() net.Addr {
137 return &net.UnixAddr{Name: "grpc", Net: "unixpacket"}
138}
139
140func (g *GrpcClientRW) RemoteAddr() net.Addr {
141 return &net.UnixAddr{Name: "grpc", Net: "unixpacket"}
142}
143
144func (g *GrpcClientRW) SetDeadline(_ time.Time) error {
145 return nil
146}
147
148func (g *GrpcClientRW) SetReadDeadline(_ time.Time) error {
149 return nil
150}
151
152func (g *GrpcClientRW) SetWriteDeadline(_ time.Time) error {
153 return nil
154}