跳到主要内容

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

浏览文件

password_target_darwin_arm64.go

150 行 · 4.0 KB历史下载
1//go:build darwin && arm64 && cgo && !no_vz
2
3package maclogin
4
5/*
6#include "bridge.h"
7*/
8import "C"
9
10import (
11 "bufio"
12 "context"
13 "encoding/json"
14 "errors"
15 "io"
16 "os/exec"
17 "time"
18
19 "github.com/sylvinus/agent-vm/internal/guestos"
20)
21
22type passwordProbe struct {
23 command *exec.Cmd
24 input io.WriteCloser
25 output io.ReadCloser
26 scanner *bufio.Scanner
27 cancel context.CancelFunc
28 user string
29}
30
31func (s *loginSession) openPasswordProbe() (*passwordProbe, error) {
32 bound, err := guestos.LoadMacOSProfile(s.profile)
33 if err != nil || bound == nil || bound.SetupID == "" {
34 return nil, errors.New("首次设置身份已变化,请重新打开授权画面")
35 }
36 administrator, err := guestos.MacOSProvisionUser(s.profile)
37 if err != nil {
38 return nil, err
39 }
40 ctx, cancel := context.WithTimeout(context.Background(), 7*time.Second)
41 command, err := guestGUIProbeCommand(ctx, s.profile, administrator, bound.User, "--setup-password-probe")
42 if err != nil {
43 cancel()
44 return nil, err
45 }
46 input, err := command.StdinPipe()
47 if err != nil {
48 cancel()
49 return nil, err
50 }
51 output, err := command.StdoutPipe()
52 if err != nil {
53 input.Close()
54 cancel()
55 return nil, err
56 }
57 if err = command.Start(); err != nil {
58 input.Close()
59 output.Close()
60 cancel()
61 return nil, err
62 }
63 scanner := bufio.NewScanner(output)
64 scanner.Buffer(make([]byte, 4096), 32<<10)
65 return &passwordProbe{command: command, input: input, output: output, scanner: scanner, cancel: cancel, user: bound.User}, nil
66}
67
68func (probe *passwordProbe) close() {
69 probe.input.Close()
70 probe.output.Close()
71 probe.cancel()
72 _ = probe.command.Wait()
73}
74
75func (probe *passwordProbe) target() (passwordTarget, error) {
76 var target passwordTarget
77 if _, err := io.WriteString(probe.input, "{\"action\":\"probe\"}\n"); err != nil {
78 return target, err
79 }
80 if !probe.scanner.Scan() {
81 return target, errors.New("无法确认系统授权窗口,请重新打开授权画面")
82 }
83 if json.Unmarshal(probe.scanner.Bytes(), &target) != nil || !target.valid(probe.user) {
84 return target, errors.New("请打开 Apple 系统授权窗口,再选中需要填写的密码框")
85 }
86 return target, nil
87}
88
89func (s *loginSession) selectPasswordPosition(x, y int) {
90 s.passwordSelection = passwordSelection{}
91 probe, err := s.openPasswordProbe()
92 if err != nil {
93 return
94 }
95 defer probe.close()
96 target, err := probe.target()
97 if err == nil && target.SelectionRequired && target.contains(x, y) {
98 s.passwordSelection = passwordSelection{Geometry: target.geometry(), X: x, Y: y, Clicked: time.Now()}
99 }
100}
101
102func (s *loginSession) fillSetupPassword(password string) error {
103 selection := s.passwordSelection
104 s.passwordSelection = passwordSelection{}
105 sequence, valid := passwordKeys(password)
106 if !s.setup || !valid {
107 return errors.New("首次设置凭据不可用,请重新打开授权画面")
108 }
109 probe, err := s.openPasswordProbe()
110 if err != nil {
111 return err
112 }
113 defer probe.close()
114 target, err := probe.target()
115 if err != nil {
116 return err
117 }
118 if !selection.permits(target, probe.user, time.Now()) {
119 return errors.New("请先点选系统授权窗口的密码框,再点击填写授权密码")
120 }
121 if !target.Accessibility && !bool(C.cageLoginClick(C.int(selection.X), C.int(selection.Y))) {
122 return errors.New("所选密码框已变化,请重新点选")
123 }
124 geometry := target.geometry()
125 check := func() error {
126 current, err := probe.target()
127 if err != nil {
128 return err
129 }
130 if current.geometry() != geometry || !selection.permits(current, probe.user, time.Now()) {
131 return errors.New("系统授权窗口或密码框已变化,填写已停止,请重新选择")
132 }
133 return nil
134 }
135 if err = check(); err != nil {
136 return err
137 }
138 if !bool(C.cageLoginSelectAll()) {
139 return errors.New("系统密码框尚未准备好,请重试")
140 }
141 for _, key := range sequence {
142 if err = check(); err != nil {
143 return err
144 }
145 if !bool(C.cageLoginKey(C.int(key.code), C.bool(key.shift))) {
146 return errors.New("授权密码未能填写,请重新选择密码框后重试")
147 }
148 }
149 return nil
150}