#!/bin/zsh
# iHomeMac home-line check: run on a remote Mac to see whether its internet exit
# looks like a household broadband line (not a data centre, VPN or proxy).
# Read it first, then run:  zsh home-line-check.sh
# Public IPs are masked in the output; nothing is sent anywhere except the
# lookups to ipinfo.io, ip-api.com and api64.ipify.org, plus Apple's speed test.
set -u

mask_ip() { python3 -c 'import sys; ip=sys.argv[1]; print(".".join(ip.split(".")[:2]+["x","x"]) if "." in ip else ":".join(ip.split(":")[:3])+":…")' "$1"; }

echo "## Time"
date -u "+%Y-%m-%d %H:%M UTC"

echo "\n## Machine"
system_profiler SPHardwareDataType | grep -E "Model Name|Model Identifier|Chip|Memory"
echo "macOS $(sw_vers -productVersion)"

echo "\n## Is any VPN or proxy in the way? (both must be empty or Enable : 0)"
env | grep -i "_proxy=" || echo "no proxy env vars"
scutil --proxy | grep -E "Enable" || true
echo "default route interface: $(route -n get default 2>/dev/null | awk '/interface/ {print $2}')"
# TUN-mode VPNs keep "default" on en0 but add 0/1 + 128/1 (or 1/8 ...) routes on a utun interface.
tun=$(netstat -rn -f inet | awk '$NF ~ /^utun/ && $1 ~ /^(0\/1|128\.0\/1|1|2|4|8|16|32|64)(\/|$)/' | head -3)
[ -n "$tun" ] && echo "WARNING: traffic is routed through a VPN tunnel — results are not the home line:\n$tun" || echo "no VPN tunnel routes"

echo "\n## Public exit (ipinfo.io)"
info=$(curl -s --max-time 10 --noproxy '*' https://ipinfo.io/json)
ip=$(echo "$info" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("ip",""))')
echo "ip: $(mask_ip "$ip")"
echo "$info" | python3 -c 'import sys,json; d=json.load(sys.stdin); [print(f"{k}: {d.get(k)}") for k in ("org","city","region","country")]'
# Reverse-DNS name tells home lines apart from mobile/business ranges of the same big ISP
# (e.g. *.ipoe.ocn.ne.jp, *.v4.enabler.ne.jp, *.bbtec.net). Digits that encode the IP are masked.
echo "$info" | python3 -c 'import sys,json,re; h=json.load(sys.stdin).get("hostname") or "(no reverse DNS)"; print("hostname:", re.sub(r"\d{2,}", "…", h))'

echo "\n## Is the exit flagged as hosting / proxy / mobile? (ip-api.com)"
curl -s --max-time 10 --noproxy '*' "http://ip-api.com/json/?fields=isp,org,as,hosting,proxy,mobile,countryCode"; echo

echo "\n## IPv6 exit"
v6=$(curl -s -6 --max-time 8 --noproxy '*' https://api64.ipify.org || true)
[ -n "$v6" ] && echo "ipv6: $(mask_ip "$v6")" || echo "no IPv6 exit"

echo "\n## First network hops (home router -> ISP)"
traceroute -n -m 5 -q 1 -w 2 1.1.1.1 2>/dev/null | sed -E 's/([0-9]+\.[0-9]+)\.[0-9]+\.[0-9]+/\1.x.x/g'

echo "\n## Devices sharing this line's LAN (count only; should be the Mac and the router)"
arp -an | grep -v incomplete | wc -l | xargs echo "ARP entries:"

echo "\n## Speed (macOS networkQuality, about 20 s)"
networkQuality -s 2>/dev/null | grep -E "Uplink capacity|Downlink capacity|Idle Latency|Responsiveness"

echo "\n## Preinstalled tools"
for t in claude brew git; do printf "%-6s " "$t"; command -v "$t" >/dev/null && echo "yes" || echo "MISSING"; done
ls /Applications | grep -iE "claude|todesk|rustdesk|uu|tailscale|anydesk|parsec" || true

echo "\n## Accounts on this Mac (should be only the customer's admin account at handover)"
dscl . list /Users UniqueID | awk '$2 >= 501 {print $1}'
