#!/bin/bash
trap 'systemctl start sing-box; sleep 3; systemctl is-active sing-box >> /tmp/rawudp.log' EXIT
systemctl stop sing-box
sleep 1
python3 -u - <<'PY' >> /tmp/rawudp.log 2>&1
import socket,time,sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
    s.bind(('0.0.0.0',443))
except Exception as e:
    print('BINDFAIL',repr(e),flush=True); sys.exit(0)
print('listening on 443',flush=True)
s.settimeout(40)
try:
    d,addr=s.recvfrom(2048)
except Exception as e:
    print('NOPROBE',repr(e),flush=True); sys.exit(0)
print('probe from',addr,'len',len(d),flush=True)
t0=time.time()
for i in range(100):
    s.sendto(bytes([i%256])*1200, addr)
    time.sleep(0.05)
print('sent100 over 5s at',round(time.time()-t0,2),flush=True)
s.settimeout(16)
cnt=0; last=None
try:
    while True:
        d,a=s.recvfrom(2048)
        cnt+=1; last=round(time.time()-t0,2)
except Exception:
    pass
print('client_keepalives_received=',cnt,'last_at_s=',last,flush=True)
PY
exit 0
