diff --git a/lib/customitems.js b/lib/customitems.js index 1290c2f..5f148b3 100644 --- a/lib/customitems.js +++ b/lib/customitems.js @@ -65,6 +65,11 @@ const now = () => Math.floor(Date.now() / 1000); const baseDef = (kind, id) => (kind === "den" ? defpacks.denItemDef(id) : defpacks.itemDef(id)); +// the file name an uploaded music track is asked for by (lib/custommusic.js): +// a new upload is a new name, so no game plays an old copy from its cache. +const audioName = entry => `CustomMusic${entry.id}_${entry.audio}`; +let audioGone = () => {}; // (entry): an uploaded track's item was deleted; set by lib/custommusic.js + // the def row of an entry: its base's row with its own id, name, price and // colours, always on sale and marked new. const rowOf = entry => { @@ -80,6 +85,11 @@ const rowOf = entry => { combinedCost: "", }; if (entry.kind === "den") { + // a music track uploaded by staff (lib/custommusic.js): the game plays + // "audio/.mp3", which the server answers with its file + if (entry.audio) { + own.abbrName = audioName(entry); + } return {...base, ...own, nameStrId: String(STRING_BASE.den + entry.id - FIRST_ID), cost: String(entry.cost)}; } return {...base, ...own, titleStrId: String(STRING_BASE.clothing + entry.id - FIRST_ID), value: String(entry.cost), colors: entry.colors.join(","), isGiftable: "1"}; @@ -155,6 +165,7 @@ const fromRow = row => ({ artMode: LOOKS.includes(row.art_mode) ? row.art_mode : "sticker", pending: row.pending === 1, artPending: row.art_pending === 1, + audio: /^[0-9a-f]{8,64}$/.test(row.audio || "") ? row.audio : null, }); // a player-made item (lib/playeritems.js) has the maker's account id; @@ -179,6 +190,10 @@ const addOwnerColumn = () => { if (!columns.includes("art_pending")) { db().exec("ALTER TABLE custom_items ADD COLUMN art_pending INTEGER"); } + // a music track uploaded by staff (lib/custommusic.js): its file's version + if (!columns.includes("audio")) { + db().exec("ALTER TABLE custom_items ADD COLUMN audio TEXT"); + } }; const load = () => { @@ -269,10 +284,12 @@ const create = (kind, fields, by, ownerId = null, pending = false) => { return values; } const id = Math.max(FIRST_ID - 1, ...items.get(kind).keys()) + 1; + // `fields.audio`: an uploaded music track's version (lib/custommusic.js) + const audio = kind === "den" && /^[0-9a-f]{8,64}$/.test(String(fields.audio || "")) ? String(fields.audio) : null; const entry = {kind, id, base: baseId, colors: [], ...values, createdBy: String(by), createdAt: now(), ownerId, artMode: "sticker", pending, artPending: false, - seq: made++}; - db().prepare("INSERT INTO custom_items (kind, id, base, name, cost, members, colors, in_shop, created_by, created_at, owner_id, pending) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") - .run(kind, id, baseId, entry.name, entry.cost, entry.members ? 1 : 0, entry.colors.join(","), entry.inShop ? 1 : 0, entry.createdBy, entry.createdAt, ownerId, pending ? 1 : 0); + audio, seq: made++}; + db().prepare("INSERT INTO custom_items (kind, id, base, name, cost, members, colors, in_shop, created_by, created_at, owner_id, pending, audio) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") + .run(kind, id, baseId, entry.name, entry.cost, entry.members ? 1 : 0, entry.colors.join(","), entry.inShop ? 1 : 0, entry.createdBy, entry.createdAt, ownerId, pending ? 1 : 0, audio); items.get(kind).set(id, entry); if (!pending) { apply(entry); @@ -362,6 +379,9 @@ const remove = (kind, id) => { db().prepare("DELETE FROM custom_items WHERE kind = ? AND id = ?").run(kind, entry.id); items.get(kind).delete(entry.id); defpacks.unsetCustomItem(kind, entry.id); + if (entry.audio) { + audioGone(entry); + } return {item: entry}; }; @@ -371,6 +391,10 @@ const list = () => KINDS.flatMap(kind => [...items.get(kind).values()]) module.exports = { KINDS, FIRST_ID, + audioName, + onAudioGone: callback => { + audioGone = callback; + }, load, create, update, diff --git a/lib/custommusic.js b/lib/custommusic.js new file mode 100644 index 0000000..69bf1c2 --- /dev/null +++ b/lib/custommusic.js @@ -0,0 +1,174 @@ +"use strict"; + +const crypto = require("crypto"); +const fs = require("fs"); +const path = require("path"); +const {logText} = require("./logtext.js"); +const cdn = require("./cdn.js"); +const config = require("../config.js"); +const customItems = require("./customitems.js"); +const extraMusic = require("./extramusic.js"); + +// music tracks staff upload in the panels (permission "music.upload", +// lib/roles.js): an MP3 made a den music item while the server runs, as the +// extra tracks of lib/extramusic.js are. each is a custom den item +// (lib/customitems.js) made from the game's "Jam On", with an `audio` +// version of its own: games are sent its def row at once, players get it +// from the item finder (or the shop, if staff put it on sale), and deleting +// it takes it from everyone, as with any custom item. +// +// a den music item plays "audio/.mp3" from the content server +// (DenItemHolder); for these the server answers with the file uploaded, kept +// in data/custom-music/-.mp3 (the data folder is the one +// place the server writes to). + +const MAX_BYTES = 15 * 1024 * 1024; +const MIN_BYTES = 4 * 1024; +const VERSION_LENGTH = 12; +const FOLDER = () => path.join(config.dataDir, "custom-music"); +const fileOf = entry => path.join(FOLDER(), `${entry.id}-${entry.audio}.mp3`); + +// MPEG audio layer III, as the game's Flash plays it: an ID3v2 tag may come +// first; then a frame header whose next frame is where its length says. +const BITRATES = { + 1: [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320], + 2: [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160], +}; +const SAMPLE_RATES = {3: [44100, 48000, 32000], 2: [22050, 24000, 16000], 0: [11025, 12000, 8000]}; + +// the frame at `at`: {length, sampleRate} or null. +const frameAt = (data, at) => { + if (at + 4 > data.length || data[at] !== 0xFF || (data[at + 1] & 0xE0) !== 0xE0) { + return null; + } + const version = (data[at + 1] >> 3) & 3; // 3 MPEG-1, 2 MPEG-2, 0 MPEG-2.5 + const layer = (data[at + 1] >> 1) & 3; // 1 is layer III + const bitrateIndex = data[at + 2] >> 4; + const rateIndex = (data[at + 2] >> 2) & 3; + const padding = (data[at + 2] >> 1) & 1; + if (version === 1 || layer !== 1 || bitrateIndex === 0 || bitrateIndex === 15 || rateIndex === 3) { + return null; + } + const bitrate = BITRATES[version === 3 ? 1 : 2][bitrateIndex] * 1000; + const sampleRate = SAMPLE_RATES[version][rateIndex]; + return {length: Math.floor(((version === 3 ? 144 : 72) * bitrate) / sampleRate) + padding, sampleRate}; +}; + +// null for an MP3 the game can play, or what is wrong with it. +const problem = data => { + if (!Buffer.isBuffer(data) || data.length < MIN_BYTES) { + return "That file is too small to be a song."; + } + if (data.length > MAX_BYTES) { + return `That file is too big: ${MAX_BYTES / 1024 / 1024} MB at most.`; + } + let at = 0; + if (data.toString("latin1", 0, 3) === "ID3" && data.length > 10) { + at = 10 + (((data[6] & 0x7F) << 21) | ((data[7] & 0x7F) << 14) | ((data[8] & 0x7F) << 7) | (data[9] & 0x7F)) + (data[5] & 0x10 ? 10 : 0); + } + // the first frame: the header, then two more where their lengths say + for (const end = Math.min(data.length, at + 64 * 1024); at < end; at++) { + const first = frameAt(data, at); + const second = first && frameAt(data, at + first.length); + const third = second && frameAt(data, at + first.length + second.length); + if (third) { + return null; + } + } + return "That is not an MP3 file (MPEG audio layer III)."; +}; + +const tracks = () => customItems.list().filter(entry => entry.kind === "den" && entry.audio); + +// content path ("1830/audio/") -> entry, made again when the tracks change. +let paths = null; +const pathMap = () => { + if (!paths) { + paths = new Map(tracks().map(entry => [cdn.hashedPath(`audio/${customItems.audioName(entry)}.mp3`), entry])); + } + return paths; +}; + +// what to answer a content request with when it is for one of these +// tracks: {body, type}; undefined when it is not. +const asset = async relPath => { + const entry = pathMap().get(relPath); + if (!entry) { + return undefined; + } + const body = await fs.promises.readFile(fileOf(entry)).catch(() => null); + return {body, type: "audio/mpeg"}; +}; + +// an upload made a track: {item} or {error}. `fields`: {name, cost, inShop, members}. +const add = (data, fields, by) => { + const wrong = problem(data); + if (wrong) { + return {error: wrong}; + } + const audio = crypto.createHash("sha256").update(data).digest("hex").slice(0, VERSION_LENGTH); + fs.mkdirSync(FOLDER(), {recursive: true, mode: 0o700}); + // written before the item exists, so no game asks for a file not there yet + const waiting = path.join(FOLDER(), `new-${audio}-${process.pid}.mp3`); + fs.writeFileSync(waiting, data, {mode: 0o600}); + const result = customItems.create("den", {name: fields.name, cost: fields.cost, members: fields.members, + inShop: fields.inShop === true, base: extraMusic.TEMPLATE, audio}, by); + if (result.error) { + fs.rmSync(waiting, {force: true}); + return result; + } + fs.renameSync(waiting, fileOf(result.item)); + paths = null; + console.log(`[custom music] ${logText(by)} uploaded "${logText(result.item.name)}" (den item ${logText(result.item.id)}, ${logText(data.length)} bytes)`); + return result; +}; + +// the file of a track, for staff to listen to before or after: {data} or null. +const file = id => { + const entry = tracks().find(track => track.id === Number(id)); + if (!entry) { + return null; + } + const data = (() => { + try { + return fs.readFileSync(fileOf(entry)); + } + catch (err) { + return null; + } + })(); + return data ? {data, entry} : null; +}; + +// for the panels: newest first +const list = () => tracks().map(entry => ({id: entry.id, name: entry.name, cost: entry.cost, inShop: entry.inShop, + createdBy: entry.createdBy, createdAt: entry.createdAt, + bytes: (() => { + try { + return fs.statSync(fileOf(entry)).size; + } + catch (err) { + return 0; + } + })()})); + +// a track's item was deleted (lib/itemcontrol.js deleteCustom): its file goes too. +customItems.onAudioGone(entry => { + fs.rmSync(fileOf(entry), {force: true}); + paths = null; +}); + +// at start: files of tracks no longer there (and of uploads cut short) go. +const load = () => { + const kept = new Set(tracks().map(entry => path.basename(fileOf(entry)))); + let gone = 0; + for (const name of fs.existsSync(FOLDER()) ? fs.readdirSync(FOLDER()) : []) { + if (!kept.has(name)) { + fs.rmSync(path.join(FOLDER(), name), {force: true}); + gone++; + } + } + console.log(`[custom music] ${logText(kept.size)} uploaded tracks${gone ? `, ${logText(gone)} stray files removed` : ""}`); +}; + +module.exports = {MAX_BYTES, problem, asset, add, file, list, load}; diff --git a/lib/http.js b/lib/http.js index 7a2e47b..b90c408 100644 --- a/lib/http.js +++ b/lib/http.js @@ -24,6 +24,8 @@ const manage = require("./manage.js"); const modpanel = require("./modpanel.js"); const badges = require("./badges.js"); const extraMusic = require("./extramusic.js"); +const customMusic = require("./custommusic.js"); +const itemControl = require("./itemcontrol.js"); const masterpieces = require("./masterpieces.js"); const panel = require("./panel.js"); const totp = require("./totp.js"); @@ -230,6 +232,75 @@ const itemArtUpload = async (req, res, url, from, signedIn = null, adminSignedIn send(res, 200, result); }; +// music tracks uploaded in the panels (lib/custommusic.js), at +// /mod/api/music (staff with "music.upload") and /admin/api/music: +// GET: the tracks; GET ?play=: one's file, to listen to; +// POST ?name=&cost=&inShop=1, the MP3 as the body: a new track; +// POST ?delete=: the track deleted, and taken from every player. +// `signedIn` (req) -> account: the moderator panel's session; +// `adminSignedIn` (req) -> boolean: the admin panel's, written down as "admin". +const musicUploads = limits.limiter(30, 3600000); // tracks an hour, per staff member +const musicRoute = async (req, res, url, from, signedIn = null, adminSignedIn = null) => { + res.setHeader("Cache-Control", "no-store"); + const asAdmin = Boolean(adminSignedIn && adminSignedIn(req)); + const account = asAdmin ? null : signedIn ? signedIn(req) : null; + if (!asAdmin && !(account && roles.can(account, "music.upload"))) { + send(res, 403, {error: "Your role cannot upload music."}); + return; + } + const by = asAdmin ? "admin" : account.username; + if (req.method === "GET" || req.method === "HEAD") { + if (url.searchParams.has("play")) { + const track = customMusic.file(url.searchParams.get("play")); + if (!track) { + send(res, 404, "Not found", "text/plain"); + return; + } + res.setHeader("Content-Security-Policy", "default-src 'none'; sandbox"); + res.setHeader("X-Content-Type-Options", "nosniff"); + send(res, 200, track.data, "audio/mpeg"); + return; + } + send(res, 200, {tracks: customMusic.list(), maxBytes: customMusic.MAX_BYTES}); + return; + } + if (req.method !== "POST" || !limits.originAllowed(req)) { + send(res, 403, {error: "forbidden"}); + return; + } + if (url.searchParams.has("delete")) { + const entry = customItems.get("den", url.searchParams.get("delete")); + if (!entry || !entry.audio) { + send(res, 404, {error: "No such uploaded track."}); + return; + } + const result = itemControl.deleteCustom("den", entry.id, world); + if (result.ok) { + modlog.record(by, "music deleted", "", `den ${entry.id} ${entry.name}`); + } + send(res, result.ok ? 200 : 400, result); + return; + } + if (!asAdmin && !musicUploads.allow(account.id)) { + send(res, 429, {error: "Too many uploads. Try again later."}); + return; + } + const data = await readBody(req, customMusic.MAX_BYTES + 1).catch(() => null); + if (!data) { + send(res, 413, {error: `That file is too big: ${customMusic.MAX_BYTES / 1024 / 1024} MB at most.`}); + return; + } + const result = customMusic.add(data, {name: url.searchParams.get("name"), cost: url.searchParams.get("cost") || 0, + inShop: url.searchParams.get("inShop") === "1"}, by); + if (result.error) { + send(res, 400, result); + return; + } + modlog.record(by, "music uploaded", "", `den ${result.item.id} ${result.item.name}`); + send(res, 200, {ok: `"${result.item.name}" is den item ${result.item.id}. Players can get it from the item finder now${result.item.inShop ? ", and the shop" : ""}.`, + item: {id: result.item.id, name: result.item.name}}); +}; + // a picture still waiting for a moderator, which /itemart/ does not serve: // to its maker (the account page) and to staff who review or make items. const itemArtView = (req, res, url, signedIn, adminSignedIn) => { @@ -731,6 +802,10 @@ const handle = async (req, res) => { await itemArtUpload(req, res, url, from, null, panel.signedIn); return; } + if (url.pathname === "/admin/api/music") { + await musicRoute(req, res, url, from, null, panel.signedIn); + return; + } await panel.handle(req, res, url, from, {send, readJson}); return; } @@ -757,6 +832,10 @@ const handle = async (req, res) => { await itemArtUpload(req, res, url, from, modpanel.signedInAs); return; } + if (isMod && url.pathname === "/mod/api/music") { + await musicRoute(req, res, url, from, modpanel.signedInAs); + return; + } if (isMod) { await modpanel.handle(req, res, url, from, {send, readJson}); return; @@ -1043,8 +1122,9 @@ const handle = async (req, res) => { send(res, body ? 200 : 404, body || "Not found", body ? MIME[".swf"] : "text/plain"); return; } - // the extra den music tracks, which the CDN does not have as files. - const extra = await extraMusic.asset(relPath); + // the extra den music tracks, which the CDN does not have as files, and + // those uploaded by staff. + const extra = await extraMusic.asset(relPath) || await customMusic.asset(relPath); if (extra) { send(res, extra.body ? 200 : 404, extra.body || "Not found", extra.body ? extra.type : "text/plain"); return; diff --git a/lib/modpanel.js b/lib/modpanel.js index 04d46d7..ffd9ea8 100644 --- a/lib/modpanel.js +++ b/lib/modpanel.js @@ -395,7 +395,7 @@ const PAGE_HEADERS = { "X-Frame-Options": "DENY", "X-Content-Type-Options": "nosniff", "Referrer-Policy": "no-referrer", - "Content-Security-Policy": captcha.policy("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; " + "Content-Security-Policy": captcha.policy("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; media-src 'self'; font-src 'self'; " + "form-action 'none'; frame-ancestors 'none'; base-uri 'none'"), }; diff --git a/lib/panel.js b/lib/panel.js index 21a783f..f476813 100644 --- a/lib/panel.js +++ b/lib/panel.js @@ -600,7 +600,7 @@ const PAGE_HEADERS = { "X-Frame-Options": "DENY", "X-Content-Type-Options": "nosniff", "Referrer-Policy": "no-referrer", - "Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; " + "Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; media-src 'self'; font-src 'self'; " + "form-action 'none'; frame-ancestors 'none'; base-uri 'none'", }; diff --git a/lib/roles.js b/lib/roles.js index 74b37a7..87045f7 100644 --- a/lib/roles.js +++ b/lib/roles.js @@ -32,11 +32,12 @@ const PERMISSIONS = { "items.create": "make new items from the game's art, change and delete them, lock items out of the item finder and take items from players (!item, !lock, !unlock, !locked, !take)", "room.effects": "run temporary room-wide size and colour effects (!roomfx)", "theater.control": "play or stop a YouTube video for everyone in the theatre", + "music.upload": "upload MP3s as den music tracks, and delete those uploaded (the Music page)", }; const HELPER = ["server.view", "player.view", "staff.list", "player.warn", "player.goto", "chat.silence"]; const MODERATOR = [...HELPER, "player.kick", "player.summon", "staff.invisible", "player.ban", "player.names", "address.ban", "items.approve", "items.restrict", "theater.control"]; -const ADMIN = [...MODERATOR, "items.create", "room.effects"]; +const ADMIN = [...MODERATOR, "items.create", "room.effects", "music.upload"]; // a developer makes things, and moderates nobody. const DEVELOPER = ["server.view", "staff.list", "items.create", "room.effects", "theater.control"]; diff --git a/public/admin.html b/public/admin.html index b337ab4..714ce8b 100644 --- a/public/admin.html +++ b/public/admin.html @@ -413,6 +413,7 @@ + @@ -735,6 +736,31 @@ + +