Activate paid Wi-Fi automatically and redesign portal with payment deadline and 45-minute wait
build-image / build-push (push) Successful in 2m59s

This commit is contained in:
jfeng committed 2026-10-22 17:55:54 +00:00
1 parent b69b3afa5d
commit ce6e5980ec
10 files changed
+189 -37

No files matched your search

+27 -4
View File
@@ -5,13 +5,14 @@ import vm from 'node:vm';
const html=fs.readFileSync(new URL('../public/index.html',import.meta.url),'utf8');
const code=html.match(/<script>([\s\S]*?)<\/script>/)[1];
async function portal({search,storage={},access={ok:true,authenticated:true,seconds:120},externalAvailable=true,clipboardAvailable=true,legacyCopy=false}) {
async function portal({search,storage={},access={ok:true,authenticated:true,seconds:120},externalAvailable=true,clipboardAvailable=true,legacyCopy=false,consent=true,siteConfig={},paymentStatus={status:'pending'}}) {
const ids=[...html.matchAll(/\bid="([^"]+)"/g)].map(m=>m[1]);
const listeners=new Map(),clipboard=[];
const elements=new Map(ids.map(id=>[id,{textContent:'',hidden:false,disabled:false,value:'',innerHTML:'',classList:{toggle(){},add(){},remove(){}},addEventListener:(event,fn)=>listeners.set(id+':'+event,fn),appendChild(){}}]));
const requests=[],forms=[],intervals=[],navigations=[];
const sandbox={URL,URLSearchParams,Date,AbortController,console,location:{search},navigator:{},sessionStorage:{getItem:k=>storage[k]??null,setItem:(k,v)=>storage[k]=v,removeItem:k=>delete storage[k]},document:{getElementById:id=>{assert.ok(elements.has(id),'Missing element '+id);return elements.get(id);},body:{append(){}},createElement:tag=>({append(){},submit(){forms.push(tag);}})},setTimeout(){},clearTimeout(){},setInterval:fn=>{intervals.push(fn);return intervals.length;},clearInterval(){},window:{addEventListener(){},scrollTo(){}}};
sandbox.location.href='https://hotspot.example.test/'+(search || '');
elements.get('accessConsent').checked=consent;
sandbox.window.location={replace:url=>navigations.push(url)};
sandbox.navigator.clipboard={writeText:async text=>{if(!clipboardAvailable)throw new Error('Clipboard denied');clipboard.push(text);}};
sandbox.document.body.appendChild=()=>{};
@@ -19,11 +20,11 @@ async function portal({search,storage={},access={ok:true,authenticated:true,seco
sandbox.document.execCommand=()=>legacyCopy;
sandbox.fetch=async(path,options)=>{
requests.push({path,options});
if(path==='/api/config')return {ok:true,json:async()=>({hotspotIp:'10.10.10.1',ssid:'Test'})};
if(path==='/api/config')return {ok:true,json:async()=>({hotspotIp:'10.10.10.1',ssid:'Test',...siteConfig})};
if(path==='/api/plans')return {ok:true,json:async()=>[]};
if(path==='/api/access/start')return {ok:access.ok,status:access.ok?200:429,json:async()=>access};
if(path==='/api/checkout')return {ok:true,json:async()=>({paymentId:'Ptest',orderId:'ORDtest',price:3,planName:'1 Hora',copiaCola:'test-pix',qrCodeBase64:null})};
if(path==='/api/status/Ptest')return {ok:true,json:async()=>({status:'pending'})};
if(path==='/api/status/Ptest')return {ok:true,json:async()=>paymentStatus};
if(path==='/api/access/report')return {ok:true,json:async()=>({ok:true})};
if(path.startsWith('https://www.gstatic.com/') || path.startsWith('https://www.apple.com/')) {
// Fetch rejects no-cors requests unless redirect mode is follow, before any network traffic.
@@ -58,7 +59,7 @@ test('direct portal URL without MAC cannot silently claim temporary access',asyn
});
test('portal reports cooldown failures and offers retry',async()=>{
const p=await portal({search:'?mac=AA:BB:CC:DD:EE:11&free=1',access:{ok:false,error:'Janela encerrada.',retryAfterSeconds:180}});
assert.match(p.elements.get('freeAccessMessage').textContent,/180 segundos/);
assert.match(p.elements.get('freeAccessMessage').textContent,/3 minutos/);
assert.equal(p.elements.get('retryFreeAccess').hidden,false);
});
test('browsing plans does not consume the payment internet window',async()=>{
@@ -164,3 +165,25 @@ test('an expired or denied window cannot navigate, and a new window can revalida
const next=await portal({search,storage,access:{ok:true,authenticated:true,seconds:120,expiresAt:Date.now()+120000}});
assert.equal(next.navigations.length,1);
});
test('rules use the configured payment duration and require acknowledgement before access',async()=>{
const p=await portal({search:'?mac=AA:BB:CC:DD:EE:11',consent:false,siteConfig:{freeSeconds:180,freeCooldownSeconds:2700}});
assert.match(p.elements.get('accessRules').textContent,/3 minutos/);
assert.match(p.elements.get('accessRules').textContent,/45 minutos após o fim/);
await p.checkout();await p.click('copyBtn');
assert.equal(p.requests.filter(r=>r.path==='/api/access/start').length,0);
assert.match(p.elements.get('freeAccessMessage').textContent,/confirme acima/);
p.elements.get('accessConsent').checked=true;
await p.click('retryFreeAccess');
assert.equal(p.requests.filter(r=>r.path==='/api/access/start').length,1);
});
test('confirmed payment distinguishes activation pending from an active paid connection',async()=>{
for(const state of ['pending','active']){
const p=await portal({search:'?mac=AA:BB:CC:DD:EE:11',storage:{paymentId:'Ptest'},paymentStatus:{status:'paid',voucher:{user:'paid-user',pass:'paid-pass'},connection:{state}}});
assert.equal(p.elements.get('vUser').textContent,'paid-user');
assert.match(p.elements.get('paidConnectionMessage').textContent,state==='active'?/plano está ativo/:/Ativando/);
assert.equal(p.elements.get('freeCountdown').textContent,state==='active'?'Conectado':'Ativando');
assert.equal(p.forms.length,0);
}
});