| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "regexp" |
| 11 | "runtime" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/host" |
| 14 | "github.com/sylvinus/agent-vm/internal/version" |
| 15 | ) |
| 16 | |
| 17 | const remoteAPIProtocol = 1 |
| 18 | |
| 19 | var remoteInstanceID = regexp.MustCompile(`^[a-f0-9]{64}$`) |
| 20 | |
| 21 | type remoteInstance struct { |
| 22 | ID string `json:"id"` |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | func ensureRemoteInstance(profile string) (string, error) { |
| 28 | path := filepath.Join(profile, "remote-instance.json") |
| 29 | if err := host.CheckPrivateFile(path); err == nil { |
| 30 | data, err := os.ReadFile(path) |
| 31 | var instance remoteInstance |
| 32 | if err != nil || json.Unmarshal(data, &instance) != nil || !remoteInstanceID.MatchString(instance.ID) { |
| 33 | return "", errors.New("远程实例身份记录无效,请保留数据并检查配置") |
| 34 | } |
| 35 | return instance.ID, nil |
| 36 | } else if !os.IsNotExist(err) { |
| 37 | return "", err |
| 38 | } |
| 39 | instance := remoteInstance{ID: randomToken()} |
| 40 | if err := writePrivate(path, instance); err != nil { |
| 41 | return "", err |
| 42 | } |
| 43 | return instance.ID, nil |
| 44 | } |
| 45 | |
| 46 | type RemoteEntry struct { |
| 47 | APIProtocol int `json:"api_protocol"` |
| 48 | InstanceID string `json:"instance_id"` |
| 49 | Version string `json:"version"` |
| 50 | Platform string `json:"platform"` |
| 51 | Runtime string `json:"runtime"` |
| 52 | Origin string `json:"origin"` |
| 53 | Ticket string `json:"ticket,omitempty"` |
| 54 | Capabilities map[string]bool `json:"capabilities"` |
| 55 | } |
| 56 | |
| 57 | func (s *Server) remoteManifest(w http.ResponseWriter, r *http.Request) { |
| 58 | if s.macosGuest != nil { |
| 59 | jsonReply(w, RemoteEntry{APIProtocol: remoteAPIProtocol, InstanceID: s.instanceID, Version: version.Version, Platform: runtime.GOOS, |
| 60 | Runtime: "lima", Origin: s.origin, Capabilities: map[string]bool{"terminal": true, "projects_browse": true}}) |
| 61 | return |
| 62 | } |
| 63 | kind := "lima" |
| 64 | if _, err := os.Stat(filepath.Join(s.profile, "container.json")); err == nil { |
| 65 | kind = "container" |
| 66 | } |
| 67 | jsonReply(w, RemoteEntry{APIProtocol: remoteAPIProtocol, InstanceID: s.instanceID, |
| 68 | Version: version.Version, Platform: runtime.GOOS, Runtime: kind, Origin: s.origin, |
| 69 | Capabilities: map[string]bool{"projects_browse": true, "client_project_share": runtime.GOOS == "linux" && kind == "container", "terminal": true, |
| 70 | "browser": true, "desktop": true, "audio": true}}) |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | func ServerEntry(configPath string) (RemoteEntry, error) { |
| 77 | var result RemoteEntry |
| 78 | c, err := connectController(filepath.Dir(configPath)) |
| 79 | if err != nil { |
| 80 | return result, errors.New("云端 Cage 服务尚未运行或无法认证,请检查服务状态") |
| 81 | } |
| 82 | defer c.close() |
| 83 | entry, err := url.Parse(c.launch.URL) |
| 84 | if err != nil { |
| 85 | return result, err |
| 86 | } |
| 87 | status, err := c.call("POST", "/api/session", map[string]string{"token": entry.Fragment}, nil) |
| 88 | if err != nil || status != http.StatusOK { |
| 89 | return result, errors.New("无法建立云端入口会话") |
| 90 | } |
| 91 | status, err = c.call("GET", "/api/remote/manifest", nil, &result) |
| 92 | if err != nil || status != http.StatusOK || !remoteInstanceID.MatchString(result.InstanceID) { |
| 93 | return RemoteEntry{}, errors.New("云端服务不支持当前远程连接协议") |
| 94 | } |
| 95 | |
| 96 | |
| 97 | var launch launchResponse |
| 98 | status, err = c.call("POST", "/api/launch", map[string]string{"token": c.record.Token}, &launch) |
| 99 | if err != nil || status != http.StatusOK { |
| 100 | return RemoteEntry{}, errors.New("无法取得云端一次性入口") |
| 101 | } |
| 102 | u, err := url.Parse(launch.URL) |
| 103 | if err != nil || u.Scheme+"://"+u.Host != c.origin || u.Fragment == "" { |
| 104 | return RemoteEntry{}, errors.New("云端入口无效") |
| 105 | } |
| 106 | result.Ticket = u.Fragment |
| 107 | return result, nil |
| 108 | } |