跳到主要内容

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

浏览文件

ssh.go

112 行 · 3.2 KB历史下载
1package ssh
2
3import (
4 "bufio"
5 "bytes"
6 "errors"
7 "fmt"
8 "os"
9 "os/exec"
10 "strconv"
11 "strings"
12
13 "github.com/sirupsen/logrus"
14)
15
16type SSHConfig struct {
17 ConfigFile string
18 Persist bool
19 AdditionalArgs []string
20}
21
22func (c *SSHConfig) Binary() string {
23 return "ssh"
24}
25
26func (c *SSHConfig) Args() []string {
27 var args []string
28 if c.ConfigFile != "" {
29 args = append(args, "-F", c.ConfigFile)
30 }
31 if c.Persist {
32 args = append(args,
33 "-o", "ControlMaster=auto",
34 // TODO: Does this work on Windows?
35 "-o", "ControlPath=~/.ssh/sshocker-%r@%h:%p-"+strconv.Itoa(os.Getpid()),
36 "-o", "ControlPersist=yes",
37 )
38 }
39 args = append(args, c.AdditionalArgs...)
40 return args
41}
42
43// ExitMaster executes `ssh -O exit`
44func ExitMaster(host string, port int, c *SSHConfig) error {
45 if c == nil {
46 return errors.New("got nil SSHConfig")
47 }
48 args := c.Args()
49 args = append(args, "-O", "exit")
50 if port != 0 {
51 args = append(args, "-p", strconv.Itoa(port))
52 }
53 args = append(args, host)
54 cmd := exec.Command(c.Binary(), args...)
55 logrus.Debugf("executing ssh for exiting the master: %s %v", cmd.Path, cmd.Args)
56 out, err := cmd.CombinedOutput()
57 if err != nil {
58 return fmt.Errorf("failed to execute `%s -O exit -p %d %s`, out=%q: %w", c.Binary(), port, host, string(out), err)
59 }
60 return nil
61}
62
63// ParseScriptInterpreter extracts "#!/bin/sh" interpreter string from the script.
64// The result does not contain the "#!" prefix.
65func ParseScriptInterpreter(script string) (string, error) {
66 r := bufio.NewReader(strings.NewReader(script))
67 firstLine, partial, err := r.ReadLine()
68 if err != nil {
69 return "", fmt.Errorf("cannot determine interpreter from script %q: %w", script, err)
70 }
71 if partial {
72 return "", fmt.Errorf("cannot determine interpreter from script %q: cannot read the first line", script)
73 }
74 if !strings.HasPrefix(string(firstLine), "#!") {
75 return "", fmt.Errorf("cannot determine interpreter from script %q: the first line lacks `#!`", script)
76 }
77 interp := strings.TrimPrefix(string(firstLine), "#!")
78 if interp == "" {
79 return "", fmt.Errorf("cannot determine interpreter from script %q: empty?", script)
80 }
81 return interp, nil
82}
83
84// ExecuteScript executes the given script on the remote host via stdin.
85// Returns stdout and stderr.
86//
87// scriptName is used only for readability of error strings.
88func ExecuteScript(host string, port int, c *SSHConfig, script, scriptName string) (string, string, error) {
89 if c == nil {
90 return "", "", errors.New("got nil SSHConfig")
91 }
92 interpreter, err := ParseScriptInterpreter(script)
93 if err != nil {
94 return "", "", err
95 }
96 sshBinary := c.Binary()
97 sshArgs := c.Args()
98 if port != 0 {
99 sshArgs = append(sshArgs, "-p", strconv.Itoa(port))
100 }
101 sshArgs = append(sshArgs, host, "--", interpreter)
102 sshCmd := exec.Command(sshBinary, sshArgs...)
103 sshCmd.Stdin = strings.NewReader(script)
104 var stderr bytes.Buffer
105 sshCmd.Stderr = &stderr
106 logrus.Debugf("executing ssh for script %q: %s %v", scriptName, sshCmd.Path, sshCmd.Args)
107 out, err := sshCmd.Output()
108 if err != nil {
109 return string(out), stderr.String(), fmt.Errorf("failed to execute script %q: stdout=%q, stderr=%q: %w", scriptName, string(out), stderr.String(), err)
110 }
111 return string(out), stderr.String(), nil
112}