Created
January 13, 2012 10:12
-
-
Save spolu/1605400 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/node | |
var http = require('http'); | |
var fwk = require('fwk'); | |
var infra = require('../lib/infra.js'); | |
/* | |
* bouncer.js | |
* | |
* Bouncer is a simple http proxy protected by api key access | |
* Since the path should not be tempererd, the api key should | |
* be passed through the x-teleportd-accesskey header | |
* | |
* Access Info: | |
* ----------- | |
* service_name: 'bouncer' | |
*/ | |
http.createServer(function(req, res) { | |
infra.access('bouncer').verify(req, res, function(err) { | |
if(err) { | |
res.writeHead(500); | |
res.end('problem with access: ' + (err.stack || err)); | |
} | |
else { | |
delete req.headers['x-teleportd-accesskey']; | |
var options = { host: req.headers['host'], | |
port: 80, | |
path: req.url, | |
method: req.method, | |
headers: req.headers }; | |
var preq = http.request(options, function(pres) { | |
res.writeHead(pres.statusCode, pres.headers); | |
pres.on('data', function(chunk) { | |
res.write(chunk); | |
}); | |
pres.on('end', function() { | |
res.end(); | |
}); | |
}); | |
req.on('data', function(chunk) { | |
preq.write(chunk); | |
}); | |
req.on('end', function() { | |
preq.end(); | |
}); | |
preq.on('error', function(e) { | |
res.writeHead(500); | |
res.end('problem with request: ' + req.url + '\n' + (e.stack || e)); | |
}); | |
} | |
}); | |
}).listen(8080); | |
console.log('Listening on 8080'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment