#!/bin/bash # # Purpose : Find the other machines on the Core's tailnet, for the configure.d # items that find servers (storage, ai-servers) beyond the LAN that # mDNS reaches. Source this file. # true if this machine is on a tailnet now tailnet_joined() { command -v tailscale > /dev/null || return 1 tailscale status --json 2> /dev/null | jq -e '.BackendState == "Running"' > /dev/null 2>&1 } # The other online computers on the tailnet, one MagicDNS name per line # (box.tail.example). Phones and tablets are left out: nobody serves files or # models from them, and each one costs a probe. Nothing if Tailscale isn't # installed or running. tailnet_hosts() { command -v tailscale > /dev/null || return 0 tailscale status --json 2> /dev/null \ | jq -r 'select(.BackendState == "Running") | .Peer // {} | .[] | select(.Online) | select(.OS | IN("linux", "macOS", "windows", "freebsd")) | .DNSName // "" | rtrimstr(".") | select(. != "")' 2> /dev/null \ | sort -u } # true if host $1 takes TCP connections on port $2 within 2 seconds port_open() { timeout 2 bash -c ': < "/dev/tcp/$1/$2"' _ "$1" "$2" 2> /dev/null } # The name to show and to use in paths: box.tail.example -> box, box.local -> # box. An IP address stays whole. short_host() { if [[ "$1" =~ ^[0-9.]+$ || "$1" == *:* ]]; then echo "$1" else echo "${1%%.*}" fi } # true if $1 is a tailnet name rather than a .local one or an address is_tailnet_host() { [[ "$1" == *.* && "$1" != *.local && ! "$1" =~ ^[0-9.]+$ && "$1" != *:* ]] } # The tailnet's machines, then the hosts in $1 (one per line, e.g. from # mdns_hosts) that aren't on the tailnet too. A machine at home is both # box.local and box.tail.example; its tailnet name keeps working away from home. with_tailnet_hosts() { local host key local -A seen=() for host in $(tailnet_hosts); do key="$(short_host "${host,,}")" seen[$key]=1 echo "$host" done for host in $1; do key="$(short_host "${host,,}")" [ -n "${seen[$key]:-}" ] || echo "$host" done }