| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "os" |
| 12 | ) |
| 13 | |
| 14 | const previousLocalWorkbench = "请用 cage desktop --existing 打开已有本机工作台,保存并停止任务后重试" |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | func (d *desktopLocal) handoffOldLocal(ctx context.Context) (bool, error) { |
| 20 | c, err := connectController(d.profile) |
| 21 | if err != nil { |
| 22 | return false, errors.New("无法认证已有本机后台。" + previousLocalWorkbench) |
| 23 | } |
| 24 | defer c.close() |
| 25 | id := c.launch.Controller |
| 26 | if c.launch.DesktopGateway || id.Protocol != controlProtocol || id.PID != c.record.PID || !id.matches(c.record.Controller) || !remoteInstanceID.MatchString(id.Build) { |
| 27 | return false, errors.New("已有本机后台身份或协议不兼容。" + previousLocalWorkbench) |
| 28 | } |
| 29 | u, _ := url.Parse(c.launch.URL) |
| 30 | status, err := c.callContext(ctx, "POST", "/api/session", map[string]string{"token": u.Fragment}, nil) |
| 31 | if err != nil || status != http.StatusOK || len(c.client.Jar.Cookies(u)) == 0 { |
| 32 | return false, errors.New("无法建立已有本机后台的会话。" + previousLocalWorkbench) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | status, err = c.callContext(ctx, "POST", "/api/desktop-local/status", desktopLocalRequest{Token: c.record.Token}, nil) |
| 37 | if err != nil { |
| 38 | return false, err |
| 39 | } |
| 40 | switch status { |
| 41 | case http.StatusOK: |
| 42 | return false, nil |
| 43 | case http.StatusNotFound, http.StatusMethodNotAllowed, http.StatusLocked: |
| 44 | default: |
| 45 | return false, errors.New("无法确认已有本机后台版本。" + previousLocalWorkbench) |
| 46 | } |
| 47 | f, err := os.Open(d.executable) |
| 48 | if err != nil { |
| 49 | return false, err |
| 50 | } |
| 51 | defer f.Close() |
| 52 | h := sha256.New() |
| 53 | if _, err = io.Copy(h, f); err != nil { |
| 54 | return false, err |
| 55 | } |
| 56 | if id.Build == fmt.Sprintf("%x", h.Sum(nil)) { |
| 57 | return false, errors.New("本机后台的启动器接口不可用。" + previousLocalWorkbench) |
| 58 | } |
| 59 | var selection ClientContext |
| 60 | status, err = c.callContext(ctx, "GET", "/api/client/context", nil, &selection) |
| 61 | if err != nil || (status != http.StatusOK && status != http.StatusNotFound && status != http.StatusLocked) { |
| 62 | return false, errors.New("无法确认旧工作台的连接状态。" + previousLocalWorkbench) |
| 63 | } |
| 64 | if status == http.StatusOK && (selection.ClientOnly || selection.Mode != "local") { |
| 65 | return false, errors.New("已有工作台正在管理其他连接。" + previousLocalWorkbench) |
| 66 | } |
| 67 | var ack struct { |
| 68 | Stopping bool `json:"stopping"` |
| 69 | } |
| 70 | status, err = c.callContext(ctx, "POST", "/api/handoff", map[string]string{"token": c.record.Token}, &ack) |
| 71 | if err != nil || status != http.StatusOK || !ack.Stopping { |
| 72 | return false, errors.New("旧版本机后台仍在使用,或尚未确认空闲退出。" + previousLocalWorkbench) |
| 73 | } |
| 74 | if err := c.waitForExit(); err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | return true, nil |
| 78 | } |