README.md (4400B)
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][travis-image]][travis-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 <!-- eslint-disable no-unused-vars --> 24 25 ```js 26 var proxyaddr = require('proxy-addr') 27 ``` 28 29 ### proxyaddr(req, trust) 30 31 Return the address of the request, using the given `trust` parameter. 32 33 The `trust` argument is a function that returns `true` if you trust 34 the address, `false` if you don't. The closest untrusted address is 35 returned. 36 37 <!-- eslint-disable no-undef --> 38 39 ```js 40 proxyaddr(req, function (addr) { return addr === '127.0.0.1' }) 41 proxyaddr(req, function (addr, i) { return i < 1 }) 42 ``` 43 44 The `trust` arugment may also be a single IP address string or an 45 array of trusted addresses, as plain IP addresses, CIDR-formatted 46 strings, or IP/netmask strings. 47 48 <!-- eslint-disable no-undef --> 49 50 ```js 51 proxyaddr(req, '127.0.0.1') 52 proxyaddr(req, ['127.0.0.0/8', '10.0.0.0/8']) 53 proxyaddr(req, ['127.0.0.0/255.0.0.0', '192.168.0.0/255.255.0.0']) 54 ``` 55 56 This module also supports IPv6. Your IPv6 addresses will be normalized 57 automatically (i.e. `fe80::00ed:1` equals `fe80:0:0:0:0:0:ed:1`). 58 59 <!-- eslint-disable no-undef --> 60 61 ```js 62 proxyaddr(req, '::1') 63 proxyaddr(req, ['::1/128', 'fe80::/10']) 64 ``` 65 66 This module will automatically work with IPv4-mapped IPv6 addresses 67 as well to support node.js in IPv6-only mode. This means that you do 68 not have to specify both `::ffff:a00:1` and `10.0.0.1`. 69 70 As a convenience, this module also takes certain pre-defined names 71 in addition to IP addresses, which expand into IP addresses: 72 73 <!-- eslint-disable no-undef --> 74 75 ```js 76 proxyaddr(req, 'loopback') 77 proxyaddr(req, ['loopback', 'fc00:ac:1ab5:fff::1/64']) 78 ``` 79 80 * `loopback`: IPv4 and IPv6 loopback addresses (like `::1` and 81 `127.0.0.1`). 82 * `linklocal`: IPv4 and IPv6 link-local addresses (like 83 `fe80::1:1:1:1` and `169.254.0.1`). 84 * `uniquelocal`: IPv4 private addresses and IPv6 unique-local 85 addresses (like `fc00:ac:1ab5:fff::1` and `192.168.0.1`). 86 87 When `trust` is specified as a function, it will be called for each 88 address to determine if it is a trusted address. The function is 89 given two arguments: `addr` and `i`, where `addr` is a string of 90 the address to check and `i` is a number that represents the distance 91 from the socket address. 92 93 ### proxyaddr.all(req, [trust]) 94 95 Return all the addresses of the request, optionally stopping at the 96 first untrusted. This array is ordered from closest to furthest 97 (i.e. `arr[0] === req.connection.remoteAddress`). 98 99 <!-- eslint-disable no-undef --> 100 101 ```js 102 proxyaddr.all(req) 103 ``` 104 105 The optional `trust` argument takes the same arguments as `trust` 106 does in `proxyaddr(req, trust)`. 107 108 <!-- eslint-disable no-undef --> 109 110 ```js 111 proxyaddr.all(req, 'loopback') 112 ``` 113 114 ### proxyaddr.compile(val) 115 116 Compiles argument `val` into a `trust` function. This function takes 117 the same arguments as `trust` does in `proxyaddr(req, trust)` and 118 returns a function suitable for `proxyaddr(req, trust)`. 119 120 <!-- eslint-disable no-undef, no-unused-vars --> 121 122 ```js 123 var trust = proxyaddr.compile('loopback') 124 var addr = proxyaddr(req, trust) 125 ``` 126 127 This function is meant to be optimized for use against every request. 128 It is recommend to compile a trust function up-front for the trusted 129 configuration and pass that to `proxyaddr(req, trust)` for each request. 130 131 ## Testing 132 133 ```sh 134 $ npm test 135 ``` 136 137 ## Benchmarks 138 139 ```sh 140 $ npm run-script bench 141 ``` 142 143 ## License 144 145 [MIT](LICENSE) 146 147 [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/proxy-addr/master 148 [coveralls-url]: https://coveralls.io/r/jshttp/proxy-addr?branch=master 149 [node-image]: https://badgen.net/npm/node/proxy-addr 150 [node-url]: https://nodejs.org/en/download 151 [npm-downloads-image]: https://badgen.net/npm/dm/proxy-addr 152 [npm-url]: https://npmjs.org/package/proxy-addr 153 [npm-version-image]: https://badgen.net/npm/v/proxy-addr 154 [travis-image]: https://badgen.net/travis/jshttp/proxy-addr/master 155 [travis-url]: https://travis-ci.org/jshttp/proxy-addr