#!/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" [ -f "$CORE_CONF" ] && source "$CORE_CONF" [ -f "$TAILSCALE_CONF" ] && source "$TAILSCALE_CONF" UNITS="$HOME/.config/systemd/user" TAILDROP_UNIT="federated-connector-taildrop.service" # NetworkManager runs only dispatcher scripts that root owns and only root can # change. For the tests: DISPATCHER_DIR. CAPTIVE_PORTAL="${DISPATCHER_DIR:-/etc/NetworkManager/dispatcher.d}/90-federated-connector-captive-portal" # Tailscale's own installer adds its apt repository (pkgs.tailscale.com), so # Pop!_OS' updates keep it up to date. It's downloaded, then run, rather than # piped into a shell. install_tailscale() { local tmp status echo "Tailscale isn't installed. It comes from Tailscale's own apt repository." confirm_yes "Install it?" || return 1 tmp="$(mktemp -d)" || return 1 if curl -fsSL --max-time 60 -o "$tmp/install.sh" https://tailscale.com/install.sh; then sh "$tmp/install.sh" status=$? else echo "Couldn't download Tailscale's installer." status=1 fi rm -rf "$tmp" return $status } # 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 } # What goes with being on the network: the service that saves files sent here # with Taildrop to Downloads (system/taildrop), and the NetworkManager script # that stops using an exit node while you sign in to a captive portal # (system/captive-portal). True when both are installed and up to date. extras_ok() { cmp -s "$CONNECTOR/system/systemd/$TAILDROP_UNIT" "$UNITS/$TAILDROP_UNIT" \ && systemctl --user is-enabled -q "$TAILDROP_UNIT" 2> /dev/null \ && cmp -s "$CONNECTOR/system/captive-portal" "$CAPTIVE_PORTAL" } install_extras() { mkdir -p "$UNITS" && cp "$CONNECTOR/system/systemd/$TAILDROP_UNIT" "$UNITS/" || return 1 systemctl --user daemon-reload systemctl --user enable "$TAILDROP_UNIT" 2> /dev/null || return 1 systemctl --user restart "$TAILDROP_UNIT" 2> /dev/null if ! cmp -s "$CONNECTOR/system/captive-portal" "$CAPTIVE_PORTAL"; then sudo install -D -m 755 -o root -g root "$CONNECTOR/system/captive-portal" "$CAPTIVE_PORTAL" || return 1 fi echo "Files sent to this computer with Taildrop are saved to Downloads. To send files to" echo "your other computers on the network: tailscale file cp ... :" } remove_extras() { systemctl --user disable --now "$TAILDROP_UNIT" 2> /dev/null rm -f "$UNITS/$TAILDROP_UNIT" systemctl --user daemon-reload # The captive portal script stays: without an exit node it does nothing } # Done when on the main Core's network, with what goes with it is_configured() { [ -n "$CORE_DOMAIN" ] && [ "$TAILSCALE_DOMAIN" = "$CORE_DOMAIN" ] && joined && extras_ok } is_configured_but_extras() { [ -n "$CORE_DOMAIN" ] && [ "$TAILSCALE_DOMAIN" = "$CORE_DOMAIN" ] && joined &> /dev/null && ! extras_ok } # 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" remove_extras echo "This computer has left your Core's Tailscale network. To join again: configure tailscale" } run_configure() { local choice # On the network, from before Taildrop and the captive portal script: add # them without signing in again if is_configured_but_extras; then install_extras return fi 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 command -v tailscale > /dev/null || install_tailscale || return 1 # --reset and --force-reauth move the machine from any other tailnet; # --operator lets you 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." install_extras }