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.

35 lines
641 B

8 years ago
  1. /*!
  2. * forwarded
  3. * Copyright(c) 2014 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module exports.
  8. */
  9. module.exports = forwarded
  10. /**
  11. * Get all addresses in the request, using the `X-Forwarded-For` header.
  12. *
  13. * @param {Object} req
  14. * @api public
  15. */
  16. function forwarded(req) {
  17. if (!req) {
  18. throw new TypeError('argument req is required')
  19. }
  20. // simple header parsing
  21. var proxyAddrs = (req.headers['x-forwarded-for'] || '')
  22. .split(/ *, */)
  23. .filter(Boolean)
  24. .reverse()
  25. var socketAddr = req.connection.remoteAddress
  26. var addrs = [socketAddr].concat(proxyAddrs)
  27. // return all addresses
  28. return addrs
  29. }