Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add antibot infrastructure #359

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
antibot

node_modules
bun.lock

**/.DS_store
.env
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dotenv/config';

import { ipFilterMiddleware, ipFilterError } from './server/moderation/ip-filter.js';
import { isUsingAntibot } from './server/moderation/using-antibot.js';
import { WEBSOCKET_MAX_PAYLOAD, COOKIE_MAX_AGE } from './constants.js';
import indexRouter from './routes/index.js';
import webhookRouter from './routes/api/webhook.js';
Expand Down Expand Up @@ -44,6 +45,11 @@ app.use(cookieSession({
app.use(ipFilterMiddleware);
app.use(ipFilterError);

if (isUsingAntibot) {
const { default: antibot } = await import('./server/antibot/middleware.js');
app.use(antibot);
}

wss.on('connection', handleWssConnection);

app.use(indexRouter);
Expand Down
5 changes: 5 additions & 0 deletions server/moderation/using-antibot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fs from 'fs';
import path from 'path';

const isUsingAntibot = fs.existsSync(path.join(import.meta.dirname, '..', 'antibot'));
export { isUsingAntibot };
8 changes: 7 additions & 1 deletion server/multiplayer/handle-wss-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { checkToken } from '../authentication.js';
import getRandomName from '../../quizbowl/get-random-name.js';
import hasValidCharacters from '../moderation/has-valid-characters.js';
import isAppropriateString from '../moderation/is-appropriate-string.js';
import { isUsingAntibot } from '../moderation/using-antibot.js';

import createDOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';
Expand Down Expand Up @@ -45,7 +46,12 @@ function createAndReturnRoom (roomName, userId, isPrivate = false, isControlled
* @param {WebSocket} ws
* @param {http.IncomingMessage} req
*/
export default function handleWssConnection (ws, req) {
export default async function handleWssConnection (ws, req) {
if (isUsingAntibot) {
let clearance = (await import('../antibot/check.js')).canConnectTo(ws, req);
if (!clearance) return ws.close();
}

const parsedUrl = new url.URL(req.url, process.env.BASE_URL ?? 'http://localhost');
const isPrivate = parsedUrl.searchParams.get('private') === 'true';
const isControlled = parsedUrl.searchParams.get('controlled') === 'true';
Expand Down