pax_global_header00006660000000000000000000000064140552672560014526gustar00rootroot0000000000000052 comment=93d2f4c185edd14cb023632c0b216a9f08646ff1 forwarded-0.2.0/000077500000000000000000000000001405526725600135025ustar00rootroot00000000000000forwarded-0.2.0/.eslintignore000066400000000000000000000000421405526725600162010ustar00rootroot00000000000000.nyc_output coverage node_modules forwarded-0.2.0/.eslintrc.yml000066400000000000000000000000351405526725600161240ustar00rootroot00000000000000root: true extends: standard forwarded-0.2.0/.github/000077500000000000000000000000001405526725600150425ustar00rootroot00000000000000forwarded-0.2.0/.github/workflows/000077500000000000000000000000001405526725600170775ustar00rootroot00000000000000forwarded-0.2.0/.github/workflows/ci.yml000066400000000000000000000130761405526725600202240ustar00rootroot00000000000000name: ci on: - pull_request - push jobs: test: runs-on: ubuntu-18.04 strategy: matrix: name: - Node.js 0.6 - Node.js 0.8 - Node.js 0.10 - Node.js 0.12 - io.js 1.x - io.js 2.x - io.js 3.x - Node.js 4.x - Node.js 5.x - Node.js 6.x - Node.js 7.x - Node.js 8.x - Node.js 9.x - Node.js 10.x - Node.js 11.x - Node.js 12.x - Node.js 13.x - Node.js 14.x - Node.js 15.x - Node.js 16.x include: - name: Node.js 0.6 node-version: "0.6" npm-i: mocha@1.21.5 npm-rm: beautify-benchmark benchmark nyc - name: Node.js 0.8 node-version: "0.8" npm-i: mocha@2.5.3 npm-rm: beautify-benchmark benchmark nyc - name: Node.js 0.10 node-version: "0.10" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark - name: Node.js 0.12 node-version: "0.12" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark - name: io.js 1.x node-version: "1.8" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark - name: io.js 2.x node-version: "2.5" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark - name: io.js 3.x node-version: "3.3" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark - name: Node.js 4.x node-version: "4.9" npm-i: mocha@5.2.0 nyc@11.9.0 npm-rm: beautify-benchmark benchmark - name: Node.js 5.x node-version: "5.12" npm-i: mocha@5.2.0 nyc@11.9.0 npm-rm: beautify-benchmark benchmark - name: Node.js 6.x node-version: "6.17" npm-i: mocha@6.2.2 nyc@14.1.1 npm-rm: beautify-benchmark benchmark - name: Node.js 7.x node-version: "7.10" npm-i: mocha@6.2.2 nyc@14.1.1 npm-rm: beautify-benchmark benchmark - name: Node.js 8.x node-version: "8.17" npm-i: mocha@7.2.0 npm-rm: beautify-benchmark benchmark - name: Node.js 9.x node-version: "9.11" npm-i: mocha@7.2.0 npm-rm: beautify-benchmark benchmark - name: Node.js 10.x node-version: "10.24" npm-rm: beautify-benchmark benchmark - name: Node.js 11.x node-version: "11.15" npm-rm: beautify-benchmark benchmark - name: Node.js 12.x node-version: "12.22" npm-rm: beautify-benchmark benchmark - name: Node.js 13.x node-version: "13.14" npm-rm: beautify-benchmark benchmark - name: Node.js 14.x node-version: "14.17" npm-rm: beautify-benchmark benchmark - name: Node.js 15.x node-version: "15.12" npm-rm: beautify-benchmark benchmark - name: Node.js 16.x node-version: "16.2" npm-rm: beautify-benchmark benchmark steps: - uses: actions/checkout@v2 - name: Install Node.js ${{ matrix.node-version }} shell: bash -eo pipefail -l {0} run: | if [[ "${{ matrix.node-version }}" == 0.6* ]]; then sudo apt-get install g++-4.8 gcc-4.8 libssl1.0-dev export CC=/usr/bin/gcc-4.8 export CXX=/usr/bin/g++-4.8 fi nvm install --default ${{ matrix.node-version }} if [[ "${{ matrix.node-version }}" == 0.* ]]; then npm config set strict-ssl false fi dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH" - name: Configure npm run: npm config set shrinkwrap false - name: Remove npm module(s) ${{ matrix.npm-rm }} run: npm rm --silent --save-dev ${{ matrix.npm-rm }} if: matrix.npm-rm != '' - name: Install npm module(s) ${{ matrix.npm-i }} run: npm install --save-dev ${{ matrix.npm-i }} if: matrix.npm-i != '' - name: Setup Node.js version-specific dependencies shell: bash run: | # eslint for linting # - remove on Node.js < 10 if [[ "$(cut -d. -f1 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \ grep -E '^eslint(-|$)' | \ sort -r | \ xargs -n1 npm rm --silent --save-dev fi - name: Install Node.js dependencies run: npm install - name: List environment id: list_env shell: bash run: | echo "node@$(node -v)" echo "npm@$(npm -v)" npm -s ls ||: (npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print "::set-output name=" $2 "::" $3 }' - name: Run tests shell: bash run: | if npm -ps ls nyc | grep -q nyc; then npm run test-ci else npm test fi - name: Lint code if: steps.list_env.outputs.eslint != '' run: npm run lint - name: Collect code coverage uses: coverallsapp/github-action@master if: steps.list_env.outputs.nyc != '' with: github-token: ${{ secrets.GITHUB_TOKEN }} flag-name: run-${{ matrix.test_number }} parallel: true coverage: needs: test runs-on: ubuntu-latest steps: - name: Upload code coverage uses: coverallsapp/github-action@master with: github-token: ${{ secrets.github_token }} parallel-finished: true forwarded-0.2.0/.gitignore000066400000000000000000000001051405526725600154660ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ npm-debug.log package-lock.json forwarded-0.2.0/HISTORY.md000066400000000000000000000006201405526725600151630ustar00rootroot000000000000000.2.0 / 2021-05-31 ================== * Use `req.socket` over deprecated `req.connection` 0.1.2 / 2017-09-14 ================== * perf: improve header parsing * perf: reduce overhead when no `X-Forwarded-For` header 0.1.1 / 2017-09-10 ================== * Fix trimming leading / trailing OWS * perf: hoist regular expression 0.1.0 / 2014-09-21 ================== * Initial release forwarded-0.2.0/LICENSE000066400000000000000000000021061405526725600145060ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014-2017 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. forwarded-0.2.0/README.md000066400000000000000000000031661405526725600147670ustar00rootroot00000000000000# forwarded [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][ci-image]][ci-url] [![Test Coverage][coveralls-image]][coveralls-url] Parse HTTP X-Forwarded-For header ## Installation This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): ```sh $ npm install forwarded ``` ## API ```js var forwarded = require('forwarded') ``` ### forwarded(req) ```js var addresses = forwarded(req) ``` Parse the `X-Forwarded-For` header from the request. Returns an array of the addresses, including the socket address for the `req`, in reverse order (i.e. index `0` is the socket address and the last index is the furthest address, typically the end-user). ## Testing ```sh $ npm test ``` ## License [MIT](LICENSE) [ci-image]: https://badgen.net/github/checks/jshttp/forwarded/master?label=ci [ci-url]: https://github.com/jshttp/forwarded/actions?query=workflow%3Aci [npm-image]: https://img.shields.io/npm/v/forwarded.svg [npm-url]: https://npmjs.org/package/forwarded [node-version-image]: https://img.shields.io/node/v/forwarded.svg [node-version-url]: https://nodejs.org/en/download/ [coveralls-image]: https://img.shields.io/coveralls/jshttp/forwarded/master.svg [coveralls-url]: https://coveralls.io/r/jshttp/forwarded?branch=master [downloads-image]: https://img.shields.io/npm/dm/forwarded.svg [downloads-url]: https://npmjs.org/package/forwarded forwarded-0.2.0/benchmark/000077500000000000000000000000001405526725600154345ustar00rootroot00000000000000forwarded-0.2.0/benchmark/index.js000066400000000000000000000022561405526725600171060ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') /** * Globals for benchmark.js */ global.forwarded = require('..') global.req0 = fakerequest({}) global.req1 = fakerequest({ 'x-forwarded-for': '192.168.0.10' }) global.req2 = fakerequest({ 'x-forwarded-for': '192.168.0.10, 192.168.1.20' }) global.req5 = fakerequest({ 'x-forwarded-for': '192.168.0.10, 192.168.1.20, 192.168.1.21, 192.168.1.22, 192.168.1.23' }) var suite = new benchmark.Suite() suite.add({ name: 'no header', minSamples: 100, fn: 'var addrs = forwarded(req0)' }) suite.add({ name: '1 address', minSamples: 100, fn: 'var addrs = forwarded(req1)' }) suite.add({ name: '2 addresses', minSamples: 100, fn: 'var addrs = forwarded(req2)' }) suite.add({ name: '5 addresses', minSamples: 100, fn: 'var addrs = forwarded(req5)' }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({ async: false }) function fakerequest (headers) { return { headers: headers, connection: { remoteAddress: '10.0.0.1' } } } forwarded-0.2.0/index.js000066400000000000000000000030521405526725600151470ustar00rootroot00000000000000/*! * forwarded * Copyright(c) 2014-2017 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module exports. * @public */ module.exports = forwarded /** * Get all addresses in the request, using the `X-Forwarded-For` header. * * @param {object} req * @return {array} * @public */ function forwarded (req) { if (!req) { throw new TypeError('argument req is required') } // simple header parsing var proxyAddrs = parse(req.headers['x-forwarded-for'] || '') var socketAddr = getSocketAddr(req) var addrs = [socketAddr].concat(proxyAddrs) // return all addresses return addrs } /** * Get the socket address for a request. * * @param {object} req * @return {string} * @private */ function getSocketAddr (req) { return req.socket ? req.socket.remoteAddress : req.connection.remoteAddress } /** * Parse the X-Forwarded-For header. * * @param {string} header * @private */ function parse (header) { var end = header.length var list = [] var start = header.length // gather addresses, backwards for (var i = header.length - 1; i >= 0; i--) { switch (header.charCodeAt(i)) { case 0x20: /* */ if (start === end) { start = end = i } break case 0x2c: /* , */ if (start !== end) { list.push(header.substring(start, end)) } start = end = i break default: start = i break } } // final address if (start !== end) { list.push(header.substring(start, end)) } return list } forwarded-0.2.0/package.json000066400000000000000000000021761405526725600157760ustar00rootroot00000000000000{ "name": "forwarded", "description": "Parse HTTP X-Forwarded-For header", "version": "0.2.0", "contributors": [ "Douglas Christopher Wilson " ], "license": "MIT", "keywords": [ "x-forwarded-for", "http", "req" ], "repository": "jshttp/forwarded", "devDependencies": { "beautify-benchmark": "0.2.4", "benchmark": "2.1.4", "deep-equal": "1.0.1", "eslint": "7.27.0", "eslint-config-standard": "14.1.1", "eslint-plugin-import": "2.23.4", "eslint-plugin-node": "11.1.0", "eslint-plugin-promise": "4.3.1", "eslint-plugin-standard": "4.1.0", "mocha": "8.4.0", "nyc": "15.1.0" }, "files": [ "LICENSE", "HISTORY.md", "README.md", "index.js" ], "engines": { "node": ">= 0.6" }, "scripts": { "bench": "node benchmark/index.js", "lint": "eslint .", "test": "mocha --reporter spec --bail --check-leaks test/", "test-ci": "nyc --reporter=lcov --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test", "version": "node scripts/version-history.js && git add HISTORY.md" } } forwarded-0.2.0/scripts/000077500000000000000000000000001405526725600151715ustar00rootroot00000000000000forwarded-0.2.0/scripts/version-history.js000066400000000000000000000027771405526725600207300ustar00rootroot00000000000000'use strict' var fs = require('fs') var path = require('path') var HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md') var MD_HEADER_REGEXP = /^====*$/ var VERSION = process.env.npm_package_version var VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/ var historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n') if (!MD_HEADER_REGEXP.test(historyFileLines[1])) { console.error('Missing header in HISTORY.md') process.exit(1) } if (!VERSION_PLACEHOLDER_REGEXP.test(historyFileLines[0])) { console.error('Missing placeholder version in HISTORY.md') process.exit(1) } if (historyFileLines[0].indexOf('x') !== -1) { var versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$') if (!versionCheckRegExp.test(VERSION)) { console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0]) process.exit(1) } } historyFileLines[0] = VERSION + ' / ' + getLocaleDate() historyFileLines[1] = repeat('=', historyFileLines[0].length) fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n')) function getLocaleDate () { var now = new Date() return zeroPad(now.getFullYear(), 4) + '-' + zeroPad(now.getMonth() + 1, 2) + '-' + zeroPad(now.getDate(), 2) } function repeat (str, length) { var out = '' for (var i = 0; i < length; i++) { out += str } return out } function zeroPad (number, length) { var num = number.toString() while (num.length < length) { num = '0' + num } return num } forwarded-0.2.0/test/000077500000000000000000000000001405526725600144615ustar00rootroot00000000000000forwarded-0.2.0/test/.eslintrc.yml000066400000000000000000000000231405526725600171000ustar00rootroot00000000000000env: mocha: true forwarded-0.2.0/test/test.js000066400000000000000000000041001405526725600157710ustar00rootroot00000000000000'use strict' var assert = require('assert') var deepEqual = require('deep-equal') var forwarded = require('..') describe('forwarded(req)', function () { it('should require req', function () { assert.throws(forwarded.bind(null), /argument req.*required/) }) it('should work with X-Forwarded-For header', function () { var req = createReq('127.0.0.1') assert.ok(deepEqual(forwarded(req), ['127.0.0.1'])) }) it('should include entries from X-Forwarded-For', function () { var req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.2, 10.0.0.1' }) assert.ok(deepEqual(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])) }) it('should skip blank entries', function () { var req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.2,, 10.0.0.1' }) assert.ok(deepEqual(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])) }) it('should trim leading OWS', function () { var req = createReq('127.0.0.1', { 'x-forwarded-for': ' 10.0.0.2 , , 10.0.0.1 ' }) assert.ok(deepEqual(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])) }) describe('socket address', function () { it('should begin with socket address', function () { var req = createReq('127.0.0.1') assert.strictEqual(forwarded(req)[0], '127.0.0.1') }) it('should use address from req.socket', function () { var req = createReq('127.0.0.1') assert.strictEqual(forwarded(req)[0], req.socket.remoteAddress) }) it('should prefer req.socket', function () { var req = createReq('127.0.0.1') req.connection = { remoteAddress: '10.0.0.1' } assert.strictEqual(forwarded(req)[0], '127.0.0.1') }) it('should use fall back to req.connection', function () { var req = createReq('127.0.0.1') req.connection = { remoteAddress: '10.0.0.1' } req.socket = undefined assert.strictEqual(forwarded(req)[0], '10.0.0.1') }) }) }) function createReq (socketAddr, headers) { return { socket: { remoteAddress: socketAddr }, headers: headers || {} } }