跳到主要内容

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

浏览文件

macos_password_sync.go

86 行 · 3.0 KB历史下载
1package workbench
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "github.com/google/uuid"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "regexp"
13 "strings"
14 "time"
15
16 "al.essio.dev/pkg/shellescape"
17 "github.com/sylvinus/agent-vm/internal/guestos"
18 "github.com/sylvinus/agent-vm/internal/host"
19)
20
21func (s *Server) syncMacGuestPassword(ctx context.Context) error {
22 if s.macosGuest == nil || s.account == nil {
23 return nil
24 }
25 install, err := guestos.LoadInstallation(s.profile)
26 if err != nil {
27 return err
28 }
29 if install == nil {
30 return nil
31 }
32 return s.account.syncMacPassword(ctx, install.ID, s.macosGuest.User, func(ctx context.Context, oldPassword, newPassword string) error {
33 return s.applyMacGuestPassword(ctx, install.ID, oldPassword, newPassword)
34 })
35}
36
37func (s *Server) applyMacGuestPassword(ctx context.Context, id uuid.UUID, oldPassword, newPassword string) error {
38 ctx, cancel := context.WithTimeout(ctx, 45*time.Second)
39 defer cancel()
40 provision, err := guestos.MacOSProvisionUser(s.profile)
41 if err != nil {
42 return err
43 }
44 if provision != "cageprovision" {
45 return errors.New("客机置备身份不匹配")
46 }
47 args, err := guestos.MacOSSSHArgs(s.profile, provision)
48 if err != nil {
49 return err
50 }
51 command := func(ctx context.Context, script string) *exec.Cmd {
52 cmd := exec.CommandContext(ctx, sshExecutable(), append(append([]string{}, args...), "-T", "cage-macos", "--", "exec /bin/sh -ec "+shellescape.Quote(script))...)
53 cmd.Env = sshEnvironment()
54 host.HideCommandWindow(cmd)
55 return cmd
56 }
57 // Use this build's helper so older managed guests can adopt the shared
58 // credential without reinstalling their applications or personal disk.
59 helper, err := os.ReadFile(filepath.Join(filepath.Dir(s.exe), "guest-os", "cage-finish-initial-setup"))
60 if err != nil {
61 return err
62 }
63 out, err := command(ctx, `umask 077; mktemp -d /private/var/tmp/cage-account.XXXXXXXX`).Output()
64 stage := strings.TrimSpace(string(out))
65 if err != nil || !regexp.MustCompile(`^/private/var/tmp/cage-account\.[A-Za-z0-9]{8}$`).MatchString(stage) {
66 return errors.New("无法准备客机改密组件")
67 }
68 admin := " < /Users/" + provision + "/password"
69 defer func() {
70 cleanup, cancel := context.WithTimeout(context.Background(), 5*time.Second)
71 defer cancel()
72 _ = command(cleanup, `sudo -S -p '' /bin/rm -rf `+stage+admin).Run()
73 }()
74 upload := command(ctx, `umask 077; cat > `+stage+`/helper`)
75 upload.Stdin = bytes.NewReader(helper)
76 if err = upload.Run(); err != nil {
77 return err
78 }
79 script := `chown -R root:wheel ` + stage + `; chmod 700 ` + stage + ` ` + stage + `/helper; chmod 600 ` + stage + `/input; exec ` + stage + `/helper password ` + id.String() + ` ` + s.macosGuest.User + ` < ` + stage + `/input`
80 apply := command(ctx, `umask 077; cat > `+stage+`/input; sudo -S -p '' /bin/sh -ec `+shellescape.Quote(script)+admin)
81 apply.Stdin = strings.NewReader(oldPassword + "\n" + newPassword + "\n")
82 if out, err := apply.CombinedOutput(); err != nil {
83 return fmt.Errorf("guest password update: %w: %.2048s", err, out)
84 }
85 return nil
86}