跳到主要内容

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

浏览文件

ssh_forwarder.go

227 行 · 5.3 KB历史下载
1package sshclient
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "net"
8 "net/url"
9 "os"
10 "runtime"
11 "strings"
12 "sync"
13 "time"
14
15 "github.com/containers/gvisor-tap-vsock/pkg/fs"
16 "github.com/containers/gvisor-tap-vsock/pkg/utils"
17 "github.com/sirupsen/logrus"
18)
19
20type CloseWriteStream interface {
21 io.Reader
22 io.WriteCloser
23 CloseWrite() error
24}
25
26type CloseWriteConn interface {
27 net.Conn
28 CloseWriteStream
29}
30
31type SSHForward struct {
32 listener net.Listener
33 bastion *Bastion
34 sock *url.URL
35}
36
37type SSHDialer interface {
38 DialContextTCP(ctx context.Context, addr string) (net.Conn, error)
39}
40
41type genericTCPDialer struct {
42}
43
44var defaultTCPDialer genericTCPDialer
45
46func (dialer *genericTCPDialer) DialContextTCP(ctx context.Context, addr string) (net.Conn, error) {
47 var d net.Dialer
48 return d.DialContext(ctx, "tcp", addr)
49}
50
51func CreateSSHForward(ctx context.Context, src *url.URL, dest *url.URL, identity string, dialer SSHDialer) (*SSHForward, error) {
52 if dialer == nil {
53 dialer = &defaultTCPDialer
54 }
55
56 return setupProxy(ctx, src, dest, identity, "", dialer)
57}
58
59func CreateSSHForwardPassphrase(ctx context.Context, src *url.URL, dest *url.URL, identity string, passphrase string, dialer SSHDialer) (*SSHForward, error) {
60 if dialer == nil {
61 dialer = &defaultTCPDialer
62 }
63
64 return setupProxy(ctx, src, dest, identity, passphrase, dialer)
65}
66
67func (forward *SSHForward) AcceptAndTunnel(ctx context.Context) error {
68 return acceptConnection(ctx, forward.listener, forward.bastion, forward.sock)
69}
70
71func (forward *SSHForward) Tunnel(ctx context.Context) (CloseWriteConn, error) {
72 return connectForward(ctx, forward.bastion)
73}
74
75func (forward *SSHForward) Close() {
76 if forward.listener != nil {
77 forward.listener.Close()
78 }
79 if forward.bastion != nil {
80 forward.bastion.Close()
81 }
82}
83
84func connectForward(ctx context.Context, bastion *Bastion) (CloseWriteConn, error) {
85 for retries := 1; ; retries++ {
86 forward, err := bastion.Client.Dial("unix", bastion.Path)
87 if err == nil {
88 return forward.(CloseWriteConn), nil
89 }
90 if retries > 2 {
91 return nil, fmt.Errorf("couldn't reestablish ssh tunnel on path: %s: %w", bastion.Path, err)
92 }
93 // Check if ssh connection is still alive
94 _, _, err = bastion.Client.SendRequest("alive@gvproxy", true, nil)
95 if err != nil {
96 for bastionRetries := 1; ; bastionRetries++ {
97 err = bastion.Reconnect(ctx)
98 if err == nil {
99 break
100 }
101 if bastionRetries > 2 || !utils.Sleep(ctx, 200*time.Millisecond) {
102 return nil, fmt.Errorf("couldn't reestablish ssh connection: %s: %w", bastion.Host, err)
103 }
104 }
105 }
106
107 if !utils.Sleep(ctx, 200*time.Millisecond) {
108 retries = 3
109 }
110 }
111}
112
113func listenUnix(socketURI *url.URL) (net.Listener, error) {
114 path := socketURI.Path
115 if runtime.GOOS == "windows" {
116 path = strings.TrimPrefix(path, "/")
117 }
118
119 if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
120 return nil, err
121 }
122
123 oldmask := fs.Umask(0177)
124 defer fs.Umask(oldmask)
125 listener, err := net.Listen("unix", path)
126 if err != nil {
127 return listener, fmt.Errorf("error listening on socket: %s: %w", socketURI.Path, err)
128 }
129
130 return listener, nil
131}
132
133func setupProxy(ctx context.Context, socketURI *url.URL, dest *url.URL, identity string, passphrase string, dialer SSHDialer) (*SSHForward, error) {
134 var (
135 listener net.Listener
136 err error
137 )
138 switch socketURI.Scheme {
139 case "unix":
140 listener, err = listenUnix(socketURI)
141 if err != nil {
142 return &SSHForward{}, err
143 }
144 case "npipe":
145 listener, err = ListenNpipe(socketURI)
146 if err != nil {
147 return &SSHForward{}, err
148 }
149 case "":
150 // empty URL = Tunnel Only, no Accept
151 default:
152 return &SSHForward{}, fmt.Errorf("URI scheme not supported: %s", socketURI.Scheme)
153 }
154
155 connectFunc := func(ctx context.Context, bastion *Bastion) (net.Conn, error) {
156 timeout := 5 * time.Second
157 if bastion != nil {
158 timeout = bastion.Config.Timeout
159 }
160 ctx, cancel := context.WithTimeout(ctx, timeout)
161 conn, err := dialer.DialContextTCP(ctx, dest.Host)
162 if cancel != nil {
163 cancel()
164 }
165
166 return conn, err
167 }
168
169 createBastion := func() (*Bastion, error) {
170 conn, err := connectFunc(ctx, nil)
171 if err != nil {
172 return nil, err
173 }
174 return CreateBastion(dest, passphrase, identity, conn, connectFunc)
175 }
176 bastion, err := utils.Retry(ctx, createBastion, "Waiting for sshd")
177 if err != nil {
178 return &SSHForward{}, fmt.Errorf("setupProxy failed: %w", err)
179 }
180
181 logrus.Debugf("Socket forward established: %s -> %s\n", socketURI.Path, dest.Path)
182
183 return &SSHForward{listener, bastion, socketURI}, nil
184}
185
186func acceptConnection(ctx context.Context, listener net.Listener, bastion *Bastion, socketURI *url.URL) error {
187 con, err := listener.Accept()
188 if err != nil {
189 return fmt.Errorf("error accepting on socket: %s: %w", socketURI.Path, err)
190 }
191
192 src, ok := con.(CloseWriteStream)
193 if !ok {
194 con.Close()
195 return fmt.Errorf("underlying socket does not support half-close %s: %w", socketURI.Path, err)
196 }
197
198 var dest CloseWriteStream
199
200 dest, err = connectForward(ctx, bastion)
201 if err != nil {
202 con.Close()
203 logrus.Error(err)
204 return nil // eat
205 }
206
207 complete := new(sync.WaitGroup)
208 complete.Add(2)
209 go forward(src, dest, complete)
210 go forward(dest, src, complete)
211
212 go func() {
213 complete.Wait()
214 src.Close()
215 dest.Close()
216 }()
217
218 return nil
219}
220
221func forward(src io.ReadCloser, dest CloseWriteStream, complete *sync.WaitGroup) {
222 defer complete.Done()
223 _, _ = io.Copy(dest, src)
224
225 // Trigger an EOF on the other end
226 _ = dest.CloseWrite()
227}