Push Tracker
ria-ran--sim-drop/portal/js/script.js
A ashkan@beigi.net c6423b1bad Harden: concurrency safety (db lock timeout, cache lock, retry, portal)
- get_connection sets PRAGMA busy_timeout so concurrent writers under a
  threaded server wait instead of hitting "database is locked".
- Provision retries credential generation on IMSI/ICCID collision (up to 5x)
  instead of surfacing a duplicate as a 500.
- Guard the module-level profile cache with a threading.Lock.
- Captive portal disables the activate button while a request is in flight to
  avoid double provisioning.

Tests: +2 (collision retry + give-up); 84 pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 06:16:25 +00:00

71 lines
1.9 KiB
JavaScript

'use strict';
const STATES = ['idle', 'loading', 'success', 'error'];
function showState(name) {
STATES.forEach(function(s) {
var el = document.getElementById('state-' + s);
if (el) el.hidden = (s !== name);
});
}
function showError(msg) {
var el = document.getElementById('error-message');
if (el) el.textContent = msg;
showState('error');
}
var inFlight = false;
async function activate() {
if (inFlight) return; // ignore double-clicks while a request is pending
inFlight = true;
var btn = document.getElementById('btn-activate');
if (btn) btn.disabled = true;
showState('loading');
try {
var res = await fetch('/api/provision', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
// MAC address left empty — server resolves it via ARP from client IP
body: JSON.stringify({ mac_address: '' }),
});
var data;
try { data = await res.json(); } catch (_) { data = {}; }
if (res.status === 429) {
showError(data.error || 'You already have an eSIM for this network. Only one per device per hour.');
return;
}
if (!res.ok) {
showError(data.error || 'Server error (' + res.status + '). Please try again.');
return;
}
// Wire up download link
var link = document.getElementById('download-link');
if (link && data.profile_url) {
link.href = data.profile_url;
}
showState('success');
} catch (err) {
showError('Could not reach the server. Make sure you are connected to the WiFi network.');
} finally {
inFlight = false;
if (btn) btn.disabled = false;
}
}
document.addEventListener('DOMContentLoaded', function() {
var btnActivate = document.getElementById('btn-activate');
var btnRetry = document.getElementById('btn-retry');
if (btnActivate) btnActivate.addEventListener('click', activate);
if (btnRetry) btnRetry.addEventListener('click', function() { showState('idle'); });
});