request remote address

This commit is contained in:
Erik 2023-04-30 22:19:50 +03:00
parent 02a13c8f15
commit 99f4bd6b2d
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
2 changed files with 9 additions and 2 deletions

View File

@ -151,7 +151,8 @@ export type Request = {
verified: boolean, verified: boolean,
loginMethod: string loginMethod: string
} & Session, } & Session,
files: {[key: string]: UploadedFile} files: { [key: string]: UploadedFile },
remoteAddress: string
} & ExpressRequest } & ExpressRequest
export type Response = { export type Response = {

View File

@ -192,7 +192,8 @@ class Server extends EventEmitter {
this.#_app.use(express.json({ limit: '10mb' })); this.#_app.use(express.json({ limit: '10mb' }));
this.#_app.use(express.urlencoded({ extended: true })); 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.#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.#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 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'); 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 // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
#logError (err: Error, _req: Request, res: Response, _next: NextFunction) { #logError (err: Error, _req: Request, res: Response, _next: NextFunction) {