跳到主要内容

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

浏览文件

profile.go

98 行 · 2.8 KB历史下载
1package cage
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11)
12
13type runningProfile struct {
14 PID int `json:"pid"`
15 Started string `json:"started,omitempty"`
16 Fingerprint string `json:"fingerprint"`
17 JobID string `json:"job_id,omitempty"`
18}
19
20// ProcessStartMatches reports false only when pid now belongs to a process
21// started at a different time than the record. After a reboot or crash the
22// recorded PID can be reused by an unrelated process. Records from older
23// versions carry no start time and an unreadable start time cannot prove
24// reuse; both keep the previous PID-only behavior.
25func ProcessStartMatches(pid int, started string) bool {
26 if started == "" {
27 return true
28 }
29 current := processStartToken(pid)
30 return current == "" || current == started
31}
32
33func (r runningProfile) alive() bool {
34 return r.PID > 0 && profileProcessAlive(r.PID) && ProcessStartMatches(r.PID, r.Started)
35}
36
37func (c Config) Fingerprint() string {
38 c.ExitPolicy = c.ExitPolicy.Normalized()
39 b, _ := json.Marshal(c)
40 h := sha256.Sum256(b)
41 return hex.EncodeToString(h[:])
42}
43
44func (c Config) RecordRunning(state, name string) error {
45 if filepath.Base(name) != name {
46 return errors.New("invalid cage instance name")
47 }
48 dir := filepath.Join(state, "lima", name)
49 return c.RecordRunningAt(dir)
50}
51
52// RecordRunningAt records the network owner's identity in the runtime's real
53// instance directory; container environments do not create Lima metadata.
54func (c Config) RecordRunningAt(dir string) error {
55 pid := os.Getpid()
56 b, _ := json.Marshal(runningProfile{PID: pid, Started: processStartToken(pid), Fingerprint: c.Fingerprint(), JobID: os.Getenv(JobEnv)})
57 f, err := os.CreateTemp(dir, ".running-*")
58 if err != nil {
59 return err
60 }
61 defer os.Remove(f.Name())
62 if _, err = f.Write(b); err != nil {
63 f.Close()
64 return err
65 }
66 if err = f.Close(); err != nil {
67 return err
68 }
69 return os.Rename(f.Name(), filepath.Join(dir, "cage-running.json"))
70}
71
72// CheckRunning prevents a profile edit from silently leaving a running VM on
73// its previous proxy or locale. Stopping and listing remain possible.
74func (c Config) CheckRunning(state string) error {
75 files, err := filepath.Glob(filepath.Join(state, "lima", "*", "cage-running.json"))
76 if err != nil {
77 return err
78 }
79 containerRecords, err := filepath.Glob(filepath.Join(state, "container", "*", "cage-running.json"))
80 if err != nil {
81 return err
82 }
83 files = append(files, containerRecords...)
84 for _, file := range files {
85 b, err := os.ReadFile(file)
86 if err != nil {
87 return err
88 }
89 var r runningProfile
90 if json.Unmarshal(b, &r) != nil {
91 return errors.New("invalid running cage profile record")
92 }
93 if r.alive() && r.Fingerprint != c.Fingerprint() {
94 return fmt.Errorf("cage configuration changed while a VM is running; run cage stop before restarting")
95 }
96 }
97 return nil
98}