From 99f4bd6b2d84ac0858d03a42f6a622eb376e8ea6 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 30 Apr 2023 22:19:50 +0300 Subject: [PATCH] request remote address --- @types/Server.ts | 3 ++- src/server/Server.ts | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/@types/Server.ts b/@types/Server.ts index 8887643..23a0377 100644 --- a/@types/Server.ts +++ b/@types/Server.ts @@ -151,7 +151,8 @@ export type Request = { verified: boolean, loginMethod: string } & Session, - files: {[key: string]: UploadedFile} + files: { [key: string]: UploadedFile }, + remoteAddress: string } & ExpressRequest export type Response = { diff --git a/src/server/Server.ts b/src/server/Server.ts index b498687..c8cbe22 100644 --- a/src/server/Server.ts +++ b/src/server/Server.ts @@ -192,7 +192,8 @@ class Server extends EventEmitter { this.#_app.use(express.json({ limit: '10mb' })); this.#_app.use(express.urlencoded({ extended: true })); - // Not sure why these are a problem + // Not sure why binding is a problem + this.#_app.use(this.#attachMisc.bind(this) as never); this.#_app.use(this.#logRequest.bind(this) as never); // Logs every request this.#_app.use(this.#logError.bind(this) as never); // Logs endpoints that error and sends a 500 this.#_app.use(this.#ready.bind(this) as never); // denies requests before the server is ready @@ -232,6 +233,11 @@ class Server extends EventEmitter { this.emit('ready'); } + + #attachMisc (req: Request, _res: Response, next: NextFunction) { + req.remoteAddress = req.get('X-Forwarded-For') || req.socket.remoteAddress as string; + next(); + } // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars #logError (err: Error, _req: Request, res: Response, _next: NextFunction) {