From 80da247827403af709f3194eb277a631d35f6c1e Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Wed, 6 May 2015 12:07:31 +0200 Subject: [PATCH] Add httpsOptions option to create https.Server Note: This does not work with the current version of node-http-proxy and node 0.12.0 because req.connection.pair is not defined. --- README.md | 2 ++ lib/cors-anywhere.js | 9 ++++-- test/cert.pem | 12 ++++++++ test/key.pem | 15 +++++++++ test/test.js | 72 ++++++++++++++++++++++++++++---------------- 5 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 test/cert.pem create mode 100644 test/key.pem diff --git a/README.md b/README.md index b232743..b2b8370 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,8 @@ The following options are recognized by both methods: `createServer` recognizes the following option as well: * `httpProxyOptions` - Options for http-proxy. The documentation for these options can be found [here](https://github.com/nodejitsu/node-http-proxy#options). +* `httpsOptions` - If set, a `https.Server` will be created. The given options are passed to the + [`https.createServer`](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener) method. ## Dependencies diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index 9715204..8bc730f 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -4,7 +4,6 @@ 'use strict'; /* jshint node:true, eqnull:true, sub:true, quotmark:single, unused:true */ -var http = require('http'); var httpProxy = require('http-proxy'); var net = require('net'); var url = require('url'); @@ -308,7 +307,13 @@ exports.createServer = function createServer(options) { } var proxy = httpProxy.createServer(httpProxyOptions); - var server = http.createServer(getHandler(options, proxy)); + var requestHandler = getHandler(options, proxy); + var server; + if (options.httpsOptions) { + server = require('https').createServer(options.httpsOptions, requestHandler); + } else { + server = require('http').createServer(requestHandler); + } // When the server fails, just show a 404 instead of Internal server error proxy.on('error', function(err, req, res) { diff --git a/test/cert.pem b/test/cert.pem new file mode 100644 index 0000000..3684422 --- /dev/null +++ b/test/cert.pem @@ -0,0 +1,12 @@ +-----BEGIN CERTIFICATE----- +MIIBsTCCARoCCQDp0DuED0RAJzANBgkqhkiG9w0BAQsFADAdMRswGQYDVQQDDBJj +b3JzLWFueXdoZXJlIHRlc3QwHhcNMTUwNTA2MDcyOTM1WhcNMTUwNjA1MDcyOTM1 +WjAdMRswGQYDVQQDDBJjb3JzLWFueXdoZXJlIHRlc3QwgZ8wDQYJKoZIhvcNAQEB +BQADgY0AMIGJAoGBALzTF5ClJKvkB6h9h7kLORV+mMV3ySDs+oGZn0NgXM+yb9Zh +69r5e95zZJl/V432LFdy0hkEcVteUkC2REWG8D4COGfiwWsXyZdaP1qqLpDpPAMm +v6xFHjW6rVuxzfr4GUjE0Zh9Fg2R2SbtCOcHS/LZoDVOqOvn6+urP6XFY4aFAgMB +AAEwDQYJKoZIhvcNAQELBQADgYEAYXMhS8ouff/c8lSUUs/CLh010cj5RPk/ivS7 +aN2PArzQ6pZvhpgJKf7XAQksBtLYYZMzIpG6W8zhPSbqzly7lELAdE+sxcbbfu8A +FMjNVFQ2Fm1c8ImX8qpE3nhVrPAiwfPjGBqKHTl730gvbh1XH9TC4O4dZcbEomX3 +5MsxQfc= +-----END CERTIFICATE----- diff --git a/test/key.pem b/test/key.pem new file mode 100644 index 0000000..5680ec9 --- /dev/null +++ b/test/key.pem @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQC80xeQpSSr5AeofYe5CzkVfpjFd8kg7PqBmZ9DYFzPsm/WYeva ++Xvec2SZf1eN9ixXctIZBHFbXlJAtkRFhvA+Ajhn4sFrF8mXWj9aqi6Q6TwDJr+s +RR41uq1bsc36+BlIxNGYfRYNkdkm7QjnB0vy2aA1Tqjr5+vrqz+lxWOGhQIDAQAB +AoGBAISy8OelN01Zlowxk/VWTsqtSl3UHdP21uHHfWaTTQZlxzTpYiBknkmp3LQH +CxfoPidCuSX9ulBUzAdQUFBwUVp8wyPIRjpNyRiD58dLNxG0G+OACqnLxNWqIf6F +vS3UqrRGIA5u+GSz+0g3DAeVA5JmsAyHQGkJsh3pcuD8/7wNAkEA7MScGfySy9td +dDBekVU5/GaVg4DA4ELtDNfa99ARB89XP0ps/XrOPEL9yxTjWIHH+qxuhpfG6zGN +ouxZlvBT9wJBAMwpig4A4JE8M8pBDwMY4213gud8B1grQTbhz5bv51aTaIEQFcxw +sGfEmAfVToI+kVTrdFggy42YCSMSvwuF4mMCQQDZHkqPwf/TlSwT2i8+UstD28aL +uswkWvsKZf9UdKbJZKd7UIK1x6HLvRsC2frJNOnvw6PvJMuy7dQWbWqScXxtAkBv +/5msdO68vbnriiUiHdUliBpXwsKEq7Xq1ZV7x7+wzszVgG106ZzcUAzWvz2CVbCE +VWZNsi/4TR82DmKff6LhAkBA/xceWaZjxh5dkWkIrMFWd2GFhGlpfwYw7oELwRL8 +RYXzc1Mr2fDdZDgwgjg67JQqIhOQ3E4RGKPgZ+E7Pk3/ +-----END RSA PRIVATE KEY----- diff --git a/test/test.js b/test/test.js index 3eb59d9..d8e8227 100644 --- a/test/test.js +++ b/test/test.js @@ -236,32 +236,6 @@ describe('Basic functionality', function() { }, done); }); - // Skipped because x-forwarded-proto == http and port == 80 - it.skip('X-Forwarded-* headers (https)', function(done) { - request(cors_anywhere) - .get('/https://example.com/echoheaders') - .set('test-include-xfwd', '') - .expect('Access-Control-Allow-Origin', '*') - .expectJSON({ - host: 'example.com', - 'x-forwarded-port': '443', - 'x-forwarded-proto': 'https', - }, done); - }); - - // Skipped because x-forwarded-proto == http - it.skip('X-Forwarded-* headers (https, non-standard port)', function(done) { - request(cors_anywhere) - .get('/https://example.com:1337/echoheaders') - .set('test-include-xfwd', '') - .expect('Access-Control-Allow-Origin', '*') - .expectJSON({ - host: 'example.com:1337', - 'x-forwarded-port': '1337', - 'x-forwarded-proto': 'https', - }, done); - }); - it('Ignore cookies', function(done) { request(cors_anywhere) .get('/example.com/setcookie') @@ -277,7 +251,53 @@ describe('Basic functionality', function() { }) .end(done); }); +}); +describe('server on https', function() { + var NODE_TLS_REJECT_UNAUTHORIZED; + before(function() { + cors_anywhere = createServer({ + httpsOptions: { + key: fs.readFileSync(path.join(__dirname, 'key.pem')), + cert: fs.readFileSync(path.join(__dirname, 'cert.pem')), + }, + }); + // Disable certificate validation in case the certificate expires. + NODE_TLS_REJECT_UNAUTHORIZED = process.env.NODE_TLS_REJECT_UNAUTHORIZED; + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; + }); + after(function(done) { + if (NODE_TLS_REJECT_UNAUTHORIZED === undefined) { + delete process.env.NODE_TLS_REJECT_UNAUTHORIZED; + } else { + process.env.NODE_TLS_REJECT_UNAUTHORIZED = NODE_TLS_REJECT_UNAUTHORIZED; + } + stopServer(done); + }); + + it('X-Forwarded-* headers (https)', function(done) { + request(cors_anywhere) + .get('/https://example.com/echoheaders') + .set('test-include-xfwd', '') + .expect('Access-Control-Allow-Origin', '*') + .expectJSON({ + host: 'example.com', + 'x-forwarded-port': '443', + 'x-forwarded-proto': 'https', + }, done); + }); + + it('X-Forwarded-* headers (https, non-standard port)', function(done) { + request(cors_anywhere) + .get('/https://example.com:1337/echoheaders') + .set('test-include-xfwd', '') + .expect('Access-Control-Allow-Origin', '*') + .expectJSON({ + host: 'example.com:1337', + 'x-forwarded-port': '1337', + 'x-forwarded-proto': 'https', + }, done); + }); }); describe('requireHeader', function() {