Bangkok’26
Dec 24 — Jan 01
{
const el = document.getElementById('data'), box = JSON.parse(el.textContent);
const b64 = s => Uint8Array.from(atob(s), ch => ch.charCodeAt(0));
const KEY = 'bkk26-key-' + box.s.slice(0, 8);
const idb = (mode, fn) => new Promise((ok, no) => {
const r = indexedDB.open('bkk26', 1);
r.onupgradeneeded = () => r.result.createObjectStore('k');
r.onerror = () => no(r.error);
r.onsuccess = () => { const tx = r.result.transaction('k', mode), q = fn(tx.objectStore('k')); tx.oncomplete = () => ok(q?.result); tx.onerror = () => no(tx.error); };
});
const getKey = () => idb('readonly', s => s.get(KEY)).catch(() => null);
const putKey = k => idb('readwrite', s => { s.clear(); s.put(k, KEY); }).catch(() => {});
const dropKey = () => idb('readwrite', s => s.delete(KEY)).catch(() => {});
const open = async key => {
try { const txt = new TextDecoder().decode(await crypto.subtle.decrypt({ name: 'AES-GCM', iv: b64(box.i) }, key, b64(box.c))); window.__boot(txt); return true; }
catch (e) { return false; }
};
// old builds kept the raw key in localStorage: move it into IndexedDB as non-extractable
const legacy = localStorage.getItem(KEY);
Object.keys(localStorage).filter(k => k.startsWith('bkk26-key-')).forEach(k => localStorage.removeItem(k));
if (legacy) { try { const k = await crypto.subtle.importKey('raw', b64(legacy), 'AES-GCM', false, ['decrypt']); if (await open(k)) { await putKey(k); return; } } catch (e) {} }
const saved = await getKey();
if (saved) { if (await open(saved)) return; await dropKey(); }
const lock = document.createElement('form');
lock.className = 'lock';
lock.innerHTML = 'Bangkok ’26
输入密码打开(每台手机输入一次)
';
document.body.appendChild(lock);
const inp = lock.querySelector('input'), err = lock.querySelector('.lk-e'); inp.focus();
lock.addEventListener('submit', async e => {
e.preventDefault(); err.textContent = '解锁中…';
const base = await crypto.subtle.importKey('raw', new TextEncoder().encode(inp.value.trim()), 'PBKDF2', false, ['deriveKey']);
const key = await crypto.subtle.deriveKey({ name: 'PBKDF2', salt: b64(box.s), iterations: box.n, hash: 'SHA-256' }, base, { name: 'AES-GCM', length: 256 }, false, ['decrypt']);
if (await open(key)) { lock.remove(); await putKey(key); return; }
// Cached page may be older than the live one (new password/salt): check the fresh copy and reload into it.
try { const t = await (await fetch(location.pathname, { cache: 'no-store' })).text(); const m = t.match(/"s":"([^"]+)"/); if (m && m[1] !== box.s) { err.textContent = '正在更新到新版本…'; location.reload(); return; } } catch (x) {}
err.textContent = '密码不对,再试一次'; inp.select();
});
})();