Fix for undefined url param in some pages

This commit is contained in:
yawhn 2022-11-03 02:03:27 +02:00
parent 9bc3f885bc
commit dba4d2240e
1 changed files with 553 additions and 544 deletions

View File

@ -2,48 +2,51 @@
* Web Client Server * Web Client Server
*/ */
import { dirname } from 'node:path'; import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs'; import { readFileSync } from 'node:fs';
import Koa from 'koa'; import Koa from 'koa';
import Router from '@koa/router'; import Router from '@koa/router';
import send from 'koa-send'; import send from 'koa-send';
import favicon from 'koa-favicon'; import favicon from 'koa-favicon';
import views from 'koa-views'; import views from 'koa-views';
import sharp from 'sharp'; import sharp from 'sharp';
import { createBullBoard } from '@bull-board/api'; import { createBullBoard } from '@bull-board/api';
import { BullAdapter } from '@bull-board/api/bullAdapter.js'; import { BullAdapter } from '@bull-board/api/bullAdapter.js';
import { KoaAdapter } from '@bull-board/koa'; import { KoaAdapter } from '@bull-board/koa';
import { In, IsNull } from 'typeorm'; import { In, IsNull } from 'typeorm';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { fetchMeta } from '@/misc/fetch-meta.js';
import config from '@/config/index.js'; import config from '@/config/index.js';
import { Users, Notes, UserProfiles, Pages, Channels, Clips, GalleryPosts } from '@/models/index.js'; import { Users, Notes, UserProfiles, Pages, Channels, Clips, GalleryPosts } from '@/models/index.js';
import * as Acct from '@/misc/acct.js'; import * as Acct from '@/misc/acct.js';
import { getNoteSummary } from '@/misc/get-note-summary.js'; import { getNoteSummary } from '@/misc/get-note-summary.js';
import { queues } from '@/queue/queues.js'; import { queues } from '@/queue/queues.js';
import { genOpenapiSpec } from '../api/openapi/gen-spec.js'; import { genOpenapiSpec } from '../api/openapi/gen-spec.js';
import { urlPreviewHandler } from './url-preview.js'; import { urlPreviewHandler } from './url-preview.js';
import { manifestHandler } from './manifest.js'; import { manifestHandler } from './manifest.js';
import packFeed from './feed.js'; import packFeed from './feed.js';
import { MINUTE, DAY } from '@/const.js'; import { MINUTE, DAY } from '@/const.js';
const _filename = fileURLToPath(import.meta.url); const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename); const _dirname = dirname(_filename);
const staticAssets = `${_dirname}/../../../assets/`; const staticAssets = `${_dirname}/../../../assets/`;
const clientAssets = `${_dirname}/../../../../client/assets/`; const clientAssets = `${_dirname}/../../../../client/assets/`;
const assets = `${_dirname}/../../../../../built/_client_dist_/`; const assets = `${_dirname}/../../../../../built/_client_dist_/`;
const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`; const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`;
// Init app // Init app
const app = new Koa(); const app = new Koa();
//#region Bull Dashboard //#region Bull Dashboard
const bullBoardPath = '/queue'; const bullBoardPath = '/queue';
// Authenticate // used as a url param to prevent caching css and images
app.use(async (ctx, next) => { const nowDateMs = Date.now();
// Authenticate
app.use(async (ctx, next) => {
if (ctx.path === bullBoardPath || ctx.path.startsWith(bullBoardPath + '/')) { if (ctx.path === bullBoardPath || ctx.path.startsWith(bullBoardPath + '/')) {
const token = ctx.cookies.get('token'); const token = ctx.cookies.get('token');
if (token == null) { if (token == null) {
@ -57,21 +60,21 @@ app.use(async (ctx, next) => {
} }
} }
await next(); await next();
}); });
const serverAdapter = new KoaAdapter(); const serverAdapter = new KoaAdapter();
createBullBoard({ createBullBoard({
queues: queues.map(q => new BullAdapter(q)), queues: queues.map(q => new BullAdapter(q)),
serverAdapter, serverAdapter,
}); });
serverAdapter.setBasePath(bullBoardPath); serverAdapter.setBasePath(bullBoardPath);
app.use(serverAdapter.registerPlugin()); app.use(serverAdapter.registerPlugin());
//#endregion //#endregion
// Init renderer // Init renderer
app.use(views(_dirname + '/views', { app.use(views(_dirname + '/views', {
extension: 'pug', extension: 'pug',
options: { options: {
version: config.version, version: config.version,
@ -80,52 +83,52 @@ app.use(views(_dirname + '/views', {
JSON.parse(readFileSync(`${_dirname}/../../../../../built/_client_dist_/manifest.json`, 'utf-8'))['src/init.ts'], JSON.parse(readFileSync(`${_dirname}/../../../../../built/_client_dist_/manifest.json`, 'utf-8'))['src/init.ts'],
config, config,
}, },
})); }));
// Serve favicon // Serve favicon
app.use(favicon(`${_dirname}/../../../assets/favicon.ico`)); app.use(favicon(`${_dirname}/../../../assets/favicon.ico`));
// Common request handler // Common request handler
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
// IFrameの中に入れられないようにする // IFrameの中に入れられないようにする
ctx.set('X-Frame-Options', 'DENY'); ctx.set('X-Frame-Options', 'DENY');
await next(); await next();
}); });
// Init router // Init router
const router = new Router(); const router = new Router();
//#region static assets //#region static assets
router.get('/static-assets/(.*)', async ctx => { router.get('/static-assets/(.*)', async ctx => {
await send(ctx as any, ctx.path.replace('/static-assets/', ''), { await send(ctx as any, ctx.path.replace('/static-assets/', ''), {
root: staticAssets, root: staticAssets,
maxage: 7 * DAY, maxage: 7 * DAY,
}); });
}); });
router.get('/client-assets/(.*)', async ctx => { router.get('/client-assets/(.*)', async ctx => {
await send(ctx as any, ctx.path.replace('/client-assets/', ''), { await send(ctx as any, ctx.path.replace('/client-assets/', ''), {
root: clientAssets, root: clientAssets,
maxage: 7 * DAY, maxage: 7 * DAY,
}); });
}); });
router.get('/assets/(.*)', async ctx => { router.get('/assets/(.*)', async ctx => {
await send(ctx as any, ctx.path.replace('/assets/', ''), { await send(ctx as any, ctx.path.replace('/assets/', ''), {
root: assets, root: assets,
maxage: 7 * DAY, maxage: 7 * DAY,
}); });
}); });
// Apple touch icon // Apple touch icon
router.get('/apple-touch-icon.png', async ctx => { router.get('/apple-touch-icon.png', async ctx => {
await send(ctx as any, '/apple-touch-icon.png', { await send(ctx as any, '/apple-touch-icon.png', {
root: staticAssets, root: staticAssets,
}); });
}); });
router.get('/twemoji/(.*)', async ctx => { router.get('/twemoji/(.*)', async ctx => {
const path = ctx.path.replace('/twemoji/', ''); const path = ctx.path.replace('/twemoji/', '');
if (!path.match(/^[0-9a-f-]+\.svg$/)) { if (!path.match(/^[0-9a-f-]+\.svg$/)) {
@ -139,9 +142,9 @@ router.get('/twemoji/(.*)', async ctx => {
root: `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`, root: `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`,
maxage: 30 * DAY, maxage: 30 * DAY,
}); });
}); });
router.get('/twemoji-badge/(.*)', async ctx => { router.get('/twemoji-badge/(.*)', async ctx => {
const path = ctx.path.replace('/twemoji-badge/', ''); const path = ctx.path.replace('/twemoji-badge/', '');
if (!path.match(/^[0-9a-f-]+\.png$/)) { if (!path.match(/^[0-9a-f-]+\.png$/)) {
@ -182,42 +185,42 @@ router.get('/twemoji-badge/(.*)', async ctx => {
ctx.set('Cache-Control', 'max-age=2592000'); ctx.set('Cache-Control', 'max-age=2592000');
ctx.set('Content-Type', 'image/png'); ctx.set('Content-Type', 'image/png');
ctx.body = buffer; ctx.body = buffer;
}); });
// ServiceWorker // ServiceWorker
router.get(`/sw.js`, async ctx => { router.get(`/sw.js`, async ctx => {
await send(ctx as any, `/sw.js`, { await send(ctx as any, `/sw.js`, {
root: swAssets, root: swAssets,
maxage: 10 * MINUTE, maxage: 10 * MINUTE,
}); });
}); });
// Manifest // Manifest
router.get('/manifest.json', manifestHandler); router.get('/manifest.json', manifestHandler);
router.get('/robots.txt', async ctx => { router.get('/robots.txt', async ctx => {
await send(ctx as any, '/robots.txt', { await send(ctx as any, '/robots.txt', {
root: staticAssets, root: staticAssets,
}); });
}); });
//#endregion //#endregion
// Docs // Docs
router.get('/api-doc', async ctx => { router.get('/api-doc', async ctx => {
await send(ctx as any, '/redoc.html', { await send(ctx as any, '/redoc.html', {
root: staticAssets, root: staticAssets,
}); });
}); });
// URL preview endpoint // URL preview endpoint
router.get('/url', urlPreviewHandler); router.get('/url', urlPreviewHandler);
router.get('/api.json', async ctx => { router.get('/api.json', async ctx => {
ctx.body = genOpenapiSpec(); ctx.body = genOpenapiSpec();
}); });
const getFeed = async (acct: string) => { const getFeed = async (acct: string) => {
const meta = await fetchMeta(); const meta = await fetchMeta();
if (meta.privateMode) { if (meta.privateMode) {
return; return;
@ -230,10 +233,10 @@ const getFeed = async (acct: string) => {
}); });
return user && await packFeed(user); return user && await packFeed(user);
}; };
// Atom // Atom
router.get('/@:user.atom', async ctx => { router.get('/@:user.atom', async ctx => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user);
if (feed) { if (feed) {
@ -242,10 +245,10 @@ router.get('/@:user.atom', async ctx => {
} else { } else {
ctx.status = 404; ctx.status = 404;
} }
}); });
// RSS // RSS
router.get('/@:user.rss', async ctx => { router.get('/@:user.rss', async ctx => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user);
if (feed) { if (feed) {
@ -254,10 +257,10 @@ router.get('/@:user.rss', async ctx => {
} else { } else {
ctx.status = 404; ctx.status = 404;
} }
}); });
// JSON // JSON
router.get('/@:user.json', async ctx => { router.get('/@:user.json', async ctx => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user);
if (feed) { if (feed) {
@ -266,11 +269,11 @@ router.get('/@:user.json', async ctx => {
} else { } else {
ctx.status = 404; ctx.status = 404;
} }
}); });
//#region SSR (for crawlers) //#region SSR (for crawlers)
// User // User
router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => { router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
const { username, host } = Acct.parse(ctx.params.user); const { username, host } = Acct.parse(ctx.params.user);
const user = await Users.findOneBy({ const user = await Users.findOneBy({
usernameLower: username.toLowerCase(), usernameLower: username.toLowerCase(),
@ -295,6 +298,7 @@ router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
icon: meta.iconUrl, icon: meta.iconUrl,
themeColor: meta.themeColor, themeColor: meta.themeColor,
privateMode: meta.privateMode, privateMode: meta.privateMode,
nowDateMs: nowDateMs,
}); });
ctx.set('Cache-Control', 'public, max-age=15'); ctx.set('Cache-Control', 'public, max-age=15');
} else { } else {
@ -302,9 +306,9 @@ router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
// モデレータがAPI経由で参照可能にするために404にはしない // モデレータがAPI経由で参照可能にするために404にはしない
await next(); await next();
} }
}); });
router.get('/users/:user', async ctx => { router.get('/users/:user', async ctx => {
const user = await Users.findOneBy({ const user = await Users.findOneBy({
id: ctx.params.user, id: ctx.params.user,
host: IsNull(), host: IsNull(),
@ -317,10 +321,10 @@ router.get('/users/:user', async ctx => {
} }
ctx.redirect(`/@${user.username}${ user.host == null ? '' : '@' + user.host}`); ctx.redirect(`/@${user.username}${ user.host == null ? '' : '@' + user.host}`);
}); });
// Note // Note
router.get('/notes/:note', async (ctx, next) => { router.get('/notes/:note', async (ctx, next) => {
const note = await Notes.findOneBy({ const note = await Notes.findOneBy({
id: ctx.params.note, id: ctx.params.note,
visibility: In(['public', 'home']), visibility: In(['public', 'home']),
@ -340,6 +344,7 @@ router.get('/notes/:note', async (ctx, next) => {
icon: meta.iconUrl, icon: meta.iconUrl,
privateMode: meta.privateMode, privateMode: meta.privateMode,
themeColor: meta.themeColor, themeColor: meta.themeColor,
nowDateMs: nowDateMs,
}); });
ctx.set('Cache-Control', 'public, max-age=15'); ctx.set('Cache-Control', 'public, max-age=15');
@ -348,10 +353,10 @@ router.get('/notes/:note', async (ctx, next) => {
} }
await next(); await next();
}); });
// Page // Page
router.get('/@:user/pages/:page', async (ctx, next) => { router.get('/@:user/pages/:page', async (ctx, next) => {
const { username, host } = Acct.parse(ctx.params.user); const { username, host } = Acct.parse(ctx.params.user);
const user = await Users.findOneBy({ const user = await Users.findOneBy({
usernameLower: username.toLowerCase(), usernameLower: username.toLowerCase(),
@ -377,6 +382,7 @@ router.get('/@:user/pages/:page', async (ctx, next) => {
icon: meta.iconUrl, icon: meta.iconUrl,
themeColor: meta.themeColor, themeColor: meta.themeColor,
privateMode: meta.privateMode, privateMode: meta.privateMode,
nowDateMs: nowDateMs,
}); });
if (['public'].includes(page.visibility)) { if (['public'].includes(page.visibility)) {
@ -389,11 +395,11 @@ router.get('/@:user/pages/:page', async (ctx, next) => {
} }
await next(); await next();
}); });
// Clip // Clip
// TODO: 非publicなclipのハンドリング // TODO: 非publicなclipのハンドリング
router.get('/clips/:clip', async (ctx, next) => { router.get('/clips/:clip', async (ctx, next) => {
const clip = await Clips.findOneBy({ const clip = await Clips.findOneBy({
id: ctx.params.clip, id: ctx.params.clip,
}); });
@ -410,6 +416,7 @@ router.get('/clips/:clip', async (ctx, next) => {
privateMode: meta.privateMode, privateMode: meta.privateMode,
icon: meta.iconUrl, icon: meta.iconUrl,
themeColor: meta.themeColor, themeColor: meta.themeColor,
nowDateMs: nowDateMs,
}); });
ctx.set('Cache-Control', 'public, max-age=15'); ctx.set('Cache-Control', 'public, max-age=15');
@ -418,10 +425,10 @@ router.get('/clips/:clip', async (ctx, next) => {
} }
await next(); await next();
}); });
// Gallery post // Gallery post
router.get('/gallery/:post', async (ctx, next) => { router.get('/gallery/:post', async (ctx, next) => {
const post = await GalleryPosts.findOneBy({ id: ctx.params.post }); const post = await GalleryPosts.findOneBy({ id: ctx.params.post });
if (post) { if (post) {
@ -436,6 +443,7 @@ router.get('/gallery/:post', async (ctx, next) => {
icon: meta.iconUrl, icon: meta.iconUrl,
themeColor: meta.themeColor, themeColor: meta.themeColor,
privateMode: meta.privateMode, privateMode: meta.privateMode,
nowDateMs: nowDateMs,
}); });
ctx.set('Cache-Control', 'public, max-age=15'); ctx.set('Cache-Control', 'public, max-age=15');
@ -444,10 +452,10 @@ router.get('/gallery/:post', async (ctx, next) => {
} }
await next(); await next();
}); });
// Channel // Channel
router.get('/channels/:channel', async (ctx, next) => { router.get('/channels/:channel', async (ctx, next) => {
const channel = await Channels.findOneBy({ const channel = await Channels.findOneBy({
id: ctx.params.channel, id: ctx.params.channel,
}); });
@ -461,6 +469,7 @@ router.get('/channels/:channel', async (ctx, next) => {
icon: meta.iconUrl, icon: meta.iconUrl,
themeColor: meta.themeColor, themeColor: meta.themeColor,
privateMode: meta.privateMode, privateMode: meta.privateMode,
nowDateMs: nowDateMs,
}); });
ctx.set('Cache-Control', 'public, max-age=15'); ctx.set('Cache-Control', 'public, max-age=15');
@ -469,10 +478,10 @@ router.get('/channels/:channel', async (ctx, next) => {
} }
await next(); await next();
}); });
//#endregion //#endregion
router.get('/_info_card_', async ctx => { router.get('/_info_card_', async ctx => {
const meta = await fetchMeta(true); const meta = await fetchMeta(true);
if (meta.privateMode) { if (meta.privateMode) {
ctx.status = 403; ctx.status = 403;
@ -488,35 +497,35 @@ router.get('/_info_card_', async ctx => {
originalUsersCount: await Users.countBy({ host: IsNull() }), originalUsersCount: await Users.countBy({ host: IsNull() }),
originalNotesCount: await Notes.countBy({ userHost: IsNull() }), originalNotesCount: await Notes.countBy({ userHost: IsNull() }),
}); });
}); });
router.get('/bios', async ctx => { router.get('/bios', async ctx => {
await ctx.render('bios', { await ctx.render('bios', {
version: config.version, version: config.version,
}); });
}); });
router.get('/cli', async ctx => { router.get('/cli', async ctx => {
await ctx.render('cli', { await ctx.render('cli', {
version: config.version, version: config.version,
}); });
}); });
const override = (source: string, target: string, depth = 0) => const override = (source: string, target: string, depth = 0) =>
[, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/'); [, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/');
router.get('/flush', async ctx => { router.get('/flush', async ctx => {
await ctx.render('flush'); await ctx.render('flush');
}); });
// streamingに非WebSocketリクエストが来た場合にbase htmlをキャシュ付きで返すと、Proxy等でそのパスがキャッシュされておかしくなる // streamingに非WebSocketリクエストが来た場合にbase htmlをキャシュ付きで返すと、Proxy等でそのパスがキャッシュされておかしくなる
router.get('/streaming', async ctx => { router.get('/streaming', async ctx => {
ctx.status = 503; ctx.status = 503;
ctx.set('Cache-Control', 'private, max-age=0'); ctx.set('Cache-Control', 'private, max-age=0');
}); });
// Render base html for all requests // Render base html for all requests
router.get('(.*)', async ctx => { router.get('(.*)', async ctx => {
const meta = await fetchMeta(); const meta = await fetchMeta();
let motd = ['Loading...']; let motd = ['Loading...'];
if (meta.customMOTD.length > 0) { if (meta.customMOTD.length > 0) {
@ -526,7 +535,6 @@ router.get('(.*)', async ctx => {
if (meta.customSplashIcons.length > 0) { if (meta.customSplashIcons.length > 0) {
splashIconUrl = meta.customSplashIcons[Math.floor(Math.random() * meta.customSplashIcons.length)]; splashIconUrl = meta.customSplashIcons[Math.floor(Math.random() * meta.customSplashIcons.length)];
} }
const nowDateMs = Date.now();
await ctx.render('base', { await ctx.render('base', {
img: meta.bannerUrl, img: meta.bannerUrl,
title: meta.name || 'Calckey', title: meta.name || 'Calckey',
@ -540,9 +548,10 @@ router.get('(.*)', async ctx => {
nowDateMs: nowDateMs, nowDateMs: nowDateMs,
}); });
ctx.set('Cache-Control', 'public, max-age=3'); ctx.set('Cache-Control', 'public, max-age=3');
}); });
// Register router // Register router
app.use(router.routes()); app.use(router.routes());
export default app;
export default app;