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.

137 lines
3.9 KiB

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