| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "sort" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | type desktopLocalRequest struct { |
| 14 | Token string `json:"token"` |
| 15 | ControllerID string `json:"controller_id,omitempty"` |
| 16 | Stop bool `json:"stop,omitempty"` |
| 17 | Running bool `json:"running,omitempty"` |
| 18 | Terminals int `json:"terminals,omitempty"` |
| 19 | JobID string `json:"job_id,omitempty"` |
| 20 | } |
| 21 | |
| 22 | type desktopLocalReply struct { |
| 23 | DesktopLocalStatus |
| 24 | Identity ControllerIdentity `json:"identity"` |
| 25 | OwnerID string `json:"owner_id"` |
| 26 | InstanceID string `json:"instance_id"` |
| 27 | LaunchID string `json:"launch_id,omitempty"` |
| 28 | JobID string `json:"job_id,omitempty"` |
| 29 | JobRunning bool `json:"job_running,omitempty"` |
| 30 | JobFailed bool `json:"job_failed,omitempty"` |
| 31 | } |
| 32 | |
| 33 | func localControllerID(token string) string { |
| 34 | return fmt.Sprintf("%x", sha256.Sum256([]byte("desktop-local\x00"+token))) |
| 35 | } |
| 36 | |
| 37 | func (s *Server) desktopLocalSnapshot() desktopLocalReply { |
| 38 | job := s.jobs.snapshot() |
| 39 | reply := desktopLocalReply{ |
| 40 | DesktopLocalStatus: DesktopLocalStatus{Available: true, ServiceRunning: true}, |
| 41 | Identity: s.identity, OwnerID: localControllerID(s.token), InstanceID: s.instanceID, |
| 42 | LaunchID: os.Getenv("CAGE_DESKTOP_START_ID"), JobID: job.ID, JobRunning: job.Running, JobFailed: job.Error != "", |
| 43 | } |
| 44 | owners, err := controllerVMOwners(s.profile) |
| 45 | reply.Running = len(owners) != 0 |
| 46 | if err != nil { |
| 47 | reply.Message = "本机环境状态待确认" |
| 48 | return reply |
| 49 | } |
| 50 | reply.Known = true |
| 51 | var terminals []string |
| 52 | for _, session := range s.terminals.snapshot() { |
| 53 | if session["closed"] == false { |
| 54 | reply.LiveTerminals++ |
| 55 | id, _ := session["id"].(string) |
| 56 | terminals = append(terminals, id) |
| 57 | } |
| 58 | } |
| 59 | s.mu.Lock() |
| 60 | reply.Busy = s.closing || job.Running |
| 61 | s.mu.Unlock() |
| 62 | if len(s.mobileCLI.activeAccounts()) != 0 || (s.remote != nil && s.remote.isRemote()) { |
| 63 | reply.Busy = true |
| 64 | } |
| 65 | if reply.Busy { |
| 66 | reply.Message = "本机控制服务正在执行操作或被其他入口使用" |
| 67 | } |
| 68 | |
| 69 | |
| 70 | sort.Strings(terminals) |
| 71 | reply.ControllerID = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s\x00%q\x00%s\x00%t\x00%q", reply.OwnerID, owners, job.ID, reply.Busy, terminals)))) |
| 72 | return reply |
| 73 | } |
| 74 | |
| 75 | func (s *Server) desktopLocalControl(w http.ResponseWriter, r *http.Request) { |
| 76 | var request desktopLocalRequest |
| 77 | if !readJSON(w, r, &request) { |
| 78 | return |
| 79 | } |
| 80 | if s.clientOnly || !equalSecret(request.Token, s.token) { |
| 81 | http.Error(w, "Launcher refused", http.StatusUnauthorized) |
| 82 | return |
| 83 | } |
| 84 | switch r.PathValue("action") { |
| 85 | case "status": |
| 86 | jsonReply(w, s.desktopLocalSnapshot()) |
| 87 | case "release": |
| 88 | if !s.op.TryLock() { |
| 89 | http.Error(w, "本机正在执行操作,请稍后重试", http.StatusConflict) |
| 90 | return |
| 91 | } |
| 92 | view := s.desktopLocalSnapshot() |
| 93 | if !view.Known || view.Busy || request.ControllerID != view.ControllerID || request.Running != view.Running || request.Terminals != view.LiveTerminals || (request.JobID != "" && request.JobID != view.JobID) { |
| 94 | s.op.Unlock() |
| 95 | http.Error(w, "本机任务或控制服务已变化,请重新确认", http.StatusConflict) |
| 96 | return |
| 97 | } |
| 98 | if !view.Running && view.LiveTerminals == 0 { |
| 99 | s.handoffLocked(w) |
| 100 | return |
| 101 | } |
| 102 | defer s.op.Unlock() |
| 103 | if !request.Stop { |
| 104 | http.Error(w, "本机环境仍在运行", http.StatusConflict) |
| 105 | return |
| 106 | } |
| 107 | if _, unlocked := s.account.access(); !unlocked { |
| 108 | http.Error(w, "请先返回本机解锁,再停止本机环境", http.StatusLocked) |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | s.mu.Lock() |
| 115 | if s.closing || s.jobs.snapshot().Running { |
| 116 | s.mu.Unlock() |
| 117 | http.Error(w, "本机正在执行操作,请稍后重试", http.StatusConflict) |
| 118 | return |
| 119 | } |
| 120 | job, err := s.jobs.submit(&s.op, "stop", s.safety.pauseVersion, nil, nil, func() error { |
| 121 | _ = s.pauseNetworkNow("正在停止本机环境") |
| 122 | return s.stopEnvironment() |
| 123 | }) |
| 124 | s.mu.Unlock() |
| 125 | if err != nil { |
| 126 | requestError(w, err) |
| 127 | return |
| 128 | } |
| 129 | jsonReply(w, desktopLocalReply{JobID: job.ID, JobRunning: true}) |
| 130 | default: |
| 131 | http.NotFound(w, r) |
| 132 | } |
| 133 | } |