diff --git a/README.md b/README.md index 063a28d..3910950 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ The user ip is determined by the following order: 10. `req.socket.remoteAddress` 11. `req.connection.socket.remoteAddress` 12. `req.info.remoteAddress` +13. `request.raw` (Fastify) If an IP address cannot be found, it will return `null`. diff --git a/src/index.js b/src/index.js index 52e426a..a9b4316 100644 --- a/src/index.js +++ b/src/index.js @@ -119,6 +119,7 @@ function getClientIp(req) { } // Remote address checks. + // Deprecated if (is.existy(req.connection)) { if (is.ip(req.connection.remoteAddress)) { return req.connection.remoteAddress; @@ -141,6 +142,11 @@ function getClientIp(req) { return req.requestContext.identity.sourceIp; } + // Fastify https://www.fastify.io/docs/latest/Reference/Request/ + if (is.existy(req.raw)) { + return getClientIp(req.raw); + } + return null; } diff --git a/test/index.js b/test/index.js index 693d6fd..6303ea6 100644 --- a/test/index.js +++ b/test/index.js @@ -544,3 +544,23 @@ test('request to Google Cloud App Engine (x-appengine-user-ip)', (t) => { }); }); }); + +test('Fastify (request.raw) found', (t) => { + t.plan(1); + const found = requestIp.getClientIp({ + raw: { + headers: { + forwarded: '91.203.163.199', + }, + }, + }); + t.equal(found, '91.203.163.199'); +}); + +test('Fastify (request.raw) not found', (t) => { + t.plan(1); + const found = requestIp.getClientIp({ + raw: {}, + }); + t.equal(found, null); +});