You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.0 KiB

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