Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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