Files
Morning Star 7d7534ded7 feat(pkgmgr): install xfsprogs from the Server packages card; say when a disk came out ext4 (#9)
The Server packages card offers xfsprogs as an optional need with its own Install; the format result says when a disk came out ext4.
2026-11-22 15:15:48 +07:00

1592 lines
143 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Bootstrap colour mode follows the OS (light / dark), also when it changes while the page is open.
const darkQ=matchMedia('(prefers-color-scheme: dark)');
const setTheme=()=>document.documentElement.dataset.bsTheme=darkQ.matches?'dark':'light';
setTheme();darkQ.addEventListener('change',setTheme);
async function j(url,opt){const r=await fetch(url,opt);
if(r.status===401){location.replace('/login');throw new Error('login required');}
if(!r.ok)throw new Error(await r.text());return r.json()}
const esc=s=>(s==null?'':String(s)).replace(/[&<>"'`]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;','`':'&#96;'}[c]));
// Icons = symbols of /icons.svg, drawn in the text colour (class n colours ok / err / wait / warn; '' = none).
const ICON_LABEL={ok:'ok',err:'error',wait:'working',warn:'warning'};
const ic=(n,c)=>`<svg class="ic ${c??n}" ${ICON_LABEL[n]?`role="img" aria-label="${ICON_LABEL[n]}"`:'aria-hidden="true"'}><use href="/icons.svg#${n}"/></svg>`;
// Status text keeps its ✓ / ✗ / ⏳ marks (the server's job statuses start with them, and code checks them):
// say() shows it in el, escaped, with the marks drawn as icons.
const MARK={'✓':'ok','✗':'err','⏳':'wait'};
function say(el,t){if(el)el.innerHTML=esc(t).replace(/[✓✗⏳]/g,c=>ic(MARK[c]));}
// Badges used in several tables: machine on/off, image OS.
const led=on=>`<span class="badge ${on?'text-bg-success':'text-bg-secondary'}">${on?'ON':'OFF'}</span>`;
const osPill=os=>`<span class="badge rounded-pill ${os==='linux'?'text-bg-success':'text-bg-info'}">${esc(os)}</span>`;
// No data in inline JS (onclick="f('${esc(x)}')" is unsafe: the browser decodes &#39; back to ' before running
// it; the CSP blocks inline handlers anyway). Elements name an ACTIONS entry in data-click / data-change /
// data-input and carry JSON args in data-args; one listener per event type calls ACTIONS[name](...args, el, event).
const act=(name,...args)=>`data-click="${name}" data-args="${esc(JSON.stringify(args))}"`;
for(const ev of ['click','change','input'])document.addEventListener(ev,e=>{
const el=e.target.closest&&e.target.closest('[data-'+ev+']');if(!el)return;
const f=ACTIONS[el.dataset[ev]];if(f)f(...JSON.parse(el.dataset.args||'[]'),el,e);
});
// ---- tabs: load HTML fragments on demand (/ui/<page>) + only load data for the tab being viewed ----
const loaders={machines:loadStatus,remote:loadRemote,server:loadServer,devices:loadDevices,groups:loadGroups,images:loadImages,games:loadGames,diskless:loadDiskless,drivers:loadDrivers,network:loadDhcp,windows:loadCafe,system:()=>{loadCafe();loadUsers();loadTls();},benchmark:loadBench,logs:loadLogs};
const fragCache={};
let curPage=null;
async function showPage(p){
if(iscsiES&&p!=='server'){iscsiES();iscsiES=null;}
if(p!=='remote'){if(rmWs){rmWs.onclose=null;rmWs.close();rmWs=null;rmCtx=null;}rmCloseDec();closeWall();}
document.querySelectorAll('nav a').forEach(x=>x.classList.toggle('active',x.dataset.page===p));
const grp=document.querySelector(`nav a[data-page="${CSS.escape(p)}"]`)?.closest('details');if(grp)grp.open=true; // its group shows
if(fragCache[p]===undefined){
try{const r=await fetch('/ui/'+p);
if(r.status===401){location.replace('/login');return;} // session expired: never cache the "login required" text
if(!r.ok)throw new Error(r.status);
fragCache[p]=await r.text();}
catch(e){document.getElementById('main').innerHTML='<p class="form-text">failed to load page '+p+'</p>';return;}
}
document.getElementById('main').innerHTML=fragCache[p];
curPage=p;
if(loaders[p])loaders[p]();
}
// Page from the URL (/machines, /images, ...); "/" or unknown → machines.
const pageFromPath=()=>{const p=location.pathname.slice(1);return loaders[p]?p:'machines';};
document.querySelectorAll('nav a').forEach(a=>a.onclick=e=>{
if(e.ctrlKey||e.metaKey||e.shiftKey||e.button!==0)return; // new tab/window: let the browser handle it
e.preventDefault();
bootstrap.Offcanvas.getInstance('#sidebar')?.hide(); // small screens: close the slid-in menu
if(a.dataset.page!==curPage)history.pushState(null,'','/'+a.dataset.page);
showPage(a.dataset.page);
});
window.onpopstate=()=>showPage(pageFromPath());
// Menu groups: folded by default, each one's open/closed remembered in this browser (localStorage may be off: then
// everything just starts folded, the current page's group open).
document.querySelectorAll('nav details[data-group]').forEach(d=>{
const k='navgrp:'+d.dataset.group;
try{if(localStorage.getItem(k)==='1')d.open=true;}catch(e){}
d.addEventListener('toggle',()=>{try{localStorage.setItem(k,d.open?'1':'0');}catch(e){}});
});
// ---- machines (filter + client-side paging) ----
let allMachines=[], mPage=1;
// Rows per page (Machines): 15 / 50 / 100 / all (0 → Infinity), remembered in this browser.
let M_PAGE=15;
try{const s=localStorage.getItem('m_psize');if(s!==null)M_PAGE=+s||Infinity;}catch(e){}
function mPageSize(){const n=+document.getElementById('m_psize').value;M_PAGE=n||Infinity;
try{localStorage.setItem('m_psize',String(n));}catch(e){}mPage=1;renderMachines();}
// Windows images for the base mode picker (Machines page): read once per page load.
let winImages=null;
async function loadStatus(){
if(!winImages)winImages=(await j('/api/images').catch(()=>[])).filter(i=>i.os==='windows');
allMachines=await j('/api/status');
const online=allMachines.filter(m=>m.online).length;
document.getElementById('onlinecount').textContent=online+'/'+allMachines.length+' online';
if(curPage==='machines')renderMachines();
}
// Per-column filters (the second header row of the machine table). Text = case-insensitive "contains".
const MF=['mf_host','mf_grp','mf_ip','mf_mac','mf_state','mf_lic','mf_reg'];
const mf=id=>((document.getElementById(id)||{}).value||'').trim().toLowerCase();
const licErr=m=>!!m.license_result&&!/activated successfully/i.test(m.license_result);
function filteredMachines(){
const has=(v,q)=>!q||(v||'').toLowerCase().includes(q);
const [host,grp,ip,mac,state,lic,reg]=MF.map(mf);
return allMachines.filter(m=>has(m.hostname,host)&&has(m.grp,grp)&&has(m.ip,ip)&&has(m.mac,mac)
&&(!state||(state==='on')===!!m.online)
&&(!lic||(lic==='none'?!m.license_tail:lic==='error'?licErr(m):m.license_state===lic))
&&(!reg||(reg==='registered')===!!m.registered));
}
function clearMFilters(){MF.forEach(id=>{const e=document.getElementById(id);if(e)e.value='';});mPage=1;renderMachines();}
function renderMachines(){
const rows=filteredMachines(), pages=Math.max(1,Math.ceil(rows.length/M_PAGE));
if(mPage>pages)mPage=pages; if(mPage<1)mPage=1;
const page=rows.slice((mPage-1)*M_PAGE,mPage*M_PAGE);
document.querySelector('#machines tbody').innerHTML=page.map(m=>
`<tr>
<td>${m.registered&&m.id!=null?`<input type="checkbox" class="form-check-input" data-change="mSelOne" data-args="${esc(JSON.stringify([m.id]))}"${mSel.has(m.id)?' checked':''} aria-label="select">`:''}</td>
<td>${m.hostname?esc(m.hostname):'<span class="mono">—</span>'} ${m.registered?'':'<span class="badge rounded-pill text-bg-warning">new</span>'}</td>
<td class="mono">${m.grp?esc(m.grp):'—'}</td>
<td class="mono">${esc(m.ip)}</td><td class="mono">${esc(m.mac)}</td>
<td>${led(m.online)}${m.not_reset?' <span class="badge text-bg-danger" title="Windows started from the SSD without a PXE boot at '+new Date(m.not_reset*1000).toLocaleString()+' — this session was NOT reset (cable out, server down, boot order changed?). Cleared by the next PXE boot.">not reset</span>':''}${m.ssd_low?' <span class="badge text-bg-danger" title="SSD almost full during the session: '+gbs(m.ssd_low[0])+' free at '+new Date(m.ssd_low[1]*1000).toLocaleString()+' (the guest wrote a lot; Windows → SSD room). Cleared by the next PXE boot.">SSD full</span>':''}${pwBadge(m)}${mxBadge(m.metrics)}${upBadge(m)}${baseBadge(m)}</td>
<td class="mono" title="${esc(m.license_result||'')}">${licCell(m)}</td>
<td><div class="d-flex gap-1">${m.registered
? `<button class="btn btn-sm btn-outline-secondary" data-perm="wake" ${act('wake',m.mac)}>Wake</button>
<button class="btn btn-sm btn-outline-secondary" ${act('manage',m.id)} data-owner>Manage</button>
${basePick(m)}
${m.remote?`<button class="btn btn-sm btn-outline-primary" data-perm="remote" ${act('remoteTo',m.hostname)} title="See and control this machine">Remote</button>`:''}
<button class="btn btn-sm btn-outline-secondary" ${act('hwToggle',m.id)} title="What it is made of (sent by its Broom agent)">Hardware</button>`
: `<button class="btn btn-sm btn-outline-primary" ${act('regMachine',m.mac,m.ip,m.hostname)} data-owner>Register</button>`}</div></td>
</tr>${hwOpen.has(m.id)?`<tr><td></td><td colspan="7">${hwHtml(hwData[m.id],m.id)}</td></tr>`:''}`).join('') || '<tr><td colspan=8 class="mono">no matching machines</td></tr>';
document.getElementById('m_count').textContent=rows.length+' machines';
mSelCount();
document.getElementById('m_page').textContent='Page '+mPage+'/'+pages;
const ps=document.getElementById('m_psize');if(ps)ps.value=M_PAGE===Infinity?'0':String(M_PAGE);
}
// Hardware list (inventory.rs): opened under its row, fetched when opened.
const hwOpen=new Set(),hwData={};
async function hwToggle(id){
if(hwOpen.has(id)){hwOpen.delete(id);renderMachines();return;}
try{hwData[id]=await j('/api/machines/inventory?id='+encodeURIComponent(id));hwOpen.add(id);renderMachines();}
catch(e){toast(e.message,true)}}
function hwHtml(r,id){
const v=r&&r.inventory,d=(r&&r.display)||{};
// Resolution + refresh (display.rs): its own setting, from the modes its monitor offers; "" = as its group / Windows.
const cur=d.own||'';
const modes=[...new Set([cur,...((r&&r.modes)||[])].filter(Boolean))];
const disp=`<div class="d-flex flex-wrap gap-2 align-items-center small mb-2"><span class="text-body-secondary">Display</span>
<select class="form-select form-select-sm w-auto" data-owner data-change="hwDisplay" data-args="${esc(JSON.stringify([id]))}">
<option value=""${cur?'':' selected'}>${esc(d.group?'as its group ('+d.group+')':'Windows\' choice')}</option>
${modes.map(x=>`<option value="${esc(x)}"${x===cur?' selected':''}>${esc(x.replace('x','×').replace('@',' @ ')+' Hz')}</option>`).join('')}</select>
<span class="text-body-secondary">${esc(d.effective?'applied at each start: '+d.effective:'')}</span></div>`;
if(!v)return disp+'<span class="text-body-secondary small">No hardware list yet: its Broom agent sends one once it is connected (Remote).</span>';
const GB=b=>(b/2**30).toFixed(0)+' GB',row=(k,input)=>input?`<tr><th class="text-nowrap pe-3 fw-normal text-body-secondary">${esc(k)}</th><td>${input}</td></tr>`:'';
const list=(a,f)=>(a||[]).map(f).filter(Boolean).join('<br>');
return `<table class="table table-sm mb-1 small"><tbody>
${row('Windows',esc([v.os,v.build].filter(Boolean).join(' ')))}
${row('Computer',esc([v.maker,v.model].filter(Boolean).join(' ')))}
${row('Board',esc(v.board||''))}${row('BIOS',esc(v.bios||''))}
${row('CPU',list(v.cpus,c=>esc(`${c.name} — ${c.cores} cores / ${c.threads} threads`)))}
${row('RAM',esc(v.ram_total?GB(v.ram_total):'')+(v.ram&&v.ram.length?'<br>'+list(v.ram,r=>esc(`${r.slot}: ${GB(r.size)} ${r.speed} MT/s ${r.maker} ${r.part}`)):''))}
${row('Graphics',list(v.gpus,g=>esc(`${g.name} — driver ${g.driver}`)))}
${row('Drives',list(v.drives,d=>esc(`${d.model} — ${GB(d.size)} (${d.bus})`)))}
${row('Network',list(v.nics,n=>esc(`${n.name} — ${n.speed?(n.speed/1e6)+' Mb/s':'no link'}${n.up?'':' (down)'} ${n.mac||''}`)))}
${row('Monitors',list(v.monitors,m=>esc(`${m.maker} ${m.name}`)))}
${row('Displays',list(v.displays,d=>esc(`${d.device}${d.primary?' (main)':''}: ${d.w}×${d.h} @ ${d.hz} Hz — ${(d.modes||[]).length} modes offered`)))}
</tbody></table><div class="small text-body-secondary">reported ${esc(new Date(v.at*1000).toLocaleString())}</div>`.replace('<table',disp+'<table');}
async function hwDisplay(id,el){
try{const r=await j('/api/machines/display',mk({ids:[id],mode:el.value}));toast(el.value?'Display '+el.value+(r.sent?' sent':' set: applied at its next start'):'Display setting removed');
hwData[id]=await j('/api/machines/inventory?id='+encodeURIComponent(id));renderMachines();}
catch(e){toast(e.message,true)}}
// The same for the selected machines, or for a group (new machines of it too).
async function mDisplay(to){
const out=document.getElementById('m_pres'),mode=document.getElementById('m_disp').value.trim();
const body=to==='grp'?{group:document.getElementById('m_dgrp').value.trim(),mode}:{ids:[...mSel],mode};
if(to==='sel'&&!body.ids.length){say(out,' ✗ select machines first (the boxes on the left)');return;}
try{const r=await j('/api/machines/display',mk(body));say(out,' ✓ display '+(mode||'setting removed')+' — sent to '+r.sent+' running machine(s), the others at their next start');}
catch(e){say(out,' ✗ '+e.message);}}
// Power (power.rs): the selected machines (kept across refreshes and pages) → turn on / restart / shut down / cancel.
const mSel=new Set();
function mSelCount(){for(const id of [...mSel])if(!allMachines.some(m=>m.id===id))mSel.delete(id);
const e=document.getElementById('m_sel');if(e)e.textContent=mSel.size+' selected';}
function mSelOne(id,el){el.checked?mSel.add(id):mSel.delete(id);mSelCount();}
function mSelAll(el){for(const m of filteredMachines())if(m.registered&&m.id!=null){el.checked?mSel.add(m.id):mSel.delete(m.id);}renderMachines();}
async function mPower(action){
const out=document.getElementById('m_pres'),ids=[...mSel];
if(!ids.length){say(out,' ✗ select machines first (the boxes on the left)');return;}
const delay=+document.getElementById('m_delay').value,msg=document.getElementById('m_pmsg').value;
const names=allMachines.filter(m=>mSel.has(m.id)).map(m=>m.hostname||m.mac);
const verb={on:'Turn on',restart:'Restart',shutdown:'Shut down',cancel:'Cancel the restart / shutdown of'}[action];
const when=(action==='restart'||action==='shutdown')?(delay?' in '+delay+' s':' now'):'';
if(action!=='on'&&!await sure(verb+' '+ids.length+' machine(s)'+when+'? '+names.slice(0,12).join(', ')+(names.length>12?' …':'')))return;
try{const r=await j('/api/power',mk({action,ids,delay,msg}));const bad=r.results.filter(x=>!x.ok);
say(out,(r.done?' ✓ ':' ✗ ')+r.done+' of '+r.results.length+(bad.length?' — not done: '+bad.map(x=>x.name+' ('+x.msg+')').join('; '):''));}
catch(e){say(out,' ✗ '+e.message);}
loadStatus();}
// The last power action while it plays out, and "booting" (chose from its boot menu, not up yet).
function pwBadge(m){let s='';
if(m.booting)s+=' <span class="badge text-bg-info" title="network-booted in the last 5 minutes, not up yet">booting</span>';
const p=m.power;if(p){const left=Math.ceil(p.at+p.delay-Date.now()/1000);
const w={on:'turn on sent',restart:'restart',shutdown:'shut down',cancel:'cancel sent'}[p.what]||p.what;
const t=(p.what==='restart'||p.what==='shutdown')?(left>0?' in '+left+' s':' sent'):'';
s+=` <span class="badge text-bg-warning" title="sent ${esc(new Date(p.at*1000).toLocaleString())}">${esc(w+t)}</span>`;}
return s;}
// Base mode (per machine + one Windows image): that image boots its base itself, every change kept over its restarts;
// off → the next boot saves it as the base and resets to it from then on.
const BASE_TIP='Base mode: when this machine boots the chosen Windows image, everything done is KEPT, over as many restarts as needed (Windows Update, apps that restart). Turn it off when done: the next boot saves it as the base, and every boot after resets to that base.';
const baseName=m=>{const i=(winImages||[]).find(i=>i.id===m.base_image);return i?i.name:'#'+m.base_image;};
const baseBadge=m=>m.base_image?` <span class="badge text-bg-warning" title="${esc(BASE_TIP)}">BASE ${esc(baseName(m))}</span>`:'';
function basePick(m){
if(!winImages||!winImages.length)return'';
const opts=['<option value="">Base: off</option>'].concat(winImages.map(i=>`<option value="${i.id}"${i.id===m.base_image?' selected':''}>Base: ${esc(i.name)}</option>`));
return `<select class="form-select form-select-sm w-auto" data-owner title="${esc(BASE_TIP)}" data-change="setBase" data-args="${esc(JSON.stringify([m.id]))}">${opts.join('')}</select>`;}
async function setBase(id,el){
const image=el.value?+el.value:null;
if(image&&!await sure('Base mode ON: every change on this machine (in that image) is kept until you turn it off. Go on?')){el.value='';return;}
try{await j('/api/machines/base',mk({id,image}));toast(image?'Base mode on: boot the machine into that image':'Base mode off: its next boot saves the base');}
catch(e){toast(e.message,true);}loadStatus();}
function mPrev(){if(mPage>1){mPage--;renderMachines();}}
function mNext(){const pages=Math.max(1,Math.ceil(filteredMachines().length/M_PAGE));if(mPage<pages){mPage++;renderMachines();}}
// Windows computer name (NetBIOS). Keep in sync with hostname_ok() in machines.rs.
const HOST_RULE='Hostname: 1-15 characters, only letters/digits/"-", must not start or end with "-", not all digits';
const hostOk=h=>/^[A-Za-z0-9-]{1,15}$/.test(h)&&!/^-|-$/.test(h)&&!/^[0-9]+$/.test(h);
async function regMachine(mac,ip,host){
let hostname=host||'PC';
do{hostname=await ask('Hostname for '+mac,hostOk(hostname)?'':HOST_RULE,hostname);if(hostname===null)return;hostname=hostname.trim();}
while(!hostOk(hostname));
try{await j('/api/machines',mk({mac,ip:ip||null,hostname}));}catch(e){toast(e.message,true);return;}
toast('Registered. Reboot the machine to get its IP/name (full DHCP mode).');refreshMachines();
}
// Whichever machine page is open reloads after a change (Machines = quick dashboard, Devices = management).
function refreshMachines(){return curPage==='devices'?loadDevices():loadStatus();}
// Machines → Devices page with this machine's detail open.
function manage(id){dvPending=id;history.pushState(null,'','/devices');showPage('devices');}
async function wake(mac){await j('/api/wake',mk({mac}));toast('WOL sent to '+mac)}
// Windows license key: handed out ONCE to the machine's IP when its base is rebuilt (next boot). The full key +
// activation result are shown to the logged-in operator. Keep keyOk in sync with key_ok() in license.rs.
const KEY_RULE='License key: 25 letters/digits as XXXXX-XXXXX-XXXXX-XXXXX-XXXXX (empty = remove)';
const keyOk=k=>/^[A-Z0-9]{5}(-[A-Z0-9]{5}){4}$/.test(k);
// Compact by default (lists): state + masked key tail + badge, full result on hover (title). full=true (detail
// page only): whole key + the full activation result inline.
function licCell(m,full){if(!m.license_tail)return '—';
const ok=/activated successfully/i.test(m.license_result||'');
const badge=ok?' '+ic('ok'):licErr(m)?' <span class="badge rounded-pill text-bg-danger">error</span>':'';
if(!full)return `${esc(m.license_state||'')} …${esc(m.license_tail)}${badge}`;
const res=m.license_result?`<div class="mono mt-1" style="white-space:pre-wrap">${esc(m.license_result)}</div>`:'';
return `${esc(m.license_state||'')} ${esc(m.license_key||('…'+m.license_tail))}${badge}${res}`;}
async function setKey(id,who){
let k='';
do{k=await ask('Windows license key for '+who,k===''||keyOk(k)?'':KEY_RULE,k);if(k===null)return;k=k.trim().toUpperCase();}
while(k&&!keyOk(k));
try{await j('/api/machines/license',mk({id,key:k}));
toast(k?'Key saved. It is installed when '+who+' boots next (its base is rebuilt once).':'Key removed.');refreshMachines();}
catch(e){toast(e.message,true)}}
async function rearmKey(id,who){if(!await sure('Hand the key to '+who+' once more? Its base is rebuilt on the next boot.'))return;
try{await j('/api/machines/license/rearm',mk({id}));refreshMachines();}catch(e){toast(e.message,true)}}
// ---- devices (devices.rs): full machine management; the Machines page stays the quick dashboard ----
let dvAll=[], dvImages=[], dvSel=new Set(), dvPending=null, dvOpenId=null;
const dvKey=m=>m.mac; // selection key (new machines have no id yet)
const imgName=id=>(dvImages.find(i=>i.id===id)||{}).name||'';
async function loadDevices(){
[dvAll,dvImages]=await Promise.all([j('/api/status'),j('/api/images')]);
const bi=document.getElementById('dv_bulk_img');
if(bi)bi.innerHTML='<option value="">(global default image)</option>'+dvImages.map(i=>`<option value="${i.id}">${esc(i.name)}</option>`).join('');
const keys=new Set(dvAll.map(dvKey)); [...dvSel].forEach(k=>{if(!keys.has(k))dvSel.delete(k);});
dvRender();
if(dvPending){const id=dvPending;dvPending=null;dvOpen(id);} else if(dvOpenId)dvOpen(dvOpenId);
}
function dvFiltered(){
const g=id=>((document.getElementById(id)||{}).value||'').trim().toLowerCase(), has=(val,q)=>!q||(val||'').toLowerCase().includes(q);
const [host,grp,ip,mac,state,img,lic,notes]=['dvf_host','dvf_grp','dvf_ip','dvf_mac','dvf_state','dvf_img','dvf_lic','dvf_notes'].map(g);
return dvAll.filter(m=>has(m.hostname,host)&&has(m.grp,grp)&&has(m.ip,ip)&&has(m.mac,mac)&&has(imgName(m.image_id),img)&&has(m.notes,notes)
&&(!state||(state==='on'?m.online:state==='off'?!m.online:state==='registered'?m.registered:!m.registered))
&&(!lic||(lic==='none'?!m.license_tail:lic==='error'?licErr(m):m.license_state===lic)));
}
function dvRender(){
const rows=dvFiltered(), tb=document.querySelector('#devices tbody'); if(!tb)return;
tb.innerHTML=rows.map(m=>`<tr class="${dvOpenId&&m.id===dvOpenId?'table-active':''}" ${m.registered?`${act('dvOpen',m.id)} style="cursor:pointer"`:''}>
<td data-click="noop"><input class="form-check-input" type="checkbox" ${dvSel.has(dvKey(m))?'checked':''} data-change="dvToggle" data-args="${esc(JSON.stringify([dvKey(m)]))}"></td>
<td>${m.hostname?esc(m.hostname):'<span class="mono">—</span>'} ${m.registered?'':'<span class="badge rounded-pill text-bg-warning">new</span>'}</td>
<td class="mono">${esc(m.grp||'—')}</td><td class="mono">${esc(m.ip)}</td><td class="mono">${esc(m.mac)}</td>
<td>${led(m.online)}</td>
<td class="mono">${esc(imgName(m.image_id)||'—')}</td>
<td class="mono" title="${esc(m.license_result||'')}">${licCell(m)}</td>
<td style="white-space:normal">${esc(m.notes||'')}</td></tr>`).join('')
||'<tr><td colspan=9 class="mono">no matching machines</td></tr>';
document.getElementById('dv_sel').textContent=dvSel.size+' selected';
const all=document.getElementById('dv_all'); if(all)all.checked=rows.length>0&&rows.every(m=>dvSel.has(dvKey(m)));
}
function dvToggle(k,on){on?dvSel.add(k):dvSel.delete(k);document.getElementById('dv_sel').textContent=dvSel.size+' selected';}
function dvSelectAll(on){dvFiltered().forEach(m=>on?dvSel.add(dvKey(m)):dvSel.delete(dvKey(m)));dvRender();}
function dvMsg(t){say(document.getElementById('dv_msg'),t);}
const dvPicked=()=>dvAll.filter(m=>dvSel.has(dvKey(m)));
async function dvBulk(action){
const reg=dvPicked().filter(m=>m.registered);
if(!reg.length){toast('Select registered machines first',true);return;}
let value='';
if(action==='group'){value=v('dv_bulk_grp');if(value&&!/^[A-Za-z0-9_-]{1,32}$/.test(value)){toast('Group: letters/digits/_/-, max 32',true);return;}}
if(action==='image')value=document.getElementById('dv_bulk_img').value;
if(action==='delete'&&!await sure('Delete '+reg.length+' machine(s)? Their DHCP binding and license key go too.'))return;
try{const r=await j('/api/machines/bulk',mk({ids:reg.map(m=>m.id),action,value}));dvMsg('✓ '+action+': '+r.count+' machine(s)');
if(action==='delete'){dvSel.clear();dvClose();}loadDevices();}
catch(e){dvMsg('✗ '+e.message);}
}
async function dvRegister(){
const macs=dvPicked().filter(m=>!m.registered).map(m=>m.mac);
if(!macs.length){toast('Select NEW machines (not registered yet)',true);return;}
try{const r=await j('/api/machines/register-bulk',mk({macs,prefix:v('dv_prefix'),start:+v('dv_start')||1,digits:+document.getElementById('dv_digits').value}));
dvMsg('✓ registered: '+r.registered.map(x=>x.hostname+' ('+x.mac+')').join(', ')+'\nReboot them to get their IP/name.');dvSel.clear();loadDevices();}
catch(e){dvMsg('✗ '+e.message);}
}
async function dvImport(input){
const f=input.files[0];input.value='';if(!f)return;
try{const r=await fetch('/api/machines/import',{method:'POST',headers:{'content-type':'text/csv'},body:await f.text()});
const t=await r.text();
if(!r.ok){dvMsg('✗ import refused, nothing was written:\n'+t);return;}
const o=JSON.parse(t);dvMsg('✓ import: '+o.added+' added, '+o.updated+' updated, '+o.keys+' license key(s) set');loadDevices();}
catch(e){dvMsg('✗ '+e.message);}
}
async function dvAdd(){
const mac=v('dv_add_mac'),ip=v('dv_add_ip')||null,hostname=v('dv_add_host')||null;
if(!mac){toast('Enter a MAC',true);return;}
if(hostname&&!hostOk(hostname)){dvMsg('✗ '+HOST_RULE);return;}
try{await j('/api/machines',mk({mac,ip,hostname}));dvMsg('✓ added');['dv_add_mac','dv_add_ip','dv_add_host'].forEach(i=>document.getElementById(i).value='');loadDevices();}
catch(e){dvMsg('✗ '+e.message);}
}
async function dvOpen(id){
const box=document.getElementById('dv_detail'); if(!box)return;
let d; try{d=await j('/api/machines/detail?id='+id);}catch(e){dvClose();return;}
dvOpenId=id;
const m=d.machine; m.license_key=d.license_key; // key is skip-serialized on machine; detail returns it separately
const name=m.hostname||m.mac, who=esc(name), st=dvAll.find(x=>x.id===id)||{};
const opt=dvImages.map(i=>`<option value="${i.id}" ${i.id===m.image_id?'selected':''}>${esc(i.name)}</option>`).join('');
const lease=d.lease?esc(d.lease.ip||'—')+' · '+(d.lease.expires_in>0?'expires in '+Math.round(d.lease.expires_in/60)+' min':'expired'):'—';
box.hidden=false;
const fld=(label,id,input)=>`<div><label class="form-label small mb-1" for="${id}">${label}</label>${input}</div>`;
box.innerHTML=`<h2 class="h5 mb-3">${who} ${led(st.online)}</h2>
<div class="d-flex flex-wrap gap-2 align-items-end">
${fld('Hostname','dd_host',`<input class="form-control form-control-sm" id="dd_host" value="${esc(m.hostname||'')}" size="12" maxlength="15">`)}
${fld('MAC','dd_mac',`<input class="form-control form-control-sm" id="dd_mac" value="${esc(m.mac)}" size="17">`)}
${fld('Fixed IP','dd_ip',`<input class="form-control form-control-sm" id="dd_ip" value="${esc(m.ip||'')}" size="12" placeholder="(none)">`)}
${fld('Group','dd_grp',`<input class="form-control form-control-sm" id="dd_grp" value="${esc(m.grp||'')}" size="8">`)}
${fld('Default image','dd_img',`<select class="form-select form-select-sm" id="dd_img"><option value="">(global default)</option>${opt}</select>`)}
<div class="flex-grow-1" style="min-width:180px"><label class="form-label small mb-1" for="dd_notes">Notes</label><input class="form-control form-control-sm" id="dd_notes" value="${esc(m.notes||'')}" maxlength="200"></div>
</div>
<div class="d-flex flex-wrap gap-2 align-items-center mt-2">
<button class="btn btn-sm btn-primary" ${act('dvSave',id)}>Save</button>
<button class="btn btn-sm btn-outline-secondary" data-perm="wake" ${act('wake',m.mac)}>Wake</button>
<button class="btn btn-sm btn-outline-secondary" ${act('setKey',id,name)}>License key</button>
${m.license_state==='sent'?`<button class="btn btn-sm btn-outline-secondary" ${act('rearmKey',id,name)}>Re-arm key</button>`:''}
<button class="btn btn-sm btn-outline-danger" ${act('dvDelete',id,name)}>Delete</button>
<button class="btn btn-sm btn-outline-secondary" ${act('dvClose')}>Close</button>
<span id="dd_msg" class="msg"></span>
</div>
<table class="table table-sm align-middle mt-3 mb-0"><tbody>
<tr><td class="mono">Lease</td><td class="mono">${lease}</td></tr>
${st.metrics?`<tr><td class="mono">Health</td><td class="mono" style="white-space:pre-line">${esc(mxText(st.metrics))}</td></tr>`:''}
${st.agent_since?`<tr><td class="mono">Up</td><td class="mono">${upFor(st.agent_since)} (agent connected ${new Date(st.agent_since*1000).toLocaleString()})</td></tr>`:''}
${st.traffic?`<tr><td class="mono">Traffic</td><td class="mono">${esc(trText(st.traffic))}</td></tr>`:''}
<tr><td class="mono">License</td><td class="mono" style="white-space:normal">${licCell(m,true)}</td></tr>
<tr><td class="mono">Remote key</td><td class="mono" style="white-space:normal">${!d.agent_key?'— none yet: its next Windows boot gets one'
:d.agent_key.delivered?'handed over '+new Date(d.agent_key.delivered*1000).toLocaleString():'new key waiting for its next boot'}
<button class="btn btn-sm btn-outline-secondary ms-2" ${act('rekey',m.mac,name)} data-owner title="a new key, handed to the machine at its next boot — when it lost its key (new disk) or the old one may have leaked">Re-issue</button></td></tr>
<tr><td class="mono">Driver packages</td><td class="mono">${d.drivers.length?esc(d.drivers.join(', ')):'—'}</td></tr>
<tr><td class="mono">Hardware (${d.hwids.length})</td><td class="mono" style="white-space:normal">${d.hwids.length
?`<details><summary>IDs its stage reported at the last boot</summary>${d.hwids.map(esc).join('<br>')}</details>`
:'— (no Windows boot since this was added)'}</td></tr>
</tbody></table>
<h3 class="h6 mt-3">Disks</h3><div id="dd_disks" class="msg">…</div>`;
dvRender();
dvDisks(m.mac,name);
}
// The machine's local disks as its stage reported them (disks.rs): which one is the Broom disk, pick another one
// (erased at the next boot), hide the others from the guest.
async function dvDisks(mac,name){const el=document.getElementById('dd_disks');if(!el)return;
let k;try{k=await j('/api/disks?mac='+encodeURIComponent(mac));}catch(e){el.textContent='✗ '+e.message;return;}
const hide=k.hide==null?'':k.hide?'1':'0';
const rows=k.disks.map(d=>{const label=`${d.dev} ${gbs(d.size)} ${d.model}`.trim();
const role=d.broom?'<span class="badge text-bg-primary">Broom disk</span>'
:k.broom===d.id?'<span class="badge text-bg-warning" title="erased and set up at the next boot">Broom disk at next boot</span>'
:`<button class="btn btn-sm btn-outline-danger" ${act('dvBroomDisk',mac,d.id,label,name)} title="erase it at the next boot and keep Windows there instead">Use as Broom disk</button>`;
return `<tr><td class="mono">${esc(d.dev)}</td><td class="mono">${gbs(d.size)}</td><td>${esc(d.model||'—')}</td><td class="mono small">${esc(d.id)}</td><td>${role}</td></tr>`;}).join('');
el.innerHTML=`<div class="d-flex flex-wrap gap-2 align-items-center mb-2"><label class="small" for="dd_hide">Other disks</label>
<select class="form-select form-select-sm w-auto" id="dd_hide" data-change="dvHide" data-args="${esc(JSON.stringify([mac]))}">
<option value="" ${hide===''?'selected':''}>as its group (${k.group_hide?'hidden':'visible'})</option>
<option value="1" ${hide==='1'?'selected':''}>hidden from the guest</option>
<option value="0" ${hide==='0'?'selected':''}>visible</option></select>
<span class="small text-body-secondary">hidden = offline in Windows, gone on a Linux image; applies from the next boot</span><span id="dk_msg" class="msg"></span></div>
${rows?`<table class="table table-sm align-middle mb-0"><thead><tr><th>Disk</th><th>Size</th><th>Model</th><th>Id</th><th></th></tr></thead><tbody>${rows}</tbody></table>
<div class="small text-body-secondary mt-1">reported ${new Date(k.seen*1000).toLocaleString()} (Windows boots)</div>`
:'<span class="mono">— not reported yet (boot Windows on it once)</span>'}`;}
async function rekey(mac,name){
if(!await sure(`New Remote key for ${name}? Its agent is refused until its next boot brings the new key.`))return;
try{await j('/api/vnc/rekey',mk({mac}));toast('New key: '+name+' gets it at its next boot');if(dvOpenId)dvOpen(dvOpenId);}catch(e){toast(e.message,true);}}
async function dvHide(mac,el){const m=document.getElementById('dk_msg');
try{await j('/api/disks/hide',mk({mac,hide:el.value===''?null:el.value==='1'}));say(m,' ✓ saved');}catch(e){say(m,' ✗ '+e.message);}}
async function dvBroomDisk(mac,id,label,name){
const t=await ask('Erase a disk',`At its next boot ${name} ERASES ${label} and keeps Windows there; its current Broom disk loses its Broom partitions (only Broom's own files are on it). Type the machine name to confirm.`,'');
if(t==null)return;
if(t.trim()!==name){toast('Name does not match — nothing changed',true);return;}
try{await j('/api/disks/broom',mk({mac,id}));toast('Set: '+label+' becomes the Broom disk at the next boot');dvDisks(mac,name);}
catch(e){toast(e.message,true);}}
function dvClose(){dvOpenId=null;const b=document.getElementById('dv_detail');if(b)b.hidden=true;dvRender();}
async function dvSave(id){
const img=document.getElementById('dd_img').value, msg=document.getElementById('dd_msg');
const body={id,mac:v('dd_mac'),ip:v('dd_ip')||null,hostname:v('dd_host')||null,grp:v('dd_grp')||null,notes:v('dd_notes')||null,image_id:img?+img:null};
if(body.hostname&&!hostOk(body.hostname)){say(msg,' ✗ '+HOST_RULE);return;}
try{await j('/api/machines/update',mk(body));say(msg,' ✓ saved');loadDevices();}
catch(e){say(msg,' ✗ '+e.message);}
}
async function dvDelete(id,who){if(!await sure('Delete '+who+'? Its DHCP binding and license key go too.'))return;
try{await j('/api/machines/delete',mk({ids:[id]}));dvClose();loadDevices();}catch(e){toast(e.message,true);}}
// ---- images ----
const gbs=b=>b>=1e9?(b/1e9).toFixed(1)+' GB':(b/1e6).toFixed(0)+' MB';
// Hardware health (metrics.rs, pushed by the agent). badge = compact line for the Machines table; bad disk / hot → red.
const mxDiskBad=d=>d.predict_fail||(d.realloc||0)>0||(d.pending||0)>0||(d.health&&!/^(ok|healthy)$/i.test(d.health));
function mxBadge(m){if(!m)return'';
const hot=m.temps&&m.temps.some(t=>t>=85), badDisk=(m.disks||[]).some(mxDiskBad);
const bits=[`CPU ${m.cpu}%`,`RAM ${m.ram}%`];
if(m.temps&&m.temps.length)bits.push(Math.max(...m.temps)+'°C');
bits.push('⛁ '+(badDisk?'check':'OK'));
return ` <span class="badge ${hot||badDisk?'text-bg-danger':'text-bg-light border'}" title="${esc(mxText(m))}">${bits.join(' · ')}</span>`;}
// Uptime ≈ since the agent connected (it starts with Windows); traffic = what the server sends it now (sysmon.rs).
function dur(secs){const s=Math.max(0,secs|0),d=s/86400|0,h=(s%86400)/3600|0,m=(s%3600)/60|0;
return d?`${d}d ${h}h`:h?`${h}h ${m}m`:`${m}m`;}
const upFor=since=>dur(Date.now()/1000-since);
const trText=t=>`iSCSI ${(t[0]/1e6).toFixed(1)} MB/s · golden HTTP ${(t[1]/1e6).toFixed(1)} MB/s`;
function upBadge(m){let s='';
if(m.agent_since)s+=` <span class="badge text-bg-light border" title="agent connected ${new Date(m.agent_since*1000).toLocaleString()}">up ${upFor(m.agent_since)}</span>`;
const t=m.traffic;if(t&&t[0]+t[1]>=1e5)s+=` <span class="badge text-bg-info" title="${esc(trText(t))}">↓ ${((t[0]+t[1])/1e6).toFixed(1)} MB/s</span>`;
if(m.dns_blocked&&m.online)s+=` <span class="badge text-bg-warning" title="the DNS server refuses its names (no Remote agent, no recent PXE boot, not exempt): no internet — Network page → DNS">no DNS</span>`;
return s;}
function mxText(m){const l=[`CPU ${m.cpu}% RAM ${m.ram}%`];
if(m.temps&&m.temps.length)l.push('ACPI temps (best-effort): '+m.temps.map(t=>t+'°C').join(', '));
for(const d of (m.disks||[])){let s=d.name+': '+(d.health||'?');
if(d.temp!=null)s+=', '+d.temp+'°C';
if(d.realloc!=null||d.pending!=null)s+=', bad sectors '+(d.realloc||0)+' reallocated / '+(d.pending||0)+' pending';
if(d.predict_fail)s+=' — SMART predicts FAILURE';
l.push(s);}
if(m.free!=null)l.push('system drive free: '+gbs(m.free));
return l.join('\n');}
let imgRows=[];
async function loadImages(){
const [rows]=await Promise.all([j("/api/images"),loadVols()]);imgRows=rows;
document.querySelector('#images tbody').innerHTML=rows.map(i=>
`<tr><td>${esc(i.name)}${i.export?`<div class="mono" style="font-size:12px" title="exported ${i.export.created?new Date(i.export.created*1000).toLocaleString():''} — save both into one folder, open the .vmx in VMware">${ic('down','')} <a href="/api/images/export-file?id=${i.id}&f=vmx">.vmx</a> · <a href="/api/images/export-file?id=${i.id}&f=vmdk">.vmdk ${gbs(i.export.size)}</a> (${esc(i.export.version)})</div>`:''}</td><td>${osPill(i.os)}</td>
<td><a href="#" ${act('editImgGroups',i.id,i.name,i.groups)} title="machine groups that get it in their boot menu">${i.groups.length?esc(i.groups.join(', ')):'all'}</a></td>
<td class="mono">${i.cache_mode==='zram'?'RAM':'disk'}</td>
<td>${i.is_default?ic('ok'):''}</td>
<td>${i.boot_script?ic('ok'):'<span class="mono">no</span>'}</td>
<td class="mono" title="used on disk / virtual disk">${i.size==null?'—':gbs(i.used)+' / '+gbs(i.size)}${i.active_version?' <span class="badge rounded-pill text-bg-success">'+esc(i.active_version)+'</span>':''}</td>
<td>${volCell('image',i.name,i.volume)}</td>
<td class="mono" title="${esc(i.hash||'')}">${i.hash?esc(i.hash).slice(0,10):'—'}</td>
<td><div class="d-flex flex-wrap gap-1" style="min-width:320px">
<button class="btn btn-sm btn-outline-secondary" ${act('setDefault',i.id)}>Default</button>
<button class="btn btn-sm btn-outline-secondary" ${act('toggleCache',i.id,i.cache_mode||'disk',i.name)}>${(i.cache_mode==='zram')?'→disk':'→RAM'}</button>
${i.os==='windows'?'':`<button class="btn btn-sm btn-outline-secondary" ${act('toggleSsd',i.id,!i.use_ssd)} title="On: the golden is cached and the session's writes go to the machine's SSD (reset every boot). Off (one-time): nothing touches the SSD — golden over the network, writes in RAM, gone at power-off.">${i.use_ssd?'SSD: on':'SSD: off (one-time)'}</button>`}
${i.os==='windows'?`<button class="btn btn-sm btn-outline-secondary" ${act('togglePreload',i.id,!i.preload)} title="On: every machine that may boot it keeps it on its SSD ahead of use (fetched while another image boots: that boot takes longer once), so choosing it needs no download (its base is still built on its first boot). Off: fetched the first time it is chosen.">${i.preload?'Preload: on':'Preload: off'}</button>`:''}
<button class="btn btn-sm btn-outline-secondary" ${act('republish',i.id,i.name)}>Republish</button>
<button class="btn btn-sm btn-outline-secondary" ${act('showVersions',i.id,i.name,i.os)}>Versions</button>
<button class="btn btn-sm btn-outline-secondary" ${act('exportImage',i.id,i.name,null)} title="download as a VMware VM (.vmx + .vmdk) to edit the golden">Export</button>
<button class="btn btn-sm btn-outline-secondary" ${act('editBoot',i.id)}>Boot</button>
<button class="btn btn-sm btn-outline-danger" ${act('delImage',i.id,i.name)}>Delete</button>
</div></td></tr>`).join('') || '<tr><td colspan=10 class="mono">no images yet</td></tr>';
// A job still running that this tab doesn't follow (page reloaded / opened meanwhile) → follow it on the status line.
for(const i of rows)if(i.job&&i.job.startsWith('⏳')&&!jobWatch[i.name])watchJob(i.name,document.getElementById('img_status'));
// srvhost inside the images fragment → set after it is injected.
for(const id of ['srvhost2','srvhost3']){const e=document.getElementById(id);if(e)e.textContent=location.host;}
}
async function toggleCache(id,cur,name){const mode=cur==='zram'?'disk':'zram';
if(!await sure('Switch image cache to "'+(mode==='zram'?'RAM':'disk')+'"? (republish; RAM keeps a compressed copy of the golden in memory and serves clients from it)'))return;
const el=document.getElementById('img_status');
try{await j('/api/images/cache-mode',mk({id,mode}));watchJob(name,el);}catch(e){say(el,' ✗ '+e.message);}}
async function toggleSsd(id,on){
const el=document.getElementById('img_status');
try{await j('/api/images/ssd',mk({id,on}));loadImages();}catch(e){say(el,' ✗ '+e.message);}}
async function togglePreload(id,on){
const el=document.getElementById('img_status');
try{await j('/api/images/preload',mk({id,on}));loadImages();}catch(e){say(el,' ✗ '+e.message);}}
async function editImgGroups(id,name,groups,el,e){e.preventDefault();
const g=await ask('Groups of '+name,'Machine groups that get '+name+' in their boot menu, comma-separated (Devices page groups). Empty = every machine.',groups.join(', '));
if(g===null)return;const st=document.getElementById('img_status');
try{await j('/api/images/groups',mk({id,groups:[g]}));loadImages();}catch(err){say(st,' ✗ '+err.message);}}
async function setDefault(id){await j('/api/images/default',mk({id}));loadImages()}
// Job status by image name, pushed over SSE (/api/events "job") → text of el until ✓/✗.
// done(): optional, called when the job finishes (e.g. refresh the versions list).
const jobWatch={};
function showJob(name,s){const w=jobWatch[name];if(!w||!s)return;
// The page may have been rebuilt since the watch started (tab switch): then the Images page's status line.
const el=w.el.isConnected?w.el:document.getElementById(name.startsWith('bench:')?'bench_msg':'img_status');
say(el,' '+(el===w.el?'':name+': ')+s);
if(s.startsWith('✓')||s.startsWith('✗')){delete jobWatch[name];
if(curPage==='images')loadImages().then(()=>{ // an open Versions panel of this image follows too (new v1, rollback…)
const r=imgRows.find(x=>x.name===name);if(r&&verImg&&verImg.name===name)showVersions(r.id,r.name,r.os);}).catch(()=>{});
if(w.done)w.done();}}
function watchJob(name,el,done){
say(el,' ⏳ working...');jobWatch[name]={el,done};
// catch up once: the job may have moved on (or finished) before this tab started watching
j('/api/images/job?name='+encodeURIComponent(name)).then(r=>showJob(name,r.status)).catch(()=>{});
}
// ---- image versions (versions.rs: hard links to the golden; snapshot/rollback run as jobs) ----
let verImg=null;
async function showVersions(id,name,os){verImg={id,name,os};
const el=document.getElementById('img_versions');
el.innerHTML=`<h2 class="h6 mb-2">Versions — ${esc(name)}</h2><p class="mono">${ic('wait')} loading...</p>`;
const r=await j('/api/images/snapshots?id='+id);
el.innerHTML=`<h2 class="h6 mb-2">Versions — ${esc(name)} <button class="btn btn-sm btn-outline-primary ms-2" ${act('snapshotImage')}>+ Snapshot now</button> <button class="btn btn-sm btn-outline-secondary" ${act('dedupImage')} title="versions give back the data they have in common with the golden (XFS with reflink, btrfs; the golden clients load is never changed): for versions saved earlier, or after a move — a new upload does it by itself">Dedup</button></h2>
<div class="table-responsive"><table class="table table-sm table-hover align-middle"><thead><tr><th>Version</th><th>Label</th><th>Created</th><th></th></tr></thead><tbody>${
r.versions.map(v=>`<tr><td class="mono">${esc(v.version)} ${v.version===r.active?'<span class="badge rounded-pill text-bg-success">active</span>':''}</td>
<td>${esc(v.label)}</td><td class="mono">${new Date(v.created*1000).toLocaleString()}</td>
<td><div class="d-flex flex-wrap gap-1" style="min-width:320px"><button class="btn btn-sm btn-outline-secondary" ${act('rollbackImage',v.version)}>Rollback</button>
<button class="btn btn-sm btn-outline-secondary" ${act('exportImage',id,name,v.version)}>Export</button>
<button class="btn btn-sm btn-outline-secondary" ${act('versionToImage',v.version)} title="add this version as a new image on the list">→ New image</button>
<button class="btn btn-sm btn-outline-danger" ${act('deleteVersion',v.version)}>Delete</button></div></td></tr>`).join('')
|| '<tr><td colspan=5 class="mono">no versions yet — Snapshot now saves the current golden</td></tr>'}</tbody></table></div>`;}
async function snapshotImage(){const label=await ask('Snapshot '+verImg.name,'Label for this version (optional):','');if(label===null)return;
const el=document.getElementById('img_status');
try{await j('/api/images/snapshot',mk({id:verImg.id,label}));watchJob(verImg.name,el,()=>showVersions(verImg.id,verImg.name,verImg.os));}catch(e){say(el,' ✗ '+e.message);}}
async function dedupImage(){const el=document.getElementById('img_status');
try{await j('/api/images/dedup',mk({id:verImg.id}));watchJob(verImg.name,el,()=>showVersions(verImg.id,verImg.name,verImg.os));}catch(e){say(el,' ✗ '+e.message);}}
async function rollbackImage(version){
if(!await sure('Roll '+verImg.name+' back to '+version+'?'+(verImg.os==='windows'?' Every client will download the golden again.':' Running clients lose their iSCSI disk until publish finishes.')))return;
const el=document.getElementById('img_status');
try{await j('/api/images/rollback',mk({id:verImg.id,version}));watchJob(verImg.name,el,()=>showVersions(verImg.id,verImg.name,verImg.os));}catch(e){say(el,' ✗ '+e.message);}}
async function deleteVersion(version){if(!await sure('Delete version '+version+' of '+verImg.name+'?'))return;
try{await j('/api/images/version-delete',mk({id:verImg.id,version}));}catch(e){toast(e.message,true);}
showVersions(verImg.id,verImg.name,verImg.os);}
async function exportImage(id,name,version){
if(!await sure('Export '+name+(version?' '+version:' (current golden)')+' as a VMware VM (.vmx + .vmdk)?\nNeeds free disk space on the server ≈ the golden size; replaces the previous export of this image.'))return;
const el=document.getElementById('img_status');
try{await j('/api/images/export',mk({id,version}));watchJob(name,el);}catch(e){say(el,' ✗ '+e.message);}}
async function versionToImage(version){
const name=await ask('Version '+version+' → new image','New image name for '+verImg.name+' '+version+' (letters, digits, _ -):',verImg.name+'-'+version);
if(!name)return;
const el=document.getElementById('img_status');
try{await j('/api/images/from-version',mk({id:verImg.id,version,name}));loadImages();watchJob(name,el);}catch(e){say(el,' ✗ '+e.message);}}
async function republish(id,name){const el=document.getElementById('img_status');
try{await j('/api/images/publish',mk({id}));watchJob(name,el);}catch(e){say(el,' ✗ '+e.message);}}
async function editBoot(id){const cur=(imgRows.find(i=>i.id===id)||{}).boot_script||'';const s=await ask('iPXE boot script','',cur,true);if(s===null)return;
await j('/api/images/boot-script',mk({id,boot_script:s}));loadImages();}
async function delImage(id,name){if(!await sure('Delete image "'+name+'"?'))return;
await j('/api/images/delete',mk({id}));loadImages();}
// Chunked upload: every file in 8 MB pieces, 4 in flight; a failed piece is retried (2,4,8,16,32 s).
const CHUNK=8<<20,LANES=4;
async function putChunk(api,name,f,off){
const url=api+'/upload-chunk?name='+encodeURIComponent(name)+'&file='+encodeURIComponent(f.name)+'&offset='+off+'&total='+f.size;
for(let t=0;;t++){
let r=null;try{r=await fetch(url,{method:'PUT',body:f.slice(off,off+CHUNK)});}catch(_){} // null = network error
if(r&&r.ok)return;
if(r&&r.status<500)throw new Error(await r.text()); // rejected: retrying won't help
if(t>=5)throw new Error(r?await r.text():'network error');
await new Promise(ok=>setTimeout(ok,2000<<t));
}
}
// api = '/api/images' (golden) or '/api/drivers' (driver package): same start / chunk / done protocol.
async function uploadFiles(api,name,files,prog,msg){
await j(api+'/upload-start',mk({name}));
const q=[];for(const f of files)for(let o=0;o===0||o<f.size;o+=CHUNK)q.push([f,o]);
const total=files.reduce((s,f)=>s+f.size,0)||1,t0=Date.now();let done=0;
const bar=prog.firstElementChild;prog.style.display='';bar.style.width='0%';
const lane=async()=>{while(q.length){const [f,o]=q.shift();
try{await putChunk(api,name,f,o);}catch(e){q.length=0;throw e;} // one piece failed for good → stop all lanes
done+=Math.min(CHUNK,f.size-o);bar.style.width=done/total*100+'%';
msg.textContent=' upload '+Math.round(done/total*100)+'% · '+(done/1e6/Math.max(1,(Date.now()-t0)/1000)).toFixed(0)+' MB/s';}};
await Promise.all(Array.from({length:LANES},lane));
msg.textContent=' upload 100% — finishing on the server…'; // driver packages: unzip + read .inf + pack
return j(api+'/upload-done',mk({name}));
}
async function addImage(){
const dir=[...document.getElementById('up_dir').files].filter(f=>/\.(vmdk|vmx|img|raw)$/i.test(f.name));
const one=document.getElementById('up_file').files[0],files=dir.length?dir:(one?[one]:[]);
const name=v('up_name'),msg=document.getElementById('up_msg'),cache=document.getElementById('up_cache').value;
if(!files.length||!name){toast('Enter a name + choose a VM folder or a file',true);return;}
const os=document.getElementById('up_os').value;
try{await j('/api/images',mk({name,os,cache_mode:cache,volume:v('up_vol')||null}));}catch(e){}
try{await uploadFiles('/api/images',name,files,document.getElementById('up_prog'),msg);}
catch(e){say(msg,' ✗ '+e.message);return;}
say(msg,' ✓ upload done');loadImages();watchJob(name,msg); // convert + publish run in the background → SSE
}
// ---- server disks (volumes.rs): more disks for images and games disks ----
let vols={volumes:[],disks:[],home:null};
const volFree=s=>s?gbs(Math.max(0,s.total-s.used))+' free':'?';
// The broom disks (Images / Games pages): the pickers for a new image / games disk, shown once there is a second one.
async function loadVols(){try{vols=await j('/api/volumes');}catch(e){return;}
const up=vols.volumes.filter(x=>x.mounted);
const opts=`<option value="">broom folder (${volFree(vols.home&&vols.home.space)})</option>`
+up.map(x=>`<option value="${esc(x.id)}">${esc(x.id)} (${volFree(x.space)})</option>`).join('');
for(const id of ['up_vol','gm_vol']){const s=document.getElementById(id);if(s)s.innerHTML=opts;}
document.querySelectorAll('.vol-pick').forEach(e=>e.hidden=!up.length);}
// The disk an image / games disk is on; another one picked = moved there (a job).
function volCell(kind,name,cur){
if(!vols.volumes.length&&!cur)return '<span class="mono">broom folder</span>';
const o=(id,label,dis)=>`<option value="${esc(id)}"${(cur||'')===id?' selected':''}${dis?' disabled':''}>${esc(label)}</option>`;
return `<select class="form-select form-select-sm w-auto" data-owner data-change="volMove" data-args="${esc(JSON.stringify([kind,name,cur||'']))}">`
+o('','broom folder')+vols.volumes.map(x=>o(x.id,x.id+(x.mounted?'':' (missing)'),!x.mounted&&x.id!==cur)).join('')+'</select>';}
async function volMove(kind,name,cur,el){const to=el.value,what=(kind==='image'?'image ':'iSCSI disk ')+name;
if(!await sure(`Move ${what} to ${to||'the broom folder'}? Its files are copied (a while for a big one), then the old copy is deleted. Machines using it must be shut down first.`)){el.value=cur;return;}
const job=kind==='image'?name:'games:'+name,m=document.getElementById(kind==='image'?'img_status':'gm_msg');
try{await j('/api/volumes/move',mk({kind,name,to}));watchJob(job,m,()=>kind==='image'?loadImages():loadGames());}
catch(e){toast(e.message,true);el.value=cur;}}
// Server page → Disks: every disk of the server, broom's disks, format a free one / add a mounted folder.
async function loadServerDisks(){const el=document.getElementById('b_disks');if(!el)return;
let r;try{r=await j('/api/volumes?disks=1');}catch(e){el.textContent='✗ '+e.message;return;}
vols=r;
const sp=s=>s?`${gbs(s.used)} used of ${gbs(s.total)}`:'not mounted';
const on=x=>[...x.images.map(n=>'image '+n),...x.games.map(n=>'iSCSI disk '+n)].join(', ');
const vrows=`<tr><td><b>broom folder</b></td><td class="mono">${esc(r.home.path)}</td><td class="mono">${sp(r.home.space)}</td><td>everything not on another disk</td><td></td></tr>`
+r.volumes.map(x=>`<tr><td><b>${esc(x.id)}</b>${x.mounted?'':' <span class="badge text-bg-danger" title="not mounted: unplugged? what is on it is not served">missing</span>'}</td>
<td class="mono">${esc(x.path)}${x.fstype?' ('+esc(x.fstype)+')':''}</td><td class="mono">${sp(x.space)}</td><td>${esc(on(x))||'—'}</td>
<td data-owner>${on(x)?'':`<button class="btn btn-sm btn-outline-secondary" ${act('volRemove',x.id)} title="stop using it (its files stay)">Remove</button>`}</td></tr>`).join('');
const badge={system:'text-bg-secondary','in use':'text-bg-secondary',broom:'text-bg-primary',free:'text-bg-success'};
const drows=r.disks.map(d=>`<tr><td class="mono">${esc(d.name)}</td><td class="mono">${gbs(d.size)}</td><td>${esc(d.model||'—')}${d.rotational?' <span class="small text-body-secondary">HDD</span>':''}</td>
<td class="mono small">${esc([d.mount||'',...d.parts.map(p=>p.name+(p.mount?' → '+p.mount:'')),...d.holders.map(h=>'held by '+h)].filter(Boolean).join(', '))||'—'}</td>
<td class="small">${d.health?`<span class="${srvDiskBad(d.health)?'text-danger':''}">${esc(srvDiskHealth(d.health))}</span>`:'—'}</td>
<td><span class="badge ${badge[d.state]}">${esc(d.state)}</span></td>
<td data-owner>${d.state==='free'?`<button class="btn btn-sm btn-outline-danger" ${act('volFormat',d.name,d.model,d.size,d.old_data)}>Format</button>`:''}</td></tr>`).join('');
el.innerHTML=`<table class="table table-sm align-middle mb-3"><thead><tr><th>Broom disk</th><th>Path</th><th>Space</th><th>On it</th><th></th></tr></thead><tbody>${vrows}</tbody></table>
<table class="table table-sm align-middle mb-2"><thead><tr><th>Disk</th><th>Size</th><th>Model</th><th>Partitions / mounts</th><th>Health</th><th>State</th><th></th></tr></thead><tbody>${drows}</tbody></table>
<div class="d-flex flex-wrap gap-2 align-items-end" data-owner><div><label class="form-label small mb-1" for="vol_folder">Or a folder you mounted yourself (LVM, RAID…)</label><input class="form-control form-control-sm" id="vol_folder" size="28" placeholder="/mnt/data/broom"></div>
<button class="btn btn-sm btn-outline-primary" data-click="volFolder">Add folder</button><span id="vol_msg" class="msg align-self-center"></span></div>`;}
async function volFormat(name,model,size,old){
const t=await ask('Format '+name,`ERASES everything on ${name} (${model||'disk'} ${gbs(size)})${old?' — it has partitions / a filesystem now':''} and makes it a broom disk for images and iSCSI disks. Type ${name} to confirm.`,'');
if(t==null)return;
if(t.trim()!==name){toast('Name does not match — nothing changed',true);return;}
const m=document.getElementById('vol_msg');say(m,' formatting '+name+'…');
try{const r=await j('/api/volumes/format',mk({disk:name,confirm:t.trim()}));say(m,' ✓ '+name+' is broom disk '+r.id+(r.fstype==='ext4'?' — formatted ext4, where each image version takes its full size: install xfsprogs (Diskless page → Server packages) to format XFS, where versions share their data':''));}catch(e){say(m,' ✗ '+e.message);}
loadServerDisks();}
async function volFolder(){const m=document.getElementById('vol_msg');
try{await j('/api/volumes/folder',mk({path:v('vol_folder')}));say(m,' ✓ added');}catch(e){say(m,' ✗ '+e.message);}loadServerDisks();}
async function volRemove(id){if(!await sure('Stop using broom disk '+id+'? It is unmounted if broom mounted it; its files stay on it.'))return;
try{await j('/api/volumes/remove',mk({id}));}catch(e){toast(e.message,true);}loadServerDisks();}
// ---- driver packages (drivers.rs): .zip of an extracted driver folder → installed into base by broom-done ----
async function loadDrivers(){
const rows=await j('/api/drivers');
document.querySelector('#drivers tbody').innerHTML=rows.map(d=>
`<tr><td>${esc(d.name)}${d.net?' <span class="badge text-bg-info" title="network-card driver: also goes to Windows PE and into a golden for Add hardware">network card</span>':''}</td><td class="mono">${gbs(d.size)}</td>
<td class="mono" title="${esc(d.hwids.slice(0,40).join('\n'))}${d.hwids.length>40?'\n…':''}">${d.hwids.length}</td>
<td class="mono" style="white-space:normal">${d.machines.length?esc(d.machines.join(', ')):'—'}</td>
<td><input class="form-check-input" type="checkbox" id="drv_all_${d.id}" ${d.all_machines?'checked':''}></td>
<td><input class="form-control form-control-sm" id="drv_grp_${d.id}" value="${esc(d.groups.join(', '))}" placeholder="VIP, Pro" size="12"></td>
<td><div class="d-flex gap-1"><button class="btn btn-sm btn-outline-secondary" ${act('saveDrvTargets',d.id)}>Save</button>
<button class="btn btn-sm btn-outline-danger" ${act('delDriver',d.id,d.name)}>Delete</button></div></td></tr>`).join('')
|| '<tr><td colspan=7 class="mono">no driver packages yet</td></tr>';
}
async function uploadDriver(){
const f=document.getElementById('drv_file').files[0],msg=document.getElementById('drv_msg');
const name=v('drv_name')||(f?f.name.replace(/\.zip$/i,'').replace(/[^A-Za-z0-9_-]/g,'-'):'');
if(!f){toast('Choose the .zip of an extracted driver folder',true);return;}
try{const r=await uploadFiles('/api/drivers',name,[f],document.getElementById('drv_prog'),msg);
say(msg,' ✓ '+name+': '+r.infs+' .inf, '+r.hwids+' hardware IDs');loadDrivers();}
catch(e){say(msg,' ✗ '+e.message);}
}
async function saveDrvTargets(id){
const all_machines=document.getElementById('drv_all_'+id).checked, groups=document.getElementById('drv_grp_'+id).value;
try{await j('/api/drivers/targets',mk({id,all_machines,groups}));loadDrivers();}catch(e){toast(e.message,true)}}
async function delDriver(id,name){if(!await sure('Delete driver package "'+name+'"? Machines that had it rebuild their base on the next boot.'))return;
try{await j('/api/drivers/delete',mk({id}));loadDrivers();}catch(e){toast(e.message,true)}}
// ---- network ----
// DNS servers: one box each (order = preference), up to 8; sent to the server as one comma-separated list.
const DNS_MAX=8;
function dnsBox(v){const w=document.createElement('div');w.className='input-group input-group-sm w-auto';
w.innerHTML='<input class="dns form-control" size="14" placeholder="e.g. 1.1.1.1"><button type="button" class="btn btn-outline-secondary" title="remove" aria-label="remove">×</button>';
w.querySelector('input').value=v||'';w.querySelector('button').onclick=()=>{w.remove();dnsButton();};return w;}
function dnsButton(){const l=document.getElementById('dns_list');if(!l)return;let b=document.getElementById('dns_add');
if(!b){b=document.createElement('button');b.id='dns_add';b.type='button';b.className='btn btn-sm btn-outline-secondary';b.textContent='+ DNS';
b.onclick=()=>{l.insertBefore(dnsBox(''),b);dnsButton();};l.appendChild(b);}
b.style.display=l.querySelectorAll('input.dns').length>=DNS_MAX?'none':'';}
function setDns(csv){const l=document.getElementById('dns_list');if(!l)return;l.innerHTML='';
const v=(csv||'').split(',').map(s=>s.trim()).filter(Boolean);for(const x of (v.length?v:[''])) l.appendChild(dnsBox(x));dnsButton();}
function getDns(){return [...document.querySelectorAll('#dns_list input.dns')].map(e=>e.value.trim()).filter(Boolean).join(',');}
// DNS for healthy machines only (dns.rs).
async function loadDnsGate(){const box=document.getElementById('dns_groups');if(!box)return;
let d,g;try{[d,g]=await Promise.all([j('/api/dns'),j('/api/groups')]);}catch(e){return;}
document.getElementById('dns_mode').value=d.mode;document.getElementById('dns_grace').value=d.grace_min;
document.getElementById('dns_resolve').value=d.resolve;document.getElementById('dns_up').value=d.upstream.join(', ');
document.getElementById('dns_ips').value=d.exempt_ips;dnsResolveShow();
// DNS on: the DHCP card hands out this server — its own DNS list is not used then.
const on=d.mode==='on';document.getElementById('dhcp_dns_field').hidden=on;document.getElementById('dhcp_dns_self').hidden=!on;
box.innerHTML=g.groups.map(n=>`<label class="form-check"><input class="form-check-input" type="checkbox" value="${esc(n)}"${d.exempt_groups.some(x=>x.toLowerCase()===n.toLowerCase())?' checked':''}> ${esc(n)}</label>`).join('')||'<span class="mono">no groups yet</span>';
const ups=d.upstream.join(', '),how=d.resolve==='recursive'?`recursive: cache (${d.cached} answers), then ${ups||'the root servers'}`:`passthrough to ${ups}`;
document.getElementById('dnsg_state').textContent=d.active
?`Running on ${d.server}:53, ${how} · ${d.allowed} IPs may resolve now`+(d.dhcp_full?'':` · the DHCP server is off: set the router's DHCP DNS to ${d.server}`)
:'Off: clients use the DNS servers of the DHCP card.';}
function dnsResolveShow(){const l=document.getElementById('dns_up_label');if(l)l.textContent=v('dns_resolve')==='recursive'?'Asked on a cache miss (empty = root servers)':'DNS servers';}
async function saveDnsGate(){const m=document.getElementById('dnsg_msg');
const body={mode:v('dns_mode'),resolve:v('dns_resolve'),upstream:v('dns_up').split(/[\s,]+/).filter(Boolean),
grace_min:+v('dns_grace'),exempt_ips:document.getElementById('dns_ips').value,
exempt_groups:[...document.querySelectorAll('#dns_groups input:checked')].map(e=>e.value)};
try{const r=await j('/api/dns',mk(body));say(m,' ✓ '+(r.status||'saved'));}catch(e){say(m,' ✗ '+e.message);}loadDnsGate();}
async function loadDhcp(){loadDnsGate();const c=await j('/api/dhcp');
['mode','iface','server_ip','subnet','netmask','range_start','range_end','gateway'].forEach(k=>{const e=document.getElementById('dhcp_'+k);if(e)e.value=c[k]||'';});
for(const k of ['ipxe_signed','strict_reset','rapid_commit','ipxe_fast','authoritative','send_hostname']){const e=document.getElementById('dhcp_'+k);if(e)e.checked=!!c[k];}
setDns(c.dns);
// Pool vs the machines that need an address from it (registered without a fixed IP + other devices with a lease).
const pl=document.getElementById('dhcp_pool');
if(pl){const tight=c.pool_need>c.pool_size*0.9;
pl.className='small mt-2'+(tight?' alert alert-warning py-1 px-2 mb-0':' text-body-secondary');
pl.textContent=`Pool: ${c.pool_size} addresses, ${c.pool_need} machines/devices need one from it.`
+(tight?' Too tight: widen the range, or give machines a fixed IP (Devices).':'');}
const dm=document.getElementById('dhcp_mode');if(dm)dm.onchange=toggleFull;toggleFull();}
function toggleFull(){const dm=document.getElementById('dhcp_mode');if(!dm)return;
for(const id of ['full_only','full_only_opts']){const e=document.getElementById(id);if(e)e.style.display=dm.value==='full'?'':'none';}}
async function applyDhcp(){const b={};['mode','iface','server_ip','subnet','netmask','range_start','range_end','gateway'].forEach(k=>b[k]=document.getElementById('dhcp_'+k).value);
for(const k of ['ipxe_signed','strict_reset','rapid_commit','ipxe_fast','authoritative','send_hostname']){const e=document.getElementById('dhcp_'+k);if(e)b[k]=e.checked?'1':'0';}
b.dns=getDns();
const m=document.getElementById('dhcp_msg');
try{const r=await j('/api/dhcp',mk(b));say(m,' ✓ '+(r.status||'applied'));}catch(e){say(m,' ✗ '+e.message);}}
// ---- games disks (Games page) ----
let gmDisks=[];
async function loadGames(){const [g]=await Promise.all([j('/api/games'),loadVols(),loadVersions()]);const tb=document.querySelector('#games tbody');if(!tb)return;
gmDisks=g.disks;
document.getElementById('gm_grouplist').innerHTML=g.groups.map(x=>`<option value="${esc(x)}">`).join('');
const opts=sel=>'<option value="">— none —</option>'+g.machines.map(m=>
`<option value="${m.id}"${m.id===sel?' selected':''}>${esc(m.name)}${m.grp?' ('+esc(m.grp)+')':''}</option>`).join('');
tb.innerHTML=g.disks.map(x=>{const d=x.disk,s=x.status;
return `<tr><td>${esc(d.name)}</td><td class="mono">${esc(d.letter)}:</td><td class="mono">${d.size_gb} GB</td>
<td>${d.groups.length?esc(d.groups.join(', ')):'<span class="mono">all</span>'}</td>
<td class="mono text-nowrap">${s?verCell('games',d.name,s):'<span class="text-danger">not served</span>'}</td>
<td class="mono">${s?s.sessions+(s.old_sessions?` <span title="still on an older version until they reboot">(${s.old_sessions} older)</span>`:''):''}</td>
<td><select class="form-select form-select-sm d-inline-block w-auto" data-owner data-change="setGamesUpdate" data-args="${esc(JSON.stringify([d.name]))}">${opts(d.update)}</select>${d.update!=null&&!x.update_ip?' <span class="text-danger" title="no fixed IP or lease known">no IP</span>':''}${s&&s.update_connected?' <span class="text-success">'+ic('dot','')+' writable</span>':''}</td>
<td class="mono">${s&&s.update_bytes?gbs(s.update_bytes)+` <button class="btn btn-sm btn-outline-primary" data-perm="games" ${act('saveGamesUpdate',d.name)}>Save</button> <button class="btn btn-sm btn-outline-secondary" data-perm="games" ${act('discardGamesUpdate',d.name)}>Discard</button>`:'—'}</td>
<td>${volCell('games',d.name,x.volume)}</td>
<td data-owner><button class="btn btn-sm btn-outline-secondary" ${act('toggleGamesRam',d.name,!d.ram)} title="RAM: the disk is served from a copy in the server's RAM (loaded in the background; needs about as much free RAM as the data on it — games barely compress). Disk: read from its file (the page cache keeps what is read most).">${d.ram?(s&&s.ram?'Cache: RAM':(s&&s.ram_loading?'Cache: RAM (loading…)':'Cache: RAM (not loaded)')):'Cache: disk'}</button> <button class="btn btn-sm btn-outline-secondary" ${act('editGames',d.name)}>Edit</button> <button class="btn btn-sm btn-outline-danger" ${act('delGames',d.name)}>Delete</button></td></tr>`+verRow('games',d.name,10);}).join('')
||'<tr><td colspan="10" class="mono">no iSCSI disk yet</td></tr>';
const m=document.getElementById('gm_msg');if(m&&g.error)say(m,' ✗ not served: '+g.error);}
async function gamesCall(url,body,q){if(q&&!await sure(q))return;await post(url,body,'gm_msg',r=>r.status||'saved',loadGames);}
const saveGames=()=>gamesCall('/api/games/disk',{name:v('gm_name'),letter:v('gm_letter'),size_gb:+v('gm_size'),groups:[v('gm_groups')],volume:v('gm_vol')||null});
const setGamesUpdate=(name,el)=>gamesCall('/api/games/update',{name,id:el.value?+el.value:null});
const toggleGamesRam=(name,ram)=>gamesCall('/api/games/cache',{name,ram},ram?'Serve iSCSI disk '+name+' from RAM? It is loaded in the background (served from the disk meanwhile) and needs about as much free RAM as the data on it; not enough RAM → it stays on the disk (see the log).':null);
const saveGamesUpdate=name=>gamesCall('/api/games/save',{name},'Save the update of '+name+' as its new version? Its update machine must be shut down. Each machine gets it at its next boot.');
const discardGamesUpdate=name=>gamesCall('/api/games/discard',{name},'Throw the update of '+name+' away?');
const delGames=name=>gamesCall('/api/games/delete',{name},'Delete iSCSI disk '+name+' and EVERYTHING on it? Machines using it keep it until they reboot.');
function editGames(name){const d=gmDisks.find(x=>x.disk.name===name).disk;
for(const [k,val] of [['gm_name',d.name],['gm_letter',d.letter],['gm_size',d.size_gb],['gm_groups',d.groups.join(', ')]])document.getElementById(k).value=val;}
// ---- versions of goldens / iSCSI disks (pins.rs): kept until a drive runs short, a group held on one ----
let verData=null;const verOpen=new Set();
async function loadVersions(){verData=await j('/api/versions').catch(()=>verData);return verData;}
const verEntry=(kind,name)=>verData&&(kind==='golden'?verData.goldens:verData.games).find(x=>x.name===name);
const pinOf=(pins,g)=>{for(const [k,x] of Object.entries(pins||{}))if(k.toLowerCase()===String(g).toLowerCase())return x;return null;};
function verOpts(vers,cur){return `<option value=""${cur==null?' selected':''}>newest</option>`+[...vers].sort((a,b)=>b-a).map(x=>`<option value="${esc(x)}"${x===cur?' selected':''}>g${esc(x)}</option>`).join('');}
// The Version column: the newest, how many are kept, the panel's button.
function verCell(kind,name,s){const n=(s.versions&&s.versions.length)||(s.layers+1);
return `g${esc(s.ver)}${s.merging?' (merging…)':''} <button class="btn btn-sm btn-outline-secondary py-0" ${act('verToggle',kind,name)} title="versions kept, which one each group boots">${esc(n)} kept</button>`;}
function verRow(kind,name,cols){return verOpen.has(kind+':'+name)?`<tr><td colspan="${esc(cols)}">${verPanel(kind,verEntry(kind,name))}</td></tr>`:'';}
function verPanel(kind,e){if(!e)return '<span class="small text-body-secondary">no version information (not served, or the iSCSI daemon is of an older build)</span>';
const held=v=>Object.entries(e.pins||{}).filter(([,x])=>x===v).map(([k])=>k).join(', ');
const rows=[...(e.versions||[])].reverse().map(x=>`<tr><td class="mono">g${esc(x.ver)}${x.ver===e.newest?' <span class="badge text-bg-primary">newest</span>':''}</td>
<td class="mono">${esc(x.bytes?gbs(x.bytes):'(in the disk file)')}</td><td class="small">${esc(x.saved?new Date(x.saved*1000).toLocaleString():'')}</td>
<td class="small">${esc(x.sessions?x.sessions+' on it now':'')}</td><td class="small">${esc(held(x.ver))}</td></tr>`).join('');
const vers=(e.versions||[]).map(x=>x.ver);
const fs=e.fs_size?gbs(e.fs_free)+' free of '+gbs(e.fs_size)+' on its drive':'';
const how=verData.keep?`Every saved version is kept until its drive has less than ${verData.min_free_pct}% free; then the oldest goes — never one a group is held on or a machine is on now.`
:'Older versions are merged away once no machine is on them (keeping them: Diskless page, Where things are kept). A version a group is held on stays, with every newer one.';
return `<div class="small mb-1 text-body-secondary">${esc(how)}${esc(fs?' '+fs+'.':'')}</div>
<div class="table-responsive"><table class="table table-sm w-auto mb-2"><thead><tr><th>Version</th><th>Its own data</th><th>Saved</th><th>In use</th><th>Held for</th></tr></thead><tbody>${rows}</tbody></table></div>
<div class="d-flex flex-wrap gap-3 align-items-center"><b class="small">Version each group boots</b>${verPicks(kind,e,vers)}
<button class="btn btn-sm btn-outline-danger" data-owner ${act('verDrop',kind,e.name)}>Drop the oldest version</button></div>`;}
// A version picker per group that gets it: changing one holds that group there at once (machines: next boot).
function verPicks(kind,e,vers){return (e.groups||[]).map(g=>`<label class="d-flex align-items-center gap-1 small">${esc(g)} <select class="form-select form-select-sm w-auto py-0" data-perm="versions" data-change="verPin" data-args="${esc(JSON.stringify([kind,e.name,g]))}">${verOpts(vers,pinOf(e.pins,g))}</select></label>`).join('')
||'<span class="small text-body-secondary">for every machine — hold a group on a version from the Groups page</span>';}
const verReload=kind=>kind==='golden'?loadDiskless():loadGames();
async function verToggle(kind,name){const k=kind+':'+name;if(verOpen.has(k))verOpen.delete(k);else verOpen.add(k);verReload(kind);}
async function verPin(kind,name,group,el){
try{const r=await j('/api/versions/pin',mk({kind,name,group,ver:el.value===''?null:+el.value}));toast(r.status);}catch(e){toast(e.message,true);}
verReload(kind);}
async function verDrop(kind,name){if(!await sure('Drop the oldest kept version of '+name+' now? It can no longer be booted or gone back to. (Not possible while a machine is on it or a group is held on it.)'))return;
try{const r=await j('/api/versions/drop-oldest',mk({kind,name}));toast(r.status);}catch(e){toast(e.message,true);}
verReload(kind);}
const verKeepFree=()=>post('/api/versions/keep-free',{keep:document.getElementById('ver_keep').checked,pct:+v('ver_pct')},'ver_msg',()=>'saved',loadDiskless);
// ---- diskless goldens (Diskless page) ----
let dlData=null;
const mac12=m=>(m||'').toLowerCase().replace(/[^0-9a-f]/g,'');
async function loadDiskless(){
const [d,vl]=await Promise.all([j('/api/diskless'),j('/api/volumes').catch(()=>({volumes:[]})),loadVersions()]);
dlData=d;const tb=document.querySelector('#dl_goldens tbody');if(!tb)return;
const opts=sel=>'<option value="">— none —</option>'+d.machines.map(m=>
`<option value="${esc(m.id)}"${m.id===sel?' selected':''}>${esc(m.name)}${m.group?' ('+esc(m.group)+')':''}</option>`).join('');
tb.innerHTML=d.goldens.map(x=>{const g=x.golden,s=x.status?x.status.disk:null;
return `<tr><td><b>${esc(g.name)}</b></td><td>${osPill(g.os)}</td><td class="mono">${esc(g.size_gb)} GB</td>
<td><div class="mono small" style="max-width:16rem;white-space:normal;overflow-wrap:anywhere">${esc(g.iso||'—')}</div></td>
<td>${g.groups.length?esc(g.groups.join(', ')):'<span class="text-body-secondary">none</span>'}</td>
<td class="mono text-nowrap">${s?verCell('golden',g.name,s):'<span class="text-danger">not served</span>'}</td>
<td class="mono">${x.status?x.status.machines.filter(m=>m.connected).length:''}</td>
<td><select class="form-select form-select-sm d-inline-block w-auto" data-perm="maintain" data-change="setMaint" data-args="${esc(JSON.stringify([g.name]))}">${opts(g.maint)}</select>${s&&s.update_connected?' <span class="text-success">'+ic('dot','')+' writable</span>':''}</td>
<td class="mono">${s&&s.update_bytes?gbs(s.update_bytes)+` <button class="btn btn-sm btn-outline-primary" data-perm="maintain" ${act('saveMaint',g.name)}>Save</button> <button class="btn btn-sm btn-outline-secondary" data-perm="maintain" ${act('discardMaint',g.name)}>Discard</button>`:'—'}</td>
<td class="text-nowrap"><button class="btn btn-sm btn-outline-secondary" data-owner ${act('editGolden',g.name)}>Edit</button> <button class="btn btn-sm btn-outline-danger" data-owner ${act('delGolden',g.name)}>Delete</button></td></tr>`+verRow('golden',g.name,10);}).join('')
||'<tr><td colspan="10" class="mono">no golden yet — create one below</td></tr>';
const vp=document.getElementById('ver_pct');if(vp&&verData&&document.activeElement!==vp)vp.value=verData.min_free_pct;
const rm=dlData&&dlData.room;
if(rm){for(const [id,val] of [['room_gb',rm.pc_gb],['room_pct',rm.free_pct],['room_pf',rm.pagefile_gb]]){const e=document.getElementById(id);if(e&&document.activeElement!==e)e.value=val;}
const rn=document.getElementById('room_note');
if(rn)rn.textContent=rm.free_pct?`Below ${rm.free_pct}% free, only machines holding less than ${rm.fair_gb} GB still get new room: the big writer is stopped (its writes fail until it restarts), the others go on.`:'No reserve: one machine can fill the drive for all.';}
const vk=document.getElementById('ver_keep');if(vk&&verData&&document.activeElement!==vk)vk.checked=!!verData.keep;
// Their machines: each with its own layers (from the daemon) and target.
const mb=document.querySelector('#dl_machines tbody');
const rows=d.goldens.flatMap(x=>x.machines.map(m=>{const st=x.status?x.status.machines.find(y=>y.mac===mac12(m.mac)):null;
return `<tr><td>${esc(x.golden.name)}</td><td>${esc(m.name)}</td><td class="mono">${esc(m.mac)}</td><td class="mono">${esc(m.ip)}</td>
<td class="mono">${st&&st.session_ver!=null?gbs(st.session_bytes)+' on g'+esc(st.session_ver):'—'}</td>
<td class="mono">${st&&st.base_ver!=null?gbs(st.base_bytes)+' (g'+esc(st.base_ver)+')':'—'}</td>
<td>${st&&st.connected?'<span class="text-success">'+ic('dot','')+' on</span>':'—'}${st&&st.refused?' <span class="text-danger" title="its session takes no more writes (its limit, or the drive\'s reserve): Windows restarts by itself">out of room</span>':''}</td>
<td class="mono small">${esc(m.target)}</td>
<td><button class="btn btn-sm btn-outline-secondary" data-perm="maintain" title="its base is dropped at its next start: name, Remote agent key and first-boot setup made anew" ${act('dlRebuild',x.golden.name,m.id,m.name)}>Set up again</button></td></tr>`;})).join('');
mb.innerHTML=rows||'<tr><td colspan="9" class="mono">no machine yet: put registered machines with a fixed IP in a golden\'s group</td></tr>';
const noip=d.goldens.filter(x=>x.machines_without_fixed_ip.length).map(x=>x.golden.name+': '+x.machines_without_fixed_ip.join(', ')+' (no fixed IP — not served)');
document.getElementById('dl_noip').textContent=noip.join(' · ');
const groups=[...new Set([...d.machines.map(m=>m.group).filter(Boolean),...d.goldens.flatMap(x=>x.golden.groups)])];
document.getElementById('dl_grouplist').innerHTML=groups.map(x=>`<option value="${esc(x)}">`).join('');
dlIsoOptions();
// ISO library.
const ib=document.querySelector('#dl_isos tbody');
ib.innerHTML=d.isos.map(i=>`<tr><td class="mono">${esc(i.name)}</td>
<td><select class="form-select form-select-sm d-inline-block w-auto" data-owner data-change="isoOs" data-args="${esc(JSON.stringify([i.name]))}"><option value="windows"${i.os==='windows'?' selected':''}>Windows</option><option value="linux"${i.os==='linux'?' selected':''}>Linux</option></select></td>
<td class="mono">${gbs(i.size)}</td><td class="mono">${i.added?new Date(i.added*1000).toLocaleString():''}</td>
<td data-owner><button class="btn btn-sm btn-outline-danger" ${act('delIso',i.name)}>Delete</button></td></tr>`).join('')
||'<tr><td colspan="5" class="mono">no ISO yet — upload an installer below</td></tr>';
// Places: broom's own folder or a server disk each.
const labels={golden:'Goldens',writeback:'Machines\' layers (writeback)',iso:'ISO library'};
document.getElementById('dl_places').innerHTML=Object.keys(labels).map(k=>{const p=d.places[k]||{};
const opt='<option value="">broom\'s own folder</option>'+(vl.volumes||[]).map(v=>`<option value="${esc(v.id)}"${v.id===p.volume?' selected':''}>${esc(v.id)} — ${esc(v.path)}</option>`).join('');
return `<div class="d-flex flex-wrap gap-2 align-items-center"><span style="min-width:220px">${esc(labels[k])}</span>
<select class="form-select form-select-sm w-auto" id="dl_place_${esc(k)}">${opt}</select>
<button class="btn btn-sm btn-outline-primary" ${act('setPlace',k)}>Set</button>
<span class="mono small text-body-secondary">${esc(p.path||'')}</span></div>`;}).join('');
// The machines reach the server at its boot-LAN address, which may not be the one this page came from.
const srv=d.server||location.host;
const sc=document.getElementById('dl_scriptcmd');if(sc)sc.textContent='irm http://'+srv+'/broom-diskless.ps1 | iex';
const sh=document.getElementById('dl_scriptcmd_sh');if(sh)sh.textContent='curl -fsS http://'+srv+'/broom-diskless.sh | sudo sh';
const m=document.getElementById('dl_msg');if(m)say(m,d.error?' ✗ not served: '+d.error:'');
loadPackages().catch(()=>{});loadOnboard().catch(()=>{});loadVm().catch(()=>{});}
// "Server packages" (pkgmgr.rs): what the VM features need from the operating system, and the owner's Install.
let pkTimer=null,pkCmd={},pkWas=false;
async function loadPackages(){
const r=await j('/api/packages').catch(()=>null);const st=document.getElementById('pk_state');if(!r||!st)return;
const run=!!r.job&&r.job.state==='running';
document.getElementById('pk_os').textContent=(r.os?r.os+'. ':'')+'Package manager: '+(r.manager||r.other&&r.other+' (not driven by Broom)'||'none found')+'.';
// One block per need: what is missing, the command, and the owner's Install for it.
const row=(key,title,f)=>{const miss=f.missing||[];pkCmd[key]=f.command||'';
if(!miss.length)return `<div class="mb-2"><b>${esc(title)}:</b> <span class="text-success">✓ installed.</span></div>`;
return `<div class="mb-2"><b>${esc(title)}:</b> missing ${esc(miss.map(m=>m.what+(m.package?' ('+m.package+')':'')).join(', '))}`
+(r.supported?`<div class="mono mt-1">${esc(f.command)}</div><button class="btn btn-sm btn-primary mt-1" data-owner ${act('pkInstall',key)}${run?' disabled':''}>Install</button>`
:`<div class="text-danger mt-1">${esc(f.hint||'')}</div>`)+'</div>';};
let h=row('vm','QEMU and OVMF (Maintain in the browser, Add hardware)',r.vm)+row('xfs','xfsprogs (XFS server disks, optional)',r.xfs);
if(!r.kvm)h+='<div class="text-danger">/dev/kvm is missing: a VM needs KVM (turn on virtualisation in the firmware, or nested virtualisation when this server is itself a VM).</div>';
st.innerHTML=h;
const lg=document.getElementById('pk_log'),jb=r.job;
lg.hidden=!jb;
if(jb){lg.textContent=(jb.log||[]).join('\n')+(jb.state==='failed'?'\n✗ '+jb.msg:jb.state==='done'?'\n✓ '+jb.msg:'');lg.scrollTop=lg.scrollHeight;}
clearTimeout(pkTimer);
if(run&&pageFromPath()==='diskless')pkTimer=setTimeout(()=>loadPackages().catch(()=>{}),2000);
// The VM cards say what was missing: refresh them once an install has ended.
if(pkWas&&!run)loadVm().catch(()=>{});
pkWas=run;
}
async function pkInstall(feature){
if(!await sure('Run on this server, as root:\n\n'+(pkCmd[feature]||'')+'\n\nIt can take several minutes.'))return;
try{await j('/api/packages/install',mk({feature}));}catch(e){toast(e.message,true);}
loadPackages().catch(()=>{});
}
// "Add hardware": a new PC's network card installed into a Windows golden (onboard.rs).
let obTimer=null;
async function loadOnboard(){
const o=await j('/api/onboard').catch(()=>null);if(!o||!dlData)return;
const gs=document.getElementById('ob_golden'),ms=document.getElementById('ob_machine');if(!gs)return;
const keepG=gs.value,keepM=ms.value;
gs.innerHTML=dlData.goldens.filter(x=>x.golden.os==='windows').map(x=>`<option value="${esc(x.golden.name)}"${x.golden.name===keepG?' selected':''}>${esc(x.golden.name)}</option>`).join('');
ms.innerHTML=dlData.machines.map(m=>`<option value="${esc(m.id)}"${String(m.id)===keepM?' selected':''}>${esc(m.name)}${m.group?' ('+esc(m.group)+')':''}</option>`).join('');
document.getElementById('ob_missing').textContent=o.missing?'Not possible on this server yet: '+o.missing:'';
const jb=o.job,box=document.getElementById('ob_job');
if(jb){const cls={done:'text-success',failed:'text-danger',cancelled:'text-body-secondary'}[jb.state]||'text-primary';
box.innerHTML=`<div><b>${esc(jb.golden)}</b> · <span class="${esc(cls)}">${esc(jb.state)}</span> · ${esc(jb.msg)}</div>`
+(jb.nics&&jb.nics.length?'<div class="small">'+jb.nics.map(n=>esc(n.name)+' <span class="mono">'+esc(n.instance)+'</span>').join('<br>')+'</div>':'')
+`<details class="small"><summary>Steps</summary><pre class="mono small mb-0" style="white-space:pre-wrap">${esc((jb.log||[]).join('\n'))}</pre></details>`;}
else box.innerHTML='';
if(jb&&jb.boot_card)box.innerHTML+=`<div class="small">Network card seen at network boot: <span class="mono">${esc(jb.boot_card)}</span></div>`;
if(o.hint)box.innerHTML+=`<div class="small text-warning-emphasis">${esc(o.hint)}</div>`;
const rows=Object.entries(o.hardware||{}).flatMap(([g,l])=>l.map(h=>`<tr><td>${esc(g)}</td><td>${esc(h.name)}</td><td class="mono small" style="overflow-wrap:anywhere">${esc(h.instance)}</td>
<td class="mono small">${esc(h.service)}${h.inf?' ('+esc(h.inf)+')':''}</td><td class="mono">g${esc(h.ver)}</td><td class="mono small">${h.added?new Date(h.added*1000).toLocaleString():''}</td></tr>`)).join('');
document.querySelector('#ob_hw tbody').innerHTML=rows||'<tr><td colspan="6" class="mono">none added yet</td></tr>';
// Follow a running job.
clearTimeout(obTimer);
if(jb&&(jb.state==='waiting'||jb.state==='installing')&&pageFromPath()==='diskless')obTimer=setTimeout(()=>loadOnboard().catch(()=>{}),4000);
}
// "Maintain in the browser" (vmmaint.rs): a Windows golden + games disks in a VM on the server, its screen at /vm-console.
let vmTimer=null;
async function loadVm(){
const r=await j('/api/vm').catch(()=>null);const box=document.getElementById('vm_box');if(!r||!box)return;
const s=r.session,run=s&&['starting','running','stopping'].includes(s.state);
const gs=document.getElementById('vm_golden'),keepG=gs.value;
gs.innerHTML=r.goldens.map(g=>`<option value="${esc(g.name)}"${g.name===keepG?' selected':''}>${esc(g.name)}</option>`).join('');
const was=new Set([...document.querySelectorAll('#vm_games input:checked')].map(i=>i.value));
document.getElementById('vm_games').innerHTML=r.games.filter(d=>d.served).map(d=>`<label class="form-check mb-0"><input class="form-check-input" type="checkbox" value="${esc(d.name)}"${was.has(d.name)?' checked':''}> ${esc(d.name)} <span class="mono">(${esc(d.letter)}:)</span></label>`).join('')||'<span class="text-body-secondary">none</span>';
// Shown only once the server has said no VM is running (hidden in the page until then: a second start is refused).
document.getElementById('vm_form').style.setProperty('display',run?'none':'flex','important');
const vc=document.getElementById('vm_cpus'),vg=document.getElementById('vm_gb');
if(vc&&document.activeElement!==vc&&document.activeElement!==vg){vc.value=r.cpus;vc.max=r.max_cpus;vg.value=r.mem_gb;vg.max=r.max_gb;}
const tx=(id,t)=>{const e=document.getElementById(id);if(e)e.textContent=t;};
tx('vm_size_txt',`${r.cpus} CPU threads and ${r.mem_gb} GB of memory`);
tx('vm_size_max',`at most ${r.max_cpus} threads and ${r.max_gb} GB here: 4 threads and 4 GB always stay with the server. Used from the VM's next start.`);
document.getElementById('vm_problem').textContent=r.problem?'Not possible on this server yet: '+r.problem:'';
if(!s){box.innerHTML='';return;}
const cls={running:'text-success',failed:'text-danger',stopped:'text-body-secondary'}[s.state]||'text-primary';
const games=(s.games||[]).map(g=>g.name+' ('+g.letter+':)').join(', ');
let h=`<div><b>${esc(s.golden)}</b> · <span class="${esc(cls)}">${esc(s.state)}</span> · started ${esc(new Date(s.started*1000).toLocaleString())} by ${esc(s.by)}`
+(s.ended?' · ended '+esc(new Date(s.ended*1000).toLocaleString()):'')+`</div><div class="small">games disks: ${esc(games||'none')} · Windows changes ${s.keep?'kept for Save / Discard':'thrown away at the end'}</div>`;
if(s.msg&&!run)h+=`<div class="small">${esc(s.msg)}</div>`;
if(run)h+=`<div class="d-flex flex-wrap gap-2 mt-2"><a class="btn btn-sm btn-primary" data-perm="${s.keep?'maintain':'vm'}" href="/vm-console" target="_blank" rel="noopener">Open the screen</a>
<button class="btn btn-sm btn-outline-warning" data-perm="${s.keep?'maintain':'vm'}" ${act('vmStop',false)}>Shut down</button>
<button class="btn btn-sm btn-outline-danger" data-perm="${s.keep?'maintain':'vm'}" ${act('vmStop',true)} title="like pulling the plug — only if Windows hangs">Turn off</button></div>`;
else{const pend=r.games.filter(d=>(s.games||[]).some(g=>g.name===d.name)&&d.update_bytes>0);
if(pend.length)h+='<div class="d-flex flex-wrap gap-2 align-items-center mt-2 small">'+pend.map(d=>`<span><b>${esc(d.name)}</b>: ${gbs(d.update_bytes)} written</span>
<button class="btn btn-sm btn-outline-primary" data-perm="games" ${act('vmGamesSave',d.name)}>Save</button>
<button class="btn btn-sm btn-outline-secondary" data-perm="games" ${act('vmGamesDiscard',d.name)}>Discard</button>`).join(' ')+'</div>';}
box.innerHTML=h;
clearTimeout(vmTimer);
if(run&&pageFromPath()==='diskless')vmTimer=setTimeout(()=>loadVm().catch(()=>{}),5000);
}
function vmSize(){post('/api/vm/size',{cpus:+v('vm_cpus'),mem_gb:+v('vm_gb')},'vm_msg',()=>'saved — used from the VM\'s next start',loadVm);}
function vmStart(){const games=[...document.querySelectorAll('#vm_games input:checked')].map(i=>i.value),keep=!!document.getElementById('vm_keep')?.checked;
post('/api/vm/start',{golden:v('vm_golden'),games,keep},'vm_msg',()=>'started — Open the screen',loadVm);}
const vmStop=force=>sure(force?'Turn the VM off at once, like pulling the plug? Unsaved work in Windows is lost; use it only if Windows hangs.':'Press the VM\'s power button? Windows shuts down (close what you are doing first).').then(ok=>ok&&post('/api/vm/stop',{force},'vm_msg',r=>r.msg,loadVm));
const vmGamesSave=name=>sure('Save the VM\'s changes to '+name+' as its new version? Each machine gets it at its next boot.').then(ok=>ok&&post('/api/games/save',{name},'vm_msg',r=>r.status||'saved',loadVm));
const vmGamesDiscard=name=>sure('Throw the VM\'s changes to '+name+' away?').then(ok=>ok&&post('/api/games/discard',{name},'vm_msg',()=>'discarded',loadVm));
const startOnboard=()=>post('/api/onboard/start',{golden:v('ob_golden'),machine:+v('ob_machine')},'ob_msg',r=>r.status||'started',loadOnboard);
const retryOnboard=()=>post('/api/onboard/retry',{},'ob_msg',r=>r.status||'running',loadOnboard);
const cancelOnboard=()=>sure('Cancel adding hardware? A running VM is stopped and its work thrown away.').then(ok=>ok&&post('/api/onboard/cancel',{},'ob_msg',()=>'cancelled',loadOnboard));
// The installer ISO choice follows the OS chosen for the golden (Windows by default).
function dlIsoOptions(keep){const sel=document.getElementById('dl_iso');if(!sel||!dlData)return;
const os=document.getElementById('dl_os').value,cur=keep??sel.value;
sel.innerHTML='<option value="">— none —</option>'+dlData.isos.filter(i=>i.os===os).map(i=>
`<option value="${esc(i.name)}"${i.name===cur?' selected':''}>${esc(i.name)}</option>`).join('');}
function dlOsChanged(){const os=document.getElementById('dl_os').value,sz=document.getElementById('dl_size');
if(sz&&(sz.value==='256'||sz.value==='64'))sz.value=os==='linux'?'64':'256';dlIsoOptions('');}
const dlCall=(url,body,q,msg)=>(q?sure(q):Promise.resolve(true)).then(ok=>ok&&post(url,body,msg||'dl_msg',r=>r.status||'saved',loadDiskless));
const saveGolden=()=>dlCall('/api/diskless/golden',{name:v('dl_name'),os:v('dl_os'),size_gb:+v('dl_size'),iso:v('dl_iso')||null,groups:[v('dl_groups')]},null,'dl_fmsg');
const setMaint=(name,el)=>dlCall('/api/diskless/maint',{name,id:el.value?+el.value:null});
const saveMaint=name=>dlCall('/api/diskless/save',{name},'Save the maintenance work on '+name+' as its next version? Its maintenance machine must be shut down. Every machine gets it at its next boot (and sets itself up again).');
const discardMaint=name=>dlCall('/api/diskless/discard',{name},'Throw the maintenance work on '+name+' away?');
const delGolden=name=>dlCall('/api/diskless/golden/delete',{name},'Delete golden '+name+', all its versions and every machine\'s layers of it?');
const isoOs=(name,el)=>dlCall('/api/diskless/iso/os',{name,os:el.value});
const delIso=name=>dlCall('/api/diskless/iso/delete',{name},'Delete '+name+' from the ISO library?');
const setRoom=()=>post('/api/diskless/room',{pc_gb:+v('room_gb'),free_pct:+v('room_pct'),pagefile_gb:+v('room_pf')},'room_msg',()=>'saved - machines get it at their next start',loadDiskless);
const setPlace=k=>dlCall('/api/diskless/volume',{kind:k,id:document.getElementById('dl_place_'+k).value},null,'dl_pmsg');
function editGolden(name){const g=dlData.goldens.find(x=>x.golden.name===name).golden;
for(const [k,val] of [['dl_name',g.name],['dl_os',g.os],['dl_size',g.size_gb],['dl_groups',g.groups.join(', ')]])document.getElementById(k).value=val;
dlIsoOptions(g.iso||'');}
// ISO upload: same chunked protocol as goldens / drivers, with the OS chosen (Windows by default).
async function uploadIso(){const f=document.getElementById('dl_isofile').files[0],msg=document.getElementById('dl_isomsg');
if(!f){toast('Choose an installer .iso',true);return;}
const name=f.name.replace(/[^A-Za-z0-9._-]/g,'-'),os=v('dl_isoos'),prog=document.getElementById('dl_isoprog');
try{await j('/api/diskless/iso/upload-start',mk({name,os}));
const q=[];for(let o=0;o===0||o<f.size;o+=CHUNK)q.push(o);
const t0=Date.now();let done=0;const bar=prog.firstElementChild;prog.style.display='';bar.style.width='0%';
const file=new File([f],name);
const lane=async()=>{while(q.length){const o=q.shift();
try{await putChunk('/api/diskless/iso',name,file,o);}catch(e){q.length=0;throw e;}
done+=Math.min(CHUNK,f.size-o);bar.style.width=done/f.size*100+'%';
msg.textContent=' upload '+Math.round(done/f.size*100)+'% · '+(done/1e6/Math.max(1,(Date.now()-t0)/1000)).toFixed(0)+' MB/s';}};
await Promise.all(Array.from({length:LANES},lane));
await j('/api/diskless/iso/upload-done',mk({name,os}));say(msg,' ✓ '+name+' added');}
catch(e){say(msg,' ✗ '+e.message);}
loadDiskless();}
// ---- groups (Groups page) ----
let grData=null,grCur=null;
const grIn=(x,n)=>x.groups.some(y=>y.toLowerCase()===n.toLowerCase());
// One name a line when there are several: a long list in one line pushed the table (and its buttons) off the page.
const grNames=l=>l.length?(l.length>2?'<div class="small" style="white-space:normal;min-width:12rem;max-height:6.5em;overflow:auto">':'<div>')+l.map(x=>esc(x.name)+(x.letter?' ('+esc(x.letter)+':)':'')).join(l.length>2?'<br>':', ')+'</div>':'—';
async function loadGroups(){const g=grData=await j('/api/groups');const tb=document.querySelector('#groups tbody');if(!tb)return;
const cnt=n=>g.machines.filter(m=>(m.grp||'').toLowerCase()===n.toLowerCase()).length;
const nm=(g.new_machines||'').toLowerCase(),nmEl=document.getElementById('gr_newm');
if(nmEl&&document.activeElement!==nmEl)nmEl.value=g.new_machines||'';
const gl=document.getElementById('gr_grouplist');if(gl)gl.innerHTML=g.groups.map(x=>`<option value="${esc(x)}">`).join('');
tb.innerHTML=g.groups.map(n=>`<tr><td><b>${esc(n)}</b>${n.toLowerCase()===nm?' <span class="badge text-bg-warning" title="unknown PCs join it by themselves and wait here for you">new machines</span>':''}</td><td class="mono">${cnt(n)}</td>
<td>${grNames(g.images.filter(x=>grIn(x,n)))}</td><td>${grNames(g.disks.filter(x=>grIn(x,n)))}</td><td>${grNames((g.goldens||[]).filter(x=>grIn(x,n)))}</td><td>${grNames(g.drivers.filter(x=>grIn(x,n)))}</td>
<td><div class="d-flex gap-1"><button class="btn btn-sm btn-outline-secondary" ${act('editGroup',n)}>Edit</button> <button class="btn btn-sm btn-outline-secondary" ${act('renameGroup',n)}>Rename</button> <button class="btn btn-sm btn-outline-danger" ${act('deleteGroup',n)}>Delete</button></div></td></tr>`).join('')
+`<tr><td><i>every machine</i></td><td class="mono" title="machines without a group">${g.machines.filter(m=>!m.grp).length} without</td>
<td>${grNames(g.images.filter(x=>!x.groups.length))}</td><td>${grNames(g.disks.filter(x=>!x.groups.length))}</td><td title="a golden boots only on the machines of its groups">—</td><td>${grNames(g.drivers.filter(x=>x.all))}</td><td></td></tr>`;
if(grCur&&g.groups.some(n=>n===grCur))editGroup(grCur);else closeGroup();}
function editGroup(n){if(!grData)return;grCur=n;const g=grData,lc=n.toLowerCase();
document.getElementById('gr_edit').style.display='';document.getElementById('gr_title').textContent='Group '+n;document.getElementById('gr_emsg').textContent='';
const box=(v,on,label,off,title)=>`<label class="form-check text-nowrap"${title?` title="${esc(title)}"`:''}><input class="form-check-input" type="checkbox" data-v="${esc(String(v))}"${on?' checked':''}${off?' disabled':''}> ${label}</label>`;
const all=' <span class="mono">(every machine)</span>',allT='for every machine — limit it to groups on its own page';
document.getElementById('gr_machines').innerHTML=g.machines.map(m=>box(m.id,(m.grp||'').toLowerCase()===lc,
esc(m.name)+(m.grp&&m.grp.toLowerCase()!==lc?` <span class="mono">(${esc(m.grp)})</span>`:''))).join('')||'—';
document.getElementById('gr_images').innerHTML=g.images.map(i=>box(i.id,!i.groups.length||grIn(i,n),
esc(i.name)+' '+osPill(i.os)+(i.groups.length?'':all),!i.groups.length,i.groups.length?'':allT)).join('')||'—';
// Each with the version this group boots (pins.rs): newest, or held on a kept one.
const pick=(key,d)=>`<select class="form-select form-select-sm w-auto py-0" data-ver="${esc(key)}" title="the version this group's machines boot">${verOpts(d.versions||[],pinOf(d.pins,n))}</select>`;
document.getElementById('gr_disks').innerHTML=g.disks.map(d=>'<div class="d-flex align-items-center gap-2">'+box(d.name,!d.groups.length||grIn(d,n),
esc(d.name)+' ('+esc(d.letter)+':)'+(d.groups.length?'':all),!d.groups.length,d.groups.length?'':allT)+pick('d'+d.name,d)+'</div>').join('')||'—';
document.getElementById('gr_goldens').innerHTML=(g.goldens||[]).map(d=>'<div class="d-flex align-items-center gap-2">'+box(d.name,grIn(d,n),esc(d.name)+' '+osPill(d.os))+pick('g'+d.name,d)+'</div>').join('')||'—';
document.getElementById('gr_drivers').innerHTML=g.drivers.map(d=>box(d.id,d.all||grIn(d,n),
esc(d.name)+(d.all?all:''),d.all,d.all?'for every machine — change it on the Drivers page':'')).join('')||'—';
document.getElementById('gr_hide').checked=(g.hide_disks||[]).some(x=>x.toLowerCase()===lc);}
function closeGroup(){grCur=null;const e=document.getElementById('gr_edit');if(e)e.style.display='none';}
async function saveGroup(){const m=document.getElementById('gr_emsg');
const ids=(id,num)=>[...document.querySelectorAll('#'+id+' input:checked:not(:disabled)')].map(e=>num?+e.dataset.v:e.dataset.v);
const versions=Object.fromEntries([...document.querySelectorAll('#gr_edit select[data-ver]')].map(e=>[e.dataset.ver,e.value===''?null:+e.value]));
const body={name:grCur,machines:ids('gr_machines',1),images:ids('gr_images',1),disks:ids('gr_disks',0),goldens:ids('gr_goldens',0),drivers:ids('gr_drivers',1),hide_disks:document.getElementById('gr_hide').checked,versions};
try{await j('/api/groups/set',mk(body));say(m,' ✓ saved');}
catch(e){if(/EVERY machine/.test(e.message)&&await sure(e.message+'.\n\nSave anyway?')){
try{await j('/api/groups/set',mk({...body,allow_all:true}));say(m,' ✓ saved');}catch(e2){say(m,' ✗ '+e2.message);}}
else say(m,' ✗ '+e.message);}
loadGroups();}
const grCall=(url,body,el)=>post(url,body,el||'gr_msg','done',loadGroups);
const saveNewMachines=()=>post('/api/groups/new-machines',{group:v('gr_newm')},'gr_nmsg',()=>v('gr_newm')?'unknown PCs join '+v('gr_newm'):'off',loadGroups);
const createGroup=()=>{const n=v('gr_new');if(n)grCall('/api/groups/create',{name:n}).then(()=>{document.getElementById('gr_new').value='';});};
async function renameGroup(n){const to=await ask('Rename group '+n,'New name (an existing name merges the two):',n);
if(to&&to!==n){if(grCur===n)grCur=to.trim();grCall('/api/groups/rename',{from:n,to});}}
async function deleteGroup(n){if(await sure('Delete group '+n+'? Its machines get no group; it leaves every image, iSCSI disk and driver package.'))grCall('/api/groups/delete',{name:n});}
// ---- system ----
async function loadCafe(){
j('/api/config').then(c=>{const t=document.getElementById('timeout'),z=document.getElementById('zram_reserve');
if(t)t.value=c.boot_timeout;if(z)z.value=c.zram_reserve_mb;
const al=document.getElementById('alert_pct');if(al)al.value=c.alert_pct;
for(const k of ['room','warn','reboot']){const e=document.getElementById('ssd_'+k);if(e)e.value=c['ssd_'+k+'_gb'];}
const pf=document.getElementById('ssd_pagefile');if(pf)pf.value=c.pagefile_gb;
showGoldenStreams(c);}).catch(()=>{});
const c=await j('/api/cafe-user');
const u=document.getElementById('cafe_user');if(u)u.value=c.user||'';
const p=document.getElementById('cafe_password');if(p)p.placeholder=c.password_set?'(unchanged)':'set a password';}
async function saveCafe(){const m=document.getElementById('cafe_msg');
// Password is write-only: send it only when the field is filled (blank = keep the stored one).
const body={user:v('cafe_user')},pw=document.getElementById('cafe_password').value;if(pw)body.password=pw;
try{await j('/api/cafe-user',mk(body));say(m,' ✓ saved');document.getElementById('cafe_password').value='';loadCafe();}
catch(e){say(m,' ✗ '+e.message);}}
async function savePw(){const m=document.getElementById('pw_msg'),g=id=>document.getElementById(id).value;
if(g('pw_new').length<8){say(m,' ✗ at least 8 characters');return;}
if(g('pw_new')!==g('pw_new2')){say(m,' ✗ passwords do not match');return;}
// A fresh handshake with the current password; the new one goes as its verifier sealed with that handshake's key.
say(m,' …');
try{if(!me.user)await loadMe();
const h=await handshake(me.user,g('pw_cur'));
const r=await j('/api/password',mk({hs:h.hs,proof:h.proof,cred:sealPw(h.sk,me.user,g('pw_new'))}));
if(!h.serverOk(r.server_proof))throw new Error('the server could not prove it holds this account');
keepSk(h.sk);say(m,' ✓ changed, your other sessions signed out');
for(const id of ['pw_cur','pw_new','pw_new2'])document.getElementById(id).value='';}
catch(e){say(m,' ✗ '+e.message);}}
// Another account's new password, sealed with this session's key (crypto.js): it never crosses the network.
function sealFor(name,pw){if(pw.length<8)throw new Error('password: at least 8 characters');
const sk=sessionSk();if(!sk)throw new Error('sign out and in again: this browser has no key to seal passwords with');
return sealPw(sk,name,pw);}
// ---- accounts (users.rs; owners): who is signed in decides what the page shows (body[data-role]) ----
let me={};
async function loadMe(){try{me=await j('/api/auth/status');}catch(e){return;}
// owner / the role's name: CSS shows the owner's controls to the owner, and each [data-perm] to who has it.
document.body.dataset.role=me.role||'';
document.body.dataset.perms=(me.perms||[]).join(' ');
const w=document.getElementById('whoami');if(w)w.textContent=me.user?`${me.user} · ${me.role}`:'';}
async function loadUsers(){if(!me.role)await loadMe();
const tb=document.querySelector('#users tbody');if(!tb||me.role!=='owner')return;
const [u,ro]=await Promise.all([j('/api/users'),j('/api/roles')]);
const names=ro.roles.map(r=>r.name);
tb.innerHTML=u.map(x=>`<tr><td class="mono">${esc(x.name)}${x.name===me.user?' <span class="badge text-bg-secondary">you</span>':''}</td>
<td><select class="form-select form-select-sm w-auto" data-change="userRole" data-args="${esc(JSON.stringify([x.name]))}">${[...new Set([...names,x.role])].map(r=>`<option${r===x.role?' selected':''}>${esc(r)}</option>`).join('')}</select>${names.includes(x.role)?'':' <span class="text-danger small">this role no longer exists: the account can only look</span>'}</td>
<td><div class="d-flex gap-1">${x.name===me.user?'':`<button class="btn btn-sm btn-outline-secondary" ${act('userPw',x.name,x.role)}>New password</button> <button class="btn btn-sm btn-outline-danger" ${act('userDel',x.name)}>Delete</button>`}</div></td></tr>`).join('');
const sel=document.getElementById('us_role');
if(sel){const keep=sel.value||'staff';sel.innerHTML=[...names].reverse().map(r=>`<option${r===keep?' selected':''}>${esc(r)}</option>`).join('');}
renderRoles(ro);}
// Roles (users.rs): owner and staff are fixed; the others are a name + ticked permissions. Saved per role.
function renderRoles(ro){const tb=document.querySelector('#roles tbody');if(!tb)return;
tb.innerHTML=ro.roles.map(r=>`<tr data-role-row="${esc(r.name)}"><td class="mono">${esc(r.name)}${r.builtin?' <span class="badge text-bg-secondary">built in</span>':''}</td>
<td>${r.name==='owner'?'<span class="small text-body-secondary">everything</span>':ro.perms.map(p=>`<label class="form-check form-check-inline mb-0 small" title="${esc(p.text)}"><input class="form-check-input" type="checkbox" value="${esc(p.id)}"${r.perms.includes(p.id)?' checked':''}${r.builtin?' disabled':''}> ${esc(p.id)}</label>`).join('')}</td><td class="small">${esc(r.users)}</td>
<td>${r.builtin?'':`<div class="d-flex gap-1"><button class="btn btn-sm btn-outline-primary" ${act('roleSave',r.name)}>Save</button> <button class="btn btn-sm btn-outline-danger" ${act('roleDel',r.name)}>Delete</button></div>`}</td></tr>`).join('');
const lg=document.getElementById('role_legend');if(lg)lg.innerHTML=ro.perms.map(p=>`<b>${esc(p.id)}</b>: ${esc(p.text)}`).join(' · ');}
const rolePerms=name=>[...document.querySelectorAll(`#roles tr[data-role-row="${CSS.escape(name)}"] input:checked`)].map(i=>i.value);
const roleSave=name=>post('/api/roles',{name,perms:rolePerms(name)},'ro_msg',()=>name+' saved - counts from their next click',loadUsers);
const roleAdd=()=>post('/api/roles',{name:v('ro_name').toLowerCase(),perms:[]},'ro_msg',()=>'added - tick what it may do, then Save',loadUsers);
async function roleDel(name){if(await sure('Delete role '+name+'?'))post('/api/roles/delete',{name},'ro_msg',()=>name+' deleted',loadUsers);}
const userCall=(url,body,done)=>post(url,body,'us_msg',done,loadUsers);
async function userAdd(){const name=v('us_name').toLowerCase(),p=document.getElementById('us_pw');let cred;
try{cred=sealFor(name,p.value);}catch(e){say(document.getElementById('us_msg'),' ✗ '+e.message);return;}
await userCall('/api/users',{name,role:document.getElementById('us_role').value,cred},name+' saved');p.value='';}
const userRole=(name,el)=>userCall('/api/users',{name,role:el.value},name+' is now '+el.value);
async function userPw(name,role){const p=await ask('New password for '+name,'At least 8 characters. Their open sessions end.','');
if(p==null)return;let cred;
try{cred=sealFor(name,p);}catch(e){say(document.getElementById('us_msg'),' ✗ '+e.message);return;}
userCall('/api/users',{name,role,cred},'password of '+name+' changed');}
async function userDel(name){if(await sure('Delete account '+name+'? Their open sessions end.'))userCall('/api/users/delete',{name},name+' deleted');}
// ---- HTTPS for the web admin (tls.rs; owners) ----
const fpFmt=h=>(h||'').toUpperCase().match(/../g)?.join(':')||'';
async function loadTls(){if(!me.role)await loadMe();const el=document.getElementById('tls_info');if(!el||me.role!=='owner')return;
let t;try{t=await j('/api/config/tls');}catch(e){say(el,' ✗ '+e.message);return;}
document.getElementById('tls_on').checked=!!t.https;
const until=t.until?new Date(t.until*1000):null,soon=until&&until-Date.now()<30*86400e3;
el.innerHTML=(t.listening?`${ic('ok')} HTTPS on port ${esc(t.port)}`:`${ic('err')} HTTPS is not running (port ${esc(t.port)} taken? see Logs) — the web admin is on plain HTTP`)
+`<br>${t.kind==='uploaded'?'Your certificate':'Self-signed certificate'+(t.names?.length?' for '+esc(t.names.join(', ')):'')}`
+(until?` · valid until <span class="${soon?'text-danger fw-semibold':''}">${esc(until.toLocaleDateString())}</span>${t.kind!=='uploaded'?' (renewed by itself)':''}`:'')
+`<br>SHA-256 <span class="mono" style="word-break:break-all">${esc(fpFmt(t.sha256))}</span>`;}
async function tlsOn(el){const on=el.checked;
if(!on&&!await sure('Serve the web admin on plain http:// too? Passwords never travel, but the session and every page do, readable on the network.')){el.checked=true;return;}
await post('/api/config/tls',{https:on},'tls_msg',on?'HTTPS only':'plain HTTP too',loadTls);}
async function tlsUpload(){const m=document.getElementById('tls_msg'),c=document.getElementById('tls_cert').files[0],k=document.getElementById('tls_key').files[0];
if(!c||!k){say(m,' ✗ pick the certificate and its key');return;}
if(!await sure('Use this certificate now? Open the page by a name it was made for, or browsers will warn.'))return;
await post('/api/config/tls',{cert:await c.text(),key:await k.text()},'tls_msg','in use — reload the page',loadTls);
document.getElementById('tls_key').value='';}
async function tlsSelfSigned(){if(!await sure('Make a new self-signed certificate? Browsers warn again once (new fingerprint).'))return;
await post('/api/config/tls',{self_signed:true},'tls_msg','new certificate in use — reload the page',loadTls);}
async function setTimeout_(){await j('/api/config/timeout',mk({seconds:+document.getElementById('timeout').value}));say(document.getElementById('to_msg'),' ✓ saved');}
async function setSsd(){const m=document.getElementById('ssd_msg');
try{await j('/api/config/ssd',mk({room_gb:+v('ssd_room'),warn_gb:+v('ssd_warn'),reboot_gb:+v('ssd_reboot'),pagefile_gb:+v('ssd_pagefile')}));say(m,' ✓ saved');}
catch(e){say(m,' ✗ '+e.message);}}
// Golden download queue: "auto" (number from the link speed) or the admin's number; 0 = no queue.
let gsAuto=8;
function showGoldenStreams(c){const sel=document.getElementById('gs_mode');if(!sel)return;
gsAuto=c.golden_streams_auto;const auto=c.golden_streams==='auto';sel.value=auto?'auto':'manual';
document.getElementById('gs_n').value=auto?gsAuto:c.golden_streams;gsMode();
document.getElementById('gs_info').textContent=`Auto: ${gsAuto} (${c.golden_link||'no network interface found'}). Now: ${c.golden_active} downloading, ${c.golden_waiting} waiting.`;}
function gsMode(){const auto=document.getElementById('gs_mode').value==='auto',n=document.getElementById('gs_n');
n.disabled=auto;if(auto)n.value=gsAuto;}
async function setGoldenStreams(){const m=document.getElementById('gs_msg');
const value=document.getElementById('gs_mode').value==='auto'?'auto':String(+v('gs_n'));
try{await j('/api/config/golden-streams',mk({value}));say(m,' ✓ saved');}catch(e){say(m,' ✗ '+e.message);}}
async function setAlert(){const m=document.getElementById('al_msg');
try{await j('/api/config/alert',mk({pct:+document.getElementById('alert_pct').value}));say(m,' ✓ saved');}catch(e){say(m,' ✗ '+e.message);}}
async function setZramReserve(){await j('/api/config/zram-reserve',mk({mb:+document.getElementById('zram_reserve').value}));say(document.getElementById('zr_msg'),' ✓ saved');}
// ---- logs (logbuf.rs): the server's newest log lines; Follow asks for the new ones every 3 s ----
let lgLast=0,lgTimer=null,lgSearch=null;
async function loadLogs(){logsReload();logsFollow();}
function logsReload(){lgLast=0;const o=document.getElementById('lg_out');if(o)o.textContent='';logsFetch(true);}
function logsSearch(){clearTimeout(lgSearch);lgSearch=setTimeout(logsReload,300);}
async function logsFetch(first){const o=document.getElementById('lg_out');if(!o)return;
const p=new URLSearchParams({after:lgLast,level:document.getElementById('lg_level').value,q:document.getElementById('lg_q').value.trim()});
let r;try{r=await j('/api/logs?'+p);}catch(e){return;}
const atEnd=o.scrollTop+o.clientHeight>=o.scrollHeight-20;
for(const l of r.lines){const s=document.createElement('span');s.textContent=l.text+'\n';
const head=l.text.slice(0,48);if(head.includes(' ERROR '))s.className='text-danger';else if(head.includes(' WARN '))s.className='text-warning';
o.appendChild(s);}
while(o.childNodes.length>2000)o.removeChild(o.firstChild); // the page holds no more than the server keeps
lgLast=r.last;
document.getElementById('lg_n').textContent=o.childNodes.length+' lines';
if(first||atEnd)o.scrollTop=o.scrollHeight;} // stay at the bottom unless the reader scrolled up
function logsFollow(){clearInterval(lgTimer);lgTimer=null;
if(!document.getElementById('lg_follow')?.checked)return;
lgTimer=setInterval(()=>{if(curPage!=='logs'){clearInterval(lgTimer);lgTimer=null;return;}if(!document.hidden)logsFetch(false);},3000);}
// ---- remote (vnc.rs): watch + control a Windows machine over a WebSocket ----
let rmWs=null, rmCtx=null, rmW=0, rmH=0;
const rmProto=()=>location.protocol==='https:'?'wss:':'ws:';
async function loadRemote(){
try{rmAgents=await j('/api/vnc/agents');}catch(e){rmAgents=[];}
document.getElementById('rm_hosts').innerHTML=rmAgents.map(a=>`<option value="${esc(a.host)}">`).join('');
// Group filter: the groups of the machines with an agent (the choice is kept when still there).
const gs=document.getElementById('rm_wgrp'),cur=gs.value||rmWallGrp;
const groups=[...new Set(rmAgents.map(a=>a.grp).filter(Boolean))].sort();
gs.innerHTML='<option value="">All</option>'+groups.map(g=>`<option value="${esc(g)}">${esc(g)}</option>`).join('');
gs.value=groups.includes(cur)?cur:'';rmWallGrp=gs.value;
rmWallSizeInit();
const q=new URLSearchParams(location.search).get('host');
if(q){openOne(q);}else{showWall();}
}
// Wall: a low-rate thumbnail of every machine running the agent, page by page; click one to control it. Only the
// screens of the page shown are streamed: leaving a page closes its sockets, so those agents stop capturing and the
// browser decodes nothing for them. A hidden tab streams nothing either.
let rmAgents=[], rmWall=[], rmWallPg=1, rmWallGrp='', rmWallN=12, rmWallGen=0;
function closeWall(){rmWallGen++;for(const w of rmWall){w.onclose=null;w.onerror=null;w.close();}rmWall=[];}
const rmWallList=()=>rmAgents.filter(a=>!rmWallGrp||a.grp===rmWallGrp);
function rmWallSizeInit(){
let n=12;try{n=+localStorage.getItem('rm_wall_n')||12;}catch(e){}
rmWallN=Math.min(200,Math.max(1,n));
const sel=document.getElementById('rm_wsize'),inp=document.getElementById('rm_wsize_n');
const preset=[...sel.options].some(o=>o.value===String(rmWallN));
sel.value=preset?String(rmWallN):'custom';inp.value=rmWallN;inp.classList.toggle('d-none',preset);
}
function rmWallSize(){
const sel=document.getElementById('rm_wsize'),inp=document.getElementById('rm_wsize_n');
const custom=sel.value==='custom';inp.classList.toggle('d-none',!custom);
const n=Math.min(200,Math.max(1,Math.round(+(custom?inp.value:sel.value)||12)));
if(custom)inp.value=n;
rmWallN=n;try{localStorage.setItem('rm_wall_n',String(n));}catch(e){}
rmWallPg=1;showWall();
}
function rmWallFilter(){rmWallGrp=document.getElementById('rm_wgrp').value;rmWallPg=1;showWall();}
function rmWallPrev(){if(rmWallPg>1){rmWallPg--;showWall();}}
function rmWallNext(){if(rmWallPg<Math.ceil(rmWallList().length/rmWallN)){rmWallPg++;showWall();}}
function showWall(){
closeWall();rmClose(true);
document.getElementById('rm_one').classList.add('d-none');
document.getElementById('rm_wall_box').classList.remove('d-none');
const list=rmWallList(),pages=Math.max(1,Math.ceil(list.length/rmWallN));
if(rmWallPg>pages)rmWallPg=pages;
const first=(rmWallPg-1)*rmWallN,page=list.slice(first,first+rmWallN);
document.getElementById('rm_wall_n').textContent=rmAgents.length?'('+rmAgents.length+')':'';
document.getElementById('rm_wall_empty').classList.toggle('d-none',rmAgents.length>0);
document.getElementById('rm_wpage').textContent=list.length?`${first+1}–${first+page.length} of ${list.length}`:'0';
const grid=document.getElementById('rm_wall');grid.innerHTML='';
if(document.hidden)return; // nothing streams in a hidden tab; shown again → visibilitychange redraws
const gen=rmWallGen;
for(const a of page){
const fig=document.createElement('figure');fig.className='m-0 text-center position-relative';fig.style.cursor='pointer';
fig.title='Control '+a.host;fig.onclick=()=>remoteTo(a.host);
const cv=document.createElement('canvas');cv.width=320;cv.height=180;
cv.className='border rounded';cv.style.width='320px';cv.style.maxWidth='100%';cv.style.height='auto';cv.style.background='#111';
const note=document.createElement('div');note.className='position-absolute top-50 start-50 translate-middle small text-white d-none';
note.textContent='in use';
const cap=document.createElement('figcaption');cap.className='small mono mt-1';cap.textContent=a.host;
fig.append(cv,note,cap);grid.appendChild(fig);
if(a.busy)wallBusy(a.host,cv,note,gen);else wallTile(a.host,cv,note,gen);
}
}
// Someone controls this machine (or it refused the thumbnail): say so, try again in 15 s while this page is shown.
function wallBusy(host,cv,note,gen){note.classList.remove('d-none');
setTimeout(()=>{if(gen===rmWallGen&&!document.hidden){note.classList.add('d-none');wallTile(host,cv,note,gen);}},15000);}
// One thumbnail stream: low quality, scaled to 320px wide, acked slowly so it refreshes ~1/s.
function wallTile(host,cv,note,gen){
const ws=new WebSocket(`${rmProto()}//${location.host}/api/vnc/view?host=${encodeURIComponent(host)}&q=35&w=320`);
ws.binaryType='arraybuffer';rmWall.push(ws);
let ctx=null;
ws.onmessage=ev=>{const b=new Uint8Array(ev.data);
if(b[0]===2){cv.width=b[1]|b[2]<<8;cv.height=b[3]|b[4]<<8;ctx=cv.getContext('2d');return;}
if(b[0]===1&&ctx){const x=b[1]|b[2]<<8,y=b[3]|b[4]<<8;
createImageBitmap(new Blob([b.subarray(9)],{type:'image/jpeg'})).then(bm=>{ctx.drawImage(bm,x,y);bm.close();
setTimeout(()=>{if(ws.readyState===1)ws.send('{"op":"ack"}');},1000);}).catch(()=>{});}};
// Refused (in use) or taken over by a control view: still on this page → "in use" and retry.
ws.onclose=()=>{rmWall=rmWall.filter(x=>x!==ws);if(gen===rmWallGen)wallBusy(host,cv,note,gen);};
ws.onerror=()=>{};
}
// Hidden tab: close the wall's streams; shown again: reopen the current page.
document.addEventListener('visibilitychange',()=>{
if(curPage!=='remote'||document.getElementById('rm_wall_box')?.classList.contains('d-none'))return;
if(document.hidden)closeWall();else showWall();
});
// Machines page or a wall tile: open the Remote page controlling this host.
function remoteTo(host){history.pushState(null,'','/remote?host='+encodeURIComponent(host||''));showPage('remote');}
function openOne(host){
closeWall();
document.getElementById('rm_wall_box').classList.add('d-none');
document.getElementById('rm_one').classList.remove('d-none');
document.getElementById('rm_host').value=host;
rmConnect();
}
function rmBack(){history.pushState(null,'','/remote');loadRemote();}
// Wall: open a machine by its hostname (typed or picked from the list).
function rmGo(){const h=v('rm_go');if(h)remoteTo(h);}
function rmSay(m){const e=document.getElementById('rm_msg');if(e)say(e,m);}
function rmClose(silent){if(rmWs){rmWs.onclose=null;rmWs.close();rmWs=null;}rmCtx=null;rmCloseDec();
const c=document.getElementById('rm_canvas');if(c)c.classList.add('d-none');if(!silent)rmSay(' disconnected');}
let rmDec=null; // WebCodecs VideoDecoder while the agent streams H.264
function rmCloseDec(){if(rmDec){try{rmDec.close();}catch(e){}rmDec=null;}}
function rmConnect(){
rmClose(true);
const host=document.getElementById('rm_host').value.trim();if(!host){rmSay(' ✗ enter a hostname');return;}
const q=+document.getElementById('rm_q').value;
// Ask for H.264 when the browser can decode it (Chrome/Edge): smooth, catches full-screen games well. The agent
// falls back to JPEG (and says so with a codec message) if it cannot encode.
const h264='VideoDecoder'in window;
const ws=new WebSocket(`${rmProto()}//${location.host}/api/vnc/view?host=${encodeURIComponent(host)}&q=${q}${h264?'&codec=h264':''}`);
ws.binaryType='arraybuffer';rmWs=ws;rmSay(' connecting…');
const canvas=document.getElementById('rm_canvas');canvas.style.cursor='';
const ack=()=>{if(rmWs===ws&&ws.readyState===1)ws.send('{"op":"ack"}');};
ws.onopen=()=>{rmSay(' connected');canvas.classList.remove('d-none');canvas.focus();};
ws.onclose=()=>{if(rmWs===ws){rmWs=null;rmCloseDec();rmSay(' ✗ connection closed (agent gone or another viewer took over)');canvas.classList.add('d-none');}};
ws.onerror=()=>rmSay(' ✗ could not connect (no agent for that host?)');
ws.onmessage=ev=>{
const b=new Uint8Array(ev.data);
if(b[0]===2){rmW=b[1]|b[2]<<8;rmH=b[3]|b[4]<<8;canvas.width=rmW;canvas.height=rmH;rmCtx=canvas.getContext('2d');return;}
if(b[0]===5){ // the machine's cursor shape: drawn by the browser, so it follows the mouse at once (no second,
// lagging cursor in the picture). [5] alone = hidden (a game hides it).
if(b.length<6){canvas.style.cursor='none';return;}
const hx=b[1]|b[2]<<8,hy=b[3]|b[4]<<8;let s='';for(const c of b.subarray(5))s+=String.fromCharCode(c);
canvas.style.cursor=`url(data:image/png;base64,${btoa(s)}) ${hx} ${hy}, default`;return;}
if(b[0]===4){ // codec decision: 0 JPEG, 1 H.264
rmCloseDec();
if(b[1]===1){rmDec=new VideoDecoder({
output:f=>{if(rmCtx)rmCtx.drawImage(f,0,0);f.close();ack();},
error:e=>{rmSay(' ✗ decode error: '+e.message);rmCloseDec();}});
rmDec.configure({codec:'avc1.42E01E',optimizeForLatency:true});}
return;}
if(b[0]===3&&rmDec){ // H.264 access unit, b[1] bit0 = keyframe
try{rmDec.decode(new EncodedVideoChunk({type:(b[1]&1)?'key':'delta',timestamp:performance.now()*1000,data:b.subarray(2)}));}
catch(e){ack();}
return;}
if(b[0]===1&&rmCtx){
const x=b[1]|b[2]<<8,y=b[3]|b[4]<<8;
createImageBitmap(new Blob([b.subarray(9)],{type:'image/jpeg'})).then(bm=>{
if(rmCtx)rmCtx.drawImage(bm,x,y);bm.close();ack();
}).catch(ack);
}
};
// Mouse: canvas pixels -> screen pixels (canvas is scaled by CSS). b = button bitmask.
const send=o=>{if(rmWs===ws&&ws.readyState===1)ws.send(JSON.stringify(o));};
const pos=e=>{const r=canvas.getBoundingClientRect();
return[Math.round((e.clientX-r.left)/r.width*rmW),Math.round((e.clientY-r.top)/r.height*rmH)];};
const btn=e=>(e.buttons&1)|(e.buttons&2)|(e.buttons&4); // same bit layout as Windows
canvas.onmousemove=e=>{const[x,y]=pos(e);send({op:'mouse',x,y,b:btn(e)});};
canvas.onmousedown=e=>{e.preventDefault();canvas.focus();const[x,y]=pos(e);send({op:'mouse',x,y,b:btn(e)});};
canvas.onmouseup=e=>{const[x,y]=pos(e);send({op:'mouse',x,y,b:btn(e)});};
canvas.oncontextmenu=e=>e.preventDefault();
canvas.onwheel=e=>{e.preventDefault();send({op:'wheel',d:e.deltaY<0?1:-1});};
// Keys held now: the page losing focus (Win / Alt+Tab on this PC, another tab) never sees their keyup → released
// here, or the machine keeps e.g. the Windows key pressed.
const held=new Set();
canvas.onkeydown=e=>{e.preventDefault();held.add(e.code);send({op:'key',code:e.code,down:true});};
canvas.onkeyup=e=>{e.preventDefault();held.delete(e.code);send({op:'key',code:e.code,down:false});};
canvas.onblur=()=>{for(const c of held)send({op:'key',code:c,down:false});held.clear();};
}
function rmSend(o){if(rmWs&&rmWs.readyState===1)rmWs.send(JSON.stringify(o));}
function rmQuality(){rmSend({op:'start',q:+document.getElementById('rm_q').value});}
// Send a chord: press the codes in order, release in reverse. The buttons work even when the browser won't let the
// page capture these keys from the physical keyboard (only full screen does).
function rmChord(codes){for(const c of codes)rmSend({op:'key',code:c,down:true});
for(let i=codes.length-1;i>=0;i--)rmSend({op:'key',code:codes[i],down:false});}
function rmSas(){rmSend({op:'sas'});}
function rmWin(){rmChord(['MetaLeft']);}
function rmAltTab(){rmChord(['AltLeft','Tab']);}
async function rmPaste(){
let text='';
try{text=await navigator.clipboard.readText();} // needs a secure context; LAN http usually lacks it
catch(e){text=prompt('Clipboard not available in the browser here. Paste the text to send:')||'';}
if(!text)return;
rmSend({op:'clip',text}); // set the machine's clipboard, then Ctrl+V on it
setTimeout(()=>rmChord(['ControlLeft','KeyV']),150);
rmSay(' pasted '+text.length+' chars');
}
function rmFull(){const c=document.getElementById('rm_canvas');if(!c)return;
if(c.requestFullscreen)c.requestFullscreen().then(()=>{if(navigator.keyboard&&navigator.keyboard.lock)navigator.keyboard.lock();}).catch(()=>{});}
// Leaving the Remote page closes the socket (showPage calls this via the leave hook below).
// ---- server liveness (SSE /api/events) ----
// Any event counts: "ping" comes on connect and only after 5 s without other events (axum's keep-alive restarts
// its timer on every event, and "res" arrives every 2 s). Red on connection error or nothing for 12 s (e.g. server
// powered off, half-open link). EventSource reconnects by itself → green again on the next event.
let lastPing=0;
const alive=()=>{lastPing=Date.now();setServer(true);};
function setServer(up){const d=document.getElementById('srvdot');
d.classList.toggle('up',up);d.classList.toggle('down',!up);d.title=up?'server online':'server offline';}
// ---- benchmark (bench.rs) ----
const BENCH_KINDS={srv_seq:['Server sequential','MB/s'],srv_rand:['Server random 4K','IOPS'],disk:['Disk read','MB/s'],net:['Network','MB/s'],boot:['Boot','s']};
let benchData=null,iscsiES=null;
const bName=mac=>{if(!mac)return 'server';const m=benchData&&benchData.machines.find(x=>x.mac===mac);return (m&&m.hostname)||mac;};
const bVal=r=>r?`${r.value.toLocaleString()} ${BENCH_KINDS[r.kind]?BENCH_KINDS[r.kind][1]:''}`:'—';
// Value + a bar against the best machine (higher = better: disk, network).
const bBar=(r,max)=>r?`${esc(bVal(r))}<div class="progress d-inline-flex align-middle ms-2" style="width:70px;height:6px"><div class="progress-bar" style="width:${Math.round(100*r.value/max)}%"></div></div>`:'—';
const bTime=t=>new Date(t*1000).toLocaleString();
async function loadBench(){
const d=benchData=await j('/api/bench');
const seq=document.getElementById('b_seq');if(!seq)return;
const last=(mac,k)=>d.latest.find(r=>r.mac===mac&&r.kind===k);
seq.textContent=bVal(last(null,'srv_seq'));document.getElementById('b_rand').textContent=bVal(last(null,'srv_rand'));
if(d.job&&d.job.startsWith('⏳')&&!jobWatch['bench:server'])watchJob('bench:server',document.getElementById('bench_msg'),benchDone);
const max=k=>Math.max(1,...d.latest.filter(r=>r.mac&&r.kind===k).map(r=>r.value));
// Registered machines + any other MAC with results.
const macs=[...new Set([...d.machines.map(m=>m.mac),...d.latest.filter(r=>r.mac).map(r=>r.mac)])];
const checked=new Set([...document.querySelectorAll('.b_sel:checked')].map(x=>x.value));
document.querySelector('#b_clients tbody').innerHTML=macs.map(mac=>{
const boot=last(mac,'boot'),disk=last(mac,'disk'),net=last(mac,'net');
const at=Math.max(0,...[boot,disk,net].filter(Boolean).map(r=>r.at));
const stage=boot&&/stage (\d+)s/.exec(boot.detail);
return `<tr><td><input type="checkbox" class="b_sel form-check-input" value="${esc(mac)}"${checked.has(mac)?' checked':''}></td>
<td><b>${esc(bName(mac))}</b> <span class="mono">${esc(mac)}</span>${d.pending.includes(mac)?' <span class="badge rounded-pill text-bg-warning" title="measured at its next boot">waiting</span>':''}</td>
<td title="${esc(boot?boot.detail:'')}">${esc(bVal(boot))}${stage?` <span class="mono">(stage ${esc(stage[1])} s)</span>`:''}</td>
<td title="${esc(disk?disk.detail:'')}">${bBar(disk,max('disk'))}</td><td>${bBar(net,max('net'))}</td>
<td class="mono">${at?esc(bTime(at)):'—'}</td></tr>`;}).join('')||'<tr><td colspan=6 class="mono">no machines yet</td></tr>';
const sel=document.getElementById('b_hmac'),cur=sel.value;
sel.innerHTML='<option value="">all</option><option value="server">server</option>'+macs.map(m=>`<option value="${esc(m)}">${esc(bName(m))}</option>`).join('');
sel.value=cur;
loadBenchHist();
}
// Server page: live load + disk health ("res" SSE), iSCSI now, and its alert / RAM-reserve settings.
function loadServer(){loadIscsi();if(lastRes)showRes(lastRes);loadCafe();drawCharts();loadServices();loadServerDisks();
if(!svcTimer)svcTimer=setInterval(()=>{if(curPage!=='server'){clearInterval(svcTimer);svcTimer=null;return;}loadServices();},10000);}
// Services (sysmon.rs /api/server/status): each one up or down, refreshed every 10 s while the page is open.
let svcTimer=null;
async function loadServices(){const el=document.getElementById('b_svc');if(!el)return;
let s;try{s=await j('/api/server/status');}catch(e){el.textContent='✗ '+e.message;return;}
// ok: true running · false down · null off (by choice) · a string = a plain label.
const row=(name,ok,detail)=>`<tr><td>${name}</td><td><span class="badge ${ok===true?'text-bg-success':ok===false?'text-bg-danger':'text-bg-secondary'}">${typeof ok==='string'?esc(ok):ok===true?'running':ok===false?'down':'off'}</span></td><td class="mono">${esc(detail)}</td></tr>`;
const i=s.iscsi;
el.innerHTML=`<table class="table table-sm w-auto mb-0"><tbody>
${row('Web admin + boot HTTP',true,`bootrom-mgmt up ${dur(s.uptime)}`+(s.host_uptime!=null?` · server up ${dur(s.host_uptime)}`:''))}
${s.dhcp.mode==='full'?row('DHCP server',s.dhcp.running,`:67 on ${s.dhcp.iface||'?'}`+(s.dhcp.running?'':' — could not bind (see Logs)')):row('DHCP server',null,'the LAN\'s own DHCP hands out addresses')}
${row('TFTP (iPXE)',s.tftp.running,':69')}
${row('iSCSI target',!!i,i?`${i.targets} targets, ${i.sessions} sessions`+(i.clients!=null?`, ${i.clients} clients`:''):'daemon not running')}
${s.dns&&s.dns.on?row('DNS (healthy machines)',s.dns.running,s.dns.running?`:53 ${s.dns.recursive?'recursive, '+s.dns.cached+' cached':'passthrough → '+s.dns.upstream.join(', ')} · ${s.dns.allowed} IPs may resolve`:'could not start (see Logs)'):''}
${row('Remote agents',String(s.agents),'Windows machines connected')}
${row('Golden downloads',String(s.golden.running),`running, ${s.golden.waiting} waiting their turn`)}
</tbody></table>`;}
// Charts (Chart.js, vendored: chart.umd.min.js). The tab keeps the last RES_KEEP "res" samples (10 min at 2 s), across
// page changes; they are drawn only while the Server page shows.
const RES_KEEP=300, resHist=[], charts={};
const fmtNum=v=>String(Number(v.toFixed(v>=10?0:v>=1?1:2)));
// Per canvas: its lines (sample field, label, Bootstrap color variable) and y axis (max = fixed top, unit).
const CHARTS={
ch_cpu:[[['cpu','CPU','--bs-primary'],['ram','RAM','--bs-success']],{max:100,unit:'%'}],
ch_net:[[['tx','out','--bs-primary'],['rx','in','--bs-success'],['iscsi','iSCSI','--bs-warning'],['http','golden HTTP','--bs-danger']],{unit:' MB/s'}],
ch_disk:[[['busy','busy','--bs-primary']],{max:100,unit:'%'}],
};
// Colors from Bootstrap's variables, read when the chart is made (each visit of the page): light / dark theme follow.
function mkChart(cv,lines,o){
const css=getComputedStyle(document.documentElement),col=v=>css.getPropertyValue(v).trim()||'#888';
const fg=col('--bs-secondary-color'),grid=col('--bs-border-color-translucent');
return new Chart(cv,{type:'line',
data:{labels:[],datasets:lines.map(([,label,c])=>({label,data:[],borderColor:col(c),backgroundColor:col(c),borderWidth:1.5,pointRadius:0,tension:.2}))},
options:{animation:false,responsive:true,maintainAspectRatio:false,interaction:{mode:'index',intersect:false},
plugins:{legend:{labels:{color:fg,boxWidth:10,boxHeight:3}},
tooltip:{callbacks:{label:c=>`${c.dataset.label}: ${c.parsed.y==null?'—':fmtNum(c.parsed.y)+o.unit}`}}},
scales:{x:{ticks:{color:fg,maxTicksLimit:6,maxRotation:0},grid:{color:grid}},
y:{min:0,max:o.max,ticks:{color:fg,maxTicksLimit:5},grid:{color:grid}}}}});}
function drawCharts(){if(curPage!=='server'||typeof Chart==='undefined')return;
const labels=resHist.map(r=>new Date(r.t).toLocaleTimeString());
for(const [id,[lines,o]] of Object.entries(CHARTS)){const cv=document.getElementById(id);if(!cv)return;
let c=charts[id];if(!c||c.canvas!==cv){c?.destroy();c=charts[id]=mkChart(cv,lines,o);} // the page was re-rendered
c.data.labels=labels;lines.forEach(([k],i)=>{c.data.datasets[i].data=resHist.map(r=>r[k]);});c.update('none');}}
function resSample(d){const mb=b=>b==null?null:b/1e6;
resHist.push({t:Date.now(),cpu:d.cpu.pct,ram:d.ram.pct,rx:mb(d.net.rx),tx:mb(d.net.tx),
iscsi:mb(d.egress?.iscsi),http:mb(d.egress?.http),busy:d.disk.busy});
if(resHist.length>RES_KEEP)resHist.shift();}
async function loadBenchHist(){
const sel=document.getElementById('b_hmac');if(!sel)return;
const rows=await j('/api/bench/history?mac='+encodeURIComponent(sel.value));
document.querySelector('#b_hist tbody').innerHTML=rows.map(r=>`<tr><td class="mono">${esc(bTime(r.at))}</td><td>${esc(bName(r.mac))}</td>
<td>${esc(BENCH_KINDS[r.kind]?BENCH_KINDS[r.kind][0]:r.kind)}</td><td><b>${esc(bVal(r))}</b></td><td class="mono">${esc(r.detail)}</td></tr>`).join('')
||'<tr><td colspan=5 class="mono">no results yet</td></tr>';
}
// iSCSI now: pushed every second over SSE while the Server page is open (showPage closes it on leaving).
// One stream for every tab showing that page (sharedStream).
function loadIscsi(){
if(iscsiES)return;
iscsiES=sharedStream('broom-iscsi','/api/bench/iscsi/live',['iscsi'],(t,d)=>{
if(t==='iscsi')showIscsi(JSON.parse(d));
else if(t==='down'){const el=document.getElementById('b_iscsi');if(el)el.textContent='reconnecting...';}},0);
}
function showIscsi(d){
const el=document.getElementById('b_iscsi');if(!el)return;
if(d.daemon==='none'){el.textContent='iSCSI daemon not running';return;}
if(!d.targets.length&&!d.peers.length){el.textContent='no client connected';return;}
const mb=x=>x.toFixed(1)+' MB/s';
el.innerHTML=`<div class="d-flex flex-wrap align-items-start gap-4">
<table class="table table-sm w-auto mb-0"><thead><tr><th>Target</th><th>Sessions</th><th>Read</th></tr></thead><tbody>${d.targets.map(t=>
`<tr><td>${esc(t.name)}${t.ram?' <span class="badge text-bg-secondary">RAM</span>':''}</td><td>${t.sessions}</td><td>${mb(t.mbps)}</td></tr>`).join('')}</tbody></table>
<table class="table table-sm w-auto mb-0"><thead><tr><th>Client</th><th>Read</th></tr></thead><tbody>${d.peers.map(p=>
`<tr><td>${esc(p.host||p.ip)} <span class="mono">${esc(p.ip)}</span></td><td>${p.mbps>0?mb(p.mbps):'<span class="mono">idle</span>'}</td></tr>`).join('')||'<tr><td colspan=2 class="mono">none</td></tr>'}</tbody></table></div>`;
}
// Server resources (sysmon.rs): "res" on /api/events every 2 s → the alert banner (every page) + "Server now".
let lastRes=null;
function showRes(d){lastRes=d;
const bar=document.getElementById('alertbar');
if(bar){bar.hidden=!d.alerts.length;
if(d.alerts.length)bar.innerHTML=`<b>${ic('warn')} Server at its limit:</b> ${esc(d.alerts.join(', '))} (threshold ${d.limit}%) — clients boot and load slower. <a class="alert-link" href="/server" data-click="go" data-args='["server"]'>Details</a>`;}
const el=document.getElementById('b_res');if(!el)return;
// Measured values first; a bar + % only where the whole is known (none for a VM's virtual network card).
const row=(name,pct,txt)=>`<tr><td>${name}</td><td>${pct==null?'':
`<div class="progress d-inline-flex align-middle me-2" style="width:140px;height:8px"><div class="progress-bar${pct>=d.limit?' bg-danger':''}" style="width:${Math.min(100,pct).toFixed(0)}%"></div></div>${pct.toFixed(0)}%`}</td><td class="mono">${esc(txt)}</td></tr>`;
const rate=b=>(b/1e6).toFixed(1)+' MB/s';
const {cpu:c,ram:r,net:n,disk:k,space:s}=d;
el.innerHTML=`<table class="table table-sm w-auto mb-0"><tbody>
${row('CPU',c.pct,`${c.cores} cores${c.load!=null?`, load ${c.load.toFixed(2)} (1 min)`:''}`)}
${row('RAM',r.pct,`${gbs(r.used)} used of ${gbs(r.total)} (+ ${gbs(r.cache)} page cache, given back when needed)`)}
${row('Network',n.pct,`${n.iface||'?'}: out ${rate(n.tx)}, in ${rate(n.rx)}`+(n.link?`, link ${n.link>=1000?n.link/1000+' Gbit/s':n.link+' Mbit/s'}`:'')
+(n.virtual?` — virtual card (${n.virtual})`:''))}
${row('Disk busy',k.busy,k.dev?`${k.dev}: read ${rate(k.read)}, write ${rate(k.write)}`:'images on a filesystem without a block device')}
${row('Disk space',s.pct,s.total?`${gbs(s.used)} used of ${gbs(s.total)} (image storage)`:'image storage: unknown')}
${k.health?`<tr><td>Disk health</td><td class="mono ${srvDiskBad(k.health)?'text-danger':''}">${esc(srvDiskHealth(k.health))}</td><td></td></tr>`:''}
</tbody></table>`;}
const srvDiskBad=h=>/fail/i.test(h.status)||(h.realloc||0)>0||(h.pending||0)>0;
function srvDiskHealth(h){const p=[];
p.push(h.status||'SMART: no overall result');
if(h.temp!=null)p.push(h.temp+'°C');
if(h.realloc!=null||h.pending!=null)p.push('bad sectors '+(h.realloc||0)+' reallocated / '+(h.pending||0)+' pending');
return p.join(' · ');}
const benchDone=()=>{if(curPage==='benchmark')loadBench();};
async function benchServer(){const el=document.getElementById('bench_msg');
try{await j('/api/bench/server',{method:'POST'});watchJob('bench:server',el,benchDone);}catch(e){say(el,' ✗ '+e.message);}}
async function benchAsk(cancel){const m=document.getElementById('bq_msg');
const macs=[...document.querySelectorAll('.b_sel:checked')].map(x=>x.value);
if(!macs.length){toast('Tick the machines first',true);return;}
try{await j('/api/bench/request',mk({macs,cancel}));say(m,cancel?' ✓ cancelled':` ✓ ${macs.length} machine(s) measured at their next boot`);}
catch(e){say(m,' ✗ '+e.message);}
loadBench();}
function bSelAll(el){document.querySelectorAll('.b_sel').forEach(x=>x.checked=el.checked);}
// ---- helpers ----
function v(id){return document.getElementById(id).value.trim()}
function mk(body){return {method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(body)}}
// POST `body`, its outcome in message element `msg` (`ok`: text, or made from the answer), then `reload` the page's list.
async function post(url,body,msg,ok,reload){const m=document.getElementById(msg);
try{const r=await j(url,mk(body));say(m,' ✓ '+(typeof ok==='function'?ok(r):ok));}catch(e){say(m,' ✗ '+e.message);}
if(reload)reload();}
// What the admin should know (notices.rs), on every page until dismissed (the owner): redrawn only when it changed.
let noticeIds=null;
async function loadNotices(){const b=document.getElementById('noticebar');if(!b)return;
let l;try{l=await j('/api/notices');}catch(e){return;}
const key=l.map(x=>x.id).join(',');if(key===noticeIds)return;noticeIds=key;b.hidden=!l.length;
b.innerHTML=l.slice().reverse().map(x=>`<div class="alert alert-warning d-flex gap-2 align-items-start py-2 mb-2" role="alert">
<div class="flex-grow-1"><span class="small text-body-secondary">${esc(new Date(x.at*1000).toLocaleString())}${x.n>1?' · seen '+esc(x.n)+' times':''}</span><br>${esc(x.text)}</div>
<button class="btn btn-sm btn-outline-secondary" data-owner ${act('noticeDismiss',x.id)}>Dismiss</button></div>`).join('')
+(l.length>1?`<div class="text-end mb-2"><button class="btn btn-sm btn-link" data-owner ${act('noticeDismiss',null)}>Dismiss all</button></div>`:'');}
async function noticeDismiss(id){try{await j('/api/notices/dismiss',mk(id==null?{}:{id}));}catch(e){toast(e.message,true);}noticeIds=null;loadNotices();}
async function dlRebuild(golden,id,name){
if(!await sure(`Set ${name} up again? At its next start its base is dropped: its name, Remote agent key and first-boot setup are made anew.`))return;
try{const r=await j('/api/diskless/rebuild',mk({name:golden,id}));toast(r.status);}catch(e){toast(e.message,true);}}
// Non-blocking notification (Bootstrap toast, bottom right); errors stay longer and in red.
function toast(msg,err){const t=document.getElementById('toast');
document.getElementById('toast_text').textContent=msg;
t.classList.toggle('text-danger-emphasis',!!err);t.classList.toggle('bg-danger-subtle',!!err);t.classList.toggle('border-danger',!!err);
bootstrap.Toast.getInstance(t)?.dispose(); // the delay is fixed per instance
new bootstrap.Toast(t,{delay:err?6000:3500}).show();}
// The app's dialog (#dlg in index.html) instead of prompt() / confirm(), which a browser lets the user silence.
// ask(): the typed text, or null on Cancel / Esc (multi = a textarea). sure(): true on OK. Both resolve once the
// dialog is fully hidden, so the caller can open it again at once (asking again after a bad value).
function dialog(title,text,value,multi){
const $=id=>document.getElementById(id),m=$('dlg'),f=$(multi?'dlg_area':'dlg_input');
$('dlg_title').textContent=title;$('dlg_text').textContent=text||'';$('dlg_text').hidden=!text;
$('dlg_input').hidden=$('dlg_area').hidden=true;
if(value!=null){f.hidden=false;f.value=value;}
return new Promise(done=>{let ok=false;
$('dlg_form').onsubmit=e=>{e.preventDefault();ok=true;bootstrap.Modal.getInstance(m).hide();};
m.addEventListener('shown.bs.modal',()=>(value!=null?f:$('dlg_ok')).focus(),{once:true});
m.addEventListener('hidden.bs.modal',()=>done(value!=null?(ok?f.value:null):ok),{once:true});
bootstrap.Modal.getOrCreateInstance(m).show();});
}
const ask=(title,text,value,multi)=>dialog(title,text,value??'',multi);
const sure=text=>dialog('Confirm',text);
// ---- admin login (auth.rs) ----
// Login lives on its own page (/login). This page is only served to a signed-in admin: the server redirects an
// unauthenticated browser here → /login. A 401 on any API call (session expired) sends us back to /login.
async function logout(){try{await fetch('/api/auth/logout',{method:'POST'});}catch(e){}
try{localStorage.removeItem(SK_KEY);}catch(e){}location.replace('/login');}
// Everything an element may name in data-click / data-change / data-input (see act()). Called as f(...args, el, event).
const ACTIONS={
noop(){}, // a cell that must not open its row (checkbox column)
go(p,el,e){e.preventDefault();history.pushState(null,'','/'+p);showPage(p);},
href(url){location.href=url;},
mFilter(){mPage=1;renderMachines();},
dvToggle:(k,el)=>dvToggle(k,el.checked), dvSelectAll:el=>dvSelectAll(el.checked),
wake,manage,regMachine,loadStatus,setBase,clearMFilters,mPrev,mNext,mPageSize,setKey,rearmKey,
loadDevices,dvOpen,dvSave,dvDelete,dvClose,dvBulk,dvRegister,dvImport,dvAdd,dvRender,dvHide,dvBroomDisk,
volMove,volFormat,volFolder,volRemove,loadServerDisks,
userAdd,userRole,userPw,userDel,rekey,saveDnsGate,dnsResolveShow,
loadImages,addImage,setDefault,toggleCache,toggleSsd,togglePreload,editImgGroups,republish,showVersions,exportImage,editBoot,delImage,
snapshotImage,dedupImage,rollbackImage,versionToImage,deleteVersion,
loadGames,saveGames,setGamesUpdate,toggleGamesRam,saveGamesUpdate,discardGamesUpdate,editGames,delGames,
loadDiskless,dlOsChanged,mPower,mSelOne,mSelAll,hwToggle,hwDisplay,mDisplay,startOnboard,cancelOnboard,retryOnboard,verToggle,verPin,verDrop,verKeepFree,setRoom,noticeDismiss,vmSize,dlRebuild,roleSave,roleAdd,roleDel,vmStart,vmStop,vmGamesSave,vmGamesDiscard,saveGolden,setMaint,saveMaint,discardMaint,delGolden,editGolden,isoOs,delIso,setPlace,uploadIso,
loadDrivers,uploadDriver,saveDrvTargets,delDriver,
loadGroups,editGroup,closeGroup,saveGroup,createGroup,renameGroup,deleteGroup,saveNewMachines,
loadServer,loadBench,loadBenchHist,logsReload,logsFollow,logsSearch,benchServer,benchRequest:()=>benchAsk(false),benchCancel:()=>benchAsk(true),bSelAll,
applyDhcp,savePw,saveCafe,setTimeout_,tlsOn,tlsUpload,tlsSelfSigned,setZramReserve,setAlert,setSsd,gsMode,setGoldenStreams,logout,
remoteTo,rmGo,rmConnect,rmClose,rmBack,rmWallSize,rmWallFilter,rmWallPrev,rmWallNext,rmQuality,rmFull,rmSas,rmWin,rmAltTab,rmPaste,loadRemote,
};
// A stream of server events, for every tab of this browser through ONE connection. A browser gives a site six
// connections, and a stream of its own in each tab left none for anything else once six tabs were open — every
// page then waited forever. The tab holding the Web Lock `name` keeps the stream and passes each event on
// (BroadcastChannel); when it closes or lets go, the next tab takes over. `quiet` ms without any event (the tab
// holding it was put to sleep by the browser) → this tab opens its own; 0 = never. A browser without these two
// opens a stream per tab, as before. `on(type, data)`: the stream's events, plus 'up' / 'down' (connected / lost).
// Returns what ends this tab's part.
function sharedStream(name,url,types,on,quiet){
const shared=typeof BroadcastChannel==='function'&&navigator.locks&&navigator.locks.request;
const ch=shared?new BroadcastChannel(name):null,ac=shared?new AbortController():null;
let es=null,letGo=null,ended=false,last=Date.now();
const got=(type,data)=>{last=Date.now();on(type,data);};
if(ch)ch.onmessage=e=>got(e.data.type,e.data.data);
const pass=(type,data)=>{got(type,data);if(ch)ch.postMessage({type,data});};
const open=()=>new Promise(res=>{ // settles only when this tab lets go: the stream and the lock are its until then
if(es||ended){res();return;}
letGo=res;es=new EventSource(url);
for(const t of types)es.addEventListener(t,e=>pass(t,e.data));
es.addEventListener('logout',()=>{es.close();pass('logout','');});
es.onerror=()=>pass('down','');
es.onopen=()=>pass('up','');
});
if(shared)navigator.locks.request(name,{signal:ac.signal},open).catch(()=>{});else open();
const timer=shared&&quiet?setInterval(()=>{if(!es&&Date.now()-last>quiet)open();},5000):0;
return ()=>{ended=true;clearInterval(timer);if(ac)ac.abort();if(es){es.close();es=null;}if(letGo)letGo();if(ch)ch.close();};
}
// The server's own events: alive, job status, the server's load.
function onServerEvent(type,data){
if(type==='ping')alive();
else if(type==='job'){alive();const d=JSON.parse(data);showJob(d.name,d.status);}
else if(type==='res'){alive();const d=JSON.parse(data);showRes(d);resSample(d);drawCharts();}
// The server ended the stream because the session is gone (expired / password changed elsewhere) → sign in again.
else if(type==='logout')location.replace('/login');
else if(type==='down')setServer(false);
// (Re)connected: job events sent while the link was down are lost → ask for the status of every followed job.
else if(type==='up'){for(const n in jobWatch)j('/api/images/job?name='+encodeURIComponent(n)).then(r=>showJob(n,r.status)).catch(()=>{});}
}
const serverEvents=()=>sharedStream('broom-events','/api/events',['ping','job','res'],onServerEvent,20000);
let started=false;
function startApp(){
if(started)return; started=true;
loadMe();
serverEvents();
setInterval(()=>{if(lastPing&&Date.now()-lastPing>12000)setServer(false);},3000);
if(location.pathname!=='/'+pageFromPath())history.replaceState(null,'','/'+pageFromPath());
showPage(pageFromPath());
// Every page shows the online count in the sidebar: keep it fresh everywhere (the Machines table redraws too).
if(pageFromPath()!=='machines')loadStatus().catch(()=>{});
setInterval(()=>{if(!document.hidden)loadStatus().catch(()=>{});},15000);
loadNotices();setInterval(()=>{if(!document.hidden)loadNotices();},20000);
}
startApp();