Skip to content

Commit

Permalink
feat: move username filter regexes to config
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanger committed Oct 17, 2024
1 parent 532a953 commit 9f8b12d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
5 changes: 5 additions & 0 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ export interface ConfigType {
* Limits the number of teams that can be created by any one IP address.
*/
readonly maxTeams?: number

/**
* If a player's username matches one of the regexes in this array, it will be replaced with the default username.
*/
readonly usernameFilters?: RegExp[]
}

/**
Expand Down
3 changes: 1 addition & 2 deletions server/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ import { PluginManager } from "./pluginManager";
import { Team } from "./team";
import { Grid } from "./utils/grid";
import { IDAllocator } from "./utils/idAllocator";
import { Logger, removeFrom } from "./utils/misc";
import { cleanUsername, Logger, removeFrom } from "./utils/misc";
import { createServer, forbidden, getIP } from "./utils/serverHelpers";
import { cleanUsername } from "./utils/usernameFilter";

/*
eslint-disable
Expand Down
2 changes: 1 addition & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { findGame, games, newGame } from "./gameManager";
import { CustomTeam, CustomTeamPlayer, type CustomTeamPlayerContainer } from "./team";
import { Logger } from "./utils/misc";
import { cors, createServer, forbidden, getIP, textDecoder } from "./utils/serverHelpers";
import { cleanUsername } from "./utils/usernameFilter";
import { cleanUsername } from "./utils/misc";

export interface Punishment {
readonly id: string
Expand Down
13 changes: 13 additions & 0 deletions server/src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { NullString, type ObjectDefinition, type ReferenceTo } from "@common/uti
import { weightedRandom } from "@common/utils/random";

import { LootTiers, type WeightedItem } from "../data/lootTables";
import { Config } from "../config";
import { GameConstants } from "@common/constants";

export const Logger = {
log(...message: string[]): void {
Expand All @@ -23,6 +25,17 @@ function internalLog(...message: string[]): void {
);
}

export function cleanUsername(name?: string | null): string {
return (
!name?.length
|| name.length > 16
|| Config.protection?.usernameFilters?.some(regex => regex.test(name))
|| /[^\x20-\x7E]/g.test(name) // extended ASCII chars
)
? GameConstants.player.defaultName
: name;
}

export class LootItem {
constructor(
public readonly idString: ReferenceTo<LootDefinition>,
Expand Down
25 changes: 0 additions & 25 deletions server/src/utils/usernameFilter.ts

This file was deleted.

0 comments on commit 9f8b12d

Please sign in to comment.