| 1 | |
| 2 | |
| 3 | package maclogin |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | import "C" |
| 9 | |
| 10 | import ( |
| 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 | |
| 22 | type 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 | |
| 31 | func (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 | |
| 68 | func (probe *passwordProbe) close() { |
| 69 | probe.input.Close() |
| 70 | probe.output.Close() |
| 71 | probe.cancel() |
| 72 | _ = probe.command.Wait() |
| 73 | } |
| 74 | |
| 75 | func (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 | |
| 89 | func (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 | |
| 102 | func (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 | } |