跳到主要内容

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

浏览文件

client.go

66 行 · 1.3 KB历史下载
1package main
2
3import (
4 "fmt"
5 "net"
6 "os"
7 "time"
8
9 "golang.org/x/crypto/ssh"
10)
11
12type client struct {
13 Conn net.Conn
14 Config *ssh.ClientConfig
15}
16
17func newClient(conn net.Conn, user string, key string) (*client, error) {
18 config, err := newConfig(user, key)
19 if err != nil {
20 return nil, fmt.Errorf("error getting config for native Go SSH: %s", err)
21 }
22
23 return &client{
24 Conn: conn,
25 Config: config,
26 }, nil
27}
28
29func newConfig(user string, keyFile string) (*ssh.ClientConfig, error) {
30 key, err := os.ReadFile(keyFile)
31 if err != nil {
32 return nil, err
33 }
34 privateKey, err := ssh.ParsePrivateKey(key)
35 if err != nil {
36 return nil, err
37 }
38 return &ssh.ClientConfig{
39 User: user,
40 Auth: []ssh.AuthMethod{ssh.PublicKeys(privateKey)},
41 // #nosec G106
42 HostKeyCallback: ssh.InsecureIgnoreHostKey(),
43 Timeout: time.Minute,
44 }, nil
45}
46
47func (client *client) output(command string) (string, error) {
48 c, chans, reqs, err := ssh.NewClientConn(client.Conn, "", client.Config)
49 if err != nil {
50 return "", err
51 }
52 conn := ssh.NewClient(c, chans, reqs)
53 session, err := conn.NewSession()
54 if err != nil {
55 _ = conn.Close()
56 return "", err
57 }
58 defer conn.Close()
59 defer session.Close()
60
61 output, err := session.CombinedOutput(command)
62 if err != nil {
63 return "", err
64 }
65 return string(output), nil
66}