diff --git a/backend/diagnose-hotspot.mjs b/backend/diagnose-hotspot.mjs index eccccb4..01dca7d 100644 --- a/backend/diagnose-hotspot.mjs +++ b/backend/diagnose-hotspot.mjs @@ -25,10 +25,23 @@ 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 {spawnSync}=await import('node:child_process'); -const check=spawnSync(process.execPath,['-e',`const fs=require("fs"),path=require("path"),crypto=require("crypto");const dir=process.env.DIAG_DATA_DIR;const a=path.join(dir,".write-probe-"+crypto.randomBytes(8).toString("hex"));const b=a+".done";try{fs.writeFileSync(a,"ok",{flag:"wx",mode:0o600});fs.renameSync(a,b);fs.unlinkSync(b);console.log(JSON.stringify({uid:process.getuid(),writeProbe:"passed",directoryOwner:fs.statSync(dir).uid,dbOwner:fs.statSync(path.join(dir,"db.json")).uid,dbMode:(fs.statSync(path.join(dir,"db.json")).mode&511).toString(8)}));}catch(e){console.log(JSON.stringify({uid:process.getuid(),writeProbe:"failed",code:e.code,message:e.message}));process.exitCode=1;}finally{for(const p of [a,b])try{fs.unlinkSync(p)}catch{}}`],{uid:1000,gid:1000,env:{...process.env,DIAG_DATA_DIR:config.dataDir},encoding:'utf8'}); -console.log('DATABASE-WRITE-CHECK',check.stdout.trim()); -if(check.stderr)console.log('DATABASE-CHECK-ERROR',check.stderr.slice(0,300)); -const db=JSON.parse(fs.readFileSync(config.dataDir+'/db.json','utf8')); -console.log('PAYMENT-SUMMARY',JSON.stringify({counts:db.payments.reduce((a,p)=>{a[p.status]=(a[p.status]||0)+1;return a;},{}),latest:db.payments.slice(-5).map(p=>({at:p.createdAt,status:p.status,hasOrder:!!p.mpId,hasVoucher:!!p.voucher}))})); -console.log('CLIENT-OBSERVATIONS',JSON.stringify((db.tempAccess||[]).map(t=>({device:hash(t.mac),expiresAt:t.expiresAt,probe:t.clientProbe||null})))); +for(const [label,command] of [ + ['FULL-FILTER',['/ip/firewall/filter/print']], + ['FULL-NAT',['/ip/firewall/nat/print']], + ['DNS-STATIC',['/ip/dns/static/print','=.proplist=name,regexp,address,disabled']], + ['GOOGLE-DNS',['/ping','=address=www.gstatic.com','=count=1']], + ['APPLE-DNS',['/ping','=address=www.apple.com','=count=1']], + ['GOOGLE-HTTP',['/tool/fetch','=url=http://www.gstatic.com/generate_204','=keep-result=no']], + ['APPLE-HTTP',['/tool/fetch','=url=http://www.apple.com/library/test/success.html','=keep-result=no']], +]){ + try{console.log(label,redact(JSON.stringify((await rosCommand(command,15000)).map(object))));} + catch(e){console.log(label+'-ERROR',redact(e.message));} +} +const hosts=(await rosCommand(['/ip/hotspot/host/print','=.proplist=address,mac-address,server'])).map(object).filter(h=>h.server===config.mikrotik.server); +const connections=(await rosCommand(['/ip/firewall/connection/print','=.proplist=src-address,dst-address,seen-reply,protocol,tcp-state,srcnat,dstnat'])).map(object); +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;} +for(const host of hosts){ + const rows=connections.filter(c=>(c['src-address']||'').split(':')[0]===host.address); + const outside=rows.filter(c=>!cloudflare(c['dst-address'])&&!/^(10\\.|192\\.168\\.|172\\.(1[6-9]|2\\d|3[01])\\.)/.test(c['dst-address']||'')); + 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})); +}