diff --git a/users/pbsds/home/profiles/bashrc.d/adb-tailscale-connect.sh b/users/pbsds/home/profiles/bashrc.d/adb-tailscale-connect.sh new file mode 100644 index 0000000..17acbbf --- /dev/null +++ b/users/pbsds/home/profiles/bashrc.d/adb-tailscale-connect.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +# problem: +adb-tailscale-connect() { + if ! command -v adb >/dev/null; then + return 1 + fi + + declare -a hostnames=() + readarray -td $'\n' hostnames < <( + tailscale status --json \ + | jq '.MagicDNSSuffix as $suffix | .Peer[].DNSName | rtrimstr(".") | rtrimstr("." + $suffix)' -r \ + | sort \ + | gum filter --height=12 + ) + if [[ "${#hostnames[@]}" -eq 0 ]]; then + return 1 + fi + + ( + declare pids=() + declare hostname + for hostname in "${hostnames[@]}"; do + ( + (set -x; tailscale ping "$hostname") + declare port + port=$( + (set -x; nmap -p 30000-49999 "$hostname") \ + | grep -E '^[0-9]+/tcp open' | cut -d/ -f1 + ) + if [[ -n "$port" ]]; then + echo "$hostname port=$port" + fi + ) & + pids+=("$!") + done + trap : INT + wait + if [[ "$?" -eq 130 ]]; then # if interrupted + (set -x; kill "${pids[@]}") + fi + ) +}