跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

client.go

109 行 · 3.1 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package client
5
6// Forked from https://github.com/rootless-containers/rootlesskit/blob/v0.14.2/pkg/api/client/client.go
7// Apache License 2.0
8
9import (
10 "context"
11 "encoding/json"
12 "errors"
13 "fmt"
14 "io"
15 "net/http"
16 "net/url"
17
18 "github.com/lima-vm/lima/v2/pkg/hostagent/api"
19 "github.com/lima-vm/lima/v2/pkg/httpclientutil"
20)
21
22type HostAgentClient interface {
23 HTTPClient() *http.Client
24 Info(context.Context) (*api.Info, error)
25 // Screenshot captures the VM display. format is "png" or "bmp" (default "png").
26 Screenshot(context.Context, string) ([]byte, error)
27}
28
29// NewHostAgentClient creates a client.
30// socketPath is a path to the UNIX socket, without unix:// prefix.
31func NewHostAgentClient(socketPath string) (HostAgentClient, error) {
32 hc, err := httpclientutil.NewHTTPClientWithSocketPath(socketPath)
33 if err != nil {
34 return nil, err
35 }
36 return NewHostAgentClientWithHTTPClient(hc), nil
37}
38
39func NewHostAgentClientWithHTTPClient(hc *http.Client) HostAgentClient {
40 return &client{
41 Client: hc,
42 version: "v1",
43 dummyHost: "lima-hostagent",
44 }
45}
46
47type client struct {
48 *http.Client
49 // version is always "v1"
50 // TODO(AkihiroSuda): negotiate the version
51 version string
52 dummyHost string
53}
54
55func (c *client) HTTPClient() *http.Client {
56 return c.Client
57}
58
59func (c *client) Info(ctx context.Context) (*api.Info, error) {
60 u := fmt.Sprintf("http://%s/%s/info", c.dummyHost, c.version)
61 resp, err := httpclientutil.Get(ctx, c.HTTPClient(), u)
62 if err != nil {
63 return nil, err
64 }
65 defer resp.Body.Close()
66 var info api.Info
67 dec := json.NewDecoder(resp.Body)
68 if err := dec.Decode(&info); err != nil {
69 return nil, err
70 }
71 return &info, nil
72}
73
74func (c *client) Screenshot(ctx context.Context, format string) ([]byte, error) {
75 if format == "" {
76 format = "png"
77 }
78 if format != "png" && format != "bmp" {
79 return nil, fmt.Errorf("unsupported format %q: must be png or bmp", format)
80 }
81 params := url.Values{"format": {format}}
82 u := fmt.Sprintf("http://%s/%s/screenshot?%s", c.dummyHost, c.version, params.Encode())
83 resp, err := httpclientutil.Get(ctx, c.HTTPClient(), u)
84 if err != nil {
85 if httpErr, ok := errors.AsType[*httpclientutil.HTTPStatusError](err); ok {
86 switch httpErr.StatusCode {
87 case http.StatusNotFound:
88 return nil, errors.New("hostagent does not support screenshot — restart the instance to pick up the latest hostagent binary")
89 case http.StatusNotImplemented:
90 // httpErr.Error() extracts ErrorJSON.Message from the body, which
91 // includes the driver name set by the hostagent.
92 return nil, httpErr
93 case http.StatusUnprocessableEntity:
94 return nil, errors.New("VM has no display configured — set video.display (e.g., \"default\") in the instance config")
95 }
96 }
97 return nil, err
98 }
99 defer resp.Body.Close()
100 const maxScreenshotSize = 32 << 20 // 32 MB
101 data, err := io.ReadAll(io.LimitReader(resp.Body, maxScreenshotSize+1))
102 if err != nil {
103 return nil, err
104 }
105 if len(data) > maxScreenshotSize {
106 return nil, fmt.Errorf("screenshot response exceeds %d MB limit", maxScreenshotSize>>20)
107 }
108 return data, nil
109}