| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "runtime" |
| 6 | |
| 7 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 8 | ) |
| 9 | |
| 10 | func (s *Server) macAccountHost() bool { |
| 11 | return runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" && s.localVMStorage() |
| 12 | } |
| 13 | |
| 14 | func (s *Server) accountStatus(w http.ResponseWriter, r *http.Request) { |
| 15 | v := s.account.viewFor(s.hasTrustedView(r)) |
| 16 | v.MacOSAccountHost = s.macAccountHost() |
| 17 | v.BackgroundRunning = s.vmRunning() |
| 18 | if s.macosGuest != nil { |
| 19 | v.GuestSystem = "macos" |
| 20 | v.InitialMacSetup = s.macSetupPending() |
| 21 | v.MacOSGuestUser = s.macosGuest.User |
| 22 | } |
| 23 | if s.windowsGuest != nil { |
| 24 | v.GuestSystem = "windows-11" |
| 25 | } |
| 26 | if v.MacOSAccountHost && v.Unlocked && !v.MacOSCredentialsReady && v.MacOSGuestUser == "" { |
| 27 | if install, err := guestos.LoadInstallation(s.profile); err == nil && install != nil { |
| 28 | if previous, err := s.account.legacyMacGuestPassword(r.Context(), install.ID); err == nil && previous != "" { |
| 29 | v.MacOSGuestUser = "cage" |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | jsonReply(w, v) |
| 34 | } |
| 35 | |
| 36 | func (s *Server) accountAction(w http.ResponseWriter, r *http.Request) { |
| 37 | if s.account == nil { |
| 38 | http.Error(w, "本地账户尚未初始化", 503) |
| 39 | return |
| 40 | } |
| 41 | action := r.PathValue("action") |
| 42 | if action != "prepare-macos" && action != "create" && action != "unlock" && action != "profile" && action != "password" && action != "lock" && action != "logout" && action != "suspend" && action != "device-enroll" && action != "device-revoke" && action != "device-unlock" { |
| 43 | http.NotFound(w, r) |
| 44 | return |
| 45 | } |
| 46 | if action != "create" && action != "unlock" && action != "device-unlock" { |
| 47 | if _, ok := s.account.accessFor(s.hasTrustedView(r)); !ok { |
| 48 | http.Error(w, "请先解锁 Cage", http.StatusLocked) |
| 49 | return |
| 50 | } |
| 51 | } |
| 52 | if (action == "lock" || action == "logout" || action == "device-enroll") && s.account.view().TrustedLocal { |
| 53 | http.Error(w, "请先在本机的「设置 → 账户」设置密码", http.StatusConflict) |
| 54 | return |
| 55 | } |
| 56 | var input struct { |
| 57 | Username string `json:"username"` |
| 58 | Password string `json:"password"` |
| 59 | ConfirmPassword string `json:"confirm_password"` |
| 60 | OldPassword string `json:"old_password"` |
| 61 | Avatar string `json:"avatar"` |
| 62 | Kind string `json:"kind"` |
| 63 | Credential deviceCredential `json:"credential"` |
| 64 | } |
| 65 | if !readJSON(w, r, &input, 3<<20) { |
| 66 | return |
| 67 | } |
| 68 | s.account.mu.Lock() |
| 69 | vaultLease := s.account.vaultLease |
| 70 | s.account.mu.Unlock() |
| 71 | var err error |
| 72 | if action == "password" || action == "prepare-macos" { |
| 73 | if !s.op.TryLock() { |
| 74 | http.Error(w, "环境操作仍在进行,请等待完成后设置密码", http.StatusConflict) |
| 75 | return |
| 76 | } |
| 77 | defer s.op.Unlock() |
| 78 | if s.busy() { |
| 79 | http.Error(w, "环境操作仍在进行,请等待完成后设置密码", http.StatusConflict) |
| 80 | return |
| 81 | } |
| 82 | } |
| 83 | switch action { |
| 84 | case "create": |
| 85 | if input.Password != input.ConfirmPassword { |
| 86 | http.Error(w, "两次密码不一致", 400) |
| 87 | return |
| 88 | } |
| 89 | err = s.account.createAccount(input.Username, input.Password, input.Avatar, s.macAccountHost()) |
| 90 | case "prepare-macos": |
| 91 | if !s.macAccountHost() || !s.hasTrustedView(r) { |
| 92 | http.Error(w, "请在本机 Cage 窗口设置共用账密", http.StatusForbidden) |
| 93 | return |
| 94 | } |
| 95 | if input.Password != input.ConfirmPassword { |
| 96 | http.Error(w, "两次密码不一致", http.StatusBadRequest) |
| 97 | return |
| 98 | } |
| 99 | guests := map[string]macGuestCredential{} |
| 100 | install, loadErr := guestos.LoadInstallation(s.profile) |
| 101 | if loadErr != nil { |
| 102 | requestError(w, loadErr) |
| 103 | return |
| 104 | } |
| 105 | if s.macosGuest != nil && install == nil { |
| 106 | http.Error(w, "原客机安装记录缺失,请恢复备份后继续", http.StatusConflict) |
| 107 | return |
| 108 | } |
| 109 | if install != nil { |
| 110 | previous, loadErr := s.account.savedMacGuestPassword(r.Context(), install.ID) |
| 111 | if loadErr != nil { |
| 112 | requestError(w, loadErr) |
| 113 | return |
| 114 | } |
| 115 | if s.macosGuest != nil && previous == "" { |
| 116 | http.Error(w, "原客机凭据缺失,请恢复加密保险箱后继续", http.StatusConflict) |
| 117 | return |
| 118 | } |
| 119 | if previous != "" { |
| 120 | user := "cage" |
| 121 | if s.macosGuest != nil { |
| 122 | user = s.macosGuest.User |
| 123 | } |
| 124 | if input.Username != user { |
| 125 | http.Error(w, "请使用现有客机用户名;安装记录和原文件已保留", http.StatusConflict) |
| 126 | return |
| 127 | } |
| 128 | guests[install.ID.String()] = macGuestCredential{Username: user, Password: previous} |
| 129 | } |
| 130 | } |
| 131 | err = s.account.prepareMacAccount(input.Username, input.Password, guests) |
| 132 | case "unlock": |
| 133 | err = s.account.unlock(input.Password) |
| 134 | case "profile": |
| 135 | err = s.account.updateProfile(input.Username, input.Avatar) |
| 136 | case "password": |
| 137 | if input.Password != input.ConfirmPassword { |
| 138 | http.Error(w, "两次密码不一致", 400) |
| 139 | return |
| 140 | } |
| 141 | err = s.account.changePassword(input.OldPassword, input.Password) |
| 142 | case "lock": |
| 143 | err = s.account.lockAccess(false) |
| 144 | case "suspend": |
| 145 | s.account.lock() |
| 146 | case "device-enroll": |
| 147 | var credential deviceCredential |
| 148 | credential, err = s.account.enrollDevice(input.Kind) |
| 149 | if err == nil { |
| 150 | jsonReply(w, credential) |
| 151 | return |
| 152 | } |
| 153 | case "device-revoke": |
| 154 | err = s.account.revokeDevice(input.Kind) |
| 155 | case "device-unlock": |
| 156 | err = s.account.unlockDevice(input.Kind, input.Credential) |
| 157 | case "logout": |
| 158 | s.requestStop(w, true) |
| 159 | return |
| 160 | } |
| 161 | if err != nil { |
| 162 | requestError(w, err) |
| 163 | return |
| 164 | } |
| 165 | if action == "lock" || action == "suspend" { |
| 166 | s.clearBrowserVault(vaultLease) |
| 167 | } |
| 168 | if (action == "password" || action == "prepare-macos") && s.macosGuest != nil && s.vmRunning() { |
| 169 | |
| 170 | |
| 171 | _ = s.syncMacGuestPassword(r.Context()) |
| 172 | } |
| 173 | s.accountStatus(w, r) |
| 174 | } |