From e6401ace5160427eb5762b129d1a074bf027e1d6 Mon Sep 17 00:00:00 2001 From: Olly Smith Date: Mon, 1 Oct 2012 19:10:40 +0100 Subject: [PATCH] Working HTTPS proxy. --- index.js | 13 +++++++++++++ test/fixtures/billy.crt | 21 +++++++++++++++++++++ test/fixtures/billy.key | 27 +++++++++++++++++++++++++++ test/test.js | 32 +++++++++++++++++++++++++++----- 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/billy.crt create mode 100644 test/fixtures/billy.key diff --git a/index.js b/index.js index a3d3d2e..d74916e 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ var http = require('http'), + net = require('net'), + url = require('url'), request = require('request'); module.exports = function (callback) { @@ -15,6 +17,17 @@ module.exports = function (callback) { req.pipe(proxy_req).pipe(res); }); + proxy.on('connect', function (req, socket, head) { + var parts = req.url.split(':'); + socket.pause(); + var proxy_sock = net.connect(parseInt(parts[1], 10), parts[0], function () { + socket.write( "HTTP/1.0 200 Connection established\r\nProxy-agent: Netscape-Proxy/1.1\r\n\r\n"); + proxy_sock.write(head); + socket.pipe(proxy_sock).pipe(socket); + socket.resume(); + }); + }); + proxy.listen(0); proxy.on('listening', function () { diff --git a/test/fixtures/billy.crt b/test/fixtures/billy.crt new file mode 100644 index 0000000..8d92a3f --- /dev/null +++ b/test/fixtures/billy.crt @@ -0,0 +1,21 @@ +-----BEGIN CERTIFICATE----- +MIIDejCCAmICCQDH1Rg2Riyt9jANBgkqhkiG9w0BAQUFADB/MQswCQYDVQQGEwJV +SzETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMRMwEQYDVQQDEwpPbGx5IFNtaXRoMSMwIQYJKoZIhvcNAQkBFhRv +bGx5LnNtaXRoQGdtYWlsLmNvbTAeFw0xMjEwMDExNzEyMzlaFw0xMzEwMDExNzEy +MzlaMH8xCzAJBgNVBAYTAlVLMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQK +ExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEzARBgNVBAMTCk9sbHkgU21pdGgx +IzAhBgkqhkiG9w0BCQEWFG9sbHkuc21pdGhAZ21haWwuY29tMIIBIjANBgkqhkiG +9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzvmtLDn1MXpL4Y8cUY/XfKlqE6GpfIcPdpIl +3DwJVNZvBYI0+xENWwKurLwyTe9FP1MUgFSHt+WOFicOUSIgulbFi9WLiUW2HfeS +eguh5nyXP8DZuGpIWOJckVLUxLLkO6eBp1kut+VgRSHKtfvZXJvugYVFCj5r22cT +qjxoshhid6S0LcDGwkSx7bYZJ6pHyvTedjZAEwfwmA1YYiDJPvlpp1WugUhqx88B +2tPtkQz37Qew7SOI72XUPjaANkiGt71uQHO3dD1IHAHxJvZEzTJ9KGrDAARDl2c9 +8Y7iu0QmqyIf9udFFH6+Dfcjo0CWoMjF501rD7hJFSJggXxw7QIDAQABMA0GCSqG +SIb3DQEBBQUAA4IBAQAdnxBexrqTgFOirw6NoFV4vOMreQ4XbNJHNDnS21OZ4vVL +yLBtnOd7ghrAZuQgkMwlFwpujU6FsE6GUbYH7sYsKB0ii25WoilIlufAR4GBR+Sm +S6BUCCgboKag+ADilqxs9W+hYRZcyJbifJuh28Z+gcwlsUoVJj/078zlPlcUsL0u +IzGoEjc4RS6UA1dXscCXJgtwip+sacCu7NmXJoIlX0fEeDcgFDwGr61ITzRDlSK5 +B1BabRUjenapjP6sihD5/TH7pUl7ojbTo60s1MKGx5sHTYBTdhvU9lh2BqB3DKHQ +LuQR9ZwXwacQuyjxbcofLUrwbn0HJkRUUTLM+uV0 +-----END CERTIFICATE----- diff --git a/test/fixtures/billy.key b/test/fixtures/billy.key new file mode 100644 index 0000000..8d4b526 --- /dev/null +++ b/test/fixtures/billy.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAzvmtLDn1MXpL4Y8c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MRoTj4 +CHEJM5ErWDgGiNv/AU83Jr9e+0t0hl+yW+bP6Gfucd5xXBvA3XrbYuP8MntlyZZD +UC6FK/seZ5ieF1Q7HdAvuW2D9yWDAoFyIE5OmAa0ju3ftHAE6TvT2CNt+eZNlyuq +64ithANzX8/wNpSkUgkuUwKBgQDPUJtSySbTnlz/n3sHIazlpyhvF8aTcNmbKVxX +/IkV5BWBCulHtbCJBwmbwTbWYnY7TF2g3a18xHydfN4uwF33cuGUti50ggdcXEl8 +Xo7lL0ORzbPcX1Ng0aR2SpOMqYElGdGDXFYmqALLkGhDlcjg5TbGNZXUnRh8BYxB +CZ2RpwKBgQDkg1yh3HL6Jo9ywDfXoqtWQZg8qpKEy3QlspUalcYvTHRMZ6lm/dqt +n+HGNXyb3pF2no/pSpFPvdR9xdwpgaPtwmDQ/5b0z01MgNjHBFd/kLKx1d6USVFA +CAoJrsIadfOSGs0rJ4418p3b1ou9nRpu4MJA8xXsnk93dHXP57zzFw== +-----END RSA PRIVATE KEY----- diff --git a/test/test.js b/test/test.js index 3d4a06c..19089a8 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,7 @@ var assert = require('assert'), + fs = require('fs'), http = require('http'), + https = require('https'), request = require('request'), connect = require('connect'), async = require('async'), @@ -8,18 +10,27 @@ var assert = require('assert'), describe('puffing-billy', function () { beforeEach(function (done) { var self = this; + var app = connect(); + app.use(function (req, res) { + res.end(req.url); + }); async.parallel([ function (callback) { self.proxy = billy(callback); }, function (callback) { - var app = connect(); - app.use(function (req, res) { - res.end(req.url); - }); self.server = http.createServer(app); self.server.listen(0, '127.0.0.1'); self.server.on('listening', callback); + }, + function (callback) { + var options = { + key: fs.readFileSync('test/fixtures/billy.key'), + cert: fs.readFileSync('test/fixtures/billy.crt') + }; + self.ssl_server = https.createServer(options, app); + self.ssl_server.listen(0, '127.0.0.1'); + self.ssl_server.on('listening', callback); } ], done); }); @@ -41,7 +52,18 @@ describe('puffing-billy', function () { }); }); - it('should proxy HTTPS to upstream servers'); + it('should proxy HTTPS to upstream servers', function (done) { + request.get({ + url: 'https://localhost:' + this.ssl_server.address().port + '/foobarbill', + proxy: 'http://localhost:' + this.proxy.address().port + }, function (error, response, body) { + assert.equal(error, null); + assert.equal(response.statusCode, 200); + assert.equal(body, '/foobarbill'); + done(); + }); + }); + it('should mock HTTP responses'); it('should mock HTTPS responses'); it('should cache upstream requests that are cacheable');