| 1 | |
| 2 | |
| 3 | |
| 4 | package client |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | import ( |
| 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 | |
| 22 | type HostAgentClient interface { |
| 23 | HTTPClient() *http.Client |
| 24 | Info(context.Context) (*api.Info, error) |
| 25 | |
| 26 | Screenshot(context.Context, string) ([]byte, error) |
| 27 | } |
| 28 | |
| 29 | |
| 30 | |
| 31 | func 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 | |
| 39 | func NewHostAgentClientWithHTTPClient(hc *http.Client) HostAgentClient { |
| 40 | return &client{ |
| 41 | Client: hc, |
| 42 | version: "v1", |
| 43 | dummyHost: "lima-hostagent", |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | type client struct { |
| 48 | *http.Client |
| 49 | |
| 50 | |
| 51 | version string |
| 52 | dummyHost string |
| 53 | } |
| 54 | |
| 55 | func (c *client) HTTPClient() *http.Client { |
| 56 | return c.Client |
| 57 | } |
| 58 | |
| 59 | func (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 | |
| 74 | func (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 | |
| 91 | |
| 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 |
| 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 | } |