diff --git a/app/index.html b/app/index.html index b7456e1..853f87b 100644 --- a/app/index.html +++ b/app/index.html @@ -371,7 +371,7 @@ + I am 18 or older, and old enough in my country to agree to this myself. I agree to the Terms of Service and the Privacy Policy.
@@ -391,7 +391,7 @@ - + @@ -402,6 +402,8 @@ + +Attach client.log to a bug report.
@@ -1651,6 +1653,7 @@ } }); $("auto-update").addEventListener("change", () => window.client.setAutoUpdate($("auto-update").checked)); + $("show-log").addEventListener("click", () => window.client.showLog()); window.client.info().then(result => { info = result; diff --git a/app/log.js b/app/log.js new file mode 100644 index 0000000..3045cae --- /dev/null +++ b/app/log.js @@ -0,0 +1,139 @@ +"use strict"; + +const {app} = require("electron"); +const fs = require("fs"); +const os = require("os"); +const path = require("path"); +const util = require("util"); + +// the client's log, for players to attach to a bug report: what the client +// itself says, what its pages and the game print to their consoles +// (warnings and errors), and crashes and pages that would not load. it is +// /logs/client.log; once that is MAX_BYTES it becomes +// client.old.log (the one before it goes) and a new one starts, so the two +// are never more than twice that. "Show client log" in the client's +// settings (index.html) opens the folder. +// +// what could let someone into an account is kept out: an address's query +// (where sign-in tokens travel) and anything written as password=, token= +// and the like. + +const MAX_BYTES = 1024 * 1024; +const MAX_ENTRY = 4000; // characters of one entry; a long stack is cut +// a page printing in a loop fills the log with one thing: past this many +// entries in BURST_MS the rest are counted, not written +const BURST_ENTRIES = 200; +const BURST_MS = 10000; + +let file = null; +let oldFile = null; +let size = 0; +let header = ""; // which client and computer: the first line of each file +let burstStart = 0; +let burstCount = 0; +let leftOut = 0; + +const SECRET = /\b(password|passwd|pass|token|session|secret|auth|nonce|code|key)(["']?\s*[:=]\s*["']?)[^\s&"',;}]+/gi; +const QUERY = /\b(https?:\/\/[^\s?#"'<>]+)\?[^\s"'<>]*/gi; +const scrub = text => text.replace(QUERY, "$1?…").replace(SECRET, "$1$2[hidden]"); + +const stamp = () => new Date().toISOString(); + +const append = text => { + if (file === null) { + return; + } + try { + const bytes = Buffer.byteLength(text); + if (size + bytes > MAX_BYTES) { + fs.renameSync(file, oldFile); + fs.writeFileSync(file, header); + size = Buffer.byteLength(header); + } + fs.appendFileSync(file, text); + size += bytes; + } + catch (err) { + // a log that cannot be written is no reason to stop the client + } +}; + +// `level` "info", "warn" or "error"; `source` what said it ("main", "page", +// "game"); `parts` as console.log takes them. +const write = (level, source, ...parts) => { + const now = Date.now(); + if (now - burstStart > BURST_MS) { + if (leftOut > 0) { + append(`${stamp()} warn [log] ${leftOut} more entries in ${BURST_MS / 1000} seconds were left out\n`); + } + burstStart = now; + burstCount = 0; + leftOut = 0; + } + if (++burstCount > BURST_ENTRIES) { + leftOut++; + return; + } + let text = scrub(util.format(...parts)); + if (text.length > MAX_ENTRY) { + text = `${text.slice(0, MAX_ENTRY)}… (${text.length - MAX_ENTRY} more characters)`; + } + append(`${stamp()} ${level.padEnd(5)} [${source}] ${text.replace(/\r?\n/g, "\n ")}\n`); +}; + +// Chromium's console levels: 0 verbose, 1 info, 2 warning, 3 error +const CONSOLE_LEVELS = [null, null, "warn", "error"]; + +const watchContents = contents => { + const source = contents.getType() === "webview" ? "game" : "page"; + contents.on("console-message", (event, level, message, line, sourceId) => { + if (CONSOLE_LEVELS[level]) { + write(CONSOLE_LEVELS[level], source, `${message}${sourceId ? ` (${path.basename(String(sourceId).split("?")[0])}:${line})` : ""}`); + } + }); + contents.on("did-fail-load", (event, code, description, url, isMainFrame) => { + if (code !== -3) { // -3: the load was stopped for another, not a failure + write("warn", source, `could not load ${url}${isMainFrame ? "" : " (part of the page)"}: ${description} (${code})`); + } + }); + contents.on("plugin-crashed", (event, name, version) => write("error", source, `the ${name} plugin ${version} crashed`)); + contents.on("unresponsive", () => write("warn", source, "stopped responding")); + contents.on("responsive", () => write("info", source, "responding again")); +}; + +// starts the log; the data folder must be chosen by then +const start = version => { + const dir = path.join(app.getPath("userData"), "logs"); + try { + fs.mkdirSync(dir, {recursive: true}); + file = path.join(dir, "client.log"); + oldFile = path.join(dir, "client.old.log"); + size = fs.existsSync(file) ? fs.statSync(file).size : 0; + } + catch (err) { + file = null; + return; + } + header = `${stamp()} info [main] Burrow ${version}, Electron ${process.versions.electron}, ${os.type()} ${os.release()} ${process.arch}, ${Math.round(os.totalmem() / 1048576)} MB\n`; + append(header); + // the main process's own console goes to the log as well + for (const [method, level] of [["log", "info"], ["info", "info"], ["warn", "warn"], ["error", "error"]]) { + const original = console[method].bind(console); + console[method] = (...parts) => { + original(...parts); + write(level, "main", ...parts); + }; + } + process.on("uncaughtExceptionMonitor", err => write("error", "main", `uncaught: ${err && err.stack ? err.stack : err}`)); + process.on("unhandledRejection", err => write("error", "main", `unhandled rejection: ${err && err.stack ? err.stack : err}`)); + app.on("web-contents-created", (event, contents) => watchContents(contents)); + app.on("render-process-gone", (event, contents, details) => write("error", "main", `a page's process ended: ${details.reason} (exit code ${details.exitCode})`)); + app.on("child-process-gone", (event, details) => { + if (details.reason !== "clean-exit") { + write("error", "main", `the ${details.name || details.type} process ended: ${details.reason} (exit code ${details.exitCode})`); + } + }); + app.on("will-quit", () => write("info", "main", "quit")); +}; + +module.exports = {start, write, file: () => file}; diff --git a/app/main.js b/app/main.js index 6219ea6..989a332 100644 --- a/app/main.js +++ b/app/main.js @@ -5,6 +5,7 @@ const fs = require("fs"); const path = require("path"); const device = require("./device.js"); const discord = require("./discord.js"); +const log = require("./log.js"); const relay = require("./relay.js"); const updater = require("./updater.js"); @@ -41,6 +42,7 @@ if (!app.requestSingleInstanceLock() && pendingLink) { // (boot.js), kept elsewhere; the plugin and build.json stay beside that one. const SHIPPED = global.burrow ? global.burrow.shipped : __dirname; const VERSION = require("./package.json").version; +log.start(VERSION); // a packaged build names its server in build.json (tools/build.sh --server) const builtFor = () => { @@ -518,6 +520,13 @@ ipcMain.on("showFile", (event, file) => { } }); +// the client's log (log.js), shown in its folder for a bug report +ipcMain.on("showLog", event => { + if (win && event.sender === win.webContents && log.file()) { + shell.showItemInFolder(log.file()); + } +}); + ipcMain.on("openPage", (event, url) => { if (/^https?:\/\//.test(String(url))) { shell.openExternal(String(url)); diff --git a/app/package.json b/app/package.json index 32317fc..31e88de 100644 --- a/app/package.json +++ b/app/package.json @@ -1,7 +1,7 @@ { "name": "aj-private-client", "productName": "Burrow", - "version": "1.3.3", + "version": "1.4.0", "description": "The client for the Burrow private server", "main": "boot.js", "license": "UNLICENSED" diff --git a/app/preload.js b/app/preload.js index 754bcd0..e6f4679 100644 --- a/app/preload.js +++ b/app/preload.js @@ -27,6 +27,8 @@ contextBridge.exposeInMainWorld("client", { // saves a picture of the game; resolves to the file's path, or null. screenshot: gameContentsId => ipcRenderer.invoke("screenshot", gameContentsId), showFile: file => ipcRenderer.send("showFile", file), + // the client's log file in its folder, to attach to a bug report (log.js) + showLog: () => ipcRenderer.send("showLog"), toggleFullscreen: () => ipcRenderer.send("toggleFullscreen"), openPage: url => ipcRenderer.send("openPage", url), // a picture from the game to print (main.js): {image, width, height}