[6a3a178] | 1 | # proxy-addr
|
---|
| 2 |
|
---|
| 3 | [![NPM Version][npm-version-image]][npm-url]
|
---|
| 4 | [![NPM Downloads][npm-downloads-image]][npm-url]
|
---|
| 5 | [![Node.js Version][node-image]][node-url]
|
---|
| 6 | [![Build Status][ci-image]][ci-url]
|
---|
| 7 | [![Test Coverage][coveralls-image]][coveralls-url]
|
---|
| 8 |
|
---|
| 9 | Determine address of proxied request
|
---|
| 10 |
|
---|
| 11 | ## Install
|
---|
| 12 |
|
---|
| 13 | This is a [Node.js](https://nodejs.org/en/) module available through the
|
---|
| 14 | [npm registry](https://www.npmjs.com/). Installation is done using the
|
---|
| 15 | [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
---|
| 16 |
|
---|
| 17 | ```sh
|
---|
| 18 | $ npm install proxy-addr
|
---|
| 19 | ```
|
---|
| 20 |
|
---|
| 21 | ## API
|
---|
| 22 |
|
---|
| 23 | ```js
|
---|
| 24 | var proxyaddr = require('proxy-addr')
|
---|
| 25 | ```
|
---|
| 26 |
|
---|
| 27 | ### proxyaddr(req, trust)
|
---|
| 28 |
|
---|
| 29 | Return the address of the request, using the given `trust` parameter.
|
---|
| 30 |
|
---|
| 31 | The `trust` argument is a function that returns `true` if you trust
|
---|
| 32 | the address, `false` if you don't. The closest untrusted address is
|
---|
| 33 | returned.
|
---|
| 34 |
|
---|
| 35 | ```js
|
---|
| 36 | proxyaddr(req, function (addr) { return addr === '127.0.0.1' })
|
---|
| 37 | proxyaddr(req, function (addr, i) { return i < 1 })
|
---|
| 38 | ```
|
---|
| 39 |
|
---|
| 40 | The `trust` arugment may also be a single IP address string or an
|
---|
| 41 | array of trusted addresses, as plain IP addresses, CIDR-formatted
|
---|
| 42 | strings, or IP/netmask strings.
|
---|
| 43 |
|
---|
| 44 | ```js
|
---|
| 45 | proxyaddr(req, '127.0.0.1')
|
---|
| 46 | proxyaddr(req, ['127.0.0.0/8', '10.0.0.0/8'])
|
---|
| 47 | proxyaddr(req, ['127.0.0.0/255.0.0.0', '192.168.0.0/255.255.0.0'])
|
---|
| 48 | ```
|
---|
| 49 |
|
---|
| 50 | This module also supports IPv6. Your IPv6 addresses will be normalized
|
---|
| 51 | automatically (i.e. `fe80::00ed:1` equals `fe80:0:0:0:0:0:ed:1`).
|
---|
| 52 |
|
---|
| 53 | ```js
|
---|
| 54 | proxyaddr(req, '::1')
|
---|
| 55 | proxyaddr(req, ['::1/128', 'fe80::/10'])
|
---|
| 56 | ```
|
---|
| 57 |
|
---|
| 58 | This module will automatically work with IPv4-mapped IPv6 addresses
|
---|
| 59 | as well to support node.js in IPv6-only mode. This means that you do
|
---|
| 60 | not have to specify both `::ffff:a00:1` and `10.0.0.1`.
|
---|
| 61 |
|
---|
| 62 | As a convenience, this module also takes certain pre-defined names
|
---|
| 63 | in addition to IP addresses, which expand into IP addresses:
|
---|
| 64 |
|
---|
| 65 | ```js
|
---|
| 66 | proxyaddr(req, 'loopback')
|
---|
| 67 | proxyaddr(req, ['loopback', 'fc00:ac:1ab5:fff::1/64'])
|
---|
| 68 | ```
|
---|
| 69 |
|
---|
| 70 | * `loopback`: IPv4 and IPv6 loopback addresses (like `::1` and
|
---|
| 71 | `127.0.0.1`).
|
---|
| 72 | * `linklocal`: IPv4 and IPv6 link-local addresses (like
|
---|
| 73 | `fe80::1:1:1:1` and `169.254.0.1`).
|
---|
| 74 | * `uniquelocal`: IPv4 private addresses and IPv6 unique-local
|
---|
| 75 | addresses (like `fc00:ac:1ab5:fff::1` and `192.168.0.1`).
|
---|
| 76 |
|
---|
| 77 | When `trust` is specified as a function, it will be called for each
|
---|
| 78 | address to determine if it is a trusted address. The function is
|
---|
| 79 | given two arguments: `addr` and `i`, where `addr` is a string of
|
---|
| 80 | the address to check and `i` is a number that represents the distance
|
---|
| 81 | from the socket address.
|
---|
| 82 |
|
---|
| 83 | ### proxyaddr.all(req, [trust])
|
---|
| 84 |
|
---|
| 85 | Return all the addresses of the request, optionally stopping at the
|
---|
| 86 | first untrusted. This array is ordered from closest to furthest
|
---|
| 87 | (i.e. `arr[0] === req.connection.remoteAddress`).
|
---|
| 88 |
|
---|
| 89 | ```js
|
---|
| 90 | proxyaddr.all(req)
|
---|
| 91 | ```
|
---|
| 92 |
|
---|
| 93 | The optional `trust` argument takes the same arguments as `trust`
|
---|
| 94 | does in `proxyaddr(req, trust)`.
|
---|
| 95 |
|
---|
| 96 | ```js
|
---|
| 97 | proxyaddr.all(req, 'loopback')
|
---|
| 98 | ```
|
---|
| 99 |
|
---|
| 100 | ### proxyaddr.compile(val)
|
---|
| 101 |
|
---|
| 102 | Compiles argument `val` into a `trust` function. This function takes
|
---|
| 103 | the same arguments as `trust` does in `proxyaddr(req, trust)` and
|
---|
| 104 | returns a function suitable for `proxyaddr(req, trust)`.
|
---|
| 105 |
|
---|
| 106 | ```js
|
---|
| 107 | var trust = proxyaddr.compile('loopback')
|
---|
| 108 | var addr = proxyaddr(req, trust)
|
---|
| 109 | ```
|
---|
| 110 |
|
---|
| 111 | This function is meant to be optimized for use against every request.
|
---|
| 112 | It is recommend to compile a trust function up-front for the trusted
|
---|
| 113 | configuration and pass that to `proxyaddr(req, trust)` for each request.
|
---|
| 114 |
|
---|
| 115 | ## Testing
|
---|
| 116 |
|
---|
| 117 | ```sh
|
---|
| 118 | $ npm test
|
---|
| 119 | ```
|
---|
| 120 |
|
---|
| 121 | ## Benchmarks
|
---|
| 122 |
|
---|
| 123 | ```sh
|
---|
| 124 | $ npm run-script bench
|
---|
| 125 | ```
|
---|
| 126 |
|
---|
| 127 | ## License
|
---|
| 128 |
|
---|
| 129 | [MIT](LICENSE)
|
---|
| 130 |
|
---|
| 131 | [ci-image]: https://badgen.net/github/checks/jshttp/proxy-addr/master?label=ci
|
---|
| 132 | [ci-url]: https://github.com/jshttp/proxy-addr/actions?query=workflow%3Aci
|
---|
| 133 | [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/proxy-addr/master
|
---|
| 134 | [coveralls-url]: https://coveralls.io/r/jshttp/proxy-addr?branch=master
|
---|
| 135 | [node-image]: https://badgen.net/npm/node/proxy-addr
|
---|
| 136 | [node-url]: https://nodejs.org/en/download
|
---|
| 137 | [npm-downloads-image]: https://badgen.net/npm/dm/proxy-addr
|
---|
| 138 | [npm-url]: https://npmjs.org/package/proxy-addr
|
---|
| 139 | [npm-version-image]: https://badgen.net/npm/v/proxy-addr
|
---|