From 04d65da7793700a68a03ca1e024c7abb643b5979 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Sat, 13 Jul 2024 21:32:57 +0200 Subject: [PATCH] Fix substring error if remoteAddress is undefined and replace deprecated `request.connection` with `request.socket` (fixes #308) --- server/peer.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server/peer.js b/server/peer.js index 3ff9277..2db112b 100644 --- a/server/peer.js +++ b/server/peer.js @@ -44,15 +44,18 @@ export default class Peer { _setIP(request) { if (request.headers['cf-connecting-ip']) { this.ip = request.headers['cf-connecting-ip'].split(/\s*,\s*/)[0]; - } else if (request.headers['x-forwarded-for']) { + } + else if (request.headers['x-forwarded-for']) { this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0]; - } else { - this.ip = request.connection.remoteAddress; + } + else { + this.ip = request.socket.remoteAddress ?? ''; } // remove the prefix used for IPv4-translated addresses - if (this.ip.substring(0,7) === "::ffff:") + if (this.ip.substring(0,7) === "::ffff:") { this.ip = this.ip.substring(7); + } let ipv6_was_localized = false; if (this.conf.ipv6Localize && this.ip.includes(':')) {