Files
Reims-Go/internal/guesttools/image.go
T

692 lines
23 KiB
Go

package guesttools
import (
"bytes"
_ "embed"
"fmt"
"io"
"os"
"path/filepath"
"github.com/Nwflower/reims-go/internal/installer"
)
const (
ToolsVolumeName = "REIMSTOOLS"
InstallCommand = "sh /Volumes/REIMSTOOLS/Install-Reims-Tools.command"
)
//go:embed file_sync.sh
var guestFileSync string
// BuildToolsDiskImage creates a small read-only HFS+ disk image ("REIMSTOOLS")
// containing the per-VM Bearer token config, Guest Tools POSIX agent script
// (text + PNG image clipboard with long-poll, WebDAV live mount with fallback
// sync, and caffeinate prevent-sleep daemon), plus install/uninstall/pmset scripts.
func BuildToolsDiskImage(outPath string, cfg VMConfig) error {
if err := os.MkdirAll(filepath.Dir(outPath), 0o755); err != nil {
return err
}
confBytes := []byte(fmt.Sprintf("REIMS_HOST_URL=\"http://10.0.2.2:%d\"\nREIMS_HOST_PORT=\"%d\"\nREIMS_TOKEN=\"%s\"\nREIMS_VM=\"%s\"\n",
cfg.Port, cfg.Port, cfg.Token, cfg.VMName))
agentBytes := GuestAgentScript()
installBytes := GuestInstallScript()
uninstallBytes := GuestUninstallScript()
pmsetBytes := GuestPmsetScript()
files := []installer.HFSFile{
{
Name: "config",
Mode: 0o100644,
Size: int64(len(confBytes)),
Open: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(confBytes)), nil },
},
{
Name: "guest-tools.sh",
Mode: 0o100755,
Size: int64(len(agentBytes)),
Open: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(agentBytes)), nil },
},
{
Name: "Install-Reims-Tools.command",
Mode: 0o100755,
Size: int64(len(installBytes)),
Open: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(installBytes)), nil },
},
{
Name: "Uninstall-Reims-Tools.command",
Mode: 0o100755,
Size: int64(len(uninstallBytes)),
Open: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(uninstallBytes)), nil },
},
{
Name: "Apply-Prevent-Sleep-pmset.command",
Mode: 0o100755,
Size: int64(len(pmsetBytes)),
Open: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(pmsetBytes)), nil },
},
}
return installer.BuildHFSPlusDiskImage(outPath, ToolsVolumeName, files)
}
// GuestAgentScript returns the Guest Tools stock-macOS POSIX shell daemon
// (no Xcode or Python required):
// - Two-way UTF-8 text + PNG screenshot clipboard with server long-polling
// - Zero-duplication WebDAV shared folder mount (~/Desktop/ReimsShare) with automatic sync fallback
// - Host-controlled read-only Windows fixed-drive mounts with automatic unmount
// - Live host-controlled prevent-sleep guard via `caffeinate -dims` (no sudo required)
func GuestAgentScript() []byte {
return []byte(`#!/bin/sh
# Reims Guest Tools: clipboard, WebDAV share, read-only Windows drives, prevent-sleep.
set -u
# Force UTF-8 for macOS pbpaste/pbcopy under launchd (otherwise Simplified
# Chinese macOS defaults __CF_USER_TEXT_ENCODING to GB18030/GBK, causing
# mojibake when decoded as UTF-8 on Windows).
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export __CF_USER_TEXT_ENCODING="0x$(printf '%X' "$(id -u)"):0x08000100:0x08000100"
APP_DIR="$HOME/Library/Application Support/ReimsGuestTools"
CONF="$APP_DIR/config"
SHARE_SYNC="$HOME/ReimsShare"
SHARE_MOUNT="$HOME/ReimsShareMount"
DRIVE_MOUNT_ROOT="$HOME/ReimsWindowsDrives"
DESKTOP_LINK="$HOME/Desktop/ReimsShare"
TMP_DIR="$APP_DIR/tmp"
INJECT_LOCK="$TMP_DIR/injecting"
mkdir -p "$APP_DIR" "$SHARE_SYNC" "$SHARE_MOUNT" "$DRIVE_MOUNT_ROOT" "$TMP_DIR"
rm -f "$INJECT_LOCK" 2>/dev/null || true
CAFFEINATE_PID=""
TEXT_INPUT_PID=""
cleanup() {
if [ -n "$CAFFEINATE_PID" ]; then
kill "$CAFFEINATE_PID" 2>/dev/null || true
fi
if [ -n "$TEXT_INPUT_PID" ]; then
kill "$TEXT_INPUT_PID" 2>/dev/null || true
fi
rm -f "$INJECT_LOCK" 2>/dev/null || true
}
trap cleanup EXIT INT TERM
load_conf() {
if [ -f "/Volumes/REIMSTOOLS/config" ]; then
cp "/Volumes/REIMSTOOLS/config" "$CONF" 2>/dev/null || true
chmod 600 "$CONF" 2>/dev/null || true
fi
if [ -f "$CONF" ]; then
# shellcheck disable=SC1090
. "$CONF"
fi
}
REIMS_HOST_URL=""
REIMS_HOST_PORT=""
REIMS_TOKEN=""
REIMS_VM=""
load_conf
urlencode() {
LC_ALL=C awk 'BEGIN {
for (i = 0; i < 256; i++) ord[sprintf("%c", i)] = i
s = ARGV[1]
out = ""
for (i = 1; i <= length(s); i++) {
c = substr(s, i, 1)
if (c ~ /[A-Za-z0-9._~\/-]/) out = out c
else out = out sprintf("%%%02X", ord[c])
}
printf "%s", out
}' "$1"
}
hash_file() {
shasum -a 256 "$1" 2>/dev/null | awk '{print $1}'
}
hash_png_file() {
# Matches server payloadHash(ClipPNG): sha256("png:" + bytes)
{ printf 'png:'; cat "$1"; } | shasum -a 256 2>/dev/null | awk '{print $1}'
}
update_desktop_link() {
target="$1"
if [ -L "$DESKTOP_LINK" ]; then
cur=$(readlink "$DESKTOP_LINK" 2>/dev/null || true)
[ "$cur" = "$target" ] && return 0
rm -f "$DESKTOP_LINK" 2>/dev/null || true
elif [ -e "$DESKTOP_LINK" ]; then
return 0
fi
ln -s "$target" "$DESKTOP_LINK" 2>/dev/null || true
}
is_webdav_mounted() {
mount 2>/dev/null | grep -F "on $SHARE_MOUNT (" >/dev/null 2>&1
}
mount_mode="sync"
ensure_webdav_mount() {
[ -n "$REIMS_HOST_URL" ] && [ -n "$REIMS_TOKEN" ] && [ -n "$REIMS_VM" ] || return 0
if is_webdav_mounted; then
mount_mode="webdav"
update_desktop_link "$SHARE_MOUNT"
return 0
fi
if ! command -v mount_webdav >/dev/null 2>&1; then
mount_mode="sync"
update_desktop_link "$SHARE_SYNC"
return 0
fi
host_addr=${REIMS_HOST_URL#http://}
host_addr=${host_addr#https://}
host_addr=${host_addr%/}
dav_url="http://reims:${REIMS_TOKEN}@${host_addr}/dav/${REIMS_VM}/"
mkdir -p "$SHARE_MOUNT"
if mount_webdav -v "ReimsShare" "$dav_url" "$SHARE_MOUNT" >/dev/null 2>&1 || \
mount_webdav "$dav_url" "$SHARE_MOUNT" >/dev/null 2>&1; then
mount_mode="webdav"
update_desktop_link "$SHARE_MOUNT"
else
mount_mode="sync"
update_desktop_link "$SHARE_SYNC"
fi
}
sleep_active="false"
drive_mount_failures=""
previous_desired_drives=""
` + guestDriveCleanup + `
mount_drive_readonly() (
drive="$1"
target="$2"
if [ -L "$target" ]; then
printf 'Mount point is a symbolic link: %s\n' "$target" >&2
return 1
fi
mkdir -p "$target" || return 1
token_length=${#REIMS_TOKEN}
if [ "$token_length" -eq 0 ] || [ "$token_length" -gt 255 ]; then
printf 'Invalid WebDAV credential length\n' >&2
return 1
fi
dav_url="${REIMS_HOST_URL%/}/dav/$(urlencode "$REIMS_VM")/drives/$drive/"
# Use the credential descriptor accepted by Apple's WebDAV NetFS helper.
# Each field has a 32-bit big-endian length: user, password, proxy
# user, proxy password, then TLS properties. The descriptor must be seekable.
auth_file=$(mktemp "$TMP_DIR/drive-auth.XXXXXX") || return 1
trap 'rm -f "$auth_file"' EXIT
{
rm -f "$auth_file" || return 1
printf '\000\000\000\005reims' >&3 || return 1
printf '\000\000\000%b%s' "\\0$(printf '%03o' "$token_length")" "$REIMS_TOKEN" >&3 || return 1
printf '\000\000\000\000\000\000\000\000\000\000\000\000' >&3 || return 1
exec mount_webdav -a 3 -S -o rdonly -v "Windows $drive" "$dav_url" "$target"
} 3<>"$auth_file"
)
mount_drive() {
letter="$1"
mount_point="$DRIVE_MOUNT_ROOT/$letter"
case "$(drive_mount_type "$mount_point")" in
webdav) ;;
"")
mount_output=$(mount_drive_readonly "$letter" "$mount_point" 2>&1) || {
mount_status=$?
case "$drive_mount_failures" in
*"$letter"*) ;;
*)
printf 'Windows drive %s: mount failed (exit %s)\n' "$letter" "$mount_status" >&2
[ -z "$mount_output" ] || printf '%s\n' "$mount_output" >&2
drive_mount_failures="$drive_mount_failures$letter"
;;
esac
return 0
}
;;
*) return 0 ;;
esac
case "$drive_mount_failures" in
*"$letter"*) drive_mount_failures=$(printf '%s' "$drive_mount_failures" | tr -d "$letter") ;;
esac
link="$HOME/Desktop/Windows $letter"
if [ ! -L "$link" ] && [ ! -e "$link" ]; then
ln -s "$mount_point" "$link" 2>/dev/null || true
fi
}
sync_drive_mounts() {
cfg_json="$1"
desired=$(printf '%s\n' "$cfg_json" | \
sed -n 's/.*"shared_drives"[[:space:]]*:[[:space:]]*\[\([^]]*\)\].*/\1/p' | \
tr ',' '\n' | sed -n 's/^[[:space:]]*"\([A-Z]\)"[[:space:]]*$/\1/p' | tr '\n' ' ')
if [ "$desired" != "$previous_desired_drives" ]; then
previous_desired_drives="$desired"
drive_mount_failures=""
fi
unmount_unused_drives "$desired"
for letter in $desired; do
mount_drive "$letter"
done
}
apply_host_config() {
[ -n "$REIMS_HOST_URL" ] && [ -n "$REIMS_TOKEN" ] || return 0
cfg_json=$(curl -fsS -m 3 -H "Authorization: Bearer $REIMS_TOKEN" "$REIMS_HOST_URL/v1/config" 2>/dev/null || true)
[ -n "$cfg_json" ] || return 0
sync_drive_mounts "$cfg_json"
if printf '%s' "$cfg_json" | grep -q '"clipboard_enabled"[[:space:]]*:[[:space:]]*false'; then
clip_enabled="0"
else
clip_enabled="1"
fi
if printf '%s' "$cfg_json" | grep -q '"prevent_sleep"[[:space:]]*:[[:space:]]*true'; then
if [ -z "$CAFFEINATE_PID" ] || ! kill -0 "$CAFFEINATE_PID" 2>/dev/null; then
if command -v caffeinate >/dev/null 2>&1; then
caffeinate -dims >/dev/null 2>&1 &
CAFFEINATE_PID=$!
fi
fi
if [ -n "$CAFFEINATE_PID" ] && kill -0 "$CAFFEINATE_PID" 2>/dev/null; then
sleep_active="true"
else
sleep_active="false"
fi
else
if [ -n "$CAFFEINATE_PID" ]; then
kill "$CAFFEINATE_PID" 2>/dev/null || true
CAFFEINATE_PID=""
fi
sleep_active="false"
fi
}
report_status() {
[ -n "$REIMS_HOST_URL" ] && [ -n "$REIMS_TOKEN" ] || return 0
curl -fsS -m 3 -X POST \
-H "Authorization: Bearer $REIMS_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"mount_mode\":\"$mount_mode\",\"sleep_active\":$sleep_active,\"agent_version\":7}" \
"$REIMS_HOST_URL/v1/status" >/dev/null 2>&1 || true
}
read_local_clipboard() {
out_file="$1"
cinfo=$(osascript -e 'clipboard info' 2>/dev/null || true)
case "$cinfo" in
*utf8*|*"Unicode text"*|*string*)
if pbpaste >"$out_file" 2>/dev/null && [ -s "$out_file" ]; then
echo "text"
return 0
fi
;;
esac
case "$cinfo" in
*PNGf*)
rm -f "$out_file"
if osascript -e "set fp to open for access POSIX file \"$out_file\" with write permission" \
-e "set eof fp to 0" \
-e "write (the clipboard as «class PNGf») to fp" \
-e "close access fp" >/dev/null 2>&1 && [ -s "$out_file" ]; then
echo "png"
return 0
fi
;;
esac
case "$cinfo" in
*TIFF*)
tiff_tmp="$TMP_DIR/clip_in.tiff"
rm -f "$tiff_tmp" "$out_file"
if osascript -e "set fp to open for access POSIX file \"$tiff_tmp\" with write permission" \
-e "set eof fp to 0" \
-e "write (the clipboard as TIFF picture) to fp" \
-e "close access fp" >/dev/null 2>&1 && [ -s "$tiff_tmp" ] &&
sips -s format png "$tiff_tmp" --out "$out_file" >/dev/null 2>&1 && [ -s "$out_file" ]; then
rm -f "$tiff_tmp"
echo "png"
return 0
fi
rm -f "$tiff_tmp"
;;
esac
case "$cinfo" in
*PNGf*|*TIFF*)
echo "empty"
;;
*)
pbpaste >"$out_file" 2>/dev/null || : >"$out_file"
echo "text"
;;
esac
}
write_local_png() {
osascript -e "set the clipboard to (read (POSIX file \"$1\") as «class PNGf»)" >/dev/null 2>&1
}
local_clip_hash() {
case "$1" in
png) hash_png_file "$2" ;;
text) hash_file "$2" ;;
empty) echo "empty" ;;
esac
}
clip_header() {
awk -v name="$1:" 'tolower($1)==name {gsub(/\r/,"",$2); print $2; exit}' "$hdr_out"
}
last_local_hash=""
last_server_hash=""
last_server_seq=""
clip_enabled="1"
tick=0
TAB=$(printf '\t')
sync_clipboard() {
[ -n "$REIMS_HOST_URL" ] && [ -n "$REIMS_TOKEN" ] || return 0
if [ -f "$INJECT_LOCK" ]; then
sleep 1
return 0
fi
clip_in="$TMP_DIR/clip_in"
clip_out="$TMP_DIR/clip_out"
hdr_out="$TMP_DIR/clip_hdr"
local_kind=$(read_local_clipboard "$clip_in")
cur_hash=$(local_clip_hash "$local_kind" "$clip_in")
post_conflict=0
if [ "$local_kind" != "empty" ] && [ -n "$last_local_hash" ] && [ "$cur_hash" != "$last_local_hash" ]; then
ct="text/plain; charset=utf-8"
[ "$local_kind" = "png" ] && ct="image/png"
set --
if [ -n "$last_server_seq" ]; then
set -- -H "X-Clip-Base-Seq: $last_server_seq"
fi
code=$(curl -sS -m 5 -X POST -D "$hdr_out" -o /dev/null -w '%{http_code}' \
-H "Authorization: Bearer $REIMS_TOKEN" \
-H "Content-Type: $ct" ${1+"$@"} \
--data-binary "@$clip_in" \
"$REIMS_HOST_URL/v1/clipboard" 2>/dev/null) || code=000
case "$code" in
200)
last_local_hash="$cur_hash"
last_server_hash=$(clip_header x-clip-hash)
last_server_seq=$(clip_header x-clip-seq)
return 0
;;
204|413)
last_local_hash="$cur_hash"
;;
409)
post_conflict=1
;;
esac
fi
# Long-poll up to 2s: host changes return immediately (~150ms), idle stays quiet.
code=$(curl -sS -m 5 -D "$hdr_out" -o "$clip_out" -w '%{http_code}' \
-H "Authorization: Bearer $REIMS_TOKEN" \
"$REIMS_HOST_URL/v1/clipboard?since=$last_server_hash&wait=2&accept=image" 2>/dev/null) || code=000
case "$code" in
200)
new_hash=$(clip_header x-clip-hash)
[ -n "$new_hash" ] || return 0
new_kind=$(clip_header x-clip-kind)
new_seq=$(clip_header x-clip-seq)
post_get_kind=$(read_local_clipboard "$clip_in")
post_get_hash=$(local_clip_hash "$post_get_kind" "$clip_in")
if [ "$post_get_hash" != "$cur_hash" ]; then
last_server_seq="$new_seq"
return 0
fi
if [ "$new_hash" != "$cur_hash" ]; then
if [ "$new_kind" = "png" ]; then
write_local_png "$clip_out" || return 0
else
pbcopy <"$clip_out" 2>/dev/null || return 0
fi
post_get_kind=$(read_local_clipboard "$clip_in")
post_get_hash=$(local_clip_hash "$post_get_kind" "$clip_in")
fi
last_local_hash="$post_get_hash"
last_server_hash="$new_hash"
last_server_seq="$new_seq"
;;
204|304)
if [ -z "$last_local_hash" ]; then
last_local_hash="$cur_hash"
fi
if [ "$code" = 204 ]; then
last_server_seq=""
else
new_seq=$(clip_header x-clip-seq)
if [ -n "$new_seq" ]; then
last_server_seq="$new_seq"
fi
new_hash=$(clip_header x-clip-hash)
if [ "$post_conflict" = 1 ] && [ -z "$new_hash" ]; then
last_local_hash="$cur_hash"
fi
fi
;;
503)
# Feature disabled on host: back off instead of busy-looping osascript + curl (M11).
sleep 5
;;
000)
sleep 2
;;
esac
}
` + guestFileSync + `
text_input_loop() {
hdr="$TMP_DIR/txt_hdr"
body="$TMP_DIR/txt_body"
bak="$TMP_DIR/txt_clip_bak"
cur_chk="$TMP_DIR/txt_clip_chk"
while :; do
load_conf
if [ -z "$REIMS_HOST_URL" ] || [ -z "$REIMS_TOKEN" ]; then
sleep 2
continue
fi
code=$(curl -sS -m 30 -D "$hdr" -o "$body" -w '%{http_code}' \
-H "Authorization: Bearer $REIMS_TOKEN" \
"$REIMS_HOST_URL/v1/text-input?wait=25" 2>/dev/null || echo "000")
if [ "$code" = "200" ]; then
input_id=$(awk 'tolower($1)=="x-input-id:" {gsub(/\r/,"",$2); print $2; exit}' "$hdr")
: > "$INJECT_LOCK"
pbpaste >"$bak" 2>/dev/null || : >"$bak"
pbcopy <"$body" 2>/dev/null || true
inj_hash=$(hash_file "$body")
if [ -n "$input_id" ]; then
curl -fsS -m 8 -X POST \
-H "Authorization: Bearer $REIMS_TOKEN" \
"$REIMS_HOST_URL/v1/text-input/ack?id=$input_id" >/dev/null 2>&1 || true
fi
# Restore prior text clipboard unless user copied something else during paste.
pbpaste >"$cur_chk" 2>/dev/null || : >"$cur_chk"
cur_after=$(hash_file "$cur_chk")
if [ "$cur_after" = "$inj_hash" ] && [ -s "$bak" ]; then
pbcopy <"$bak" 2>/dev/null || true
fi
rm -f "$INJECT_LOCK" 2>/dev/null || true
elif [ "$code" != "204" ]; then
sleep 1
fi
done
}
text_input_loop &
TEXT_INPUT_PID=$!
while :; do
if [ $((tick % 5)) -eq 0 ]; then
load_conf
ensure_webdav_mount
apply_host_config
report_status
fi
if [ "$clip_enabled" = "1" ]; then
sync_clipboard
else
sleep 3
fi
if [ "$mount_mode" != "webdav" ] && [ $((tick % 2)) -eq 0 ]; then
sync_files
fi
tick=$((tick + 1))
done
`)
}
// GuestInstallScript returns the double-clickable / one-command installer script
// placed on /Volumes/REIMSTOOLS.
func GuestInstallScript() []byte {
return []byte(`#!/bin/sh
set -e
SRC="/Volumes/REIMSTOOLS"
APP_DIR="$HOME/Library/Application Support/ReimsGuestTools"
PLIST="$HOME/Library/LaunchAgents/com.reimsgo.guest-tools.plist"
SHARE_SYNC="$HOME/ReimsShare"
SHARE_MOUNT="$HOME/ReimsShareMount"
if [ ! -f "$SRC/config" ] || [ ! -f "$SRC/guest-tools.sh" ]; then
echo "REIMSTOOLS volume not found at $SRC" >&2
exit 1
fi
mkdir -p "$APP_DIR" "$HOME/Library/LaunchAgents" "$SHARE_SYNC" "$SHARE_MOUNT"
cp "$SRC/config" "$APP_DIR/config"
cp "$SRC/guest-tools.sh" "$APP_DIR/guest-tools.sh"
chmod 600 "$APP_DIR/config"
chmod 755 "$APP_DIR/guest-tools.sh"
[ -e "$HOME/Desktop/ReimsShare" ] || ln -s "$SHARE_SYNC" "$HOME/Desktop/ReimsShare" 2>/dev/null || true
uid=$(id -u)
cf_enc="0x$(printf '%X' "$uid"):0x08000100:0x08000100"
cat >"$PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.reimsgo.guest-tools</string>
<key>EnvironmentVariables</key>
<dict>
<key>LANG</key>
<string>en_US.UTF-8</string>
<key>LC_ALL</key>
<string>en_US.UTF-8</string>
<key>__CF_USER_TEXT_ENCODING</key>
<string>$cf_enc</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>$APP_DIR/guest-tools.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>$APP_DIR/agent.log</string>
<key>StandardErrorPath</key>
<string>$APP_DIR/agent.log</string>
</dict>
</plist>
EOF
launchctl bootout "gui/$uid" "$PLIST" 2>/dev/null || launchctl unload "$PLIST" 2>/dev/null || true
launchctl bootstrap "gui/$uid" "$PLIST" 2>/dev/null || launchctl load -w "$PLIST"
# Verify host bridge connectivity immediately.
. "$APP_DIR/config"
if curl -fsS -m 5 -H "Authorization: Bearer $REIMS_TOKEN" "$REIMS_HOST_URL/v1/ping" >/dev/null 2>&1; then
echo "Reims Guest Tools installed and connected!"
echo "Clipboard: UTF-8 text + PNG screenshots (two-way)"
echo "Shared folder: ~/Desktop/ReimsShare (WebDAV live mount, auto-fallback to sync)"
echo "Prevent-sleep: controlled live from Reims-Go host UI"
else
echo "Reims Guest Tools installed (waiting for Windows host bridge at $REIMS_HOST_URL)..."
fi
`)
}
// GuestUninstallScript cleanly removes the LaunchAgent while preserving user files.
func GuestUninstallScript() []byte {
return []byte(`#!/bin/sh
set -e
APP_DIR="$HOME/Library/Application Support/ReimsGuestTools"
PLIST="$HOME/Library/LaunchAgents/com.reimsgo.guest-tools.plist"
SHARE_MOUNT="$HOME/ReimsShareMount"
DRIVE_MOUNT_ROOT="$HOME/ReimsWindowsDrives"
uid=$(id -u)
launchctl bootout "gui/$uid" "$PLIST" 2>/dev/null || launchctl unload "$PLIST" 2>/dev/null || true
umount "$SHARE_MOUNT" 2>/dev/null || true
` + guestDriveCleanup + `
unmount_unused_drives ""
rm -f "$PLIST"
rm -rf "$APP_DIR"
echo "Reims Guest Tools uninstalled. Your ~/ReimsShare files were kept."
`)
}
const guestDriveCleanup = `
drive_mount_type() {
[ ! -L "$1" ] || return 0
mount 2>/dev/null | awk -v target=" on $1 (" '
pos = index($0, target) {
split(substr($0, pos + length(target)), options, /[,)]/)
print options[1]
exit
}
'
}
unmount_unused_drives() {
for drive_path in "$DRIVE_MOUNT_ROOT"/*; do
[ -d "$drive_path" ] && [ ! -L "$drive_path" ] || continue
drive_letter=${drive_path##*/}
case "$drive_letter" in [A-Z]) ;; *) continue ;; esac
case " $1 " in *" $drive_letter "*) continue ;; esac
case "$(drive_mount_type "$drive_path")" in
webdav) umount "$drive_path" >/dev/null 2>&1 || continue ;;
"") ;;
*) continue ;;
esac
drive_link="$HOME/Desktop/Windows $drive_letter"
if [ -L "$drive_link" ] && [ "$(readlink "$drive_link")" = "$drive_path" ]; then
rm -f "$drive_link" 2>/dev/null || true
fi
rmdir "$drive_path" 2>/dev/null || true
done
}
`
// GuestPmsetScript offers an optional system-wide pmset configuration command
// for users who want persistent no-sleep settings even before login.
func GuestPmsetScript() []byte {
return []byte(`#!/bin/sh
set -e
echo "Applying recommended macOS VM power settings (disables idle/display/disk sleep and Power Nap)..."
sudo pmset -a sleep 0 displaysleep 0 disksleep 0 powernap 0
echo "Done! Current pmset settings:"
pmset -g custom
`)
}