#!/bin/bash # configure reads TITLE and DESCRIPTION # shellcheck disable=SC2034 TITLE="Tailscale" # shellcheck disable=SC2034 DESCRIPTION="Join your Core's Tailscale network" CORE_CONF="$CONF_DIR/core.conf" TAILSCALE_CONF="$CONF_DIR/tailscale.conf" source "$CONNECTOR/configure.d/lib/json.sh" source "$CONNECTOR/configure.d/lib/apps.sh" [ -f "$CORE_CONF" ] && source "$CORE_CONF" [ -f "$TAILSCALE_CONF" ] && source "$TAILSCALE_CONF" # The Tailscale app's own command line, in both the App Store and the # standalone app tailscale() { local app app="$(app_path tailscale)" || return 1 "$app/Contents/MacOS/Tailscale" "$@" } running() { tailscale status --json 2> /dev/null | json_get 'd.BackendState === "Running"' > /dev/null } # Tailscale has no status field naming its login server, so tailscale.conf # records which Core it was set up for joined() { [ -n "$TAILSCALE_DOMAIN" ] && running } # Done when on the main Core's network is_configured() { [ -n "$CORE_DOMAIN" ] && [ "$TAILSCALE_DOMAIN" = "$CORE_DOMAIN" ] && joined } # Log out rather than only disconnect, so this Mac's key is expired on # headscale and it's off the network until it joins again leave_network() { tailscale logout || return 1 rm -f "$TAILSCALE_CONF" echo "This Mac has left your Core's Tailscale network. To join again: configure tailscale" } # The first time it opens, Tailscale asks you to allow its VPN configuration # (and, for the standalone app, its system extension); until then its command # line can't reach it tailscale_ready() { tailscale status --json 2> /dev/null | json_get 'd.BackendState' > /dev/null } run_configure() { local choice if joined &> /dev/null; then echo "This Mac is on $TAILSCALE_DOMAIN's Tailscale network (headscale.$TAILSCALE_DOMAIN)." echo if [ "$TAILSCALE_DOMAIN" = "$CORE_DOMAIN" ]; then echo " 1) Join it again" else echo " 1) Join your main Core's network instead (headscale.$CORE_DOMAIN)" fi echo " 2) Leave the network" echo " q) Leave it as it is" echo read -rp "Choose: " choice case "$choice" in 1) ;; 2) leave_network; return ;; *) return 0 ;; esac fi if [ -z "$CORE_DOMAIN" ]; then echo "This joins your Core's Tailscale network, so set up your Core first: configure core" return 1 fi app_ensure tailscale "It joins this Mac to your Core's network." || return 1 if ! tailscale_ready; then open -a "$(app_path tailscale)" echo "Tailscale is opening. If it asks to add a VPN configuration or allow a system" echo "extension, allow it (System Settings may open for that)." wait_until 300 tailscale_ready || { echo "Tailscale isn't ready yet. Run this again when it is."; return 1; } fi # --reset and --force-reauth move the Mac from any other tailnet echo "Open the link below to sign in to your Core's Tailscale network (headscale.$CORE_DOMAIN)." tailscale up --reset --force-reauth --login-server "https://headscale.$CORE_DOMAIN" \ --accept-routes || return 1 mkdir -p "$(dirname "$TAILSCALE_CONF")" printf 'TAILSCALE_DOMAIN=%q\n' "$CORE_DOMAIN" > "$TAILSCALE_CONF" echo "This Mac is on your Core's Tailscale network." }