#!/bin/bash
export LC_ALL=C
sleep 25
echo "===== STATUS ====="
docker ps --filter name=n8n --format '{{.Names}} | {{.Status}}'
docker exec n8n n8n --version 2>/dev/null | tail -1

echo
echo "===== HEALTH ====="
docker exec n8n sh -c 'wget -q -T 5 -O /dev/null http://127.0.0.1:5678/healthz && echo OK || echo FAIL' 2>&1 | tail -1
curl -s -o /dev/null -w '  localhost:5678 -> %{http_code} in %{time_total}s\n' --max-time 10 http://127.0.0.1:5678/

echo
echo "===== STARTUP LOG ====="
docker logs n8n --since 5m 2>&1 | grep -viE 'license|deprecat' | tail -14

echo
echo "===== PROXY REACHABLE FROM CONTAINER? ====="
docker exec n8n sh -c 'wget -q -T 5 -O /dev/null http://172.19.0.1:20808 2>&1; echo "wget exit=$?"' 2>&1 | tail -1
docker exec n8n node -e '
const net=require("net");const s=net.connect(20808,"172.19.0.1",()=>{console.log("  tcp 172.19.0.1:20808 OK");s.end()});
s.on("error",e=>console.log("  tcp error",e.code));s.setTimeout(5000,()=>{console.log("  tcp timeout");s.destroy()});
' 2>&1 | head -2

echo
echo "===== PROXY THROUGH TUNNEL (real http) ====="
docker exec n8n node -e '
const http=require("http");
const r=http.request({host:"172.19.0.1",port:20808,method:"CONNECT",path:"www.gstatic.com:443",timeout:10000});
r.on("connect",(res,sock)=>{console.log("  CONNECT ->",res.statusCode);sock.destroy()});
r.on("error",e=>console.log("  error",e.code));
r.on("timeout",()=>{console.log("  timeout");r.destroy()});
r.end();' 2>&1 | head -2

echo
echo "===== WORKFLOWS ====="
python3 -c "
import sqlite3,json
c=sqlite3.connect('/var/lib/docker/volumes/n8n_n8n_data/_data/database.sqlite')
for name,active in c.execute('select name,active from workflow_entity'):
    print(f'  {name} (active={active})')
"
