跳到主要内容

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

浏览文件

maintain-macos-workbench.sh

186 行 · 8.3 KB历史下载
1#!/bin/bash
2# Runs through the verified provisioning identity of an already bound Mac.
3# The ordinary account and its home directory are never initialized here.
4set -euo pipefail
5export PATH=/usr/bin:/bin:/usr/sbin:/sbin
6[[ $# == 3 && $1 == /* && $2 =~ ^[a-z][a-z0-9_]{0,30}$ && ( $3 == components || $3 == tools ) ]]
7[[ $EUID == 0 && $(uname -sm) == 'Darwin arm64' && $(sysctl -n hw.model) =~ ^VirtualMac[0-9]+,[0-9]+$ ]]
8[[ -f /var/db/cage-guest-os && ! -L /var/db/cage-guest-os && $(stat -f %u /var/db/cage-guest-os) == 0 && $(cat /var/db/cage-guest-os) == macos ]]
9lock=/var/run/cage-component-update.lock
10[[ ! -L $lock && ( ! -e $lock || ( -f $lock && $(stat -f %u "$lock") == 0 ) ) ]]
11exec 9>"$lock"
12/usr/bin/lockf -s -t 0 9 || { echo '另一项组件更新仍在运行,请稍后重试。' >&2; exit 1; }
13umask 022
14kit=$(cd -P "$1" && pwd)
15# Helpers run as the ordinary guest user and need a traversable working directory.
16cd /
17user=$2
18mode=$3
19uid=$(id -u "$user")
20[[ $uid -ge 500 && " $(id -Gn "$user") " != *' admin '* ]]
21[[ $(stat -f %u "$kit") == 0 && -z $(find "$kit" \( -type l -o ! -user root -o -perm -0020 -o -perm -0002 \) -print -quit) ]]
22manifest="$kit/native-components.json"
23/usr/bin/perl "$kit/native-component-manifest.pl" source "$manifest" "$kit"
24backup=$(mktemp -d /private/var/tmp/cage-components-backup.XXXXXXXX)
25chmod 700 "$backup"
26changed=()
27existed=()
28loaded=()
29previous_loaded=()
30apps=()
31app_existed=()
32app_applied=0
33cli_previous=''
34if [[ -L /usr/local/libexec/cage-cli ]]; then cli_previous=$(readlink /usr/local/libexec/cage-cli); fi
35
36finish() {
37 local status=$? index target label
38 trap - EXIT
39 if (( status != 0 )); then
40 for ((index=0; index<${#loaded[@]}; index++)); do
41 label=${loaded[$index]}
42 sudo -Hu "$user" /bin/launchctl bootout "gui/$uid/$label" >/dev/null 2>&1 || true
43 done
44 for ((index=${#changed[@]}-1; index>=0; index--)); do
45 target=${changed[$index]}
46 if [[ ${existed[$index]} == yes ]]; then
47 mv -f "$backup/$index" "$target" || status=1
48 elif [[ -f $target && ! -L $target ]]; then
49 rm "$target" || status=1
50 fi
51 done
52 for ((index=app_applied-1; index>=0; index--)); do
53 target=${apps[$index]}
54 if [[ -d $target && ! -L $target ]]; then rm -rf "$target"; fi
55 if [[ ${app_existed[$index]} == yes ]]; then mv "$backup/old-app-$index" "$target" || status=1; fi
56 done
57 if [[ -n $cli_previous ]]; then
58 ln -s "$cli_previous" "$backup/cli-previous"
59 mv -fh "$backup/cli-previous" /usr/local/libexec/cage-cli || status=1
60 fi
61 for ((index=0; index<${#previous_loaded[@]}; index++)); do
62 label=${previous_loaded[$index]}
63 if ! sudo -Hu "$user" /bin/launchctl bootstrap "gui/$uid" "/Users/$user/Library/LaunchAgents/$label.plist" >/dev/null 2>&1; then status=1; fi
64 done
65 fi
66 if (( status == 0 )); then
67 rm -rf "$backup"
68 else
69 printf '组件更新未完成。已恢复可恢复的原文件,备份位于 %s。可以在 Cage 重试。\n' "$backup" >&2
70 fi
71 exit "$status"
72}
73trap finish EXIT
74trap 'exit 1' HUP INT TERM
75
76# Verify app ownership before stopping the corresponding GUI helpers.
77for app in "$kit"/*.app "$kit"/project/*.app; do
78 [[ -d $app ]] || continue
79 identifier=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$app/Contents/Info.plist")
80 [[ $identifier == dev.cage.* ]]
81 codesign --verify --deep --strict -R "=identifier \"$identifier\"" "$app"
82 target="/Applications/$(basename "$app")"
83 if [[ -e $target ]]; then
84 [[ -d $target && ! -L $target ]]
85 # Missing or damaged installed files are repairable. The incoming app
86 # is fully verified, and an existing signing team remains a constraint.
87 previous_team=$(codesign -dv "$target" 2>&1 | sed -n 's/^TeamIdentifier=//p' || true)
88 next_team=$(codesign -dv "$app" 2>&1 | sed -n 's/^TeamIdentifier=//p')
89 [[ -z $previous_team || $previous_team == not\ set || $previous_team == "$next_team" ]]
90 fi
91 index=${#apps[@]}
92 ditto "$app" "$backup/new-app-$index"
93 chmod -R a+rX,go-w "$backup/new-app-$index"
94 codesign --verify --deep --strict "$backup/new-app-$index"
95 apps+=("$target")
96 if [[ -d $target ]]; then app_existed+=(yes); else app_existed+=(no); fi
97done
98for label in dev.cage.GuestClaudeWindow dev.cage.GuestClipboard dev.cage.GuestBrowser; do
99 if sudo -Hu "$user" /bin/launchctl print "gui/$uid/$label" >/dev/null 2>&1; then
100 loaded+=("$label")
101 previous_loaded+=("$label")
102 sudo -Hu "$user" /bin/launchctl bootout "gui/$uid/$label"
103 fi
104done
105# Project shares and workbench terminals have already been closed by the host.
106while read -r pid owner executable; do
107 if [[ $owner == "$uid" && $executable == /Applications/CageProjectAccess.app/Contents/MacOS/CageProjectAccess ]]; then kill -TERM "$pid" 2>/dev/null || true; fi
108done < <(ps -axo pid=,uid=,comm=)
109
110for ((index=0; index<${#apps[@]}; index++)); do
111 target=${apps[$index]}
112 if [[ ${app_existed[$index]} == yes ]]; then mv "$target" "$backup/old-app-$index"; fi
113 app_applied=$((index+1))
114 mv "$backup/new-app-$index" "$target"
115done
116
117while IFS= read -r -d '' source && IFS= read -r -d '' target; do
118 # The CLI installer publishes a new immutable helper directory and moves
119 # its owned symlink. It also generates the verified Python entry shebang.
120 [[ $target != /usr/local/libexec/cage-cli/* && $target != /Applications/* ]] || continue
121 parent=$(dirname "$target")
122 check=$parent
123 while [[ $check != / ]]; do
124 [[ ! -L $check ]]
125 check=$(dirname "$check")
126 done
127 mkdir -p "$parent"
128 [[ ! -L $target && ( ! -e $target || -f $target ) ]]
129 if [[ -f $target ]] && cmp -s "$kit/$source" "$target"; then continue; fi
130 index=${#changed[@]}
131 if [[ -f $target ]]; then
132 cp -p "$target" "$backup/$index"
133 existed+=(yes)
134 else
135 existed+=(no)
136 fi
137 changed+=("$target")
138 stage=$(mktemp "$parent/.cage-component.XXXXXXXX")
139 cp -p "$kit/$source" "$stage"
140 case "$target" in
141 */controller/cage-tailnet|/usr/local/libexec/cage-register-*|/usr/local/libexec/cage-macos-editor|/usr/local/libexec/cage-sshfs|/usr/local/libexec/cage-macos-terminal|/usr/local/libexec/cage-finish-initial-setup|/usr/local/libexec/cage-macos-ipv6-check) chmod 755 "$stage" ;;
142 *) chmod a+r,go-w "$stage" ;;
143 esac
144 mv -f "$stage" "$target"
145done < <(/usr/bin/perl "$kit/native-component-manifest.pl" targets "$manifest" "$kit")
146
147if [[ $mode == tools ]]; then
148 bash "$kit/install-macos-node.sh" "$kit/node.tgz"
149 bash "$kit/install-macos-browser.sh" "$kit" --update-tools
150 bash "$kit/cli/install-macos-cli.sh" "$kit/cli"
151else
152 bash "$kit/install-macos-cli.sh" "$kit" --components
153fi
154for app in "$kit"/*.app "$kit"/project/*.app; do
155 [[ -d $app ]] || continue
156 codesign --verify --deep --strict "/Applications/$(basename "$app")"
157done
158# Registration can migrate an existing agent's lifecycle settings. Restore
159# those files with the matching binaries if activation or publication fails.
160for label in dev.cage.GuestClaudeWindow dev.cage.GuestClipboard dev.cage.GuestBrowser; do
161 target="/Users/$user/Library/LaunchAgents/$label.plist"
162 [[ ! -L $target && ( ! -e $target || ( -f $target && $(stat -f %u "$target") == "$uid" ) ) ]]
163 index=${#changed[@]}
164 if [[ -f $target ]]; then
165 cp -p "$target" "$backup/$index"
166 existed+=(yes)
167 else
168 existed+=(no)
169 fi
170 changed+=("$target")
171done
172sudo -Hu "$user" /usr/local/libexec/cage-register-claude-window --prepare
173sudo -Hu "$user" /usr/local/libexec/cage-register-clipboard --prepare
174sudo -Hu "$user" /usr/local/libexec/cage-register-browser --prepare
175# New helpers are activated for an already logged-in GUI user as well.
176if [[ $(stat -f %u /dev/console) == "$uid" ]]; then
177 for label in dev.cage.GuestClaudeWindow dev.cage.GuestClipboard dev.cage.GuestBrowser; do
178 if [[ " ${loaded[*]-} " != *" $label "* ]]; then loaded+=("$label"); fi
179 done
180fi
181for ((index=0; index<${#loaded[@]}; index++)); do
182 label=${loaded[$index]}
183 sudo -Hu "$user" /bin/launchctl bootstrap "gui/$uid" "/Users/$user/Library/LaunchAgents/$label.plist"
184done
185/usr/bin/perl "$kit/native-component-manifest.pl" publish "$manifest" "$kit"
186printf '%s\n' '原生客机组件已更新并通过文件核验。'