"use strict"; const {app, BrowserWindow, ipcMain, net, session, shell, webContents} = require("electron"); const fs = require("fs"); const path = require("path"); const device = require("./device.js"); const discord = require("./discord.js"); const relay = require("./relay.js"); const updater = require("./updater.js"); // Burrow: a minimal client for the Burrow private server (Animal Jam Classic, // under a name of its own): a login form, then the // game and little else. it needs Electron 11 (the last with Flash support) // and a Flash plugin from before Adobe's January 2021 shut-off; both sit // beside this folder (../electron or ../electron-win, ../flash) and // ../run.sh or ../run.bat starts it. // the client was "AJ Private Client" until it was named Burrow. what it // keeps on this computer (remembered accounts, settings, the game's cache) // stays in the folder of that name, so that nobody is signed out by the // change of name. // (Not when the caller chose the folder itself, --user-data-dir.) if (!app.commandLine.hasSwitch("user-data-dir")) { app.setPath("userData", path.join(app.getPath("appData"), "AJ Private Client")); } // burrow:// links: the server's account page asks the client to sign it in // ("Sign in with the Burrow client", burrow://account?n=; index.html // does the rest). windows and linux start a new copy with the link: the copy // already running takes it, and the new one goes at once. a copy started to // play beside the first still plays; only one that came with a link gives way. // macOS hands the link to the running app (open-url) instead. const LINK_PROTOCOL = "burrow"; const linkIn = argv => argv.find(arg => String(arg).toLowerCase().startsWith(`${LINK_PROTOCOL}://`)) || null; let pendingLink = linkIn(process.argv); if (!app.requestSingleInstanceLock() && pendingLink) { process.exit(0); } // the folder the client was installed with. this one may be an update // (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; // a packaged build names its server in build.json (tools/build.sh --server) const builtFor = () => { try { const {server} = JSON.parse(fs.readFileSync(path.join(SHIPPED, "build.json"), "utf8")); return typeof server === "string" && /^https?:\/\/[^\s]+$/.test(server) ? server : ""; } catch (error) { return ""; } }; const DEFAULT_SERVER = process.env.AJ_SERVER || builtFor() || "http://127.0.0.1:8080"; const FLASH_VERSION = "32.0.0.330"; const LAUNCHER_VERSION = "1.5.7"; // the official launcher this stands in for const FLASH_PLUGIN = process.env.AJ_FLASH_PLUGIN || path.join(SHIPPED, "..", "flash", process.platform === "win32" ? "pepflashplayer.dll" : process.platform === "darwin" ? "PepperFlashPlayer.plugin" : "libpepflashplayer.so"); const GAME_PRELOAD = path.join(__dirname, "gamePreload.js"); const CAPTCHA_PRELOAD = path.join(__dirname, "captchaPreload.js"); // where cloudflare's captcha widget comes from const CAPTCHA_ORIGIN = "https://challenges.cloudflare.com"; const MAX_COPIES = 8; // what has to be known before Chromium starts, so cannot live in the page's // own storage: {gpu: false} when the graphics card is not to be used. const SETTINGS_FILE = path.join(app.getPath("userData"), "client-settings.json"); const readSettings = () => { try { return JSON.parse(fs.readFileSync(SETTINGS_FILE, "utf8")) || {}; } catch (err) { return {}; } }; let win = null; // the server the game is being played on ("https://play.burrow.moe"), // once a login chose it: the one address the game page may load from. let gameOrigin = null; // the captcha window (below), while it is open, and its server's origin let captchaBox = null; let captchaServer = null; // it has a session of its own, with a rule of its own on what it may load: // electron does not say which window a page itself is being loaded for. const CAPTCHA_PARTITION = "aj-captcha"; // --aj-no-renderer-sandbox: the pages' processes outside Chromium's sandbox, // as they were, should the Flash plugin not start with it. const SANDBOXED = !app.commandLine.hasSwitch("aj-no-renderer-sandbox"); // the main process's own calls to a server get a session to themselves, so // that the rule below about what pages may load does not apply to them. const API_PARTITION = "aj-api"; // the theatre's browser (experimental, index.html): a webview of its own // over the theatre screen, in a session of its own that is kept in memory // only, so nothing it visits stays and none of it reaches the game. const BROWSER_PARTITION = "aj-browser"; let browserSession = null; const isBrowser = contents => browserSession !== null && contents.session === browserSession; const webAddress = text => { try { const url = new URL(String(text)); return url.protocol === "https:" || url.protocol === "http:"; } catch (err) { return false; } }; // two copies cannot share one HTTP cache: the second to start moves the // first one's cache aside, and the first then gets broken game files out of // it. so each copy running takes a numbered slot, and every slot but the // first has a cache folder of its own. the folders stay, so a second window // loads the game from disk as fast as the first does. A slot is a lock file // holding the process id of the copy that has it. const pidRuns = pid => { try { process.kill(pid, 0); return true; } catch (err) { return err.code === "EPERM"; } }; const takeSlot = () => { const folder = app.getPath("userData"); fs.mkdirSync(folder, {recursive: true}); for (let slot = 1; slot <= MAX_COPIES; slot++) { const lock = path.join(folder, `copy-${slot}.lock`); try { const holder = Number(fs.readFileSync(lock, "utf8")); if (holder && holder !== process.pid && pidRuns(holder)) { continue; } fs.unlinkSync(lock); } catch (err) { // no such lock: the slot is free. } try { fs.writeFileSync(lock, String(process.pid), {flag: "wx"}); app.on("quit", () => { try { fs.unlinkSync(lock); } catch (quitErr) { // the next copy to want the slot sees that this one is gone. } }); return slot; } catch (err) { // another copy took it this instant; try the next. } } return 0; }; // not when the caller chose the folders itself (--user-data-dir). if (!app.commandLine.hasSwitch("user-data-dir")) { const slot = takeSlot(); if (slot !== 1) { app.commandLine.appendSwitch("disk-cache-dir", path.join(app.getPath("userData"), slot ? `Cache-copy-${slot}` : `Cache-copy-pid-${process.pid}`)); } } // the game reads the launcher's version out of the browser's user agent and // refuses to start below 1.5.3, so the client names itself as the official // launcher there. app.userAgentFallback = `${app.userAgentFallback.replace(/ AJPrivateClient\/\S+/, "")} AJClassic/${LAUNCHER_VERSION}`; // the 2019 flash plugin cannot start inside chromium's sandbox on linux; an // installed copy is started from a menu entry or an AppImage, not run.sh if (process.platform === "linux" && !app.commandLine.hasSwitch("no-sandbox")) { app.commandLine.appendSwitch("no-sandbox"); } app.commandLine.appendSwitch("ppapi-flash-path", FLASH_PLUGIN); app.commandLine.appendSwitch("ppapi-flash-version", FLASH_VERSION); // Chromium slows a page down once its window is covered, minimised or out // of focus. with two game windows side by side that is one of them always: // its animal stutters and its connection can time out. keep both at speed. app.commandLine.appendSwitch("disable-renderer-backgrounding"); app.commandLine.appendSwitch("disable-background-timer-throttling"); app.commandLine.appendSwitch("disable-backgrounding-occluded-windows"); // the game is Flash, which draws with the processor whatever happens; the // graphics card only puts the finished picture on the screen. Chromium's GPU // process costs about 120 MB for that (measured: 134 MB of the client's 437 // MB of private memory, 18 MB without it). it is used unless "Use GPU" under // Advanced was turned off (the owner's choice, once the game drew more // pictures a second than its own 24); without it the little that is left // runs inside the main process. --aj-gpu turns it on whatever is set. const usesGpu = readSettings().gpu !== false || app.commandLine.hasSwitch("aj-gpu"); if (!usesGpu) { app.commandLine.appendSwitch("disable-gpu"); app.commandLine.appendSwitch("in-process-gpu"); } // automatic updates (updater.js): on unless turned off, and only for an // installed build, which knows its server. ipcMain.handle("setAutoUpdate", (event, on) => { fs.writeFileSync(SETTINGS_FILE, JSON.stringify({...readSettings(), autoUpdate: on === true})); if (on === true && updates) { updates.check(); } }); ipcMain.on("restartToUpdate", () => { app.relaunch(); app.exit(0); }); let updates = null; let updateState = null; // the last thing the updater said, for a page loaded after it ipcMain.handle("setGpu", (event, on) => { fs.writeFileSync(SETTINGS_FILE, JSON.stringify({...readSettings(), gpu: on === true})); }); // what the server is told about this computer at sign-in (device.js). ipcMain.handle("device", () => device.device()); ipcMain.handle("info", () => ({ // as this run was started; a change takes effect at the next start. gpu: usesGpu, gpuSetting: readSettings().gpu !== false, defaultServer: DEFAULT_SERVER, version: VERSION, // null when this copy is not one that updates itself autoUpdate: builtFor() && global.burrow ? readSettings().autoUpdate !== false : null, update: updateState, launcherVersion: LAUNCHER_VERSION, // what the game is told it runs on; it only knows these two. os: process.platform === "darwin" ? "mac" : "win", flashPluginFound: fs.existsSync(FLASH_PLUGIN), })); // resolves to the local port the game is to connect to (see relay.js), 0 // when none could be opened. ipcMain.handle("useServer", (event, serverUrl, gamePort, gameHost) => { const url = new URL(serverUrl); gameOrigin = url.origin; // the server may name another host for the game: a web address behind // cloudflare carries no game port. a local server is never sent elsewhere. const loopback = ["127.0.0.1", "localhost", "::1"]; const named = /^[a-z0-9.-]{1,253}$/i.test(String(gameHost || "")) && !loopback.includes(gameHost) && !loopback.includes(url.hostname); return relay.use(named ? gameHost : url.hostname, Number(gamePort), url.protocol === "https:"); }); // what a page may load. the login page is ours and talks to whichever server // was typed in. the game page gets its own server and nothing else: the // game is an old Flash plugin in an old browser, and anything that made it // fetch from elsewhere (a picture named in a chat line once did) would hand // a stranger the player's address, or the plugin a stranger's file. const LOCAL_SCHEMES = ["file:", "devtools:", "chrome-devtools:", "chrome:", "data:", "blob:", "about:"]; // the theatre's video (the server's game page: the room's, played by staff, // and the player's own from its link box): the YouTube and Vimeo players // and what they load, measured in this Electron. a host here counts with // every host under it. const VIDEO_PLAYER_HOSTS = [ "youtube-nocookie.com", "youtube.com", "ytimg.com", "ggpht.com", "googlevideo.com", "jnn-pa.googleapis.com", "gstatic.com", "www.google.com", "vimeo.com", "vimeocdn.com", ]; const videoPlayerHost = host => VIDEO_PLAYER_HOSTS.some(known => host === known || host.endsWith(`.${known}`)); // the player's own sound or video file (the game page's link box): the one // address they typed, as the game page says it (ownVideo below), and where // it redirects. a