跳到主要内容

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

浏览文件

sshocker.go

92 行 · 2.5 KB历史下载
1package sshocker
2
3import (
4 "errors"
5 "fmt"
6 "os"
7 "os/exec"
8 "strconv"
9
10 "github.com/lima-vm/sshocker/pkg/mount"
11 "github.com/lima-vm/sshocker/pkg/reversesshfs"
12 "github.com/lima-vm/sshocker/pkg/ssh"
13 "github.com/sirupsen/logrus"
14)
15
16type Sshocker struct {
17 *ssh.SSHConfig
18 Host string // Required
19 Port int // Required
20 Command []string // Optional
21 Mounts []mount.Mount
22 LForwards []string
23 SSHFSAdditionalArgs []string
24 Driver reversesshfs.Driver
25 OpensshSftpServerBinary string
26}
27
28func (x *Sshocker) Run() error {
29 if x.SSHConfig == nil {
30 return errors.New("got nil SSHConfig")
31 }
32 sshBinary := x.SSHConfig.Binary()
33 args := x.SSHConfig.Args()
34 for _, l := range x.LForwards {
35 args = append(args, "-L", l)
36 }
37 if x.Port != 0 {
38 args = append(args, "-p", strconv.Itoa(x.Port))
39 }
40 args = append(args, x.Host, "--")
41 if len(x.Command) > 0 {
42 args = append(args, x.Command...)
43 }
44 cmd := exec.Command(sshBinary, args...)
45 cmd.Stdin = os.Stdin
46 cmd.Stdout = os.Stdout
47 cmd.Stderr = os.Stderr
48 cmd.Env = os.Environ()
49 for _, m := range x.Mounts {
50 switch m.Type {
51 case mount.MountTypeReverseSSHFS:
52 rsf := &reversesshfs.ReverseSSHFS{
53 Driver: x.Driver,
54 OpensshSftpServerBinary: x.OpensshSftpServerBinary,
55 SSHConfig: x.SSHConfig,
56 LocalPath: m.Source,
57 Host: x.Host,
58 Port: x.Port,
59 RemotePath: m.Destination,
60 Readonly: m.Readonly,
61 SSHFSAdditionalArgs: x.SSHFSAdditionalArgs,
62 }
63 if err := rsf.Prepare(); err != nil {
64 return fmt.Errorf("failed to prepare mounting %q (local) onto %q (remote): %w", rsf.LocalPath, rsf.RemotePath, err)
65 }
66 if err := rsf.Start(); err != nil {
67 return fmt.Errorf("failed to mount %q (local) onto %q (remote): %w", rsf.LocalPath, rsf.RemotePath, err)
68 }
69 defer func() {
70 if cErr := rsf.Close(); cErr != nil {
71 logrus.WithError(cErr).Warnf("failed to unmount %q (remote)", rsf.RemotePath)
72 }
73 }()
74 case mount.MountTypeInvalid:
75 return fmt.Errorf("invalid mount type %v", m.Type)
76 default:
77 return fmt.Errorf("unknown mount type %v", m.Type)
78 }
79 }
80 defer func() {
81 if x.SSHConfig.Persist {
82 if emErr := ssh.ExitMaster(x.Host, x.Port, x.SSHConfig); emErr != nil {
83 logrus.WithError(emErr).Error("failed to exit the master")
84 }
85 }
86 }()
87 logrus.Debugf("executing main SSH: %s %v", cmd.Path, cmd.Args)
88 if err := cmd.Run(); err != nil {
89 return err
90 }
91 return nil
92}