// Captures the Urutau dashboard for docs/img. // Usage (called by scripts/screenshots.sh): node shots.mjs // // Covers every page registered in internal/server/web/pages.json plus the campaign detail. // Desktop 1440x900 in the dark theme (English, plus Portuguese for the pages the // pt-BR docs show), a few pages in the light theme, and a few at 390x844 (phone). // File names: -dark.png, -dark-pt.png, -light.png, -mobile.png. import { execFileSync } from "node:child_process"; import { homedir } from "node:os"; const [hub, login, out] = process.argv.slice(2); const core = process.env.PUPPETEER_CORE || `${homedir()}/.claude/tools/node_modules/puppeteer-core/lib/esm/puppeteer/puppeteer-core.js`; const { default: puppeteer } = await import(core); const chrome = process.env.CHROME || execFileSync("bash", ["-c", `ls -1d "$HOME"/.cache/puppeteer/chrome/*/chrome-linux64/chrome | sort -V | tail -1`], { encoding: "utf8" }).trim(); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const DESKTOP = { width: 1440, height: 900, deviceScaleFactor: 1 }; const PHONE = { width: 390, height: 844, deviceScaleFactor: 2, isMobile: true, hasTouch: true }; const ATTACKER = "203.0.113.45"; // the multi-vector attacker of the demo data // pages: id (file name), hash, settle time (ms, after the page reports ready), optional prepare(page), // optional ready (CSS selector that must exist) and minPanels (panels with data). const R = (id, route, extra = {}) => ({ id, hash: `#/${route}`, wait: 1500, ...extra }); const pages = [ { id: "overview", hash: "#/overview?r=7d", wait: 3500 }, { id: "threats", hash: "#/threats?r=24h", wait: 3500 }, { id: "geo-world", hash: "#/geo?r=30d", wait: 3500 }, { id: "geo-brazil", hash: "#/geo/BR?r=30d", wait: 3500 }, { id: "geo-pernambuco", hash: "#/geo/BR/Pernambuco?r=30d", wait: 3500 }, { id: "live-flat", hash: "#/live?r=24h", wait: 14000, prepare: (p) => liveMode(p, "flat"), live: true }, { id: "live-globe", hash: "#/live?r=24h", wait: 14000, prepare: (p) => liveMode(p, "globe"), live: true }, { id: "traffic", hash: "#/traffic?r=30d", wait: 3500 }, { id: "ips", hash: "#/ips?r=7d", wait: 3500 }, { id: "ip-analysis", hash: `#/ip/${ATTACKER}?r=7d`, wait: 4500 }, // starts in the middle of the escalation of the demo attacker (about 25 h ago) and plays on { id: "replay", hash: () => `#/replay?r=2d&speed=300&t=${encodeURIComponent(new Date(Date.now() - (25 * 60 + 50) * 60e3).toISOString())}`, wait: 3500, live: true }, { id: "sources", hash: "#/sources?r=24h", wait: 3500 }, // pages of the later versions R("sessions", "sessions?r=7d"), R("campaigns", "campaigns?r=30d"), { id: "campaign-detail", hash: async () => `#/campaigns/${await firstCampaign()}?r=30d`, wait: 2500 }, R("search", `search?r=7d&ip=${ATTACKER}`, { wait: 2500 }), R("mitre", "mitre?r=30d"), R("defense", "defense?r=7d", { wait: 2500, full: true }), R("evolution", "evolution?r=30d", { wait: 2500 }), R("concentration", "concentration?r=30d", { wait: 2500 }), R("survival", "survival?r=30d", { wait: 2500 }), R("errors", "errors?r=7d", { wait: 2500 }), R("slo", "slo?r=7d", { wait: 2500 }), R("protocol", "protocol?r=7d", { wait: 2500 }), R("integrity", "integrity?r=7d"), R("reports", "reports?r=7d"), R("egress", "egress?r=7d", { wait: 2500, full: true }), ]; const darkEN = pages.map((p) => p.id); const darkPT = darkEN; // the pt-BR docs show every page const light = ["overview", "geo-world", "live-flat", "ip-analysis", "defense", "egress", "campaigns", "slo"]; const mobile = ["overview", "threats", "live-flat", "ips", "defense", "egress", "campaigns", "errors", "slo"]; async function firstCampaign() { const r = await fetch(`${hub}/api/v1/campaigns?range=30d`); const j = await r.json(); const c = (j.campaigns || [])[0]; if (!c) throw new Error("no campaign in the demo data (campaign-detail)"); return encodeURIComponent(c.id); } // Waits until the page left the loading state, then fails when the capture would be an // error, a disabled feature or nothing but empty states. async function waitReady(page, id) { try { await page.waitForFunction(() => { const v = document.querySelector("#view"); return v && (v.innerText || "").trim().length > 80 && !v.querySelector(".skeleton"); }, { timeout: 25000, polling: 300 }); } catch { throw new Error(`${id}: still loading after 25 s (skeleton or no content)`); } } async function checkData(page, id) { const r = await page.evaluate(() => { const v = document.querySelector("#view"); const panels = v.querySelectorAll(".panel").length; const empties = v.querySelectorAll(".state-empty").length; const txt = v.innerText || ""; return { panels, empties, len: txt.trim().length, txt: txt.slice(0, 200).replace(/\s+/g, " ") }; }); const bad = /Could not load data|Não foi possível carregar|disabled|desativad|desligad/i.test(r.txt); if (bad || r.len < 80 || (r.panels > 0 && r.empties >= r.panels) || (r.panels === 0 && r.empties > 0)) { throw new Error(`${id}: capture has no data (panels=${r.panels} empty=${r.empties}) "${r.txt}"`); } } const failures = []; async function liveMode(page, mode) { try { await page.evaluate((m) => localStorage.setItem("urutau.live.mode", m), mode); } catch {} } async function replayPlay(page) { // press play, let the replay run for a while so the map shows arcs and the list is mid-way const btn = await page.evaluateHandle(() => [...document.querySelectorAll("button")].find((b) => /play|reproduzir/i.test((b.getAttribute("aria-label") || "") + " " + (b.title || "") + " " + b.textContent))); if (btn && btn.asElement()) await btn.asElement().click(); await sleep(9000); } const browser = await puppeteer.launch({ executablePath: chrome, args: ["--no-sandbox", "--use-gl=angle", "--use-angle=swiftshader", "--enable-unsafe-swiftshader", "--ignore-gpu-blocklist"], defaultViewport: DESKTOP }); async function shoot(p, o) { try { await shoot1(p, o); } catch (e) { failures.push(e.message); console.error("FAIL", e.message); for (const pg of (await browser.pages()).slice(1)) await pg.close().catch(() => {}); } } async function shoot1(p, { theme, lang, viewport, suffix }) { const page = await browser.newPage(); await page.setViewport(viewport); await page.evaluateOnNewDocument((th, lg) => { try { localStorage.setItem("urutau.theme", th); localStorage.setItem("urutau.lang", lg); localStorage.setItem("urutau.range", "24h"); localStorage.setItem("urutau.host", "all"); } catch {} }, theme, lang); await page.goto(hub + "/", { waitUntil: "domcontentloaded" }); if (p.prepare) await p.prepare(page); const hash = typeof p.hash === "function" ? await p.hash() : p.hash; await page.goto(hub + "/" + hash, { waitUntil: "domcontentloaded" }); await page.evaluate((h) => { location.hash = h; }, hash); const name = `${p.id}-${suffix}`; await waitReady(page, name); await sleep(p.wait); if (p.id === "replay") await replayPlay(page); await checkData(page, name); await page.screenshot({ path: `${out}/${name}.png`, fullPage: !!p.full && viewport === DESKTOP }); console.log("ok", name); await page.close(); } try { for (const p of pages) { if (darkEN.includes(p.id)) await shoot(p, { theme: "dark", lang: "en", viewport: DESKTOP, suffix: "dark" }); if (darkPT.includes(p.id)) await shoot(p, { theme: "dark", lang: "pt-BR", viewport: DESKTOP, suffix: "dark-pt" }); if (light.includes(p.id)) await shoot(p, { theme: "light", lang: "en", viewport: DESKTOP, suffix: "light" }); if (mobile.includes(p.id)) await shoot(p, { theme: "dark", lang: "en", viewport: PHONE, suffix: "mobile" }); } // login (a separate instance with authentication on) for (const [lang, suffix, theme] of [["en", "dark", "dark"], ["pt-BR", "dark-pt", "dark"], ["en", "light", "light"]]) { const page = await browser.newPage(); await page.setViewport(DESKTOP); await page.evaluateOnNewDocument((th, lg) => { try { localStorage.setItem("urutau.theme", th); localStorage.setItem("urutau.lang", lg); } catch {} }, theme, lang); await page.goto(login + "/login", { waitUntil: "domcontentloaded" }); await sleep(1500); await page.screenshot({ path: `${out}/login-${suffix}.png` }); console.log("ok", `login-${suffix}`); await page.close(); } } finally { await browser.close(); } if (failures.length) { console.error(`\n${failures.length} capture(s) failed:\n - ` + failures.join("\n - ")); process.exit(1); }