From 755b33965724033f21479d7e9d6caf5cb8aedf29 Mon Sep 17 00:00:00 2001 From: cesine Date: Tue, 5 Feb 2013 11:05:13 -0500 Subject: [PATCH 01/51] modified custom target example i did some debugging and found that the lib/corsproxy.js seems to expect a well formed url instead of an object, probably just a change in the code that needed to be reflected in the readme target0 = url.parse(module.exports.options.target); --- README.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8133054..71daf50 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,7 @@ With custom target: var cors_proxy = require("corsproxy"); var http_proxy = require("http-proxy"); cors_proxy.options = { - target: { - host:"0.0.0.0", - port:5984 - } + target: "http://0.0.0.0:5984" }; http_proxy.createServer(cors_proxy).listen(1234); @@ -46,4 +43,4 @@ The cors proxy will start at http://localhost:9292. To access another domain, us http://localhost:9292/localhost:3000/sign_in http://localhost:9292/my.domain.com/path/to/resource - etc etc \ No newline at end of file + etc etc From 2910104ad4f9b0054e79c7867f04e046e8442b52 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Tue, 5 Feb 2013 17:09:08 +0100 Subject: [PATCH 02/51] version bump --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 01ff738..46165f4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "corsproxy", "description": "standalone CORS proxy and library for your convenience", - "version": "0.2.9", + "version": "0.2.10", "main": "./lib/corsproxy.js", "author": { "name": "Gregor Martynus", @@ -25,4 +25,4 @@ "type": "git", "url": "https://github.com/gr2m/CORS-Proxy" } -} \ No newline at end of file +} From 5a72d74f553e07a9d5e2fdf5d2a0ed212c4644be Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 13 Feb 2013 12:58:24 +0100 Subject: [PATCH 03/51] fixed port --- lib/corsproxy.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index fb6a331..4a1e72b 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -61,6 +61,7 @@ module.exports = function(req, res, proxy) { res.setHeader(key, value); } req.url = path; + if (! target.port) target.port = 80; return proxy.proxyRequest(req, res, target); } }; From d861d957dc99d566998888a9ce93426e585610c7 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 13 Feb 2013 12:59:16 +0100 Subject: [PATCH 04/51] versiom bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 46165f4..f461590 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "corsproxy", "description": "standalone CORS proxy and library for your convenience", - "version": "0.2.10", + "version": "0.2.11", "main": "./lib/corsproxy.js", "author": { "name": "Gregor Martynus", From afad88e347c86da88df2d580613be381308b9d36 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Tue, 29 Jan 2013 14:59:29 +0100 Subject: [PATCH 05/51] listen to port 80 if no port is specified --- lib/corsproxy.js | 2 +- src/corsproxy.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index 4a1e72b..98e82fb 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -38,7 +38,7 @@ module.exports = function(req, res, proxy) { target0 = url.parse(module.exports.options.target); target = { host: target0.hostname, - port: target0.port + port: target0.port || 80 }; path = req.url; req.headers.host = target0.hostname; diff --git a/src/corsproxy.coffee b/src/corsproxy.coffee index 2edac02..6c6ddf5 100644 --- a/src/corsproxy.coffee +++ b/src/corsproxy.coffee @@ -34,7 +34,7 @@ module.exports = (req, res, proxy) -> target0 = url.parse module.exports.options.target target = { host: target0.hostname, - port: target0.port + port: target0.port || 80 } path = req.url req.headers.host = target0.hostname; From 1cf0616c1c7f0d8698d839625b7aa1155b47b853 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sun, 17 Feb 2013 21:11:23 +0100 Subject: [PATCH 06/51] error gracefully on raw browser access. closes #10 --- lib/corsproxy.js | 5 ++--- src/corsproxy.coffee | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index 98e82fb..9c2dbb4 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -43,7 +43,7 @@ module.exports = function(req, res, proxy) { path = req.url; req.headers.host = target0.hostname; } else { - _ref1 = req.url.match(/\/([^\/]+)(.*)/), ignore = _ref1[0], hostname = _ref1[1], path = _ref1[2]; + _ref1 = req.url.match(/\/([^\/]*)(.*)/), ignore = _ref1[0], hostname = _ref1[1], path = _ref1[2]; _ref2 = hostname.split(':'), host = _ref2[0], port = _ref2[1]; target = { host: host, @@ -51,7 +51,7 @@ module.exports = function(req, res, proxy) { }; req.headers.host = hostname; } - if (!target) { + if (!(target && target.host && target.port)) { res.write("Cannot determine target host\n"); res.end(); return; @@ -61,7 +61,6 @@ module.exports = function(req, res, proxy) { res.setHeader(key, value); } req.url = path; - if (! target.port) target.port = 80; return proxy.proxyRequest(req, res, target); } }; diff --git a/src/corsproxy.coffee b/src/corsproxy.coffee index 6c6ddf5..938ce77 100644 --- a/src/corsproxy.coffee +++ b/src/corsproxy.coffee @@ -39,7 +39,7 @@ module.exports = (req, res, proxy) -> path = req.url req.headers.host = target0.hostname; else - [ignore, hostname, path] = req.url.match(/\/([^\/]+)(.*)/) + [ignore, hostname, path] = req.url.match(/\/([^\/]*)(.*)/) [host, port] = hostname.split(':') target = { host: host, @@ -47,7 +47,7 @@ module.exports = (req, res, proxy) -> } req.headers.host = hostname - unless target + unless target and target.host and target.port res.write "Cannot determine target host\n" res.end(); return; From 9294b071c30891b7a0192b105055cf848285be8f Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Mon, 25 Mar 2013 21:24:46 +0100 Subject: [PATCH 07/51] add MIT license --- LICENSE.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..c1b0524 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright (c) 2011-2013 Gregor Martynus + +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. From 1a9131ea78b76725a1a2c967ed4efe1a262325b8 Mon Sep 17 00:00:00 2001 From: Dale Harvey Date: Tue, 30 Apr 2013 01:48:52 +0100 Subject: [PATCH 08/51] Remove log statement --- lib/corsproxy.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index 9c2dbb4..db0f6d1 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -29,7 +29,6 @@ module.exports = function(req, res, proxy) { 'access-control-allow-origin': req.headers.origin || '*' }; if (req.method === 'OPTIONS') { - console.log('responding to OPTIONS request'); res.writeHead(200, cors_headers); res.end(); } else { From bd881c4938b6a8c23ee84509ede2bd1a0c134b19 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Fri, 3 May 2013 18:41:26 +0200 Subject: [PATCH 09/51] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f461590..5805032 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "corsproxy", "description": "standalone CORS proxy and library for your convenience", - "version": "0.2.11", + "version": "0.2.12", "main": "./lib/corsproxy.js", "author": { "name": "Gregor Martynus", From d1772adf40306ca70dafae5cbd876cafd1d7e65a Mon Sep 17 00:00:00 2001 From: Ken Sheedlo Date: Thu, 6 Jun 2013 11:42:29 -0700 Subject: [PATCH 10/51] Default to target port 80 if unspecified --- lib/corsproxy.js | 5 +++-- src/corsproxy.coffee | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index db0f6d1..0302a59 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 var http, httpProxy, url; url = require('url'); @@ -29,6 +29,7 @@ module.exports = function(req, res, proxy) { 'access-control-allow-origin': req.headers.origin || '*' }; if (req.method === 'OPTIONS') { + console.log('responding to OPTIONS request'); res.writeHead(200, cors_headers); res.end(); } else { @@ -46,7 +47,7 @@ module.exports = function(req, res, proxy) { _ref2 = hostname.split(':'), host = _ref2[0], port = _ref2[1]; target = { host: host, - port: port + port: port || 80 }; req.headers.host = hostname; } diff --git a/src/corsproxy.coffee b/src/corsproxy.coffee index 938ce77..ef2e183 100644 --- a/src/corsproxy.coffee +++ b/src/corsproxy.coffee @@ -43,7 +43,7 @@ module.exports = (req, res, proxy) -> [host, port] = hostname.split(':') target = { host: host, - port: port + port: port || 80 } req.headers.host = hostname From 53ee0334317c7e06dba5570fe77108081423f377 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Fri, 7 Jun 2013 20:03:40 +0200 Subject: [PATCH 11/51] removed console.log --- lib/corsproxy.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index 0302a59..6c0789e 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -29,7 +29,6 @@ module.exports = function(req, res, proxy) { 'access-control-allow-origin': req.headers.origin || '*' }; if (req.method === 'OPTIONS') { - console.log('responding to OPTIONS request'); res.writeHead(200, cors_headers); res.end(); } else { From a04826f9b06e839ea6d8f02b2fa3ce2038d7f4b5 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Thu, 25 Jul 2013 13:03:30 +0200 Subject: [PATCH 12/51] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5805032..ae1e2c3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "corsproxy", "description": "standalone CORS proxy and library for your convenience", - "version": "0.2.12", + "version": "0.2.13", "main": "./lib/corsproxy.js", "author": { "name": "Gregor Martynus", From 1e5def8af574519538ac8c6ecb5d4764c69c20d2 Mon Sep 17 00:00:00 2001 From: Dale Harvey Date: Mon, 29 Jul 2013 13:40:40 +0100 Subject: [PATCH 13/51] Add basic auth headers when they passed in --- lib/corsproxy.js | 4 ++++ src/corsproxy.coffee | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index 6c0789e..e577432 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -49,6 +49,10 @@ module.exports = function(req, res, proxy) { port: port || 80 }; req.headers.host = hostname; + if (target0.auth) { + req.headers.authorization = + 'Basic ' + new Buffer(target0.auth).toString('base64'); + } } if (!(target && target.host && target.port)) { res.write("Cannot determine target host\n"); diff --git a/src/corsproxy.coffee b/src/corsproxy.coffee index ef2e183..9dd2b63 100644 --- a/src/corsproxy.coffee +++ b/src/corsproxy.coffee @@ -4,7 +4,7 @@ httpProxy = require('http-proxy'); module.exports = (req, res, proxy) -> - + if req.headers['access-control-request-headers'] headers = req.headers['access-control-request-headers'] else @@ -17,14 +17,14 @@ module.exports = (req, res, proxy) -> 'access-control-allow-headers' : headers 'access-control-allow-credentials' : 'true' 'access-control-allow-origin' : req.headers.origin || '*' - - + + if req.method is 'OPTIONS' console.log 'responding to OPTIONS request' res.writeHead(200, cors_headers); res.end(); return - + else # Handle CORS proper. @@ -46,19 +46,20 @@ module.exports = (req, res, proxy) -> port: port || 80 } req.headers.host = hostname - + if target0.auth + req.headers.authorization = 'Basic ' + new Buffer(target0.auth).toString('base64'); unless target and target.host and target.port res.write "Cannot determine target host\n" res.end(); return; # console.log "proxying to #{target.host}:#{target.port}#{path}" - - + + res.setHeader(key, value) for key, value of cors_headers - + # req.headers.host = hostname req.url = path - + # Put your custom server logic here, then proxy proxy.proxyRequest(req, res, target); From 1d86e7a18a2fe25d12a4ebbf0c19975038f258df Mon Sep 17 00:00:00 2001 From: Dale Harvey Date: Sat, 21 Sep 2013 20:29:49 +0100 Subject: [PATCH 14/51] Add auth header when specifying credentials via target url param --- lib/corsproxy.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/corsproxy.js b/lib/corsproxy.js index e577432..f9b88c7 100644 --- a/lib/corsproxy.js +++ b/lib/corsproxy.js @@ -41,6 +41,10 @@ module.exports = function(req, res, proxy) { }; path = req.url; req.headers.host = target0.hostname; + if (target0.auth) { + req.headers.authorization = + 'Basic ' + new Buffer(target0.auth).toString('base64'); + } } else { _ref1 = req.url.match(/\/([^\/]*)(.*)/), ignore = _ref1[0], hostname = _ref1[1], path = _ref1[2]; _ref2 = hostname.split(':'), host = _ref2[0], port = _ref2[1]; @@ -49,10 +53,6 @@ module.exports = function(req, res, proxy) { port: port || 80 }; req.headers.host = hostname; - if (target0.auth) { - req.headers.authorization = - 'Basic ' + new Buffer(target0.auth).toString('base64'); - } } if (!(target && target.host && target.port)) { res.write("Cannot determine target host\n"); From 31bea3fc1c50f5cd068d6b14039bc89595a776e3 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sun, 22 Sep 2013 10:39:10 +0200 Subject: [PATCH 15/51] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ae1e2c3..9adb42f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "corsproxy", "description": "standalone CORS proxy and library for your convenience", - "version": "0.2.13", + "version": "0.2.14", "main": "./lib/corsproxy.js", "author": { "name": "Gregor Martynus", From 7b2c276cbf0b63f2725f34812d55028f93f540a2 Mon Sep 17 00:00:00 2001 From: Max Brosnahan Date: Fri, 13 Dec 2013 11:06:50 +1300 Subject: [PATCH 16/51] Update http-proxy version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9adb42f..7e95432 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "email": "jan@apache.org" }, "dependencies": { - "http-proxy": "~0.8" + "http-proxy": "~0.10" }, "bin": { "corsproxy": "./bin/index.js" From fe94061d3591dbdbece2e419360a4f1f0bfbca86 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Tue, 28 Apr 2015 11:55:50 +0200 Subject: [PATCH 17/51] chore(release): 1.0.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE Alles neu™ https://www.youtube.com/watch?v=DD0A2plMSVA --- Procfile | 1 - bin/corsproxy | 59 +++++++++++++++++++++++++++++++++++ bin/index.js | 10 ------ index.js | 12 +++++++ lib/add-cors-headers.js | 41 ++++++++++++++++++++++++ lib/corsproxy.js | 69 ----------------------------------------- lib/logger-options.js | 12 +++++++ package.json | 25 +++++++++------ public/index.html | 11 +++++++ src/corsproxy.coffee | 65 -------------------------------------- 10 files changed, 150 insertions(+), 155 deletions(-) delete mode 100644 Procfile create mode 100755 bin/corsproxy delete mode 100755 bin/index.js create mode 100644 index.js create mode 100644 lib/add-cors-headers.js delete mode 100644 lib/corsproxy.js create mode 100644 lib/logger-options.js create mode 100644 public/index.html delete mode 100644 src/corsproxy.coffee diff --git a/Procfile b/Procfile deleted file mode 100644 index 6f86b16..0000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web: node server.js \ No newline at end of file diff --git a/bin/corsproxy b/bin/corsproxy new file mode 100755 index 0000000..28c938e --- /dev/null +++ b/bin/corsproxy @@ -0,0 +1,59 @@ +#!/usr/bin/env node + +var Hapi = require('hapi') +var plugin = require('../index') +var good = require('good') +var loggerOptions = require('../lib/logger-options') + +var server = new Hapi.Server({}) +var port = parseInt(process.env.CORSPROXY_PORT || 1337, 10) + +server.connection({ + port: port +}) + +// cors plugin +server.register(plugin, function (error) { + if (error) server.log('error', error) +}) + +// logger plugin +server.register({ + register: good, + options: loggerOptions +}, function (error) { + if (error) server.log('error', error) +}) + +// proxy route +server.route({ + method: '*', + path: '/{host}/{path*}', + handler: { + proxy: { + passThrough: true, + mapUri: function(request, callback) { + request.host = request.params.host + request.path = request.path.substr(request.params.host.length + 1) + console.log('proxy to http://' + request.host + request.path) + callback(null, 'http://' + request.host + request.path, request.headers); + } + } + } +}) + +// default route +server.route({ + method: 'GET', + path: '/', + handler: { + file: 'public/index.html' + } +}) + + +server.start(function (error) { + if (error) server.log('error', error) + + server.log('info', 'CORS Proxy running at:' + server.info.uri) +}) diff --git a/bin/index.js b/bin/index.js deleted file mode 100755 index d8858f3..0000000 --- a/bin/index.js +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env node -var corsproxy = require("../lib/corsproxy"); -var httpProxy = require("http-proxy"); - -var host = process.env.HOST || process.argv[2] || "127.0.0.1"; -var port = process.env.PORT || process.argv[3] || 9292; - -httpProxy.createServer(corsproxy).listen(port, host, function() { - console.log("CORS Proxy started on %s:%d", host, port); -}); diff --git a/index.js b/index.js new file mode 100644 index 0000000..d91bf55 --- /dev/null +++ b/index.js @@ -0,0 +1,12 @@ +var addCorsHeaders = require('./lib/add-cors-headers') +var pkg = require('./package.json') + +function corsPlugin (server, options, next) { + server.ext('onPreResponse', addCorsHeaders) + next() +} + +corsPlugin.attributes = { + pkg: pkg +} +module.exports = corsPlugin diff --git a/lib/add-cors-headers.js b/lib/add-cors-headers.js new file mode 100644 index 0000000..b0592f7 --- /dev/null +++ b/lib/add-cors-headers.js @@ -0,0 +1,41 @@ +module.exports = function addCorsHeaders (request, reply) { + var allowedHeaders = [ + 'authorization', + 'content-length', + 'content-type', + 'if-match', + 'if-none-match', + 'origin', + 'x-requested-with' + ] + + function addAllowedHeaders (arr) { + for (var i = 0; i < arr.length; i++) { + if (allowedHeaders.indexOf(arr[i].trim().toLowerCase()) === -1) { + allowedHeaders.push(arr[i].trim().toLowerCase()) + } + } + } + addAllowedHeaders(Object.keys(request.headers)) + + // depending on whether we have a boom or not, + // headers need to be set differently. + var response = request.response.isBoom ? request.response.output : request.response + + if (request.method === 'options') { + response.statusCode = 200 + if (request.headers['Allow-Control-Request-Headers']) { + addAllowedHeaders( + request.headers['Allow-Control-Request-Headers'].split(',') + ) + } + } + + response.headers['access-control-allow-origin'] = request.headers.origin + response.headers['access-control-allow-headers'] = allowedHeaders.join(', ') + response.headers['access-control-expose-headers'] = 'content-type, content-length, etag' + response.headers['access-control-allow-methods'] = 'GET, PUT, POST, DELETE' + response.headers['access-control-allow-credentials'] = 'true' + + reply.continue() +} diff --git a/lib/corsproxy.js b/lib/corsproxy.js deleted file mode 100644 index f9b88c7..0000000 --- a/lib/corsproxy.js +++ /dev/null @@ -1,69 +0,0 @@ -// Generated by CoffeeScript 1.6.3 -var http, httpProxy, url; - -url = require('url'); - -http = require('http'); - -httpProxy = require('http-proxy'); - -module.exports = function(req, res, proxy) { - var cors_headers, header, headers, host, hostname, ignore, key, path, port, target, target0, value, _i, _len, _ref, _ref1, _ref2; - if (req.headers['access-control-request-headers']) { - headers = req.headers['access-control-request-headers']; - } else { - headers = 'accept, accept-charset, accept-encoding, accept-language, authorization, content-length, content-type, host, origin, proxy-connection, referer, user-agent, x-requested-with'; - _ref = req.headers; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - header = _ref[_i]; - if (req.indexOf('x-') === 0) { - headers += ", " + header; - } - } - } - cors_headers = { - 'access-control-allow-methods': 'HEAD, POST, GET, PUT, PATCH, DELETE', - 'access-control-max-age': '86400', - 'access-control-allow-headers': headers, - 'access-control-allow-credentials': 'true', - 'access-control-allow-origin': req.headers.origin || '*' - }; - if (req.method === 'OPTIONS') { - res.writeHead(200, cors_headers); - res.end(); - } else { - module.exports.options = module.exports.options || {}; - if (module.exports.options.target) { - target0 = url.parse(module.exports.options.target); - target = { - host: target0.hostname, - port: target0.port || 80 - }; - path = req.url; - req.headers.host = target0.hostname; - if (target0.auth) { - req.headers.authorization = - 'Basic ' + new Buffer(target0.auth).toString('base64'); - } - } else { - _ref1 = req.url.match(/\/([^\/]*)(.*)/), ignore = _ref1[0], hostname = _ref1[1], path = _ref1[2]; - _ref2 = hostname.split(':'), host = _ref2[0], port = _ref2[1]; - target = { - host: host, - port: port || 80 - }; - req.headers.host = hostname; - } - if (!(target && target.host && target.port)) { - res.write("Cannot determine target host\n"); - res.end(); - return; - } - for (key in cors_headers) { - value = cors_headers[key]; - res.setHeader(key, value); - } - req.url = path; - return proxy.proxyRequest(req, res, target); - } -}; diff --git a/lib/logger-options.js b/lib/logger-options.js new file mode 100644 index 0000000..7af9fcf --- /dev/null +++ b/lib/logger-options.js @@ -0,0 +1,12 @@ +var goodConsole = require('good-console') +module.exports = { + opsInterval: 1000, + reporters: [{ + reporter: goodConsole, + events: { + log: '*', + request: '*', + response: '*' + } + }] +} diff --git a/package.json b/package.json index 7e95432..fdbd5aa 100644 --- a/package.json +++ b/package.json @@ -1,28 +1,33 @@ { "name": "corsproxy", "description": "standalone CORS proxy and library for your convenience", - "version": "0.2.14", - "main": "./lib/corsproxy.js", + "version": "1.0.0", + "main": "./index.js", "author": { "name": "Gregor Martynus", "email": "gregor@martynus.net" }, - "contributors": { - "name": "Jan Lehnardt", - "email": "jan@apache.org" - }, "dependencies": { + "good": "^6.1.2", + "good-console": "^5.0.0", + "hapi": "^8.4.0", "http-proxy": "~0.10" }, "bin": { - "corsproxy": "./bin/index.js" + "corsproxy": "./bin/corsproxy" }, - "engines": { - "node": ">=0.6.x", - "npm": ">=1.1.x" + "scripts": { + "start": "./bin/corsproxy", + "test": "standard" }, "repository": { "type": "git", "url": "https://github.com/gr2m/CORS-Proxy" + }, + "engines": { + "node": "0.10.x" + }, + "devDependencies": { + "standard": "^3.7.2" } } diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..691bade --- /dev/null +++ b/public/index.html @@ -0,0 +1,11 @@ + + + + CORS Proxy + + +

CORS Proxy

+ +

Ole ole

+ + diff --git a/src/corsproxy.coffee b/src/corsproxy.coffee deleted file mode 100644 index 9dd2b63..0000000 --- a/src/corsproxy.coffee +++ /dev/null @@ -1,65 +0,0 @@ -url = require('url'); -http = require('http'); -httpProxy = require('http-proxy'); - - -module.exports = (req, res, proxy) -> - - if req.headers['access-control-request-headers'] - headers = req.headers['access-control-request-headers'] - else - headers = 'accept, accept-charset, accept-encoding, accept-language, authorization, content-length, content-type, host, origin, proxy-connection, referer, user-agent, x-requested-with' - headers += ", #{header}" for header in req.headers when req.indexOf('x-') is 0 - - cors_headers = - 'access-control-allow-methods' : 'HEAD, POST, GET, PUT, PATCH, DELETE' - 'access-control-max-age' : '86400' # 24 hours - 'access-control-allow-headers' : headers - 'access-control-allow-credentials' : 'true' - 'access-control-allow-origin' : req.headers.origin || '*' - - - if req.method is 'OPTIONS' - console.log 'responding to OPTIONS request' - res.writeHead(200, cors_headers); - res.end(); - return - - else - # Handle CORS proper. - - # do we have a target configured? - module.exports.options = module.exports.options || {} - if module.exports.options.target - target0 = url.parse module.exports.options.target - target = { - host: target0.hostname, - port: target0.port || 80 - } - path = req.url - req.headers.host = target0.hostname; - else - [ignore, hostname, path] = req.url.match(/\/([^\/]*)(.*)/) - [host, port] = hostname.split(':') - target = { - host: host, - port: port || 80 - } - req.headers.host = hostname - if target0.auth - req.headers.authorization = 'Basic ' + new Buffer(target0.auth).toString('base64'); - unless target and target.host and target.port - res.write "Cannot determine target host\n" - res.end(); - return; - - # console.log "proxying to #{target.host}:#{target.port}#{path}" - - - res.setHeader(key, value) for key, value of cors_headers - - # req.headers.host = hostname - req.url = path - - # Put your custom server logic here, then proxy - proxy.proxyRequest(req, res, target); From d4740f47b32c4c366b031de45daa7c59a58f7db1 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Tue, 28 Apr 2015 12:02:12 +0200 Subject: [PATCH 18/51] fix: allow to set port number via `proces.env.PORT` --- bin/corsproxy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/corsproxy b/bin/corsproxy index 28c938e..5599258 100755 --- a/bin/corsproxy +++ b/bin/corsproxy @@ -6,7 +6,7 @@ var good = require('good') var loggerOptions = require('../lib/logger-options') var server = new Hapi.Server({}) -var port = parseInt(process.env.CORSPROXY_PORT || 1337, 10) +var port = parseInt(process.env.CORSPROXY_PORT || process.env.PORT || 1337, 10) server.connection({ port: port From 908f3cef10128b89962252b477fa264d482ecdc2 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Tue, 28 Apr 2015 17:21:06 +0200 Subject: [PATCH 19/51] chore(package): added license MIT, updated description --- package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index fdbd5aa..2918edd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "corsproxy", - "description": "standalone CORS proxy and library for your convenience", + "description": "standalone CORS proxy", "version": "1.0.0", "main": "./index.js", "author": { @@ -29,5 +29,6 @@ }, "devDependencies": { "standard": "^3.7.2" - } + }, + "license": "MIT" } From d1acc5522c247f5453bc1d6d837c46fb516f1c03 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Tue, 28 Apr 2015 17:22:23 +0200 Subject: [PATCH 20/51] docs(README): now standalone only --- README.md | 54 ++++++++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 71daf50..242949c 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,24 @@ -## Installation +# corsproxy -As a standalone tool: +> standalone CORS proxy - $ npm install -g corsproxy +## Setup -As a dependency: +``` +npm install -g corsproxy +corsproxy +# with custom port: CORSPROXY_PORT=1234 corsproxy +``` - $ npm install corsproxy - - -## Running - -Standalone: - - $ corsproxy - CORS Proxy started on localhost:9292 - -Standalone with custom host/port: - - $ corsproxy 0.0.0.0 1234 - CORS Proxy started on 0.0.0.0:1234 - -As a dependency: - - var cors_proxy = require("corsproxy"); - var http_proxy = require("http-proxy"); - http_proxy.createServer(cors_proxy).listen(1234); - -With custom target: - - var cors_proxy = require("corsproxy"); - var http_proxy = require("http-proxy"); - cors_proxy.options = { - target: "http://0.0.0.0:5984" - }; - http_proxy.createServer(cors_proxy).listen(1234); +## Usage +The cors proxy will start at http://localhost:1337. +To access another domain, use the domain name (including port) as the first folder, e.g. -## Usage +- http://localhost:9292/localhost:3000/sign_in +- http://localhost:9292/my.domain.com/path/to/resource +- etc etc -The cors proxy will start at http://localhost:9292. To access another domain, use the domain name (including port) as the first folder, e.g. +## License - http://localhost:9292/localhost:3000/sign_in - http://localhost:9292/my.domain.com/path/to/resource - etc etc +MIT From a8833eb856815b6dca7f1f578e49efe65b4867d4 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Fri, 22 May 2015 18:51:48 +0200 Subject: [PATCH 21/51] chore: removed obsolete CAKEFILE --- CAKEFILE | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 CAKEFILE diff --git a/CAKEFILE b/CAKEFILE deleted file mode 100644 index 7288a0d..0000000 --- a/CAKEFILE +++ /dev/null @@ -1,9 +0,0 @@ -{print} = require 'util' -{spawn} = require 'child_process' - -task 'build', 'compile', -> - coffee = spawn 'coffee', ['-c', '-b', '-o', './lib', 'src'] - coffee.stderr.on 'data', (data) -> - process.stderr.write data.toString() - coffee.stdout.on 'data', (data) -> - print data.toString() \ No newline at end of file From f24bfbab4eb0344847516789579bfaa0bd95da4b Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Fri, 22 May 2015 18:52:04 +0200 Subject: [PATCH 22/51] chore: deleted LICENSE file. It's still MIT as stated in README --- LICENSE.md | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md deleted file mode 100644 index c1b0524..0000000 --- a/LICENSE.md +++ /dev/null @@ -1,8 +0,0 @@ -The MIT License (MIT) -Copyright (c) 2011-2013 Gregor Martynus - -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. From 2fd5519c1ea8af4b96f263a7f2b272d08388fb1c Mon Sep 17 00:00:00 2001 From: Den Pakizh Date: Thu, 21 May 2015 00:10:43 +0300 Subject: [PATCH 23/51] Header name fixed: 'access-control-request-headers' --- lib/add-cors-headers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/add-cors-headers.js b/lib/add-cors-headers.js index b0592f7..e8ef4f5 100644 --- a/lib/add-cors-headers.js +++ b/lib/add-cors-headers.js @@ -24,9 +24,9 @@ module.exports = function addCorsHeaders (request, reply) { if (request.method === 'options') { response.statusCode = 200 - if (request.headers['Allow-Control-Request-Headers']) { + if (request.headers['access-control-request-headers']) { addAllowedHeaders( - request.headers['Allow-Control-Request-Headers'].split(',') + request.headers['access-control-request-headers'].split(',') ) } } From 33bcb533d77f6a08e8f7f573306da84c7fc349bd Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Fri, 22 May 2015 18:57:20 +0200 Subject: [PATCH 24/51] docs(README): default port is now 1337 closes #33 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 242949c..dc5558c 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ corsproxy The cors proxy will start at http://localhost:1337. To access another domain, use the domain name (including port) as the first folder, e.g. -- http://localhost:9292/localhost:3000/sign_in -- http://localhost:9292/my.domain.com/path/to/resource +- http://localhost:1337/localhost:3000/sign_in +- http://localhost:1337/my.domain.com/path/to/resource - etc etc ## License From 4f2685591b00b44a5e916d546972ba22b491cba7 Mon Sep 17 00:00:00 2001 From: Den Pakizh Date: Mon, 25 May 2015 20:37:48 +0300 Subject: [PATCH 25/51] 'Host' request header fixed --- bin/corsproxy | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/corsproxy b/bin/corsproxy index 5599258..dc96498 100755 --- a/bin/corsproxy +++ b/bin/corsproxy @@ -35,6 +35,7 @@ server.route({ mapUri: function(request, callback) { request.host = request.params.host request.path = request.path.substr(request.params.host.length + 1) + request.headers['host'] = request.host; console.log('proxy to http://' + request.host + request.path) callback(null, 'http://' + request.host + request.path, request.headers); } From d3f8102ddf97bc607dff58fcd65e7806ecfd78d3 Mon Sep 17 00:00:00 2001 From: Den Pakizh Date: Wed, 10 Jun 2015 14:07:52 +0300 Subject: [PATCH 26/51] To forward query string parameters --- bin/corsproxy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/corsproxy b/bin/corsproxy index dc96498..4f60b1f 100755 --- a/bin/corsproxy +++ b/bin/corsproxy @@ -36,8 +36,9 @@ server.route({ request.host = request.params.host request.path = request.path.substr(request.params.host.length + 1) request.headers['host'] = request.host; + var query = request.url.search ? request.url.search : ''; console.log('proxy to http://' + request.host + request.path) - callback(null, 'http://' + request.host + request.path, request.headers); + callback(null, 'http://' + request.host + request.path + query, request.headers); } } } From 08af6cd33248b2e5f66f223743d46e0fb3157b8d Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 17 Jun 2015 12:06:57 +0200 Subject: [PATCH 27/51] 1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2918edd..a907989 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "corsproxy", "description": "standalone CORS proxy", - "version": "1.0.0", + "version": "1.1.0", "main": "./index.js", "author": { "name": "Gregor Martynus", From b40e6cdf9546eaa8d1514bffb3eaa9a1ebab616f Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 13:36:20 -0400 Subject: [PATCH 28/51] docs: add usage to public index.html page --- public/index.html | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/public/index.html b/public/index.html index 691bade..3dd4c69 100644 --- a/public/index.html +++ b/public/index.html @@ -1,11 +1,33 @@ - - CORS Proxy - - -

CORS Proxy

+ + + CORS Proxy + + + +

CORS Proxy

-

Ole ole

- +

+ To add CORS headers to another URL, set the target URL as the path for the CORS proxy's URL. + For example, if you wan to access http://my.domain.com/path/to/resource, and the CORS proxy's + URL is http://localhost:1337, the full proxy URL is http://localhost:1337/my.domain.com/path/to/resource. +

+ +

+ You do not open http://localhost:1337/my.domain.com/path/to/resource directly in your browser, + instead open it through XMLHttpRequest. + For example, if you are using jQuery, you can do $.get('http://localhost:1337/my.domain.com/path/to/resource') +

+ +

+ https://github.com/gr2m/CORS-Proxy +

+ From 41c1eb800daea180271c3297cf56529030bd55c3 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 13:39:50 -0400 Subject: [PATCH 29/51] chore(package): semantic-release setup --- package.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a907989..cba1b8f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,6 @@ { "name": "corsproxy", "description": "standalone CORS proxy", - "version": "1.1.0", "main": "./index.js", "author": { "name": "Gregor Martynus", @@ -18,7 +17,8 @@ }, "scripts": { "start": "./bin/corsproxy", - "test": "standard" + "test": "standard", + "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, "repository": { "type": "git", @@ -28,7 +28,8 @@ "node": "0.10.x" }, "devDependencies": { - "standard": "^3.7.2" + "standard": "^3.7.2", + "semantic-release": "^4.0.3" }, "license": "MIT" } From 7193c0074ada748eb86bc265bc0776833a985fb0 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 13:40:03 -0400 Subject: [PATCH 30/51] chore(travis): initial version --- .travis.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f5b7116 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +sudo: false +language: node_js +cache: + directories: + - node_modules +notifications: + email: false +node_js: + - iojs +before_install: + - npm i -g npm@^2.0.0 +before_script: + - npm prune +after_success: + - npm run semantic-release From f4871a8d4050630fe133ca7e6f3d8f9379214f12 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 13:41:46 -0400 Subject: [PATCH 31/51] fix: add empty favicon to avoid errors when loading default page --- bin/corsproxy | 9 ++++++++- public/favicon.ico | Bin 0 -> 198 bytes 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 public/favicon.ico diff --git a/bin/corsproxy b/bin/corsproxy index 4f60b1f..c0565af 100755 --- a/bin/corsproxy +++ b/bin/corsproxy @@ -35,7 +35,7 @@ server.route({ mapUri: function(request, callback) { request.host = request.params.host request.path = request.path.substr(request.params.host.length + 1) - request.headers['host'] = request.host; + request.headers['host'] = request.host; var query = request.url.search ? request.url.search : ''; console.log('proxy to http://' + request.host + request.path) callback(null, 'http://' + request.host + request.path + query, request.headers); @@ -52,6 +52,13 @@ server.route({ file: 'public/index.html' } }) +server.route({ + method: 'GET', + path: '/favicon.ico', + handler: { + file: 'public/favicon.ico' + } +}) server.start(function (error) { diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..85a4d9fac03ba838f37feca9d583478bd4afd647 GIT binary patch literal 198 xcmZQzU<5(|0VXiLfq{WR42U&=SOAC>fEXkX0u5jillcGtKc*mwA|N9u!2sz^B%}ZU literal 0 HcmV?d00001 From 2a7f06027d415410c943f50d62ae7b164b7d6d82 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:35:13 -0400 Subject: [PATCH 32/51] chore(package): updated to hapi@9. Adds vision for test server --- package.json | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index cba1b8f..46455e1 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,16 @@ "dependencies": { "good": "^6.1.2", "good-console": "^5.0.0", - "hapi": "^8.4.0", - "http-proxy": "~0.10" + "h2o2": "^4.0.1", + "hapi": "^9.0.2", + "http-proxy": "~0.10", + "inert": "^3.0.1", + "vision": "^3.0.0" + }, + "devDependencies": { + "ejs": "^2.3.3", + "standard": "^3.7.2", + "semantic-release": "^4.0.3" }, "bin": { "corsproxy": "./bin/corsproxy" @@ -24,12 +32,5 @@ "type": "git", "url": "https://github.com/gr2m/CORS-Proxy" }, - "engines": { - "node": "0.10.x" - }, - "devDependencies": { - "standard": "^3.7.2", - "semantic-release": "^4.0.3" - }, "license": "MIT" } From 2faa057d1dc01b985c5b1259b8cfc743999b02c1 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:35:39 -0400 Subject: [PATCH 33/51] feat: start test server with `DEBUG=1 npm start` --- bin/corsproxy | 70 +++++++++++++++++++++++++++++++++++-------- public/test/index.ejs | 48 +++++++++++++++++++++++++++++ public/test/test.json | 3 ++ 3 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 public/test/index.ejs create mode 100644 public/test/test.json diff --git a/bin/corsproxy b/bin/corsproxy index c0565af..3f14c60 100755 --- a/bin/corsproxy +++ b/bin/corsproxy @@ -8,12 +8,15 @@ var loggerOptions = require('../lib/logger-options') var server = new Hapi.Server({}) var port = parseInt(process.env.CORSPROXY_PORT || process.env.PORT || 1337, 10) -server.connection({ - port: port -}) +var proxy = server.connection({ port: port, labels: ['proxy'] }) + +server.register(require('inert'), function () {}) +server.register(require('h2o2'), function () {}) // cors plugin -server.register(plugin, function (error) { +server.register(plugin, { + select: ['proxy'] +}, function (error) { if (error) server.log('error', error) }) @@ -26,33 +29,33 @@ server.register({ }) // proxy route -server.route({ +proxy.route({ method: '*', path: '/{host}/{path*}', handler: { proxy: { passThrough: true, - mapUri: function(request, callback) { + mapUri: function (request, callback) { request.host = request.params.host request.path = request.path.substr(request.params.host.length + 1) - request.headers['host'] = request.host; - var query = request.url.search ? request.url.search : ''; + request.headers['host'] = request.host + var query = request.url.search ? request.url.search : '' console.log('proxy to http://' + request.host + request.path) - callback(null, 'http://' + request.host + request.path + query, request.headers); + callback(null, 'http://' + request.host + request.path + query, request.headers) } } } }) // default route -server.route({ +proxy.route({ method: 'GET', path: '/', handler: { file: 'public/index.html' } }) -server.route({ +proxy.route({ method: 'GET', path: '/favicon.ico', handler: { @@ -60,9 +63,52 @@ server.route({ } }) +if (process.env.DEBUG) { + var testport = port + 1 + var test = server.connection({ port: testport, labels: ['test'] }) + + server.register(require('vision'), function (error) { + if (error) { + throw error + } + + server.views({ + engines: { ejs: require('ejs') }, + path: 'public/test' + }) + }) + + test.route({ + method: 'GET', + path: '/favicon.ico', + handler: { + file: 'public/favicon.ico' + } + }) + test.route({ + method: 'GET', + path: '/test.json', + handler: { + file: 'public/test/test.json' + } + }) + + test.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + reply.view('index', { + proxyPort: proxy.info.port, + testPort: test.info.port + }) + } + }) + + server.log('info', 'Debug server starting at: ' + test.info.uri) +} server.start(function (error) { if (error) server.log('error', error) - server.log('info', 'CORS Proxy running at:' + server.info.uri) + server.log('info', 'CORS Proxy running at: ' + server.info.uri) }) diff --git a/public/test/index.ejs b/public/test/index.ejs new file mode 100644 index 0000000..225b771 --- /dev/null +++ b/public/test/index.ejs @@ -0,0 +1,48 @@ + + + + CORS Proxy test + + +

CORS Proxy test

+ +
+

Example 1: try loading /test.json from different host

+
var url = 'http://127.0.0.1:<%= testPort %>/test.json'
+var request = new XMLHttpRequest();
+request.addEventListener('load', function () {
+  document.querySelector('#example-1 .result').textContent = 'Success'
+})
+request.addEventListener('error', function () {
+  document.querySelector('#example-1 .result').textContent = 'Error'
+})
+request.open('get', url, true)
+request.setRequestHeader('Content-Type', 'application/json')
+request.send()
+ +

Result:

+
+ +
+

Example 2: try loading /test.json from different host via proxy

+
var url = 'http://localhost:<%= proxyPort %>/127.0.0.1:<%= testPort %>/test.json'
+var request = new XMLHttpRequest();
+request.addEventListener('load', function () {
+  document.querySelector('#example-2 .result').textContent = 'Success'
+})
+request.addEventListener('error', function () {
+  document.querySelector('#example-2 .result').textContent = 'Error'
+})
+request.open('get', url, true)
+request.setRequestHeader('Content-Type', 'application/json')
+request.send()
+ +

Result:

+
+ + + + diff --git a/public/test/test.json b/public/test/test.json new file mode 100644 index 0000000..82dae19 --- /dev/null +++ b/public/test/test.json @@ -0,0 +1,3 @@ +{ + "ok": true +} \ No newline at end of file From 263dfd664394f16bb9a1021600696990f90d3acf Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:36:51 -0400 Subject: [PATCH 34/51] docs(README): start test server with `DEBUG=1 npm start` --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dc5558c..f7eb22f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ npm install -g corsproxy corsproxy # with custom port: CORSPROXY_PORT=1234 corsproxy +# with debug server: DEBUG=1 corsproxy ``` ## Usage From 44af59d1041f2fc42144e578f39e721356785524 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:37:49 -0400 Subject: [PATCH 35/51] chore(package): updated dependencies --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 46455e1..8a37d70 100644 --- a/package.json +++ b/package.json @@ -7,17 +7,17 @@ "email": "gregor@martynus.net" }, "dependencies": { - "good": "^6.1.2", - "good-console": "^5.0.0", + "good": "^6.3.0", + "good-console": "^5.0.3", "h2o2": "^4.0.1", "hapi": "^9.0.2", - "http-proxy": "~0.10", + "http-proxy": "~1.11", "inert": "^3.0.1", "vision": "^3.0.0" }, "devDependencies": { "ejs": "^2.3.3", - "standard": "^3.7.2", + "standard": "^5.1.0", "semantic-release": "^4.0.3" }, "bin": { From ddec21e695ec4da11e748ab5a94fc6285d23f03b Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:38:58 -0400 Subject: [PATCH 36/51] docs(README): badges --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index f7eb22f..1cfba50 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,13 @@ > standalone CORS proxy +[![Build Status](https://travis-ci.org/gr2m/CORS-Proxy.svg?branch=master)](https://travis-ci.org/gr2m/CORS-Proxy) +[![Dependency Status](https://david-dm.org/gr2m/CORS-Proxy.svg)](https://david-dm.org/gr2m/CORS-Proxy) +[![devDependency Status](https://david-dm.org/gr2m/CORS-Proxy/dev-status.svg)](https://david-dm.org/gr2m/CORS-Proxy#info=devDependencies) + +[![NPM](https://nodei.co/npm/corsproxy.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/corsproxy/) + + ## Setup ``` From 936f217c5c39531f529025fcd7e5491ed5422e20 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:50:45 -0400 Subject: [PATCH 37/51] chore(package): add dependencies for CI tests --- package.json | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 8a37d70..fe99d9f 100644 --- a/package.json +++ b/package.json @@ -16,17 +16,29 @@ "vision": "^3.0.0" }, "devDependencies": { + "chai": "^3.2.0", + "chai-as-promised": "^5.1.0", + "colors": "^1.1.2", "ejs": "^2.3.3", + "mocha": "^2.2.5", + "sauce-connect-launcher": "^0.12.0", + "semantic-release": "^4.0.3", "standard": "^5.1.0", - "semantic-release": "^4.0.3" + "sv-selenium": "^0.2.6", + "wd": "^0.3.12" }, "bin": { "corsproxy": "./bin/corsproxy" }, "scripts": { + "install_selenium_and_chromedriver": "install_chromedriver && install_selenium", + "kill_selenium": "kill_selenium", + "semantic-release": "semantic-release pre && npm publish && semantic-release post", "start": "./bin/corsproxy", - "test": "standard", - "semantic-release": "semantic-release pre && npm publish && semantic-release post" + "start_app": "./bin/start-app", + "start_selenium_with_chromedriver": "start_selenium_with_chromedriver", + "test": "standard && ./bin/test-background", + "test:mocha": "mocha test/test-*.js" }, "repository": { "type": "git", From b5775e0891de6c0576734ead63267b62d399029f Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:51:01 -0400 Subject: [PATCH 38/51] chore(travis): run test in Firefox on Travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index f5b7116..a98dcd7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,9 @@ node_js: - iojs before_install: - npm i -g npm@^2.0.0 + - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start +script: TEST_CLIENT=selenium:firefox npm test before_script: - npm prune after_success: From 82f59390bbee2c7b84ebc22a4b37548c53fe7c24 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:51:21 -0400 Subject: [PATCH 39/51] test: initial version --- bin/test-background | 48 ++++++++++++++++ test/test-debug-page.js | 17 ++++++ test/test-init.js | 118 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100755 bin/test-background create mode 100644 test/test-debug-page.js create mode 100644 test/test-init.js diff --git a/bin/test-background b/bin/test-background new file mode 100755 index 0000000..8a7466f --- /dev/null +++ b/bin/test-background @@ -0,0 +1,48 @@ +#!/bin/bash + +set -e + +function cleanup { + set +e + + npm run kill_selenium + + echo "\nYou can safely ignore errors below, if you see any" + + # kill chrome driver + CHROMEDRIVER_PID=`ps -ef | grep chromedriver | grep -v grep | awk '{print $2}'` + if [ "$CHROMEDRIVER_PID" ] ; then + kill -9 $CHROMEDRIVER_PID + fi + # kill test app + APP_PID=`ps -ef | grep 'corsproxy' | grep -v grep | awk '{print $2}'` + if [ "$APP_PID" ] ; then + kill -9 $APP_PID + fi +} + +# always kill selenium, no matter if tests pass or exit +trap cleanup EXIT + +if [ ! -d "/tmp/sv-selenium" ]; then + echo 'Selenium not yet installed, downloading & installing ...' + npm run install_selenium_and_chromedriver +fi + +npm run start_selenium_with_chromedriver & +DEBUG=1 npm start & + +COUNTER=0 +until $(curl --output /dev/null --silent --head --fail http://localhost:1338); do + let COUNTER=COUNTER+1 + echo "Waiting for app ... $COUNTER / 60" + sleep 1 + if [[ COUNTER -eq 60 ]] ; then + echo "" + echo "App start timeout" + exit 1 + fi +done +echo 'App started :)' + +npm run test:mocha diff --git a/test/test-debug-page.js b/test/test-debug-page.js new file mode 100644 index 0000000..9798092 --- /dev/null +++ b/test/test-debug-page.js @@ -0,0 +1,17 @@ +/* global describe, it, beforeEach*/ +'use strict' + +describe('example app', function () { + this.timeout(60000) + + beforeEach(function () { + return this.browser + .get('http://localhost:1338') + }) + + it('Heading is "CORS Proxy test"', function () { + return this.browser + .elementByCssSelector('h1').text() + .should.eventually.equal('CORS Proxy test') + }) +}) diff --git a/test/test-init.js b/test/test-init.js new file mode 100644 index 0000000..5cbeedc --- /dev/null +++ b/test/test-init.js @@ -0,0 +1,118 @@ +/* global before, after*/ +'use strict' + +require('colors') +var wd = require('wd') +var sauceConnectLauncher = require('sauce-connect-launcher') + +var chai = require('chai') +var chaiAsPromised = require('chai-as-promised') +chai.use(chaiAsPromised) +chai.should() +var request = require('request').defaults({json: true}) + +// enables chai assertion chaining +chaiAsPromised.transferPromiseness = wd.transferPromiseness + +var SELENIUM_HUB = 'http://localhost:4444/wd/hub/status' + +var username = process.env.SAUCE_USERNAME +var accessKey = process.env.SAUCE_ACCESS_KEY +var tunnelId = process.env.TRAVIS_JOB_NUMBER || 'tunnel-' + Date.now() + +// process.env.TEST_CLIENT is a colon seperated list of +// (saucelabs|selenium):browserName:browserVerion:platform +var tmp = (process.env.TEST_CLIENT || 'selenium:chrome').split(':') +var client = { + runner: tmp[0] || 'selenium', + browser: tmp[1] || 'chrome', + version: tmp[2] || null, // Latest + platform: tmp[3] || null +} + +wd.configureHttp({timeout: 180000}) // 3 minutes + +before(function (done) { + var self = this + this.timeout(180000) + + var retries = 0 + var started = function () { + if (++retries > 60) { + done('Unable to connect to selenium') + return + } + + if (client.runner === 'saucelabs') { + startSauceConnect(startTest) + } else { + startSelenium(startTest) + } + + function startSelenium (callback) { + request(SELENIUM_HUB, function (error, resp) { + if (error) throw error + + if (resp && resp.statusCode === 200) { + self.browser = wd.promiseChainRemote() + callback() + } else { + console.log('.') + setTimeout(started, 1000) + } + }) + } + + function startSauceConnect (callback) { + console.log('connecting to saucelabs') + var options = { + username: username, + accessKey: accessKey, + tunnelIdentifier: tunnelId + } + + sauceConnectLauncher(options, function (err, process) { + if (err) { + console.error('Failed to connect to saucelabs') + console.error(err) + return process.exit(1) + } + self.browser = wd.promiseChainRemote('localhost', 4445, username, accessKey) + callback() + }) + } + + function startTest () { + // optional extra logging + self.browser.on('status', function (info) { + console.log(info.cyan) + }) + self.browser.on('command', function (eventType, command, response) { + if (eventType === 'RESPONSE') { + return console.log(' <', (response || 'ok').grey) + } + console.log(' > ' + eventType.cyan, command) + }) + + var options = { + browserName: client.browser, + version: client.version, + platform: client.platform, + tunnelTimeout: 30 * 60 * 1000, + name: client.browser + ' - ' + tunnelId, + 'max-duration': 60 * 45, + 'command-timeout': 599, + 'idle-timeout': 599, + 'tunnel-identifier': tunnelId + } + + self.browser.init(options, done) + } + } + + started() +}) + +after(function () { + return this.browser.quit() +}) From 0e97c6453dad609ddf1b7a9a6ac43c204deb139b Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 14:53:12 -0400 Subject: [PATCH 40/51] fix(package): adds missing devDependency: request --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index fe99d9f..5e303bf 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "colors": "^1.1.2", "ejs": "^2.3.3", "mocha": "^2.2.5", + "request": "^2.60.0", "sauce-connect-launcher": "^0.12.0", "semantic-release": "^4.0.3", "standard": "^5.1.0", From ecd99c168e69422966eef2d22e29ff1ae31943dc Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 15:02:00 -0400 Subject: [PATCH 41/51] test: check for request results --- test/test-debug-page.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/test-debug-page.js b/test/test-debug-page.js index 9798092..3c4c398 100644 --- a/test/test-debug-page.js +++ b/test/test-debug-page.js @@ -12,6 +12,20 @@ describe('example app', function () { it('Heading is "CORS Proxy test"', function () { return this.browser .elementByCssSelector('h1').text() - .should.eventually.equal('CORS Proxy test') + .should.eventually.equal('CORS Proxy test') + }) + + it('Example 1 result is Error', function () { + return this.browser + .waitForConditionInBrowser('!! document.querySelector("#example-1 .result").textContent', 10000) + .elementByCssSelector('#example-1 .result').text() + .should.eventually.equal('Error') + }) + + it('Example 2 result is Error', function () { + return this.browser + .waitForConditionInBrowser('!! document.querySelector("#example-2 .result").textContent', 10000) + .elementByCssSelector('#example-2 .result').text() + .should.eventually.equal('Success') }) }) From 6314b0369627e88fca0f4b70cb19d91136e62fb5 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 15:21:26 -0400 Subject: [PATCH 42/51] chore(package): hapi-cors-headers --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 5e303bf..dd729d1 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "good-console": "^5.0.3", "h2o2": "^4.0.1", "hapi": "^9.0.2", + "hapi-cors-headers": "^1.0.0", "http-proxy": "~1.11", "inert": "^3.0.1", "vision": "^3.0.0" From 180aa0c1587beee9fbae91ab4880a6597ffcebdb Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 15:21:41 -0400 Subject: [PATCH 43/51] feat: use hapi-cors-headers instead of own implementation --- index.js | 2 +- lib/add-cors-headers.js | 41 ----------------------------------------- 2 files changed, 1 insertion(+), 42 deletions(-) delete mode 100644 lib/add-cors-headers.js diff --git a/index.js b/index.js index d91bf55..8cb231e 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -var addCorsHeaders = require('./lib/add-cors-headers') +var addCorsHeaders = require('hapi-cors-headers') var pkg = require('./package.json') function corsPlugin (server, options, next) { diff --git a/lib/add-cors-headers.js b/lib/add-cors-headers.js deleted file mode 100644 index e8ef4f5..0000000 --- a/lib/add-cors-headers.js +++ /dev/null @@ -1,41 +0,0 @@ -module.exports = function addCorsHeaders (request, reply) { - var allowedHeaders = [ - 'authorization', - 'content-length', - 'content-type', - 'if-match', - 'if-none-match', - 'origin', - 'x-requested-with' - ] - - function addAllowedHeaders (arr) { - for (var i = 0; i < arr.length; i++) { - if (allowedHeaders.indexOf(arr[i].trim().toLowerCase()) === -1) { - allowedHeaders.push(arr[i].trim().toLowerCase()) - } - } - } - addAllowedHeaders(Object.keys(request.headers)) - - // depending on whether we have a boom or not, - // headers need to be set differently. - var response = request.response.isBoom ? request.response.output : request.response - - if (request.method === 'options') { - response.statusCode = 200 - if (request.headers['access-control-request-headers']) { - addAllowedHeaders( - request.headers['access-control-request-headers'].split(',') - ) - } - } - - response.headers['access-control-allow-origin'] = request.headers.origin - response.headers['access-control-allow-headers'] = allowedHeaders.join(', ') - response.headers['access-control-expose-headers'] = 'content-type, content-length, etag' - response.headers['access-control-allow-methods'] = 'GET, PUT, POST, DELETE' - response.headers['access-control-allow-credentials'] = 'true' - - reply.continue() -} From 955a8666c6efdf79c4f68e92d9f3027b680f1773 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Mon, 17 Aug 2015 15:31:52 -0400 Subject: [PATCH 44/51] fix(package): adds missing dependency: ejs --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dd729d1..edc16ac 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "email": "gregor@martynus.net" }, "dependencies": { + "ejs": "^2.3.3", "good": "^6.3.0", "good-console": "^5.0.3", "h2o2": "^4.0.1", @@ -20,7 +21,6 @@ "chai": "^3.2.0", "chai-as-promised": "^5.1.0", "colors": "^1.1.2", - "ejs": "^2.3.3", "mocha": "^2.2.5", "request": "^2.60.0", "sauce-connect-launcher": "^0.12.0", From 697e7c29611554cc2137e2f0b49d0e9a9c21bc7f Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 28 Oct 2015 12:41:46 -0400 Subject: [PATCH 45/51] chore(travis): test in node@4 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a98dcd7..c8447c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ cache: notifications: email: false node_js: - - iojs + - '4' before_install: - npm i -g npm@^2.0.0 - export DISPLAY=:99.0 From bbe3c50924ff6a06dba02276ac9640a56c9393d1 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 28 Oct 2015 13:15:41 -0400 Subject: [PATCH 46/51] docs(README): add `CORSPROXY_HOST` env option --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1cfba50..97d3354 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ npm install -g corsproxy corsproxy # with custom port: CORSPROXY_PORT=1234 corsproxy +# with custom host: CORSPROXY_HOST=localhost corsproxy # with debug server: DEBUG=1 corsproxy ``` From d3d5340b40dee06fc9930d54e44953682ff9f312 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 28 Oct 2015 13:16:04 -0400 Subject: [PATCH 47/51] feat: set custom host using `CORSPROXY_HOST` env variable closes #44 --- bin/corsproxy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/corsproxy b/bin/corsproxy index 3f14c60..acf0b22 100755 --- a/bin/corsproxy +++ b/bin/corsproxy @@ -7,8 +7,8 @@ var loggerOptions = require('../lib/logger-options') var server = new Hapi.Server({}) var port = parseInt(process.env.CORSPROXY_PORT || process.env.PORT || 1337, 10) - -var proxy = server.connection({ port: port, labels: ['proxy'] }) +var host = (process.env.CORSPROXY_HOST || 'localhost'); +var proxy = server.connection({ port: port, labels: ['proxy'], host: host}) server.register(require('inert'), function () {}) server.register(require('h2o2'), function () {}) @@ -65,7 +65,7 @@ proxy.route({ if (process.env.DEBUG) { var testport = port + 1 - var test = server.connection({ port: testport, labels: ['test'] }) + var test = server.connection({ port: testport, labels: ['test'], host: host }) server.register(require('vision'), function (error) { if (error) { From 81dcc2710bd399c1e762fd968f8ee07c4165e1cc Mon Sep 17 00:00:00 2001 From: Roc Date: Mon, 26 Sep 2016 11:24:37 +0800 Subject: [PATCH 48/51] feat: CORSPROXY_MAX_PAYLOAD option (#47) --- .travis.yml | 4 ++-- README.md | 1 + bin/corsproxy | 8 +++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index c8447c1..b60b42e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,9 @@ cache: notifications: email: false node_js: - - '4' + - '6' before_install: - - npm i -g npm@^2.0.0 + - npm i -g npm@^3.0.0 - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start script: TEST_CLIENT=selenium:firefox npm test diff --git a/README.md b/README.md index 97d3354..02fb348 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ corsproxy # with custom port: CORSPROXY_PORT=1234 corsproxy # with custom host: CORSPROXY_HOST=localhost corsproxy # with debug server: DEBUG=1 corsproxy +# with custom payload max bytes set to 10MB (1MB by default): CORSPROXY_MAX_PAYLOAD=10485760 corsproxy ``` ## Usage diff --git a/bin/corsproxy b/bin/corsproxy index acf0b22..c82f3a5 100755 --- a/bin/corsproxy +++ b/bin/corsproxy @@ -8,6 +8,7 @@ var loggerOptions = require('../lib/logger-options') var server = new Hapi.Server({}) var port = parseInt(process.env.CORSPROXY_PORT || process.env.PORT || 1337, 10) var host = (process.env.CORSPROXY_HOST || 'localhost'); +var maxPayload = parseInt(process.env.CORSPROXY_MAX_PAYLOAD || 1048576, 10) var proxy = server.connection({ port: port, labels: ['proxy'], host: host}) server.register(require('inert'), function () {}) @@ -44,6 +45,11 @@ proxy.route({ callback(null, 'http://' + request.host + request.path + query, request.headers) } } + }, + config: { + payload: { + maxBytes: maxPayload + } } }) @@ -110,5 +116,5 @@ if (process.env.DEBUG) { server.start(function (error) { if (error) server.log('error', error) - server.log('info', 'CORS Proxy running at: ' + server.info.uri) + server.log('info', 'CORS Proxy running at: ' + proxy.info.uri) }) From 62c6d9ba58e330685b6050c7ed4971683fd6b41f Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 10 May 2017 14:20:20 +0200 Subject: [PATCH 49/51] chore: activate probot-stale see https://github.com/probot/stale for more information --- .github/stale.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/stale.yml diff --git a/.github/stale.yml b/.github/stale.yml new file mode 100644 index 0000000..d3146d2 --- /dev/null +++ b/.github/stale.yml @@ -0,0 +1,3 @@ +# see available options / defaults at https://github.com/probot/stale/#usage +daysUntilStale: 60 +daysUntilClose: 7 From c29a603d39ad567e5d25f846684a01830f2be1f3 Mon Sep 17 00:00:00 2001 From: Robert Metcalf Date: Wed, 12 Jul 2017 13:46:35 +0100 Subject: [PATCH 50/51] Add instructions to run with different hostname I was using this but was unable to get it working from a different machine. I found the answer was to set CORSPROXY_HOST by looking at the source code. This isn't documented anywhere but I think it is a common enough usage to require explanation. This would have saved me some time. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 02fb348..7a9fd6b 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ To access another domain, use the domain name (including port) as the first fold - http://localhost:1337/my.domain.com/path/to/resource - etc etc +By default the cors proxy will only answer requests sent to localhost. To use another domain (e.g. machine name) set an enviroment variable CORSPROXY_HOST to the required value before launching. + ## License MIT From 8809410d175798102bc908cd51d2a658e834016d Mon Sep 17 00:00:00 2001 From: Theo Pack Date: Thu, 5 Oct 2017 19:46:04 +0200 Subject: [PATCH 51/51] Add docker alternative --- Dockerfile | 9 +++++++++ README.md | 12 ++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ac00bc8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM node:8 + +RUN npm install -g corsproxy + +EXPOSE 1337 + +ENV CORSPROXY_HOST 0.0.0.0 + +ENTRYPOINT ["corsproxy"] \ No newline at end of file diff --git a/README.md b/README.md index 7a9fd6b..14a8219 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,18 @@ corsproxy # with custom payload max bytes set to 10MB (1MB by default): CORSPROXY_MAX_PAYLOAD=10485760 corsproxy ``` +### Docker + +It is also possible to run the cors proxy in a docker container: + +``` +# Build image +docker build -t corsproxy . + +# Run container +docker run -p 1337:1337 --name corsproxy -d corsproxy +``` + ## Usage The cors proxy will start at http://localhost:1337.