跳到主要内容

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

浏览文件

scp.go

126 行 · 3.3 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package copytool
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "os/exec"
11
12 "github.com/coreos/go-semver/semver"
13
14 "github.com/lima-vm/lima/v2/pkg/limatype"
15 "github.com/lima-vm/lima/v2/pkg/sshutil"
16)
17
18type scpTool struct {
19 toolPath string
20 Options *Options
21}
22
23func newSCPTool(opts *Options) (*scpTool, error) {
24 path, err := exec.LookPath("scp")
25 if err != nil {
26 return nil, fmt.Errorf("scp not found on host: %w", err)
27 }
28 return &scpTool{toolPath: path, Options: opts}, nil
29}
30
31func (t *scpTool) Name() string {
32 return t.toolPath
33}
34
35func (t *scpTool) IsAvailableOnGuest(_ context.Context, _ []string) bool {
36 // scp is typically available on all systems with SSH
37 return true
38}
39
40func (t *scpTool) Command(ctx context.Context, paths []string, opts *Options) (*exec.Cmd, error) {
41 copyPaths, err := parseCopyPaths(ctx, paths)
42 if err != nil {
43 return nil, err
44 }
45
46 effectiveOpts := t.Options
47 if opts != nil {
48 effectiveOpts = opts
49 }
50
51 instances := make(map[string]*limatype.Instance)
52 scpFlags := []string{}
53 scpArgs := []string{}
54
55 if effectiveOpts.Verbose {
56 scpFlags = append(scpFlags, "-v")
57 } else {
58 scpFlags = append(scpFlags, "-q")
59 }
60
61 if effectiveOpts.Recursive {
62 scpFlags = append(scpFlags, "-r")
63 }
64
65 if effectiveOpts.AdditionalArgs != nil {
66 scpFlags = append(scpFlags, effectiveOpts.AdditionalArgs...)
67 }
68
69 // this assumes that ssh and scp come from the same place, but scp has no -V
70 sshExeForVersion, err := sshutil.NewSSHExe()
71 if err != nil {
72 return nil, err
73 }
74 legacySSH := sshutil.DetectOpenSSHVersion(ctx, sshExeForVersion).LessThan(*semver.New("8.0.0"))
75
76 for _, cp := range copyPaths {
77 if cp.IsRemote {
78 if legacySSH {
79 scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", cp.Instance.SSHLocalPort))
80 scpArgs = append(scpArgs, fmt.Sprintf("%s:%s", *cp.Instance.Config.User.Name+"@"+cp.Instance.SSHAddress, cp.Path))
81 } else {
82 scpArgs = append(scpArgs, fmt.Sprintf("scp://%s:%d/%s", *cp.Instance.Config.User.Name+"@"+cp.Instance.SSHAddress, cp.Instance.SSHLocalPort, cp.Path))
83 }
84 instances[cp.InstanceName] = cp.Instance
85 } else {
86 scpArgs = append(scpArgs, cp.Path)
87 }
88 }
89
90 if legacySSH && len(instances) > 1 {
91 return nil, errors.New("more than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher")
92 }
93
94 scpFlags = append(scpFlags, "-3", "--")
95 scpArgs = append(scpFlags, scpArgs...)
96
97 var sshOpts []string
98 if len(instances) == 1 {
99 // Only one (instance) host is involved; we can use the instance-specific
100 // arguments such as ControlPath. This is preferred as we can multiplex
101 // sessions without re-authenticating (MaxSessions permitting).
102 for _, inst := range instances {
103 sshExe, err := sshutil.NewSSHExe()
104 if err != nil {
105 return nil, err
106 }
107 sshOpts, err = sshutil.SSHOpts(ctx, sshExe, inst.Dir, *inst.Config.User.Name, false, false, false, false)
108 if err != nil {
109 return nil, err
110 }
111 }
112 } else {
113 // Copying among multiple hosts; we can't pass in host-specific options.
114 sshExe, err := sshutil.NewSSHExe()
115 if err != nil {
116 return nil, err
117 }
118 sshOpts, err = sshutil.CommonOpts(ctx, sshExe, false)
119 if err != nil {
120 return nil, err
121 }
122 }
123 sshArgs := sshutil.SSHArgsFromOpts(sshOpts)
124
125 return exec.CommandContext(ctx, t.toolPath, append(sshArgs, scpArgs...)...), nil
126}