Skip to content

Commit

Permalink
Fixed a nasty bug with HTTPS, added chaingang to cert gen and bumped …
Browse files Browse the repository at this point in the history
…version
  • Loading branch information
mdp committed Jul 14, 2011
1 parent 3c9da25 commit 96c49d6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
8 changes: 3 additions & 5 deletions lib/http_proxy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions lib/https_proxy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -13,6 +13,6 @@
}
});
process.on('uncaughtException', function(err) {
return console.log(err["message"]);
return console.log(err);
});
}).call(this);
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "middlefiddle",
"version": "0.1.0",
"version": "0.2.0",
"description": "Middleware as a proxy for HTTP/HTTPS traffic",
"keywords": ["proxy", "middleware", "connect"],
"repository": "git://github.com/mdp/middlefiddle.git",
Expand Down
5 changes: 2 additions & 3 deletions src/http_proxy.coffee
Expand Up @@ -43,7 +43,7 @@ exports.HttpProxy = class HttpProxy extends connect.HTTPServer

listenHTTPS: (port) ->
httpsProxy = require './https_proxy'
httpsProxy.createProxy(@bookendedMiddleware()).listen(port)
httpsProxy.createProxy(@middlewares).listen(port)

listen: (port) ->
super port
Expand All @@ -69,7 +69,6 @@ exports.HttpProxy = class HttpProxy extends connect.HTTPServer
res.end(data)
upstream_res.on 'close', ->
res.emit 'close'
res.destroy()
upstream_res.on 'error', ->
res.emit 'end'
res.abort()
Expand All @@ -80,7 +79,7 @@ exports.HttpProxy = class HttpProxy extends connect.HTTPServer
if req.ssl
upstream_request = https.request passed_opts, upstream_processor
else
upstream_request = http.request passed_opts, upstream_processor
upstream_request = http.request passed_opts, upstream_processor
upstream_request.on 'error', ->
console.log("Fail")
res.end()
Expand Down
36 changes: 28 additions & 8 deletions src/https_proxy.coffee
Expand Up @@ -3,13 +3,15 @@ STATES =
CONNECTING : 1,
CONNECTED : 2

net = require('net')
tls = require('tls')
http = require('http')
net = require('net')
tls = require('tls')
http = require('http')
HttpProxy = require('./http_proxy').HttpProxy
fs = require('fs')
path = require('path')
spawn = require('child_process').spawn
fs = require('fs')
path = require('path')
spawn = require('child_process').spawn
chainGang = require('chain-gang')
chain = chainGang.create({workers: 4})

exports.createProxy = (middlewares) ->
proxy = new HttpsProxy(middlewares)
Expand All @@ -27,7 +29,7 @@ exports.HttpsProxy = class HttpsProxy extends HttpProxy
match = headers.match("CONNECT +([^:]+):([0-9]+).*")
host = match[1]
port = match[2]
generateCerts host, (tlsContext) =>
queueGenerateCerts host, (tlsContext) =>
pair = tls.createSecurePair(tlsContext, true, false, false)
httpServer = new http.Server
httpServer.addListener 'request', @handle
Expand Down Expand Up @@ -60,14 +62,15 @@ exports.HttpsProxy = class HttpsProxy extends HttpProxy
tlsServer.listen(port)

generateCerts = (host, callback) ->
# TODO: Make async
currentCerts = getCerts(host)
if currentCerts
callback(currentCerts)
else
console.log("Generating certs for #{host}")
prc = spawn "#{__dirname}/bin/certgen.sh", [host]
prc.on 'exit', (code, err) ->
if code == 0
console.log("Generated new certs for #{host}")
callback getCerts(host)
else
console.log(err)
Expand All @@ -86,6 +89,23 @@ getCerts = (host) ->
else
return false

queueGenerateCerts = (host, tlsCallback) ->
# Using Chaingang to prevent the forked
# bash script from creating the same cert at the same time
# Hacky, but it works
# TODO: Gen and sign certs using native Node Openssl hooks
if tlsSettings = getCerts(host)
tlsCallback(tlsSettings)
else
console.log("Queuing up cert gen")
callback = (err)->
tlsCallback(getCerts(host))
job = (host)->
(worker) ->
generateCerts host, ->
worker.finish()
chain.add job(host), host, callback

pipe = (pair, socket) ->
pair.encrypted.pipe(socket)
socket.pipe(pair.encrypted)
Expand Down
2 changes: 1 addition & 1 deletion src/index.coffee
Expand Up @@ -12,5 +12,5 @@ fs.readdirSync(__dirname + '/middleware').forEach (filename) ->
# HTTPS DNS lookup errors throw an exception which
# it difficult to catch
process.on 'uncaughtException', (err)->
console.log(err["message"])
console.log(err)

0 comments on commit 96c49d6

Please sign in to comment.