跳到主要内容

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

浏览文件

lifecycle.sh

186 行 · 9.2 KB历史下载
1# Invoked over a host-key verified SSH connection. Stdout is protocol data.
2set -eu
3umask 077
4action=$1
5exe=$2
6config=${3:-"$HOME/.claude-cage/config.json"}
7export CLAUDE_CAGE_CONFIG="$config"
8profile=$(dirname -- "$config")
9root="$profile/cloud-host"
10key=$(printf %s "$config" | sha256sum | cut -c1-16)
11unit=cage-cloud-$key
12engine=$unit-engine
13units="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user"
14owned() { [[ -f "$root/owner" && $(cat "$root/owner") == "$config" ]]; }
15exists() { [[ -e "$1" || -L "$1" ]]; }
16installed() { owned && [[ -e "$root/ready" ]]; }
17manual() {
18 if [[ "$exe" != cage ]]; then [[ -x "$exe" ]] && printf '%s' "$exe"
19 elif command -v cage >/dev/null; then command -v cage
20 elif [[ -x "$HOME/.local/lib/cage/cage" ]]; then printf '%s' "$HOME/.local/lib/cage/cage"
21 else return 1
22 fi
23}
24preflight() {
25 [[ $(uname -s) == Linux && $(uname -m) == x86_64 && $(id -u) != 0 ]] || exit 61
26 [[ -c /dev/fuse && -c /dev/net/tun && -f /sys/fs/cgroup/cgroup.controllers ]] || exit 62
27 for tool in newuidmap newgidmap iptables ip tar sha256sum systemctl flock; do command -v "$tool" >/dev/null || exit 62; done
28 systemctl --user show-environment >/dev/null || exit 63
29 for map in /etc/subuid /etc/subgid; do
30 awk -F: -v user="$(id -un)" -v uid="$(id -u)" '($1==user || $1==uid) && $3>=65536 {ok=1} END {exit !ok}' "$map" || exit 62
31 done
32 # Unit templates and JSON below deliberately accept portable absolute paths.
33 [[ "$config" =~ ^/[a-zA-Z0-9._/-]+$ && "$config" != *'/../'* && "$profile" != / ]] || exit 64
34}
35vacant() {
36 if exists "$root"; then owned || exit 65
37 elif exists "$profile" && [[ -n $(ls -A -- "$profile") ]]; then exit 65
38 fi
39}
40case "$action" in
41probe)
42 if [[ "$exe" == cage ]] && installed; then printf managed
43 elif binary=$(manual); then
44 # Match the service's exact profile before starting a manual installation.
45 if [[ " $(systemctl --user show cage-server.service -p Environment --value) " == *" CLAUDE_CAGE_CONFIG=$config "* ]] &&
46 [[ "$(systemctl --user show cage-server.service -p ExecStart --value)" == *"path=$binary ;"* ]]; then printf manual
47 else
48 exec "$binary" server-entry --json
49 fi
50 elif [[ "$exe" != cage ]]; then exit 66
51 else preflight; vacant; printf missing
52 fi ;;
53entry)
54 if [[ "$exe" == cage ]] && installed; then binary="$root/bin/cage"; else binary=$(manual); fi
55 exec "$binary" server-entry --json ;;
56start)
57 if [[ "$exe" == cage ]] && installed; then systemctl --user start "$unit.service"
58 else systemctl --user start cage-server.service; fi ;;
59install)
60 preflight; vacant
61 [[ "$exe" == cage ]] || exit 66
62 digest=$4
63 [[ "$digest" =~ ^[0-9a-f]{64}$ ]]
64 mkdir -p -- "$profile"
65 if mkdir -- "$root" 2>/dev/null; then printf '%s' "$config" > "$root/owner"; fi
66 owned || exit 65
67 exec 9>"$root/install.lock"
68 flock -n 9 || exit 67
69 installed && { cat >/dev/null; exit 0; }
70 [[ $(df -Pk "$root" | awk 'END {print $4}') -gt 7340032 ]] || exit 68
71 if exists "$root/kit-id"; then [[ $(cat "$root/kit-id") == "$digest" ]] || exit 65; fi
72 stage=$(mktemp -d "$root/upload.XXXXXX")
73 trap 'rm -rf -- "$stage"' EXIT
74 cat > "$stage/payload.tar"
75 (cd "$stage"; printf '%s payload.tar\n' "$digest" | sha256sum -c --status -) || exit 69
76 printf '%s' "$digest" > "$root/kit-id"
77 tar -xf "$stage/payload.tar" -C "$stage"
78 rm "$stage/payload.tar"
79 mkdir -p "$root/bin" "$root/engine/tools" "$root/engine/client" "$units"
80 if [[ -f "$stage/engine-downloads" ]]; then
81 for name in cage mihomo; do gzip -dc "$stage/$name.gz" > "$stage/$name"; done
82 mkdir -p "$root/downloads"
83 while read -r checksum name url; do
84 cached="$root/downloads/$name"
85 if ! (cd "$root/downloads"; printf '%s %s\n' "$checksum" "$name" | sha256sum -c --status -) 2>/dev/null; then
86 # Fixed-size ranges also work through mirrors which truncate a large response.
87 if [[ -f "$cached.part" ]] && printf '%s %s\n' "$checksum" "$cached.part" | sha256sum -c --status -; then
88 mv "$cached.part" "$cached"
89 ln "$cached" "$stage/$name"
90 continue
91 fi
92 length=0
93 while [[ $length -eq 0 || $offset -lt $length ]]; do
94 offset=$(stat -c %s "$cached.part" 2>/dev/null || echo 0)
95 end=$((offset + 4194303))
96 ok=false
97 for source in "$url" "https://mirrors.ustc.edu.cn/docker-ce/linux/static/stable/x86_64/$name" "https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/$name"; do
98 if curl -fsSL --connect-timeout 8 --max-time 90 --range "$offset-$end" -D "$stage/headers" -o "$stage/chunk" "$source"; then
99 range=$(tr -d '\r' < "$stage/headers" | awk 'tolower($1)=="content-range:" {print $3}' | tail -1)
100 if [[ "$range" =~ ^([0-9]+)-([0-9]+)/([0-9]+)$ ]]; then
101 first=${BASH_REMATCH[1]} last=${BASH_REMATCH[2]} total=${BASH_REMATCH[3]}
102 if (( first == offset && last <= end && last < total && total < 268435456 && last-first+1 == $(stat -c %s "$stage/chunk") )); then
103 cat "$stage/chunk" >> "$cached.part"; length=$total; offset=$((last+1)); ok=true; break
104 fi
105 elif printf '%s %s\n' "$checksum" "$stage/chunk" | sha256sum -c --status -; then
106 mv "$stage/chunk" "$cached.part"; length=1; offset=1; ok=true; break
107 fi
108 fi
109 done
110 [[ "$ok" == true ]] || exit 74
111 done
112 printf '%s %s\n' "$checksum" "$cached.part" | sha256sum -c --status - || { rm -f "$cached.part"; exit 69; }
113 mv "$cached.part" "$cached"
114 fi
115 ln "$cached" "$stage/$name"
116 done < "$stage/engine-downloads"
117 fi
118 for name in cage mihomo; do
119 if ! cmp -s "$stage/$name" "$root/bin/$name"; then
120 install -m 700 "$stage/$name" "$root/bin/$name.new"
121 mv "$root/bin/$name.new" "$root/bin/$name"
122 fi
123 done
124 eng="$root/engine"
125 if [[ ! -f "$eng/tools/.complete" ]]; then
126 for tgz in "$stage"/docker-*.tgz; do tar -xzf "$tgz" --strip-components=1 -C "$eng/tools"; done
127 touch "$eng/tools/.complete"
128 fi
129 [[ -f "$eng/daemon.json" ]] || printf '{}\n' > "$eng/daemon.json"
130 # Install only our units. An existing independent service must never be replaced.
131 write_unit() {
132 local name=$1 src=$2
133 sed -e "s|%h/.claude-cage/container-engine|$eng|g" \
134 -e "s|%h/.claude-cage/config.json|$config|g" -e "s|%h/.local/lib/cage|$root/bin|g" \
135 -e "s|cage-container-engine|$engine|g" "$src" > "$stage/$name"
136 if exists "$units/$name"; then cmp -s "$stage/$name" "$units/$name" || exit 65
137 else ln "$stage/$name" "$units/$name" 2>/dev/null || ( set -C; cat "$stage/$name" > "$units/$name" ); fi
138 }
139 write_unit "$engine.service" "$stage/cage-container-engine.service"
140 write_unit "$unit.service" "$stage/cage-server.service"
141 systemctl --user daemon-reload
142 systemctl --user start "$engine.service" || exit 70
143 export DOCKER_HOST="unix:///run/user/$(id -u)/$engine/docker.sock" DOCKER_CONFIG="$eng/client"
144 unset DOCKER_CONTEXT
145 docker="$eng/tools/docker"
146 for i in {1..120}; do "$docker" info >/dev/null 2>&1 && break; sleep 1; done
147 [[ $("$docker" info --format '{{json .SecurityOptions}} {{.CgroupDriver}} {{.CgroupVersion}}') == *'name=rootless'*' systemd 2' ]] || exit 70
148 if [[ -f "$stage/engine-downloads" ]]; then
149 if [[ ! -f "$root/image-id" ]]; then
150 base=$(sed -n 's/^ARG BASE_IMAGE=//p' "$stage/deploy/cloud/Dockerfile.guest")
151 ref=${base#*@}
152 [[ "$ref" =~ ^sha256:[0-9a-f]{64}$ ]]
153 for repo in docker.io/library/debian docker.m.daocloud.io/library/debian docker.1ms.run/library/debian public.ecr.aws/docker/library/debian; do
154 if timeout 120 "$docker" pull "$repo@$ref" >> "$root/build.log" 2>&1; then
155 base_id=$("$docker" image inspect --format '{{.Id}}' "$repo@$ref")
156 break
157 fi
158 done
159 [[ -n "${base_id:-}" ]] || exit 75
160 mirror=http://deb.debian.org
161 best=0
162 for site in http://deb.debian.org http://mirrors.ustc.edu.cn http://mirrors.nju.edu.cn; do
163 rate=$(curl -fsSL --range 0-131071 --max-time 4 -o /dev/null -w '%{speed_download}' "$site/debian/dists/trixie/main/binary-amd64/Packages.xz") || continue
164 rate=${rate%%.*}
165 if [[ "$rate" =~ ^[0-9]+$ ]] && ((rate > best)); then mirror=$site; best=$rate; fi
166 done
167 CAGE_DEBIAN_MIRROR="$mirror" CAGE_CONTAINER_ENGINE="$docker" CAGE_SERVER_BINARY="$root/bin/cage" CAGE_GUEST_BASE_IMAGE="$base_id" \
168 CAGE_IMAGE_BUILD_DIR="$root/build" CAGE_IMAGE_ID_OUTPUT="$root/image-id.new" \
169 bash "$stage/deploy/cloud/build-guest.sh" >> "$root/build.log" 2>&1 || exit 76
170 mv "$root/image-id.new" "$root/image-id"
171 fi
172 image=$(cat "$root/image-id")
173 else
174 image=$(cat "$stage/image-id")
175 "$docker" image inspect "$image" >/dev/null 2>&1 || "$docker" load -i "$stage/guest.tar.zst" >/dev/null || exit 71
176 fi
177 [[ "$image" =~ ^sha256:[0-9a-f]{64}$ ]]
178 "$docker" image inspect "$image" >/dev/null || exit 71
179 cage="$root/bin/cage"
180 [[ -e "$config" ]] || "$cage" init >/dev/null
181 printf '{"engine":"docker","executable":"%s","socket":"%s","image":"%s","cpus":2,"memory_bytes":4294967296}\n' "$docker" "$DOCKER_HOST" "$image" | "$cage" container-configure --json >/dev/null
182 "$cage" setup >"$root/setup.log" 2>&1 || exit 72
183 systemctl --user enable --now "$unit.service" || exit 73
184 touch "$root/ready" ;;
185*) exit 2 ;;
186esac