summary refs log tree commit diff
path: root/src/gateway
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-12-07 23:18:57 +0100
committerRory& <root@rory.gay>2025-12-12 22:56:02 +0100
commitbdbf6d06b25fcdb2e9cbcdd32872ae62bfc8893c (patch)
tree76e769af417dba58a85d53996b3993ea073e2170 /src/gateway
parentMerge remote-tracking branch 'BrajamohanDas-afk/refactor/remove-getIpAddress'... (diff)
downloadserver-ts-bdbf6d06b25fcdb2e9cbcdd32872ae62bfc8893c.tar.xz
Fix IP tracking for bans
Diffstat (limited to 'src/gateway')
-rw-r--r--src/gateway/Server.ts5
-rw-r--r--src/gateway/events/Connection.ts7
-rw-r--r--src/gateway/listener/listener.ts11
-rw-r--r--src/gateway/util/WebSocket.ts1
4 files changed, 24 insertions, 0 deletions
diff --git a/src/gateway/Server.ts b/src/gateway/Server.ts

index 269cc2ed..5d7f8d87 100644 --- a/src/gateway/Server.ts +++ b/src/gateway/Server.ts
@@ -28,6 +28,7 @@ import ws from "ws"; import { Connection } from "./events/Connection"; import http from "http"; import { cleanupOnStartup } from "./util/Utils"; +import { randomString } from "@spacebar/api"; export class Server { public ws: ws.Server; @@ -50,6 +51,10 @@ export class Server { if (server) this.server = server; else { this.server = http.createServer(function (req, res) { + if(!req.headers.cookie?.split("; ").find(x => x.startsWith("__sb_sessid="))) { + res.setHeader("Set-Cookie", `__sb_sessid=${randomString(32)}; Secure; HttpOnly; SameSite=None`); + } + res.writeHead(200).end("Online"); }); } diff --git a/src/gateway/events/Connection.ts b/src/gateway/events/Connection.ts
index bc74dee6..9b132d4b 100644 --- a/src/gateway/events/Connection.ts +++ b/src/gateway/events/Connection.ts
@@ -72,6 +72,13 @@ export async function Connection( ); } + if (request.headers.cookie?.split("; ").find(x => x.startsWith("__sb_sessid="))) { + socket.fingerprint = request.headers.cookie + .split("; ") + .find((x) => x.startsWith("__sb_sessid=")) + ?.split("=")[1]; + } + //Create session ID when the connection is opened. This allows gateway dump to group the initial websocket messages with the rest of the conversation. const session_id = genSessionId(); socket.session_id = session_id; //Set the session of the WebSocket object diff --git a/src/gateway/listener/listener.ts b/src/gateway/listener/listener.ts
index 75cb6bc6..f76adfb2 100644 --- a/src/gateway/listener/listener.ts +++ b/src/gateway/listener/listener.ts
@@ -29,6 +29,7 @@ import { Message, NewUrlUserSignatureData, GuildMemberAddEvent, + Ban, } from "@spacebar/util"; import { OPCODES } from "../util/Constants"; import { Send } from "../util/Send"; @@ -174,6 +175,16 @@ async function consume(this: WebSocket, opts: EventOpts) { case "GUILD_DELETE": this.events[id]?.(); delete this.events[id]; + if (event === "GUILD_DELETE" && this.ipAddress) { + const ban = await Ban.findOne({ + where: { guild_id: id, user_id: this.user_id }, + }); + + if (ban) { + ban.ip = this.ipAddress || undefined; + await ban.save(); + } + } break; case "CHANNEL_CREATE": if (!permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) { diff --git a/src/gateway/util/WebSocket.ts b/src/gateway/util/WebSocket.ts
index 00ef9e98..d0a42eaf 100644 --- a/src/gateway/util/WebSocket.ts +++ b/src/gateway/util/WebSocket.ts
@@ -33,6 +33,7 @@ export interface WebSocket extends WS { compress?: "zlib-stream" | "zstd-stream"; ipAddress?: string; userAgent?: string; // for cdn request signing + fingerprint?: string; shard_count?: bigint; shard_id?: bigint; deflate?: Deflate;