|
1 |
| -# lib_template |
2 |
| -Template repository for bootstrap new libraries faster |
| 1 | +# fastify-ip |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +`fastify-racing` is a plugin which allows to infer the incoming request's IP based on a custom subset of well known headers used by different providers or technologies that possible sits in-front of your fastify application. |
| 6 | + |
| 7 | +## How it works? |
| 8 | + |
| 9 | +The plugin will make a best-effort to infer the request's IP based on a subset of well-known headers, which looks as follows: |
| 10 | +``` |
| 11 | +'x-client-ip' // Most common |
| 12 | +'x-forwarded-for' // Mostly used by proxies |
| 13 | +'cf-connecting-ip' // Cloudflare |
| 14 | +'Cf-Pseudo-IPv4' // Cloudflare |
| 15 | +'fastly-client-ip' |
| 16 | +'true-client-ip' // Akamai and Cloudflare |
| 17 | +'x-real-ip' // Nginx |
| 18 | +'x-cluser-client-ip' // Rackspace LB |
| 19 | +'forwarded-for' |
| 20 | +'x-forwarded' |
| 21 | +'forwarded' |
| 22 | +'x-appengine-user-ip' // GCP App Engine |
| 23 | +``` |
| 24 | + |
| 25 | +The plugin will use a FIFO strategy to infer the right IP. This can be customised by passing a custom order property that includes your custom headers. |
| 26 | + |
| 27 | +>Note: It is important to remark that this does not alters the `Request#ips` behaviour for inferring IPs when setting the `FastifyOptions#trustProxy` to `true`. It rather allows you to infer the IP of a given request by headers out of the common spec or standard. |
| 28 | +
|
| 29 | +## Setup |
| 30 | + |
| 31 | +Install by running `npm install fastify-ip`. |
| 32 | + |
| 33 | +Then register the plugin to your fastify instance: |
| 34 | + |
| 35 | +```js |
| 36 | +const fastify = require('fastify')({ |
| 37 | + logger: true |
| 38 | +}) |
| 39 | + |
| 40 | +fastify.register(require('fastify-racing'), { |
| 41 | + order: ['x-my-ip-header'], |
| 42 | + strict: false |
| 43 | +}) |
| 44 | +``` |
| 45 | + |
| 46 | +### Options |
| 47 | + |
| 48 | +- `order` - `string[] | string` - **optional**: Array of custom headers or single custom header to be appended to the prior list of well-known headers. The headers passed will be prepend to the default headers list. It can also be used to alter the order of the list as deduplication of header names is made while loading the plugin. |
| 49 | + |
| 50 | +- `strict` - `boolean` - **optional**: Indicates whether to override the default list of well-known headers and replace it with the header(s) passed through the `order` option. If set to `true` without `order` property being provided, will not take any effect on the plugin. Default `false`. |
| 51 | + |
| 52 | + |
| 53 | +### API |
| 54 | + |
| 55 | +The plugin will decorate the Request object with a set of utils that can be handy for managing IPs. |
| 56 | + |
| 57 | +- `isIP(pseudo: string): boolean` - It will return `true` if a given string is a valid IPv4 or IPv6, or `false` otherwise. |
| 58 | +- `isIPv4(pseudo: string): boolean` - Similar to `isIP` but will validate of the given string is a valid `IPv4`. |
| 59 | +- `isIPv6(pseudo: string): boolean` - Similar to `isIP` but will validate of the given string is a valid `IPv6`. |
| 60 | +- `inferIPVersion(pseudo: string): 0 | 4 | 6` - It will try to infer the IPv of a given IP, returning `4` for `IPv4` and `6` for `IPv6`, will return `0` if the given string does not match any of the IPv. |
| 61 | + |
| 62 | +## How to use it? |
| 63 | + |
| 64 | +**JavaScript** |
| 65 | + |
| 66 | +```js |
| 67 | +app.get('/', (req, reply) => { |
| 68 | + req.log.info({ ip: req.ip }, 'my ip!') |
| 69 | + req.log.info({ isValid: req.isIP('hello!') } /* false */) |
| 70 | + req.log.info({ isIPv4: req.isIPv4('127.0.0.1') }) |
| 71 | + req.log.info({ isIPv6: req.isIPv6('::1') }) |
| 72 | + req.log.info({ version: req.inferIPVersion('127.0.0.1') /* 4 */ }) |
| 73 | + req.log.info({ version: req.inferIPVersion('::1') /* 6 */ }) |
| 74 | + req.log.info({ version: req.inferIPVersion('asd') /* 0 */ }) |
| 75 | + |
| 76 | + reply.send(req.ip) |
| 77 | +}) |
| 78 | +``` |
| 79 | + |
| 80 | +**TypeScript** |
| 81 | + |
| 82 | +```ts |
| 83 | +app.post('/', (request: FastifyRequest, reply: FastifyReply) => { |
| 84 | + req.log.info({ ip: req.ip }, 'my ip!') |
| 85 | + req.log.info({ isValid: req.isIP('hello!') } /* false */) |
| 86 | + req.log.info({ isIPv4: req.isIPv4('127.0.0.1') }) |
| 87 | + req.log.info({ isIPv6: req.isIPv6('::1') }) |
| 88 | + req.log.info({ version: req.inferIPVersion('127.0.0.1') /* 4 */ }) |
| 89 | + req.log.info({ version: req.inferIPVersion('::1') /* 6 */ }) |
| 90 | + req.log.info({ version: req.inferIPVersion('asd') /* 0 */ }) |
| 91 | + |
| 92 | + reply.send(req.ip) |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +## Type Definitions |
| 97 | + |
| 98 | +```ts |
| 99 | +export interface FastifyIPOptions { |
| 100 | + order?: string[] | string; |
| 101 | + strict?: boolean; |
| 102 | +} |
| 103 | + |
| 104 | +declare module 'fastify' { |
| 105 | + interface FastifyRequest { |
| 106 | + isIP(pseudo: string): boolean; |
| 107 | + isIPv4(pseudo: string): boolean; |
| 108 | + isIPv6(pseudo: string): boolean; |
| 109 | + inferIPVersion(pseudo: string): 0 | 4 | 6; |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | +> See [test](test/index.test.js) for more examples. |
0 commit comments