#!/bin/bash
# Read-only discovery of the existing proxy / 9router setup.
export LC_ALL=C
line() { printf '\n----- %s -----\n' "$1"; }

line "PROXY BINARIES"
for b in xray v2ray sing-box privoxy; do
  p=$(command -v $b 2>/dev/null)
  [ -n "$p" ] && printf '%-9s %s  %s\n' "$b" "$p" "$($p version 2>/dev/null | head -1)"
done

line "SYSTEMD UNITS (proxy related)"
systemctl list-units --type=service --all --no-pager --no-legend 2>/dev/null \
  | grep -iE 'xray|v2ray|sing|privoxy|9router|proxy|sub' | sed 's/  */ /g' | cut -c1-110

line "CONFIG LOCATIONS"
for d in /usr/local/etc/xray /etc/xray /usr/local/etc/sing-box /etc/sing-box /etc/privoxy /opt; do
  [ -e "$d" ] && printf '%s\n' "$d" && ls -la "$d" 2>/dev/null | head -8
done

line "XRAY/SING-BOX CONFIG SUMMARY"
for f in $(ls /usr/local/etc/xray/config.json /etc/xray/config.json /usr/local/etc/sing-box/config.json /etc/sing-box/config.json 2>/dev/null); do
  printf '== %s (%s)\n' "$f" "$(stat -c '%y %s bytes' "$f")"
  python3 - "$f" <<'PY' 2>/dev/null || echo "  (python parse failed)"
import json,sys
d=json.load(open(sys.argv[1]))
if 'inbounds' in d:
    print('  inbounds:', [(i.get('tag'), i.get('port'), i.get('listen')) for i in d['inbounds']])
if 'outbounds' in d:
    print('  outbounds:', len(d['outbounds']), [o.get('tag') for o in d['outbounds']][:8])
if 'routing' in d:
    print('  routing rules:', len(d['routing'].get('rules',[])))
if 'log' in d: print('  log:', d['log'])
PY
done

line "PRIVOXY CONFIG (active lines)"
grep -vE '^\s*#|^\s*$' /etc/privoxy/config 2>/dev/null | grep -iE 'listen-address|forward|toggle' | head -8

line "CRON / TIMERS"
crontab -l 2>/dev/null | head -20
ls -la /etc/cron.d/ 2>/dev/null | head
systemctl list-timers --all --no-pager 2>/dev/null | head -8

line "EXISTING SCRIPTS"
ls -la /root/*.sh /root/*.py /root/*.json 2>/dev/null | head -20
find /root /opt -maxdepth 2 -name '*.sh' -newermt '-90 days' 2>/dev/null | head -15

line "9ROUTER CONTAINER"
docker inspect 9router --format '{{json .HostConfig.NetworkMode}} {{json .NetworkSettings.Networks}}' 2>/dev/null | cut -c1-200
docker inspect 9router --format '{{range .Config.Env}}{{println .}}{{end}}' 2>/dev/null | grep -iE 'proxy|http_proxy|https_proxy|no_proxy|api_base|base_url' | head -10
docker inspect 9router --format '{{json .HostConfig.PortBindings}}' 2>/dev/null

line "DOCKER NETWORKS"
docker network ls 2>/dev/null
docker network inspect $(docker network ls -q 2>/dev/null) --format '{{.Name}} {{range .IPAM.Config}}{{.Subnet}}{{end}}' 2>/dev/null | head -10

line "OUTBOUND REACHABILITY FROM SERVER"
for u in https://raw.githubusercontent.com https://github.com https://api.openai.com/v1/models https://www.gstatic.com/generate_204; do
  printf '%-45s ' "$u"
  curl -s -o /dev/null -w '%{http_code} %{time_total}s\n' --max-time 8 "$u" 2>&1
done

line "PROXY PORT TESTS (local)"
for p in 20808 10808 10809 8118 20128 5678; do
  printf 'port %-6s ' "$p"
  timeout 2 bash -c "</dev/tcp/127.0.0.1/$p" 2>/dev/null && echo 'open' || echo 'closed'
done

line "END"
