mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[dist] first
This commit is contained in:
commit
4d13156721
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
*.swp
|
||||
61
lib/caronte.js
Normal file
61
lib/caronte.js
Normal file
@ -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;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
4
lib/caronte/index.js
Normal file
4
lib/caronte/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
var caronte = exports;
|
||||
|
||||
caronte.createWebProxy = require('./web');
|
||||
caronte.createWsProxy = require('./ws');
|
||||
3
lib/caronte/streams/forward.js
Normal file
3
lib/caronte/streams/forward.js
Normal file
@ -0,0 +1,3 @@
|
||||
function ForwardStream() {
|
||||
|
||||
}
|
||||
3
lib/caronte/streams/proxy.js
Normal file
3
lib/caronte/streams/proxy.js
Normal file
@ -0,0 +1,3 @@
|
||||
function ProxyStream() {
|
||||
|
||||
}
|
||||
25
lib/caronte/web.js
Normal file
25
lib/caronte/web.js
Normal file
@ -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');
|
||||
};
|
||||
};
|
||||
56
lib/caronte/web/index.js
Normal file
56
lib/caronte/web/index.js
Normal file
@ -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();
|
||||
}
|
||||
22
package.json
Normal file
22
package.json
Normal file
@ -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"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user