#!/bin/bash
export LC_ALL=C
printf '\n----- 9ROUTER -> PROXY (TCP from inside container) -----\n'
docker exec 9router node -e '
const net=require("net");
for (const [h,p] of [["172.19.0.1",20808],["172.17.0.1",20808]]) {
  const s=net.connect(p,h,()=>{console.log(`  ${h}:${p} -> TCP OK`);s.end();});
  s.setTimeout(5000);
  s.on("timeout",()=>{console.log(`  ${h}:${p} -> TIMEOUT`);s.destroy();});
  s.on("error",e=>console.log(`  ${h}:${p} -> ${e.code}`));
}
' 2>&1 | head -6

printf '\n----- HARVEST IN PROGRESS? -----\n'
systemctl is-active proxylab-cycle.service
tail -n 8 /var/log/proxylab-cycle.log 2>/dev/null

printf '\n----- ENDPOINT AFTER RENDER -----\n'
for a in 127.0.0.1 172.19.0.1; do
  code=$(curl -s -o /dev/null -w '%{http_code} %{time_total}' --max-time 15 -x "http://$a:20808" https://www.gstatic.com/generate_204 2>/dev/null)
  printf '  %-12s -> %s\n' "$a" "$code"
done
printf '  exit ip: '; curl -s --max-time 20 -x http://127.0.0.1:20808 https://api.ipify.org 2>/dev/null; echo

printf '\n----- OUTBOUNDS IN SERVED CONFIG -----\n'
python3 - <<'PY'
import json
c=json.load(open('/opt/proxy-lab/data/serve.json'))
for o in c['outbounds'][:-1]:
    print('  %-4s %-10s %-34s %s' % (o.get('tag'),o.get('type'),str(o.get('server'))[:34],o.get('server_port')))
print('  group:', c['outbounds'][-1]['tag'], '->', c['outbounds'][-1]['outbounds'])
PY

printf '\n----- LEGACY TIMER -----\n'
systemctl is-enabled proxylab.timer 2>&1; systemctl is-active proxylab.timer 2>&1

printf '\n----- FAILED UNITS -----\n'
systemctl --failed --no-pager --no-legend 2>/dev/null | head -5
