跳到主要内容

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

浏览文件

config.go

90 行 · 2.9 KB历史下载
1// Package cage supplies the configuration and network boundary of Claude Cage.
2package cage
3
4import (
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "net"
10 "net/url"
11 "os"
12 "regexp"
13 "strconv"
14 "time"
15 _ "time/tzdata"
16)
17
18const ConfigEnv = "CLAUDE_CAGE_CONFIG"
19
20type Config struct {
21 ProxyURL string `json:"proxy_url"`
22 ProxyRevision string `json:"proxy_revision,omitempty"`
23 DNSURL string `json:"dns_url"`
24 Timezone string `json:"timezone"`
25 Locale string `json:"locale"`
26 BrowserLanguage string `json:"browser_language"`
27 ExitPolicy ExitPolicy `json:"exit_policy"`
28}
29
30func DefaultConfig() Config {
31 return Config{ProxyURL: "socks5://127.0.0.1:17896", DNSURL: "https://1.1.1.1/dns-query", Timezone: "Etc/UTC", Locale: "en_US.UTF-8", BrowserLanguage: "en-US"}
32}
33
34func Load(path string) (Config, error) {
35 var c Config
36 f, err := os.Open(path)
37 if err != nil {
38 return c, fmt.Errorf("cannot read cage configuration: %w", err)
39 }
40 defer f.Close()
41 d := json.NewDecoder(io.LimitReader(f, 64<<10))
42 d.DisallowUnknownFields()
43 if err = d.Decode(&c); err != nil {
44 return c, errors.New("invalid cage JSON configuration")
45 }
46 var extra any
47 if d.Decode(&extra) != io.EOF {
48 return c, errors.New("cage configuration must contain one JSON object")
49 }
50 c.ExitPolicy = c.ExitPolicy.Normalized()
51 return c, c.Validate()
52}
53
54func (c Config) Validate() error {
55 u, err := url.Parse(c.ProxyURL)
56 if err != nil || u == nil {
57 return errors.New("invalid proxy URL")
58 }
59 if u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "socks5" && u.Scheme != "socks5h" {
60 return errors.New("proxy must use http, https or socks5")
61 }
62 // The proxy endpoint itself must not require the host DNS resolver.
63 if net.ParseIP(u.Hostname()) == nil {
64 return errors.New("proxy endpoint must be a numeric IP address; DNS bootstrap is not allowed")
65 }
66 p, err := strconv.Atoi(u.Port())
67 if err != nil || p < 1 || p > 65535 {
68 return errors.New("proxy requires a valid explicit port")
69 }
70 if u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
71 return errors.New("proxy URL must not contain a path, query or fragment")
72 }
73 d, err := url.Parse(c.DNSURL)
74 if err != nil || d.Scheme != "https" || d.Hostname() == "" || d.User != nil || d.Fragment != "" {
75 return errors.New("DNS URL must be an HTTPS DNS-over-HTTPS endpoint")
76 }
77 if c.Timezone == "" || c.Timezone == "Local" {
78 return errors.New("timezone must be an explicit IANA name, e.g. Etc/UTC")
79 }
80 if _, err := time.LoadLocation(c.Timezone); err != nil {
81 return errors.New("unknown cage timezone")
82 }
83 if !regexp.MustCompile(`^[A-Za-z]{2,3}_[A-Za-z]{2,3}\.UTF-8$`).MatchString(c.Locale) {
84 return errors.New("locale must have the form en_US.UTF-8")
85 }
86 if !regexp.MustCompile(`^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})*$`).MatchString(c.BrowserLanguage) {
87 return errors.New("invalid browser language tag")
88 }
89 return c.ExitPolicy.Validate()
90}