跳到主要内容

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

浏览文件

claude-desktop-macos.sh

125 行 · 4.9 KB历史下载
1#!/bin/bash
2# Run inside a Cage macOS guest. Install accepts a local official DMG.
3set -euo pipefail
4export PATH=/usr/bin:/bin:/usr/sbin:/sbin
5
6fail() { printf '%s\n' "$*" >&2; exit 1; }
7
8case "${1:-}" in
9 inspect|launch) [[ $# == 1 ]] || fail "Usage: $0 $1" ;;
10 install) [[ $# == 2 ]] || fail "Usage: $0 install /path/to/Claude.dmg" ;;
11 *) fail "Usage: $0 inspect | install /path/to/Claude.dmg | launch" ;;
12esac
13
14[[ $(uname -s) == Darwin ]] || fail "This helper requires a Cage macOS guest."
15model=$(sysctl -n hw.model)
16[[ $model =~ ^VirtualMac[0-9]+,[0-9]+$ ]] || fail "This Mac is not a virtual Mac. Run this helper inside the Cage guest."
17marker=/var/db/cage-guest-os
18[[ -f $marker && ! -L $marker && $(stat -f '%u' "$marker") == 0 ]] ||
19 fail "Missing root-owned Cage guest marker."
20mode=$(stat -f '%OLp' "$marker")
21(( (8#$mode & 022) == 0 )) && [[ $(cat "$marker") == macos ]] ||
22 fail "Invalid Cage guest marker."
23
24app=/Applications/Claude.app
25bundle_id=com.anthropic.claudefordesktop
26team=Q6L2SF6YDW
27requirement="anchor apple generic and identifier \"$bundle_id\" and certificate leaf[subject.OU] = \"$team\" and certificate 1[field.1.2.840.113635.100.6.2.6] exists and certificate leaf[field.1.2.840.113635.100.6.1.13] exists"
28
29info() {
30 plutil -extract "$2" raw -o - "$1/Contents/Info.plist"
31}
32
33verify_app() {
34 [[ -d $1 && ! -L $1 ]] || fail "Expected a Claude.app directory at $1."
35 [[ $(info "$1" CFBundleIdentifier) == "$bundle_id" ]] || fail "Unexpected Claude bundle identifier."
36 codesign --verify --deep --strict "$1"
37 codesign --verify --strict -R "=$requirement" "$1"
38}
39
40report() {
41 printf 'status=%s\nversion=%s\nbuild=%s\nbundle_id=%s\nteam=%s\n' \
42 "$1" "$(info "$app" CFBundleShortVersionString)" "$(info "$app" CFBundleVersion)" "$bundle_id" "$team"
43}
44
45case "$1" in
46 inspect)
47 [[ -e $app || -L $app ]] || { printf 'status=absent\n'; exit 3; }
48 verify_app "$app"
49 report installed
50 ;;
51 install)
52 [[ $EUID == 0 ]] || fail "Run install as root inside the guest."
53 dmg=$2
54 [[ $dmg == /* && -f $dmg ]] || fail "Supply the absolute path of the downloaded Claude DMG."
55 exec 9>/var/run/cage-claude-install.lock
56 lockf -s -t 0 9 || fail "A Claude installation is in progress."
57 stage=$(mktemp -d /Applications/.cage-claude.XXXXXXXX)
58 mountdir="$stage/media"
59 mkdir "$mountdir"
60 cleanup() {
61 # Mounts are read-only. Keep the directory if the system cannot detach.
62 if [[ $(stat -f '%d' "$mountdir") != "$(stat -f '%d' "$stage")" ]]; then
63 hdiutil detach "$mountdir" >/dev/null || { printf 'Unable to detach %s\n' "$mountdir" >&2; return; }
64 fi
65 rm -rf "$stage"
66 }
67 trap cleanup EXIT
68 trap 'exit 1' HUP INT TERM
69 hdiutil attach -readonly -nobrowse -noautoopen -mountpoint "$mountdir" "$dmg" >/dev/null
70 source_app="$mountdir/Claude.app"
71 verify_app "$source_app"
72 # renamex_np publishes the verified copy on the same filesystem. With an
73 # existing app, RENAME_SWAP retains it in staging until the swap succeeds.
74 rename_flag=4 # RENAME_EXCL
75 if [[ -e $app || -L $app ]]; then
76 verify_app "$app"
77 if [[ $(info "$source_app" CFBundleVersion) == "$(info "$app" CFBundleVersion)" &&
78 $(info "$source_app" CFBundleShortVersionString) == "$(info "$app" CFBundleShortVersionString)" ]]; then
79 report installed
80 exit 0
81 fi
82 if pgrep -f '^/Applications/Claude\.app/Contents/MacOS/Claude([[:space:]]|$)' >/dev/null; then
83 fail "Quit the running guest Claude app before installing another version."
84 fi
85 rename_flag=2 # RENAME_SWAP
86 fi
87 staged_app="$stage/Claude.app"
88 ditto "$source_app" "$staged_app"
89 chown -R -P root:wheel "$staged_app"
90 verify_app "$staged_app"
91 /usr/bin/osascript -l JavaScript - "$staged_app" "$app" "$rename_flag" <<'JXA'
92ObjC.bindFunction("renamex_np", ["int", ["char *", "char *", "uint32_t"]]);
93function run(args) {
94 if ($.renamex_np(args[0], args[1], Number(args[2])) !== 0) {
95 throw Error("Could not publish Claude.app; previous installation preserved.");
96 }
97}
98JXA
99 report installed
100 ;;
101 launch)
102 verify_app "$app"
103 gui_uid=$(stat -f '%u' /dev/console)
104 if (( gui_uid < 500 )) || ! launchctl print "gui/$gui_uid" >/dev/null 2>&1; then
105 printf 'status=requires_gui_login\nLog in to the guest macOS desktop first.\n' >&2
106 exit 4
107 fi
108 if [[ $EUID == 0 ]]; then
109 launchctl asuser "$gui_uid" sudo -n -H -u "#$gui_uid" -- /usr/bin/open "$app"
110 elif [[ $EUID == "$gui_uid" ]]; then
111 /usr/bin/open "$app"
112 else
113 fail "Run launch as root or the guest console user."
114 fi
115 for ((attempt=0; attempt<20; attempt++)); do
116 if pid=$(pgrep -o -U "$gui_uid" -f '^/Applications/Claude\.app/Contents/MacOS/Claude([[:space:]]|$)'); then
117 report running
118 printf 'pid=%s\n' "$pid"
119 exit 0
120 fi
121 sleep 1
122 done
123 fail "The Claude application process did not start in the guest GUI session."
124 ;;
125esac