commit 22af9288e2ace15576c207cd16476bb4a13c4f59 Author: Marcus Noble Date: Thu Apr 14 05:46:04 2016 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/index.js b/index.js new file mode 100644 index 0000000..e3cb103 --- /dev/null +++ b/index.js @@ -0,0 +1,38 @@ +"use strict"; +const request = require('request'); +const http = require('http'); +const url = require('url'); +let PORT = process.env.PORT || 8000; + +process.argv.forEach(function (arg) { + if(arg.indexOf('--port=') === 0) { + PORT = parseInt(arg.replace('--port=', ''), 10); + } +}); + +http.createServer((req, response) => { + let remoteURL = url.parse(req.url.substring(1)); + if(!remoteURL.hostname || remoteURL.hostname === 'localhost') return response.end(); + + for(let key in req.headers) { + if(key.match(/host|cookie/ig)){ + delete req.headers[key]; + } + } + + let config = { + url: remoteURL, + followAllRedirects: true, + method: req.method, + headers: req.headers + }; + + if(req.method !== 'HEAD') { + config.body = req; + } + + request(config) + .on('error', () => response.end()) + .pipe(response, {end:true}); + +}).listen(PORT); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..0a82f8d --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "corsproxy", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "dependencies": { + "request": "^2.71.0" + } +}