diagnostics: distinguish database write failures from guest internet failures

This commit is contained in:
jfeng committed 2026-10-22 16:41:24 +00:00
1 parent 64add2bc68
commit 67bbafdc41
2 files changed
+10 -21

No files matched your search

+3 -3
View File
@@ -8,14 +8,14 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Read-only production diagnosis - name: Production diagnosis and database write check
shell: bash shell: bash
run: | run: |
set -euo pipefail set -euo pipefail
docker inspect pix-hotspot --format 'IMAGE={{.Config.Image}} PORTS={{json .NetworkSettings.Ports}}' docker inspect pix-hotspot --format 'IMAGE={{.Config.Image}} PORTS={{json .NetworkSettings.Ports}}'
docker exec -i pix-hotspot node --input-type=module < backend/diagnose-hotspot.mjs docker exec -i pix-hotspot node --input-type=module < backend/diagnose-hotspot.mjs
- name: Recent access errors - name: Recent sanitized errors
if: always() if: always()
shell: bash shell: bash
run: | run: |
docker logs --since 2h --tail 2000 pix-hotspot 2>&1 | node -e 'let s="";process.stdin.on("data",x=>s+=x);process.stdin.on("end",()=>console.log(s.split("\n").filter(l=>/\[ACESSO/.test(l)).slice(-40).map(l=>l.replace(/(?:[0-9a-f]{2}:){5}[0-9a-f]{2}/gi,"[MAC]").replace(/(?:\d{1,3}\.){3}\d{1,3}/g,"[IP]")).join("\n")));' docker logs --since 2h --tail 3000 pix-hotspot 2>&1 | node -e 'let s="";process.stdin.on("data",x=>s+=x);process.stdin.on("end",()=>console.log(s.split("\n").filter(l=>/\[(ACESSO|CHECKOUT|LIMPEZA|HTTP)/.test(l)).slice(-60).map(l=>l.replace(/(?:[0-9a-f]{2}:){5}[0-9a-f]{2}/gi,"[MAC]").replace(/(?:\d{1,3}\.){3}\d{1,3}/g,"[IP]").replace(/(?:APP_USR|TEST)-[\w-]+/g,"[TOKEN]").replace(/[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}/g,"[EMAIL]").slice(0,700)).join("\n")));'
+7 -18
View File
@@ -25,21 +25,10 @@ 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));}
const hosts=(await rosCommand(['/ip/hotspot/host/print','=.proplist=.id,address,to-address,mac-address,server,authorized,bypassed,uptime,idle-time,bytes-in,bytes-out,packets-in,packets-out'])).map(object).filter(h=>h.server===config.mikrotik.server); const {spawnSync}=await import('node:child_process');
const macs=[...new Set(hosts.map(h=>h['mac-address']))]; 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'});
for(const mac of macs){ console.log('DATABASE-WRITE-CHECK',check.stdout.trim());
const rows=hosts.filter(h=>h['mac-address']===mac); if(check.stderr)console.log('DATABASE-CHECK-ERROR',check.stderr.slice(0,300));
console.log('HOST-ALIASES',redact(JSON.stringify(rows.map(r=>({...r,addressHash:hash(r.address),toAddressHash:hash(r['to-address'])}))))); const db=JSON.parse(fs.readFileSync(config.dataDir+'/db.json','utf8'));
for(const [label,path] of [['LEASES','/ip/dhcp-server/lease/print'],['ARP','/ip/arp/print']]){ 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}))}));
try{console.log(label,redact(JSON.stringify((await rosCommand([path,'?mac-address='+mac,'=.proplist=address,active-address,status,server,last-seen,interface,complete'])).map(object))));} console.log('CLIENT-OBSERVATIONS',JSON.stringify((db.tempAccess||[]).map(t=>({device:hash(t.mac),expiresAt:t.expiresAt,probe:t.clientProbe||null}))));
catch(e){console.log(label+'-ERROR',redact(e.message));}
}
}
const connections=(await rosCommand(['/ip/firewall/connection/print','=.proplist=src-address,dst-address,seen-reply,protocol,tcp-state'])).map(object);
for(const h of hosts){
const c=connections.filter(c=>(c['src-address']||'').split(':')[0]===h.address);
const external=c.filter(c=>!/^(10\\.|192\\.168\\.|172\\.(1[6-9]|2\\d|3[01])\\.)/.test(c['dst-address']||''));
console.log('ALIAS-CONNECTIONS',JSON.stringify({addressHash:hash(h.address),device:hash(h['mac-address']),count:c.length,external:external.length,withReplies:external.filter(c=>c['seen-reply']==='true').length,dnsReplies:external.filter(c=>(c['dst-address']||'').endsWith(':53')&&c['seen-reply']==='true').length}));
}
console.log('DHCP-SERVERS',redact(JSON.stringify((await rosCommand(['/ip/dhcp-server/print','=.proplist=name,interface,disabled,address-pool'])).map(object))));
console.log('HOTSPOT-SETTINGS',JSON.stringify((await rosCommand(['/ip/hotspot/print','=.proplist=name,addresses-per-mac,idle-timeout,keepalive-timeout,interface'])).map(object)));