[Web] Replace express with hono. (#21)

This commit is contained in:
2026-10-08 07:35:28 -07:00
committed by GitHub
parent 2581c733a6
commit d0cb22e98f
6 changed files with 436 additions and 1249 deletions
+22 -948
View File
File diff suppressed because it is too large Load Diff
+2 -6
View File
@@ -3,8 +3,6 @@
"devDependencies": { "devDependencies": {
"@eslint/js": "9.39.5", "@eslint/js": "9.39.5",
"@stylistic/eslint-plugin": "5.2.1", "@stylistic/eslint-plugin": "5.2.1",
"@types/express": "5.0.2",
"@types/express-session": "1.18.1",
"copyfiles": "2.4.1", "copyfiles": "2.4.1",
"eslint": "9.39.5", "eslint": "9.39.5",
"rimraf": "6.0.1", "rimraf": "6.0.1",
@@ -14,12 +12,10 @@
}, },
"dependencies": { "dependencies": {
"@clynamic/dmark": "0.0.11", "@clynamic/dmark": "0.0.11",
"@hono/node-server": "2.1.1",
"@redis/client": "5.1.0", "@redis/client": "5.1.0",
"body-parser": "2.3.0",
"discord.js": "14.27.0", "discord.js": "14.27.0",
"express": "5.1.0", "hono": "4.13.10",
"express-session": "1.19.0",
"memorystore": "1.6.7",
"sqlite": "5.1.1", "sqlite": "5.1.1",
"sqlite3": "6.0.1" "sqlite3": "6.0.1"
}, },
+12 -294
View File
@@ -1,302 +1,20 @@
import bodyParser from 'body-parser';
import crypto from 'crypto';
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import express, { Request, Response } from 'express'; import { serve } from '@hono/node-server';
import session from 'express-session'; import { Hono } from 'hono';
import fs from 'fs';
import MemoryStore from 'memorystore';
import path from 'path';
import { config } from '../config'; import { config } from '../config';
import { Database } from '../shared/Database'; import routes from './routes';
import { AltData, comprehensiveAltLookupFromE621, DiscordOAuth2 } from '../utils';
import { logDebug } from '../utils/debug-utils';
import { fixPings, removeIssueLinks } from '../utils/github-user-utils';
declare module 'express-session' {
interface SessionData {
username: string;
userId: string;
oauthState: string;
}
}
const GITHUB_REPO_ID = 169334303;
const DEV_BASE_URL = `http://localhost:${config.PORT}`;
const PROD_BASE_URL = 'https://discord.e621.net';
const OAUTH_SCOPES = ['identify', 'guilds.join'];
const PAGE_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'page.html'), { encoding: 'utf-8' });
const oauth = new DiscordOAuth2({
clientId: config.DISCORD_CLIENT_ID!,
clientSecret: config.DISCORD_CLIENT_SECRET!,
redirectUri: `${config.DEV_MODE ? DEV_BASE_URL : PROD_BASE_URL}/callback`,
clientToken: config.DISCORD_TOKEN!,
credentials: Buffer.from(`${config.DISCORD_CLIENT_ID!}:${config.DISCORD_CLIENT_SECRET!}`).toString('base64')
});
const enum JoinResponse {
Success = 1,
Error = 2,
Banned = 3,
Underage = 4
};
async function joinGuild(code: string, userId: string, username: string): Promise<JoinResponse> {
let tokenResponse;
try {
if (Number.isNaN(userId)) return JoinResponse.Error;
if (!username) return JoinResponse.Error;
const id = Number(userId);
tokenResponse = await oauth.getAccessToken(code, OAUTH_SCOPES);
const user = await oauth.getUser(tokenResponse.access_token);
if (!user.id || !user.username) {
console.error(`Error joining user (${userId}) to discord. User object missing id or username.`);
console.error(user);
return JoinResponse.Error;
}
await Database.putUser(id, user);
const alts = await comprehensiveAltLookupFromE621(id, null);
if (await checkAltsForFullBans([alts])) return JoinResponse.Banned;
const response = await oauth.addMember({
accessToken: tokenResponse.access_token,
guildId: config.DISCORD_GUILD_ID!,
userId: user.id,
nickname: username
});
if (config.DEBUG) console.log(response);
if (!response) return JoinResponse.Error;
} catch (e: any) {
if (e.code == 40007) return JoinResponse.Banned;
else if (e.code == 20024) return JoinResponse.Underage;
console.error(`Error joining user (${userId}) to discord:`);
console.error(e);
return JoinResponse.Error;
} finally {
if (tokenResponse) await oauth.revokeToken(tokenResponse.access_token);
}
return JoinResponse.Success;
}
async function handleInitial(req: Request, res: Response): Promise<any> {
const { username, user_id, time, hash } = req.query;
if (!username || !user_id || !time || !hash) {
return sendBadRequest(res, 'Missing parameters');
}
if (Number.isNaN(time) || Date.now() / 1000 > Number(time)) {
return render(res, 403, 'You took too long to authorize the request. Please try again.');
}
const authString = `${username} ${user_id} ${time} ${config.LINK_SECRET}`;
const digest = crypto.createHash('sha256').update(authString).digest('hex');
if (hash !== digest) {
console.error(`Bad auth: ${hash} ${digest}`);
return sendForbidden(res, 'Bad auth');
}
const oauthState = crypto.randomBytes(16).toString('hex');
const oauthUrl = await oauth.generateOauth2Url({
state: oauthState,
scope: OAUTH_SCOPES,
type: 'code'
});
req.session.username = username as string;
req.session.userId = user_id as string;
req.session.oauthState = oauthState;
req.session.save((e) => {
if (e) {
console.error('Error saving session:');
console.error(e);
return sendInteralServerError(res);
}
res.redirect(oauthUrl);
});
}
async function handleCallback(req: Request, res: Response): Promise<any> {
if (!req.session.userId || !req.session.username || !req.session.oauthState) {
return sendForbidden(res, 'Session details missing');
}
const state = req.query.state as string;
if (state != req.session.oauthState) {
console.error('OAuth state mismatch on discord joining');
return sendForbidden(res, 'OAuth state mismatch');
}
const code = req.query.code as string;
const userId = req.session.userId;
const username = req.session.username;
req.session.destroy((e) => {
if (e) console.error(e);
});
try {
const response = await joinGuild(code, userId, username);
if (response == JoinResponse.Error) {
console.error(`Error joining user: ${username} (${userId})`);
return sendInteralServerError(res, 'Unable to join user to guild. Retry later. If issue persists, please contact staff.');
} else if (response == JoinResponse.Banned) {
return sendForbidden(res, 'User is banned.');
} else if (response == JoinResponse.Underage) {
return sendForbidden(res, 'Discord account flagged as underage by discord.');
}
} catch (e) {
console.error(e);
return sendInteralServerError(res);
}
render(res, 200, 'Success', `You have been added to the server. <a href="https://discord.com/channels/${config.DISCORD_GUILD_ID}">See you there.</a>`);
}
function sendInteralServerError(res: Response, message: string = '') {
render(res, 500, 'Internal Server Error', message);
}
function sendForbidden(res: Response, message: string = '') {
render(res, 403, 'Forbidden', message);
}
function sendBadRequest(res: Response, message: string = '') {
render(res, 400, 'Bad Request', message);
}
function render(res: Response, code: number, title: string = '', message: string = '') {
res.status(code).setHeader('Content-Type', 'text/html').send(PAGE_TEMPLATE.replaceAll('{{ title }}', title).replaceAll('{{ message }}', message));
}
async function handleGithubRelease(client: Client, req: Request, res: Response): Promise<any> {
logDebug('Received github release webhook');
const signature = (req.headers['x-hub-signature-256'] as string).split('=')[1];
const computedSignature = crypto.createHmac('sha256', config.RELEASE_SECRET!).update(req.body).digest('hex');
if (signature !== computedSignature) {
console.error('Github release webhook signature mismatch');
return res.sendStatus(401);
}
res.sendStatus(200);
const data = JSON.parse(req.body);
logDebug(`Release webhook data:\n${JSON.stringify(data, null, 4)}`);
if (data.action != 'published' || data.repository.id != GITHUB_REPO_ID) return;
const settings = await Database.getOrCreateSettings(config.DISCORD_GUILD_ID!);
if (!settings || !settings.github_release_channel) return;
const channel = await client.channels.fetch(settings.github_release_channel);
if (!channel || !channel.isSendable()) {
console.error(`Github release channel ${channel ? 'sendable' : 'found'}`);
return;
}
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const date = new Date();
let message = `## [${months[date.getUTCMonth()]} ${date.getUTCDate()}, ${date.getUTCFullYear()}](<${data.release.html_url}>)\n\n${settings.site_breaker_role_id ? `<@&${settings.site_breaker_role_id}>\n` : ''}${await fixPings(removeIssueLinks(data.release.body))}`;
logDebug('Sending github release message');
const MAX_MESSAGE_LENGTH = 2000;
const ADDITIONAL_PART = '...\n\nYou may view the full changelog on github.';
if (message.length > MAX_MESSAGE_LENGTH) {
const splitMessage = message.split('\n');
message = '';
for (const part of splitMessage) {
if (message.length + part.length + 1 >= MAX_MESSAGE_LENGTH - ADDITIONAL_PART.length) break;
message += `${part}\n`;
}
message += ADDITIONAL_PART;
}
const sentMessage = await channel.send(message);
await sentMessage.startThread({ name: data.release.tag_name });
logDebug('Github webhook processed');
}
async function checkAltsForFullBans(altData: AltData[]): Promise<boolean> {
for (const data of altData) {
if (data.type == 'discord') {
try {
const banData = await Database.getBan(data.thisId as string);
if (banData?.full_ban) return true;
} catch (e) {
console.error(e);
}
}
if (await checkAltsForFullBans(data.alts)) return true;
}
return false;
}
export function initializeWebserver(client: Client) { export function initializeWebserver(client: Client) {
const app = express(); const app = new Hono();
const Store = MemoryStore(session); routes.forEach(route => app.route(route.route, route.builder(client)));
app.set('trust proxy', 1); const server = serve({
fetch: app.fetch,
app.use(session({ port: config.PORT
secret: config.DISCORD_CLIENT_SECRET!, }, (info) => {
cookie: { console.log(`Listening on port ${info.port}`);
secure: !config.DEV_MODE,
httpOnly: !config.DEV_MODE,
sameSite: false,
maxAge: 300000
},
store: new Store({
checkPeriod: 600000,
}),
resave: false,
saveUninitialized: false
}));
app.get('/', handleInitial);
app.get('/callback', handleCallback);
app.use(bodyParser.raw({ type: 'application/json' }));
app.post('/release', handleGithubRelease.bind(null, client));
app.listen(config.PORT, (error) => {
if (error) {
throw error;
}
console.log(`Listening on port ${config.PORT}`);
}); });
return server;
} }
+273
View File
@@ -0,0 +1,273 @@
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import { Hono, type Context } from 'hono';
import { getCookie, setCookie, deleteCookie } from 'hono/cookie';
import { ContentfulStatusCode } from 'hono/utils/http-status';
import { config } from '../../config';
import { Database } from '../../shared/Database';
import { AltData, comprehensiveAltLookupFromE621, DiscordOAuth2 } from '../../utils';
const DEV_BASE_URL = `http://localhost:${config.PORT}`;
const PROD_BASE_URL = 'https://discord.e621.net';
const OAUTH_SCOPES = ['identify', 'guilds.join'];
const PAGE_TEMPLATE = fs.readFileSync(path.join(__dirname, '..', 'templates', 'page.html'), { encoding: 'utf-8' });
const SESSION_COOKIE = 'discord_oauth_session';
const SESSION_MAX_AGE = 300;
interface SessionData {
username?: string;
userId?: string;
oauthState?: string;
expiresAt: number;
}
const sessions = new Map<string, SessionData>();
const oauth = new DiscordOAuth2({
clientId: config.DISCORD_CLIENT_ID!,
clientSecret: config.DISCORD_CLIENT_SECRET!,
redirectUri: `${config.DEV_MODE ? DEV_BASE_URL : PROD_BASE_URL}/callback`,
clientToken: config.DISCORD_TOKEN!,
credentials: Buffer
.from(`${config.DISCORD_CLIENT_ID!}:${config.DISCORD_CLIENT_SECRET!}`)
.toString('base64')
});
const enum JoinResponse {
Success = 1,
Error = 2,
Banned = 3,
Underage = 4
};
// Cleanup expired sessions.
function cleanupSessions(): void {
const now = Date.now();
for (const [id, session] of sessions) {
if (session.expiresAt <= now) sessions.delete(id);
}
}
setInterval(cleanupSessions, 600_000).unref();
function getSession(c: Context): SessionData | undefined {
const sessionId = getCookie(c, SESSION_COOKIE);
if (!sessionId) return undefined;
const session = sessions.get(sessionId);
if (!session) return undefined;
if (session.expiresAt <= Date.now()) {
sessions.delete(sessionId);
return undefined;
}
return session;
}
function createSession(c: Context, data: Omit<SessionData, 'expiresAt'>): void {
const sessionId = crypto.randomBytes(32).toString('hex');
sessions.set(sessionId, {
...data,
expiresAt: Date.now() + SESSION_MAX_AGE * 1000
});
setCookie(c, SESSION_COOKIE, sessionId, {
maxAge: SESSION_MAX_AGE,
httpOnly: !config.DEV_MODE,
secure: !config.DEV_MODE,
sameSite: 'Lax',
path: '/'
});
}
function destroySession(c: Context): void {
const sessionId = getCookie(c, SESSION_COOKIE);
if (sessionId)
sessions.delete(sessionId);
deleteCookie(c, SESSION_COOKIE, {
path: '/'
});
}
async function joinGuild(code: string, userId: string, username: string): Promise<JoinResponse> {
let tokenResponse;
try {
if (Number.isNaN(Number(userId)) || !username) return JoinResponse.Error;
tokenResponse = await oauth.getAccessToken(code, OAUTH_SCOPES);
const user = await oauth.getUser(tokenResponse.access_token);
if (!user.id || !user.username) {
console.error(`Error joining user (${userId}) to discord. User object missing id or username.`, user);
return JoinResponse.Error;
}
const id = Number(userId);
await Database.putUser(id, user);
const alts = await comprehensiveAltLookupFromE621(id, null);
if (await checkAltsForFullBans([alts])) return JoinResponse.Banned;
const response = await oauth.addMember({
accessToken: tokenResponse.access_token,
guildId: config.DISCORD_GUILD_ID!,
userId: user.id,
nickname: username
});
if (config.DEBUG) console.log(response);
if (!response) return JoinResponse.Error;
} catch (e: any) {
switch (e.code) {
case 40007:
return JoinResponse.Banned;
case 20024:
return JoinResponse.Underage;
}
console.error(`Error joining user (${userId}) to discord:`, e);
return JoinResponse.Error;
} finally {
if (tokenResponse)
await oauth.revokeToken(tokenResponse.access_token);
}
return JoinResponse.Success;
}
async function handleInitial(c: Context): Promise<Response> {
const query = c.req.query();
const username = query.username;
const userId = query.user_id;
const time = query.time;
const hash = query.hash;
if (!username || !userId || !time || !hash)
return sendBadRequest(c, 'Missing parameters');
const timestamp = Number(time);
if (Number.isNaN(timestamp) || Date.now() / 1000 > timestamp)
return render(c, 403, 'You took too long to authorize the request. Please try again.');
const authString = `${username} ${userId} ${time} ${config.LINK_SECRET}`;
const digest = crypto.createHash('sha256').update(authString).digest('hex');
if (hash !== digest) {
console.error(`Bad auth: ${hash} ${digest}`);
return sendForbidden(c, 'Bad auth');
}
const oauthState = crypto.randomBytes(16).toString('hex');
const oauthUrl = oauth.generateOauth2Url({
state: oauthState,
scope: OAUTH_SCOPES,
type: 'code'
});
createSession(c, { username, userId, oauthState });
return c.redirect(oauthUrl);
}
async function handleCallback(c: Context): Promise<Response> {
const session = getSession(c);
if (!session?.userId || !session.username || !session.oauthState)
return sendForbidden(c, 'Session details missing');
const query = c.req.query();
const state = query.state;
if (state !== session.oauthState) {
console.error('OAuth state mismatch on discord joining');
return sendForbidden(c, 'OAuth state mismatch');
}
const code = query.code;
if (!code)
return sendBadRequest(c, 'Missing OAuth code');
const userId = session.userId;
const username = session.username;
destroySession(c);
try {
const response = await joinGuild(code, userId, username);
switch (response) {
case JoinResponse.Error:
console.error(`Error joining user: ${username} (${userId})`);
return sendInternalServerError(c, 'Unable to join user to guild. Retry later. If issue persists, please contact staff.');
case JoinResponse.Banned:
return sendForbidden(c, 'User is banned.');
case JoinResponse.Underage:
return sendForbidden(c, 'Discord account flagged as underage by discord.');
}
} catch (e) {
console.error(e);
return sendInternalServerError(c);
}
return render(c, 200, 'Success', `You have been added to the server. <a href="https://discord.com/channels/${config.DISCORD_GUILD_ID}">See you there.</a>`);
}
function sendInternalServerError(c: Context, message: string = ''): Response {
return render(c, 500, 'Internal Server Error', message);
}
function sendForbidden(c: Context, message: string = ''): Response {
return render(c, 403, 'Forbidden', message);
}
function sendBadRequest(c: Context, message: string = ''): Response {
return render(c, 400, 'Bad Request', message);
}
function render(c: Context, code: ContentfulStatusCode, title: string = '', message: string = ''): Response {
const html = PAGE_TEMPLATE
.replaceAll('{{ title }}', title)
.replaceAll('{{ message }}', message);
return c.html(html, code);
}
async function checkAltsForFullBans(altData: AltData[]): Promise<boolean> {
for (const data of altData) {
if (data.type === 'discord') {
try {
const banData = await Database.getBan(data.thisId as string);
if (banData?.full_ban) return true;
} catch (e) {
console.error(e);
}
}
if (await checkAltsForFullBans(data.alts)) return true;
}
return false;
}
export default {
route: '/',
builder: (): Hono => {
const app = new Hono();
app.get('/', c => handleInitial(c));
app.get('/callback', c => handleCallback(c));
return app;
}
};
+122
View File
@@ -0,0 +1,122 @@
import crypto from 'crypto';
import { Client } from 'discord.js';
import { Hono, type Context } from 'hono';
import { config } from '../../config';
import { Database } from '../../shared/Database';
import { fixPings, logDebug, removeIssueLinks } from '../../utils';
const GITHUB_REPO_ID = 169334303;
async function handleGithubRelease(client: Client, c: Context): Promise<Response> {
logDebug('Received github release webhook');
/*
Intentionally read the raw request body here.
GitHub signs the raw bytes, so parsing the body first
would potentially invalidate the signature.
*/
const rawBody = await c.req.raw.arrayBuffer();
const signatureHeader = c.req.header('x-hub-signature-256');
if (!signatureHeader) {
console.error('Github release webhook signature missing');
return c.body(null, 401);
}
const signature = signatureHeader.split('=')[1];
if (!signature) {
console.error('Github release webhook signature malformed');
return c.body(null, 401);
}
const computedSignature = crypto.createHmac('sha256', config.RELEASE_SECRET!)
.update(Buffer.from(rawBody)).digest('hex');
// Use timingSafeEqual rather than a normal string comparison for the HMAC.
const providedBuffer = Buffer.from(signature, 'hex');
const computedBuffer = Buffer.from(computedSignature, 'hex');
if (providedBuffer.length !== computedBuffer.length || !crypto.timingSafeEqual(providedBuffer, computedBuffer)) {
console.error('Github release webhook signature mismatch');
return c.body(null, 401);
}
void processGithubRelease(client, Buffer.from(rawBody));
return c.body(null, 200);
}
async function processGithubRelease(client: Client, rawBody: Buffer): Promise<void> {
try {
const data = JSON.parse(rawBody.toString('utf-8'));
logDebug(`Release webhook data:\n${JSON.stringify(data, null, 4)}`);
if (data.action !== 'published' || data.repository.id !== GITHUB_REPO_ID) return;
const settings = await Database.getOrCreateSettings(config.DISCORD_GUILD_ID!);
if (!settings.github_release_channel) return;
const channel = await client.channels.fetch(settings.github_release_channel);
if (!channel || !channel.isSendable()) {
console.error(`Github release channel not ${channel ? 'sendable' : 'found'}`);
return;
}
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
const date = new Date();
let message = `## [${months[date.getUTCMonth()]} ${date.getUTCDate()}, ${date.getUTCFullYear()}](<${data.release.html_url}>)\n\n${settings.site_breaker_role_id ? `<@&${settings.site_breaker_role_id}>\n` : ''}${await fixPings(removeIssueLinks(data.release.body))}`;
logDebug('Sending github release message');
const MAX_MESSAGE_LENGTH = 2000;
const ADDITIONAL_PART = '...\n\nYou may view the full changelog on github.';
if (message.length > MAX_MESSAGE_LENGTH) {
const splitMessage = message.split('\n');
message = '';
for (const part of splitMessage) {
if (message.length + part.length + 1 >= MAX_MESSAGE_LENGTH - ADDITIONAL_PART.length) break;
message += `${part}\n`;
}
message += ADDITIONAL_PART;
}
const sentMessage = await channel.send(message);
await sentMessage.startThread({
name: data.release.tag_name
});
logDebug('Github webhook processed');
} catch (e) {
console.error('Error processing Github release webhook:', e);
}
}
export default {
route: '/',
builder: (client: Client): Hono => {
const app = new Hono();
app.post('/release', c => handleGithubRelease(client, c));
return app;
}
};
+4
View File
@@ -0,0 +1,4 @@
import discord from './discord';
import github from './github';
export default [discord, github];