Inspect live guest connection destinations

This commit is contained in:
jfeng committed 2026-10-22 17:06:46 +00:00
1 parent f97ff6066e
commit 9165731fec
1 file changed
+4 -34
+4 -34
View File
@@ -25,38 +25,8 @@ try {
console.log('TEMP-RECORDS',JSON.stringify((db.tempAccess||[]).map(t=>({device:hash(t.mac),createdAt:t.createdAt,expiresAt:t.expiresAt,revoked:t.revoked}))));
} catch(e){console.log('DB-ERROR',redact(e.message));}
const deadline=Date.now()+5*60*1000;
let observed=false;
let previous='';
let resetCount=0;
while(Date.now()<deadline){
const active=(await rosCommand(['/ip/hotspot/active/print','=.proplist=user,address,mac-address,server,uptime,session-time-left,bytes-in,bytes-out'])).map(object)
.filter(a=>a.server===config.mikrotik.server && a.user==='FREE'+String(a['mac-address']||'').replace(/:/g,'').toUpperCase() && hash(a['mac-address'])==='f38116d39a');
if(!active.length){
if(observed){console.log('WATCH-SESSION-ENDED',JSON.stringify({resetCount}));break;}
if(previous!=='waiting'){console.log('WATCH-WAITING-FOR-GUEST');previous='waiting';}
await new Promise(r=>setTimeout(r,5000));continue;
}
observed=true;
const a=active[0],mac=a['mac-address'];
const hosts=(await rosCommand(['/ip/hotspot/host/print','?mac-address='+mac,'=.proplist=address,to-address,server,authorized'])).map(object).filter(h=>h.server===config.mikrotik.server);
const addresses=new Set(hosts.flatMap(h=>[h.address,h['to-address']]).filter(Boolean));
const connections=(await rosCommand(['/ip/firewall/connection/print','=.proplist=.id,src-address,dst-address,reply-src-address,dstnat,seen-reply,tcp-state,protocol'])).map(object);
const own=connections.filter(c=>addresses.has((c['src-address']||'').split(':')[0]));
const stale=own.filter(c=>{
const [ip,port]=(c['reply-src-address']||'').split(':');
const destination=(c['dst-address']||'').split(':')[0];
return [config.hotspotIp,config.mikrotik.host].includes(ip)&&['64873','64874','64875'].includes(port)&&![config.hotspotIp,config.mikrotik.host].includes(destination);
});
if(stale.length){
for(const c of stale.slice(0,30)){await rosCommand(['/ip/firewall/connection/remove','=numbers='+c['.id']]);resetCount++;}
console.log('WATCH-RESET-CAPTIVE-CONNECTIONS',JSON.stringify({count:stale.length,resetCount,device:hash(mac)}));
}
const db=JSON.parse(fs.readFileSync(config.dataDir+'/db.json','utf8'));
const record=(db.tempAccess||[]).find(t=>t.mac===mac);
const summary=JSON.stringify({device:hash(mac),uptime:a.uptime,remaining:a['session-time-left'],bytesIn:a['bytes-in'],bytesOut:a['bytes-out'],connections:own.length,captiveConnections:stale.length,repliedConnections:own.filter(c=>c['seen-reply']==='true').length,probe:record?.clientProbe||null});
if(summary!==previous){console.log('WATCH-ACTIVE',summary);previous=summary;}
if(record?.clientProbe?.results?.some(r=>r.ok)){console.log('WATCH-CLIENT-EXTERNAL-SUCCESS');break;}
await new Promise(r=>setTimeout(r,5000));
const active=(await rosCommand(['/ip/hotspot/active/print'])).map(object).find(a=>hash(a['mac-address'])==='f38116d39a');
if(active){
const cs=(await rosCommand(['/ip/firewall/connection/print'])).map(object).filter(c=>(c['src-address']||'').split(':')[0]===active.address);
console.log('CONNECTION-DETAIL',redact(JSON.stringify(cs.map(c=>({destination:c['dst-address'],reply:c['reply-src-address'],state:c['tcp-state'],protocol:c.protocol,replied:c['seen-reply'],originalPackets:c['orig-packets'],replyPackets:c['repl-packets'],dstnat:c.dstnat})))));
}
console.log('WATCH-FINISHED',JSON.stringify({observed,resetCount}));