Files
connector-macos/configure.d/lib/mdns.sh
T
davidandClaude Opus 5.5 08e6b2ee4f Set up a Mac for a Federated Core, like CoreDesktop's configure
A macOS counterpart of CoreDesktop's configure, with the same drop-in
items in configure.d/:

- core: Mail, Calendar and Contacts through a configuration profile,
  Superpanel as an app, Nextcloud files through the Nextcloud app, and
  Element; with test-and-fix, password change and removal
- tailscale: join the Core's network through headscale
- backup: restic to Nextcloud through rclone, run by launchd through an
  app that can be given Full Disk Access
- storage: SMB shares mounted the way Finder does, again at login and
  on network changes
- agents, claude, opencode, hermes, sandbox: install the agents and run
  them in Anthropic's sandbox runtime (Seatbelt on macOS)
- ai-servers: LLM servers on this Mac or the network, for the agents

Apps and tools macOS lacks are offered for download, checked against
their makers' Team IDs and notarization, or published SHA-256 sums.
Everything runs on macOS' bash 3.2 with only built-in tools.

smoketest/ runs scenarios in fresh macOS VMs with Tart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 08:34:42 -07:00

82 lines
2.4 KiB
Bash

#!/bin/bash
#
# Purpose : Find what's on the local network over mDNS (Bonjour), for the
# configure.d items that set up servers and storage on it. Source
# this file.
#
# macOS has dns-sd rather than avahi-browse. dns-sd keeps browsing until it's
# stopped, and macOS has no timeout command, so run_for stops it.
# run_for <seconds> <command> [args]: run the command, and stop it after that
# many seconds
run_for() {
local seconds="$1" pid
shift
"$@" &
pid=$!
sleep "$seconds"
kill -INT "$pid" 2> /dev/null
wait "$pid" 2> /dev/null
return 0
}
# The instance names dns-sd -B lists, which can have spaces in them: they're
# the rest of each line from where the "Instance Name" heading starts
dns_sd_names() {
awk '
/Instance Name/ { start = index($0, "Instance Name"); next }
start && $2 == "Add" { print substr($0, start) }'
}
# The service types announced on the network, e.g. _smb._tcp
mdns_types() {
run_for 3 dns-sd -B _services._dns-sd._udp local. 2> /dev/null \
| awk '$2 == "Add" { split($6, proto, "."); print $7 "." proto[1] }' | sort -u
}
# The instances of a service type, one name per line
mdns_instances() {
run_for 3 dns-sd -B "$1" local. 2> /dev/null | dns_sd_names | sort -u
}
# Where an instance is: "host port", host without the trailing dot
mdns_resolve() {
run_for 2 dns-sd -L "$1" "$2" local. 2> /dev/null \
| sed -nE 's/.* can be reached at ([^ ]+)\.:([0-9]+).*/\1 \2/p' | head -n 1
}
# Every instance of the types given (or of all types): "name|type|host|port"
mdns_services() {
local dir types type name n=0
dir="$(mktemp -d)" || return 1
if [ $# -gt 0 ]; then types="$*"; else types="$(mdns_types)"; fi
for type in $types; do
mdns_instances "$type" > "$dir/browse.$type" &
done
wait
for type in $types; do
while IFS= read -r name; do
[ -n "$name" ] || continue
n=$((n + 1))
# shellcheck disable=SC2046
( set -- $(mdns_resolve "$name" "$type")
[ -n "$1" ] && printf '%s|%s|%s|%s\n' "$name" "$type" "$1" "$2" ) > "$dir/resolve.$n" &
done < "$dir/browse.$type"
done
wait
cat "$dir"/resolve.* 2> /dev/null | sort -u
rm -rf "$dir"
}
# This Mac's own .local name, which mdns_hosts leaves out
mdns_own_host() {
echo "$(scutil --get LocalHostName 2> /dev/null).local"
}
# The other machines on the network: one .local name per line
mdns_hosts() {
local own
own="$(mdns_own_host)"
mdns_services | cut -d'|' -f3 | grep -vixF "$own" | sort -fu
}