Files
coredesktop/configure.d/15-tailscale.sh
T
2026-10-29 06:36:51 -06:00

74 lines
2.4 KiB
Bash

#!/bin/bash
TITLE="Tailscale"
DESCRIPTION="Join your Core's Tailscale network"
CORE_CONF="$HOME/.config/coredesktop/core.conf"
TAILSCALE_CONF="$HOME/.config/coredesktop/tailscale.conf"
[ -f "$CORE_CONF" ] && source "$CORE_CONF"
[ -f "$TAILSCALE_CONF" ] && source "$TAILSCALE_CONF"
is_installed() {
command -v tailscale
}
# Tailscale has no status field naming its login server, so tailscale.conf
# records which Core it was set up for
joined() {
[ -n "$TAILSCALE_DOMAIN" ] \
&& tailscale status --json 2> /dev/null | jq -e '.BackendState == "Running"' > /dev/null
}
# Done when on the main Core's network
is_configured() {
[ -n "$CORE_DOMAIN" ] && [ "$TAILSCALE_DOMAIN" = "$CORE_DOMAIN" ] && joined
}
# Log out rather than only go down, so this computer's key is expired on
# headscale and it's off the network until it joins again
leave_network() {
sudo tailscale logout || return 1
rm -f "$TAILSCALE_CONF"
echo "This computer has left your Core's Tailscale network. To join again: configure tailscale"
}
run_configure() {
local choice
if joined &> /dev/null; then
echo "This computer 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
# --reset and --force-reauth move the machine from any other tailnet;
# --operator lets you, and the Tailscale GNOME extension, use it without sudo
if ! systemctl is-active -q tailscaled; then
sudo systemctl enable --now tailscaled || return 1
fi
echo "Open the link below to sign in to your Core's Tailscale network (headscale.$CORE_DOMAIN)."
sudo tailscale up --reset --force-reauth --login-server "https://headscale.$CORE_DOMAIN" \
--accept-routes --operator="$USER" || return 1
mkdir -p "$(dirname "$TAILSCALE_CONF")"
printf 'TAILSCALE_DOMAIN=%q\n' "$CORE_DOMAIN" > "$TAILSCALE_CONF"
echo "This computer is on your Core's Tailscale network."
}