diagnostics: watch guest session and reset only stale captive redirects
This commit is contained in:
1 parent
b3a3e26562
commit
f97ff6066e
1 file changed
+33
-18
@@ -25,23 +25,38 @@ try {
|
|||||||
console.log('TEMP-RECORDS',JSON.stringify((db.tempAccess||[]).map(t=>({device:hash(t.mac),createdAt:t.createdAt,expiresAt:t.expiresAt,revoked:t.revoked}))));
|
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));}
|
} catch(e){console.log('DB-ERROR',redact(e.message));}
|
||||||
|
|
||||||
for(const [label,command] of [
|
const deadline=Date.now()+5*60*1000;
|
||||||
['FULL-FILTER',['/ip/firewall/filter/print']],
|
let observed=false;
|
||||||
['FULL-NAT',['/ip/firewall/nat/print']],
|
let previous='';
|
||||||
['DNS-STATIC',['/ip/dns/static/print','=.proplist=name,regexp,address,disabled']],
|
let resetCount=0;
|
||||||
['GOOGLE-DNS',['/ping','=address=www.gstatic.com','=count=1']],
|
while(Date.now()<deadline){
|
||||||
['APPLE-DNS',['/ping','=address=www.apple.com','=count=1']],
|
const active=(await rosCommand(['/ip/hotspot/active/print','=.proplist=user,address,mac-address,server,uptime,session-time-left,bytes-in,bytes-out'])).map(object)
|
||||||
['GOOGLE-HTTP',['/tool/fetch','=url=http://www.gstatic.com/generate_204','=keep-result=no']],
|
.filter(a=>a.server===config.mikrotik.server && a.user==='FREE'+String(a['mac-address']||'').replace(/:/g,'').toUpperCase() && hash(a['mac-address'])==='f38116d39a');
|
||||||
['APPLE-HTTP',['/tool/fetch','=url=http://www.apple.com/library/test/success.html','=keep-result=no']],
|
if(!active.length){
|
||||||
]){
|
if(observed){console.log('WATCH-SESSION-ENDED',JSON.stringify({resetCount}));break;}
|
||||||
try{console.log(label,redact(JSON.stringify((await rosCommand(command,15000)).map(object))));}
|
if(previous!=='waiting'){console.log('WATCH-WAITING-FOR-GUEST');previous='waiting';}
|
||||||
catch(e){console.log(label+'-ERROR',redact(e.message));}
|
await new Promise(r=>setTimeout(r,5000));continue;
|
||||||
}
|
}
|
||||||
const hosts=(await rosCommand(['/ip/hotspot/host/print','=.proplist=address,mac-address,server'])).map(object).filter(h=>h.server===config.mikrotik.server);
|
observed=true;
|
||||||
const connections=(await rosCommand(['/ip/firewall/connection/print','=.proplist=src-address,dst-address,seen-reply,protocol,tcp-state,srcnat,dstnat'])).map(object);
|
const a=active[0],mac=a['mac-address'];
|
||||||
function cloudflare(value){const ip=String(value).split(':')[0].split('.').map(Number);return ip[0]===104&&ip[1]>=16&&ip[1]<=23||ip[0]===172&&ip[1]>=64&&ip[1]<=71;}
|
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);
|
||||||
for(const host of hosts){
|
const addresses=new Set(hosts.flatMap(h=>[h.address,h['to-address']]).filter(Boolean));
|
||||||
const rows=connections.filter(c=>(c['src-address']||'').split(':')[0]===host.address);
|
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 outside=rows.filter(c=>!cloudflare(c['dst-address'])&&!/^(10\\.|192\\.168\\.|172\\.(1[6-9]|2\\d|3[01])\\.)/.test(c['dst-address']||''));
|
const own=connections.filter(c=>addresses.has((c['src-address']||'').split(':')[0]));
|
||||||
console.log('NON-WALLED-TRAFFIC',JSON.stringify({device:hash(host['mac-address']),addressHash:hash(host.address),total:rows.length,outsideCloudflare:outside.length,outsideWithReplies:outside.filter(c=>c['seen-reply']==='true').length,outsideTlsWithReplies:outside.filter(c=>(c['dst-address']||'').endsWith(':443')&&c['seen-reply']==='true').length}));
|
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));
|
||||||
|
}
|
||||||
|
console.log('WATCH-FINISHED',JSON.stringify({observed,resetCount}));
|
||||||
Reference in new issue
Block a user