| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/cage" |
| 12 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 13 | ) |
| 14 | |
| 15 | type initializationRecord struct { |
| 16 | Version int `json:"version"` |
| 17 | Completed bool `json:"completed"` |
| 18 | Started bool `json:"started,omitempty"` |
| 19 | } |
| 20 | |
| 21 | type initializationView struct { |
| 22 | Required bool `json:"required"` |
| 23 | Started bool `json:"started"` |
| 24 | Tracked bool `json:"tracked"` |
| 25 | Prepared bool `json:"prepared"` |
| 26 | } |
| 27 | |
| 28 | func (s *Server) initializationState() (initializationView, error) { |
| 29 | if !s.localVMStorage() || s.storageRecoveryStatus().Required { |
| 30 | return initializationView{}, nil |
| 31 | } |
| 32 | data, err := os.ReadFile(filepath.Join(s.profile, "initialization.json")) |
| 33 | if err == nil { |
| 34 | var record initializationRecord |
| 35 | if json.Unmarshal(data, &record) != nil || record.Version != 1 { |
| 36 | return initializationView{}, errors.New("初始化记录无法读取,请保留现有数据并导出诊断包") |
| 37 | } |
| 38 | return initializationView{Required: !record.Completed || s.macSetupPending(), Started: record.Started, Tracked: true, Prepared: s.environmentPrepared()}, nil |
| 39 | } |
| 40 | if !errors.Is(err, os.ErrNotExist) { |
| 41 | return initializationView{}, err |
| 42 | } |
| 43 | |
| 44 | |
| 45 | pending, err := guestos.LoadInstallation(s.profile) |
| 46 | if err != nil { |
| 47 | return initializationView{}, err |
| 48 | } |
| 49 | if s.macosGuest != nil || s.windowsGuest != nil { |
| 50 | return initializationView{Required: s.macSetupPending()}, nil |
| 51 | } |
| 52 | return initializationView{Required: pending != nil || !s.environmentPrepared(), Started: pending != nil}, nil |
| 53 | } |
| 54 | |
| 55 | func (s *Server) finishInitialization() error { |
| 56 | if s.macSetupPending() { |
| 57 | return errors.New("请先完成客机的系统权限确认") |
| 58 | } |
| 59 | s.mu.Lock() |
| 60 | ready, gate := s.ready, s.safety.Gate |
| 61 | s.mu.Unlock() |
| 62 | if !ready || !s.vmRunning() { |
| 63 | return errors.New("客机尚未就绪,请继续安装并完成启动检查") |
| 64 | } |
| 65 | cfg, err := loadConfig(s.configPath) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | if err := cfg.RequireManagedProxy(s.profile); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if gate == nil || gate.Mode != cage.GateOpen || !gate.PolicyVerified || gate.Fault || gate.Report == nil || !gate.Until.After(time.Now()) || gate.Configuration != cfg.Fingerprint() || gate.Report.Authorizes(gate.RunID, cfg.Fingerprint(), time.Now()) != nil { |
| 73 | return errors.New("环境网络检查尚未通过,请在初始化界面重新检查") |
| 74 | } |
| 75 | return writePrivate(filepath.Join(s.profile, "initialization.json"), initializationRecord{Version: 1, Completed: true}) |
| 76 | } |
| 77 | |
| 78 | func (s *Server) initialization(w http.ResponseWriter, r *http.Request) { |
| 79 | if r.Method == http.MethodGet { |
| 80 | state, err := s.initializationState() |
| 81 | if err != nil { |
| 82 | requestError(w, err) |
| 83 | return |
| 84 | } |
| 85 | jsonReply(w, state) |
| 86 | return |
| 87 | } |
| 88 | if !s.hasTrustedView(r) || !s.localVMStorage() { |
| 89 | http.Error(w, "请在本机 Cage 窗口完成初始化", http.StatusForbidden) |
| 90 | return |
| 91 | } |
| 92 | var input struct { |
| 93 | Action string `json:"action"` |
| 94 | } |
| 95 | if !readJSON(w, r, &input) { |
| 96 | return |
| 97 | } |
| 98 | if !s.op.TryLock() { |
| 99 | http.Error(w, "环境操作仍在进行,请等待完成后继续", http.StatusConflict) |
| 100 | return |
| 101 | } |
| 102 | defer s.op.Unlock() |
| 103 | if s.busy() { |
| 104 | http.Error(w, "环境操作仍在进行,请等待完成后继续", http.StatusConflict) |
| 105 | return |
| 106 | } |
| 107 | state, err := s.initializationState() |
| 108 | if err != nil { |
| 109 | requestError(w, err) |
| 110 | return |
| 111 | } |
| 112 | switch input.Action { |
| 113 | case "begin": |
| 114 | if state.Required { |
| 115 | err = writePrivate(filepath.Join(s.profile, "initialization.json"), initializationRecord{Version: 1, Started: state.Started}) |
| 116 | } |
| 117 | case "install": |
| 118 | if state.Required { |
| 119 | err = writePrivate(filepath.Join(s.profile, "initialization.json"), initializationRecord{Version: 1, Started: true}) |
| 120 | } |
| 121 | case "complete": |
| 122 | err = s.finishInitialization() |
| 123 | default: |
| 124 | err = errors.New("未知的初始化操作") |
| 125 | } |
| 126 | if err != nil { |
| 127 | requestError(w, err) |
| 128 | return |
| 129 | } |
| 130 | jsonReply(w, map[string]bool{"completed": input.Action == "complete" || !state.Required}) |
| 131 | } |