index.js 641 B

123456789101112131415161718192021222324252627282930313233343536
  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. }