#!/usr/bin/env bash # Adopt this machine's RustDesk into help.jebit.services: point it at the server and, with an # enrollment token, assign it in the CortenDesk console. Installs the official RustDesk # first if it's missing. An existing permanent password is left as it is. # # curl -fsSL https://help.jebit.services/adopt.sh | sudo bash # or, to read it first: curl -fsSLO https://help.jebit.services/adopt.sh && sudo bash adopt.sh # # It asks for an enrollment token (Enter skips; the user can then claim the device by # signing in to the console from RustDesk). Options, all optional: # --enroll-token TOKEN (or CORTENDESK_ENROLL_TOKEN) --enroll-user NAME # --device-group NAME --strategy NAME --address-book NAME --device-name NAME --note TEXT # Linux: Debian/Ubuntu, Fedora/RHEL and openSUSE packages (via install-helpdesk.sh). # macOS: /Applications/RustDesk.app. set -euo pipefail BASE='https://help.jebit.services' HOST='help.jebit.services' KEY='2fXPVfTQT9ox0NdI9GYY4ux5+FY9ibxnJnLvbEHIFbU=' RELAY='' API='https://help.jebit.services' CONFIG='Qfi0TViZUSIVkY2xkbK5GeilWOZZ0K1gXd0kVWHlTSk5EM49WOUFFVmZFUYZmMiojI5V2aiwiIzV2YpZnclNnL0lmYlpmLwxWZo9yL6MHc0RHaiojIpBXYiwiIiojI5FGblJnIsIyclNWa2JXZz5CdpJWZq5CcsVGaiojI0N3boJye' VERSION='1.5.0' [ "$(id -u)" -eq 0 ] || { echo "Run it with sudo: curl -fsSL $BASE/adopt.sh | sudo bash" >&2; exit 1; } tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT if [ "$(uname -s)" = Linux ]; then if ! command -v rustdesk >/dev/null && command -v flatpak >/dev/null \ && flatpak info com.rustdesk.RustDesk >/dev/null 2>&1; then echo "This is the Flatpak RustDesk. As your normal user (no sudo), run:" echo " curl -fsSL $BASE/set-server-flatpak.sh | sh" exit 1 fi curl -fsSL "$BASE/install-helpdesk.sh" -o "$tmp/install-helpdesk.sh" bash "$tmp/install-helpdesk.sh" --config "$CONFIG" --prompt-enroll "$@" exit fi [ "$(uname -s)" = Darwin ] || { echo "Unsupported system: $(uname -s)" >&2; exit 1; } # ---- macOS ----------------------------------------------------------------------------- # Paths can be overridden for tests. APP=${RUSTDESK_APP:-/Applications/RustDesk.app} BIN="$APP/Contents/MacOS/RustDesk" DAEMONS=${LAUNCHDAEMONS_DIR:-/Library/LaunchDaemons} ROOT_HOME=${ROOT_HOME:-/var/root} PREFS=Library/Preferences/com.carriez.RustDesk/RustDesk2.toml user=${SUDO_USER:-} [ -n "$user" ] && [ "$user" != root ] || { echo "Run it with sudo from your own account, not as root." >&2; exit 1; } uid=$(id -u "$user") user_home=${ADOPT_USER_HOME:-$(eval echo "~$user")} as_user() { launchctl asuser "$uid" sudo -u "$user" "$@"; } token="${CORTENDESK_ENROLL_TOKEN:-}" fields=() while [ $# -gt 0 ]; do case "$1" in --enroll-token) token="${2:-}"; shift 2 ;; --enroll-user) fields+=(--user_name "${2:-}"); shift 2 ;; --device-group) fields+=(--device_group_name "${2:-}"); shift 2 ;; --strategy) fields+=(--strategy_name "${2:-}"); shift 2 ;; --address-book) fields+=(--address_book_name "${2:-}"); shift 2 ;; --device-name) fields+=(--device_name "${2:-}"); shift 2 ;; --note) fields+=(--note "${2:-}"); shift 2 ;; *) echo "Unknown argument: $1" >&2; exit 1 ;; esac done # Only when a terminal can really be opened: RMM tools, cron and CI have none. if [ -z "$token" ] && { : < /dev/tty; } 2>/dev/null; then printf 'CortenDesk enrollment token (Enter to skip enrollment): ' > /dev/tty IFS= read -rs token < /dev/tty || true echo > /dev/tty if [ -n "$token" ] && [ "${#fields[@]}" -eq 0 ]; then printf 'Console user who owns this device (Enter for none): ' > /dev/tty IFS= read -r owner < /dev/tty || true printf 'Device group (Enter for none): ' > /dev/tty IFS= read -r group < /dev/tty || true [ -z "${owner:-}" ] || fields+=(--user_name "$owner") [ -z "${group:-}" ] || fields+=(--device_group_name "$group") fi fi if [ -n "$token" ] && [ "${#fields[@]}" -eq 0 ]; then echo "Enrollment needs an owner, device group, strategy, address book, device name or note." >&2 exit 1 fi if [ ! -d "$APP" ]; then arch=$( [ "$(uname -m)" = arm64 ] && echo aarch64 || echo x86_64 ) echo "Installing RustDesk $VERSION..." curl -fsSL "$BASE/rustdesk-$VERSION-$arch.dmg" -o "$tmp/rustdesk.dmg" hdiutil attach -nobrowse -readonly -mountpoint "$tmp/mnt" "$tmp/rustdesk.dmg" >/dev/null ditto "$tmp/mnt/RustDesk.app" "$APP" hdiutil detach "$tmp/mnt" >/dev/null fi # Write the server into the [options] table of a RustDesk2.toml, keeping everything else. # (Same edit as set-server-macos.command; awk, because python3 isn't on every Mac.) set_options() { mkdir -p "$(dirname "$1")" && touch "$1" || return 1 awk -v host="$HOST" -v key="$KEY" -v relay="$RELAY" -v api="$API" ' function emit() { printf "custom-rendezvous-server = \047%s\047\nkey = \047%s\047\nrelay-server = \047%s\047\napi-server = \047%s\047\n", host, key, relay, api } /^\[options\][ \t]*$/ { print; emit(); done = 1; next } /^(custom-rendezvous-server|key|relay-server|api-server)[ \t]*=/ { next } { print } END { if (!done) { print ""; print "[options]"; emit() } } ' "$1" > "$1.new" && mv "$1.new" "$1" } echo "Closing RustDesk..." as_user osascript -e 'quit app "RustDesk"' >/dev/null 2>&1 || true sleep 2 # The app's own settings, for the signed-in user. set_options "$user_home/$PREFS" chown -R "$user" "$user_home/Library/Preferences/com.carriez.RustDesk" # The background service, if RustDesk was installed with its own "Install" button. service="" if ls "$DAEMONS"/com.carriez.RustDesk*.plist >/dev/null 2>&1; then service=1 set_options "$ROOT_HOME/$PREFS" "$BIN" --config "$CONFIG" >/dev/null 2>&1 || true pkill -f "$BIN" >/dev/null 2>&1 || true sleep 3 fi enrolled="" if [ -n "$token" ]; then if [ -z "$service" ]; then echo "Not enrolled: that needs RustDesk's background service. Open RustDesk, click Install," >&2 echo "then run this again, or sign in from RustDesk (Settings > Account) to claim the device." >&2 else out=$("$BIN" --assign --token "$token" "${fields[@]}" 2>&1 || true) case "$out" in *Done*) enrolled=1 ;; *) echo "Enrolling the device in the console failed: $out" >&2; exit 1 ;; esac fi fi as_user open -a "$APP" >/dev/null 2>&1 || true echo echo "RustDesk on this Mac now uses $HOST." if [ -n "$enrolled" ]; then echo "Console: enrolled" else echo "Console: not enrolled here. Sign in from RustDesk (Settings > Account) with a console account to claim it." fi [ -n "$service" ] || echo "For unattended access, click Install in the RustDesk window."