Skip to content

Commit f878ae0

Browse files
committed
Hash IPs in the IP map
1 parent 1ddb16e commit f878ae0

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

web/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,15 @@ export class GmodDev extends BaseSession {}
430430

431431
const MAX_SESSIONS_PER_IP = 2;
432432

433+
async function hashIP(ip: string): Promise<string> {
434+
const data = new TextEncoder().encode(ip);
435+
const hash = await crypto.subtle.digest("SHA-256", data);
436+
return btoa(String.fromCharCode(...new Uint8Array(hash)));
437+
}
438+
433439
export class QueueDO extends DurableObject<Env> {
434440
activeSessions: Map<string, string>; // sessionId → type
435-
sessionIPs: Map<string, string>; // sessionId → IP
441+
sessionIPs: Map<string, string>; // sessionId → hashed IP
436442
waitingQueue: {
437443
ticketId: string;
438444
sessionType: string;
@@ -485,7 +491,8 @@ export class QueueDO extends DurableObject<Env> {
485491

486492
if (url.pathname === "/api/request-session") {
487493
const sessionType = url.searchParams.get("type") || "public";
488-
const clientIP = request.headers.get("CF-Connecting-IP") || "unknown";
494+
const rawIP = request.headers.get("CF-Connecting-IP") || "unknown";
495+
const clientIP = rawIP !== "unknown" ? await hashIP(rawIP) : "unknown";
489496

490497
if (clientIP !== "unknown" && this.activeSessionCountForIP(clientIP) >= MAX_SESSIONS_PER_IP) {
491498
return new Response(JSON.stringify({

0 commit comments

Comments
 (0)