跳到主要内容

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

浏览文件

server.go

104 行 · 2.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package server
5
6import (
7 "context"
8 "encoding/json"
9 "errors"
10 "fmt"
11 "net/http"
12 "strings"
13
14 "github.com/lima-vm/lima/v2/pkg/driver"
15 "github.com/lima-vm/lima/v2/pkg/hostagent"
16 "github.com/lima-vm/lima/v2/pkg/httputil"
17)
18
19type Backend struct {
20 Agent *hostagent.HostAgent
21}
22
23func (b *Backend) onError(w http.ResponseWriter, err error, ec int) {
24 // Set headers before WriteHeader — calling WriteHeader flushes headers.
25 w.Header().Set("Content-Type", "application/json")
26 w.WriteHeader(ec)
27 // err may potentially contain credential info (in a future version),
28 // but it is safe to return the err to the client, because we do not expose the socket to the internet
29 e := httputil.ErrorJSON{
30 Message: err.Error(),
31 }
32 _ = json.NewEncoder(w).Encode(e)
33}
34
35// GetInfo is the handler for GET /v1/info.
36func (b *Backend) GetInfo(w http.ResponseWriter, r *http.Request) {
37 if r.Method != http.MethodGet {
38 w.WriteHeader(http.StatusMethodNotAllowed)
39 return
40 }
41
42 ctx := r.Context()
43 ctx, cancel := context.WithCancel(ctx)
44 defer cancel()
45
46 info, err := b.Agent.Info(ctx)
47 if err != nil {
48 b.onError(w, err, http.StatusInternalServerError)
49 return
50 }
51 m, err := json.Marshal(info)
52 if err != nil {
53 b.onError(w, err, http.StatusInternalServerError)
54 return
55 }
56 w.Header().Set("Content-Type", "application/json")
57 w.WriteHeader(http.StatusOK)
58 _, _ = w.Write(m)
59}
60
61// GetScreenshot is the handler for GET /v1/screenshot.
62func (b *Backend) GetScreenshot(w http.ResponseWriter, r *http.Request) {
63 if r.Method != http.MethodGet {
64 w.Header().Set("Allow", "GET")
65 w.WriteHeader(http.StatusMethodNotAllowed)
66 return
67 }
68 ctx := r.Context()
69 ctx, cancel := context.WithCancel(ctx)
70 defer cancel()
71
72 format := strings.ToLower(r.URL.Query().Get("format"))
73 if format == "" {
74 format = "png"
75 }
76 if format != "png" && format != "bmp" {
77 b.onError(w, fmt.Errorf("unsupported format %q: must be png or bmp", format), http.StatusBadRequest)
78 return
79 }
80 data, err := b.Agent.Screenshot(ctx, format)
81 if err != nil {
82 ec := http.StatusInternalServerError
83 switch {
84 case errors.Is(err, driver.ErrDriverNotScreenshotter):
85 ec = http.StatusNotImplemented
86 case errors.Is(err, driver.ErrNoDisplay):
87 ec = http.StatusUnprocessableEntity
88 }
89 b.onError(w, err, ec)
90 return
91 }
92 ct := "image/png"
93 if format == "bmp" {
94 ct = "image/bmp"
95 }
96 w.Header().Set("Content-Type", ct)
97 w.WriteHeader(http.StatusOK)
98 _, _ = w.Write(data)
99}
100
101func AddRoutes(r *http.ServeMux, b *Backend) {
102 r.Handle("/v1/info", http.HandlerFunc(b.GetInfo))
103 r.Handle("/v1/screenshot", http.HandlerFunc(b.GetScreenshot))
104}