-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4d13156
Showing
9 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
*.swp |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/caronte'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var http = require('http'), | ||
https = require('https'), | ||
url = require('url'), | ||
caronte = require('./caronte'), | ||
events = require('eventemitter2'), | ||
proxy = exports; | ||
|
||
/** | ||
* Creates the proxy server. | ||
* | ||
* Examples: | ||
* | ||
* caronte.createProxyServer({ .. }, 8000) | ||
* // => '{ web: [Function], ws: [Function] ... }' | ||
* | ||
* @param {Object} Options Config object passed to the proxy | ||
* | ||
* @return {Object} Proxy Proxy object with handlers for `ws` and `web` requests | ||
* | ||
* @api public | ||
*/ | ||
|
||
proxy.createProxyServer = function createProxyServer(options) { | ||
if(!options) { | ||
throw new Error([ | ||
"`options` is needed and it must have the following layout:", | ||
" ", | ||
" { ", | ||
" target : <url string to be parsed with the url module> ", | ||
" forward: <url string to be parsed with the url module> ", | ||
" ssl : <object to be passed to https.createServer()> ", | ||
" ws : <true/false, if you want to proxy websockets> ", | ||
" xfwd : <true/false, adds x-forward headers> ", | ||
" } ", | ||
" ", | ||
"NOTE: `options.ws` and `options.ssl` are optional ", | ||
" either one or both `options.target` and ", | ||
" `options.forward` must exist " | ||
].join("\n")); | ||
} | ||
|
||
['target', 'forward'].forEach(function(key) { | ||
options[key] = url.parse(options[key]); | ||
}); | ||
|
||
return { | ||
__proto__: new events.EventEmitter2({ wildcard: true, delimiter: ':' }), | ||
web : caronte.createWebProxy(options), | ||
ws : caronte.createWsProxy(options), | ||
listen : function listen(port) { | ||
var server = options.ssl ? http.createServer(this.web) : https.createServer(options.ssl, this.web); | ||
|
||
if(options.ws) { | ||
server.on('upgrade', this.ws); | ||
} | ||
|
||
return server; | ||
} | ||
}; | ||
}; | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var caronte = exports; | ||
|
||
caronte.createWebProxy = require('./web'); | ||
caronte.createWsProxy = require('./ws'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function ForwardStream() { | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function ProxyStream() { | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var passes = require('./web/'); | ||
|
||
module.exports = createWebProxy; | ||
|
||
function createWebProxy(options) { | ||
passes = Object.keys(passes).map(function(pass) { | ||
return passes[pass]; | ||
}); | ||
|
||
return function(req, res) { | ||
var self = this; | ||
|
||
self.emit('caronte:web:begin', req, res); | ||
|
||
passes.forEach(function(pass) { | ||
var event = 'caronte:web:' + pass.name.toLowerCase(); | ||
|
||
self.emit(event + ':begin', req, res); | ||
pass(req, res, options); | ||
self.emit(event + ':end'); | ||
}); | ||
|
||
self.emit('caronte:web:end'); | ||
}; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var ForwardStream = require('../streams/forward'), | ||
ProxyStream = require('../streams/proxy'), | ||
passes = exports; | ||
|
||
/*! | ||
* List of passes. | ||
* | ||
* A `pass` is just a function that is executed on `req, res, options` | ||
* so that you can easily add new checks while still keeping the base | ||
* flexible. | ||
* | ||
*/ | ||
|
||
passes.XHeaders = XHeaders; | ||
passes.deleteLength = deleteLength; | ||
passes.timeout = timeout; | ||
passes.stream = stream; | ||
|
||
function deleteLength(req, res, options) { | ||
if(req.method === 'DELETE' && !req.headers['content-length']) { | ||
req.headers['content-length'] = '0'; | ||
} | ||
} | ||
|
||
function timeout(req, res, options) { | ||
if(options.timeout) { | ||
req.socket.setTimeout(options.timeout); | ||
} | ||
} | ||
|
||
function XHeaders(req, res, options) { | ||
var values = { | ||
for : req.connection.remoteAddress || req.socket.remoteAddress, | ||
port : req.connection.remotePort || req.socket.remotePort, | ||
proto: req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http') | ||
}; | ||
|
||
['for', 'port', 'proto'].forEach(function(header) { | ||
req.headers['x-forwarded-' + header] = | ||
(req.headers['x-forwarded-' + header] || '') + | ||
(req.headers['x-forwarded-' + header] ? ',' : '') + | ||
values[header] | ||
}); | ||
} | ||
|
||
function stream(req, res, options) { | ||
if(options.forward) { | ||
req.pipe(new ForwardStream(options.forward)); | ||
} | ||
|
||
if(options.target) { | ||
return req.pipe(new ProxyStream(res, options)).pipe(res); | ||
} | ||
|
||
res.end(); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name" : "caronte", | ||
"version" : "0.0.0", | ||
"description" : "HTTP proxying for the masses", | ||
"author" : "yawnt <yawn.localhost@gmail.com>", | ||
|
||
"main" : "index.js", | ||
|
||
"dependencies" : { | ||
"eventemitter2": "*" | ||
}, | ||
"devDependencies": { | ||
"mocha" : "*", | ||
"expect.js": "*", | ||
"dox" : "*" | ||
}, | ||
"scripts" : { | ||
"test" : "mocha -t 20000 -R spec -r expect test/*-test.js" | ||
}, | ||
|
||
"license" : "MIT" | ||
} |