#!/bin/bash
export LC_ALL=C
line() { printf '\n----- %s -----\n' "$1"; }

line "TREE /opt/proxy-lab /opt/proxy-check"
find /opt/proxy-lab /opt/proxy-check -maxdepth 2 -type f -printf '%TY-%Tm-%Td %10s  %p\n' 2>/dev/null | sort -k3 | head -30

line "XRAY CONFIG (full)"
cat /usr/local/etc/xray/config.json 2>/dev/null

line "PRIVOXY FORWARD RULES"
grep -E '^\s*forward' /etc/privoxy/config 2>/dev/null | head -10 || echo "(none)"

line "SYSTEMD UNITS"
for u in proxylab.service proxylab-serve.service proxylab.timer proxy-check-stage2.service; do
  printf '\n== %s\n' "$u"
  systemctl cat "$u" --no-pager 2>/dev/null | grep -vE '^\s*#' | sed -n '1,22p'
done

line "FAILED UNIT LOG (proxy-check-stage2)"
journalctl -u proxy-check-stage2 --no-pager -n 12 2>/dev/null | tail -12

line "PROXYLAB LOG (tail)"
journalctl -u proxylab-serve --no-pager -n 15 2>/dev/null | tail -15

line "9ROUTER ENV / CONFIG"
docker inspect 9router --format '{{range .Config.Env}}{{println .}}{{end}}' 2>/dev/null | head -20
printf '\nvolumes:\n'
docker inspect 9router --format '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' 2>/dev/null

line "WHO LISTENS ON 10808/8118/20808"
ss -tnp 2>/dev/null | grep -E ':(10808|8118|20808)\s' | head -5
printf '\nestablished via 10808: %s\n' "$(ss -tn state established 2>/dev/null | grep -c ':10808')"

line "END"
