asan / qorsproxy

Yet another cors proxy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Qorsproxy

buildStatus Coveralls dependencyStatus devDependencyStatus Code Climate Greenkeeper badge

Sometimes you need a proxy.

  • Get any from google
  • Write your own. That's pretty easy:
const http = require('http');
http.createServer(handler).listen(3000);

function handler(req, res) {
	console.log('serve: ' + req.url);

	const options = {
		hostname: 'example.com',
		port: 80,
		path: req.url,
		method: req.method
	};

	const proxy = http.request(options, _res => {
		_res.pipe(res, {
			end: true
		});
	});

	req.pipe(proxy, {
		end: true
	});
}
  • Try out this weird thing just for fun.

Requirements

  • Unix compliant OS
  • NodeJS 8+

Install

npm i qorsproxy --save-dev

Start

Any of you'd prefer:

npm start -- --config=path
pm2 start npm --name qorsproxy -- start -- --port=8080 --config=/Users/a.golub/repo/qorsproxy/config/qorsproxy.dev.qiwi.tools.json
npm run start_pm2 -- -- --port=8080

CLI options

  • --config sets path to custom config
  • --port defines server listen port. Defaults to 9292
  • --host DNS name or IP address

Configuration

{
  "server": {
    "host": "127.0.0.1",
    "port": 8080
  },
  "rules": {
    "localhost": {
      "from": [
        "*"
      ],
      "to": [
        "example.com"
      ],
      "mutations": [
        {
          "direction": "to",
          "headers": [
            {
              "name": "origin",
              "value": "localhost"
            }
          ]
        },
        {
          "direction": "from",
          "headers": [
            {
              "name": "set-cookie",
              "value": {
                "from": "/;Domain.+;/",
                "to": ";Domain: foobar.com;"
              }
            }
          ]
        }
      ]
    }
  }
}

Pre-flight

If you need support for OPTIONS request, extend target rule:

"interceptions": [
  {
    "req": {
      "method": "OPTIONS"
    },
    "res": {
      "status": 200
    }
  }
],

Usage

curl 'http://127.0.0.1:9292/http://example.com' -H 'origin:http://localhost'<!doctype html> ...

About

Yet another cors proxy

License:MIT License


Languages

Language:JavaScript 100.0%