mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 13:35:53 +00:00
use bouncy to proxy requests
- simplifies our codebase a lot! - queued requests are currently broken
This commit is contained in:
128
proxy.js
128
proxy.js
@@ -1,17 +1,9 @@
|
||||
var http = require('http');
|
||||
var net = require('net');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var log = require('bookrc');
|
||||
var debug = require('debug')('localtunnel-server');
|
||||
|
||||
// here be dragons, understanding of node http internals will be required
|
||||
var HTTPParser = process.binding('http_parser').HTTPParser;
|
||||
|
||||
// available parsers for requests
|
||||
// this is borrowed from how node does things by preallocating parsers
|
||||
var parsers = http.parsers;
|
||||
|
||||
var Proxy = function(opt, cb) {
|
||||
if (!(this instanceof Proxy)) {
|
||||
return new Proxy(opt, cb);
|
||||
@@ -60,8 +52,7 @@ var Proxy = function(opt, cb) {
|
||||
|
||||
// clear waiting by ending responses, (requests?)
|
||||
self.waiting.forEach(function(waiting) {
|
||||
waiting[1].end();
|
||||
waiting[3].end(); // write stream
|
||||
waiting(null);
|
||||
});
|
||||
|
||||
self.emit('end');
|
||||
@@ -80,13 +71,6 @@ var Proxy = function(opt, cb) {
|
||||
// a single connection is enough to keep client id slot open
|
||||
clearTimeout(conn_timeout);
|
||||
|
||||
// allocate a response parser for the socket
|
||||
// it only needs one since it will reuse it
|
||||
socket.parser = parsers.alloc();
|
||||
|
||||
socket._orig_ondata = socket.ondata;
|
||||
socket.ondata = upstream_response;
|
||||
|
||||
socket.once('close', function(had_error) {
|
||||
debug('client %s closed socket (error: %s)', id, had_error);
|
||||
|
||||
@@ -100,7 +84,6 @@ var Proxy = function(opt, cb) {
|
||||
}
|
||||
|
||||
// need to track total sockets, not just active available
|
||||
|
||||
debug('remaining client sockets: %s', self.sockets.length);
|
||||
|
||||
// no more sockets for this ident
|
||||
@@ -118,10 +101,10 @@ var Proxy = function(opt, cb) {
|
||||
|
||||
self.sockets.push(socket);
|
||||
|
||||
var next = self.waiting.shift();
|
||||
if (next) {
|
||||
var wait_cb = self.waiting.shift();
|
||||
if (wait_cb) {
|
||||
debug('handling queued request');
|
||||
self.proxy_request(next[0], next[1], next[2], next[3]);
|
||||
self.next_socket(wait_cb);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -132,104 +115,39 @@ var Proxy = function(opt, cb) {
|
||||
|
||||
Proxy.prototype.__proto__ = EventEmitter.prototype;
|
||||
|
||||
Proxy.prototype.proxy_request = function(req, res, rs, ws) {
|
||||
Proxy.prototype.next_socket = function(cb) {
|
||||
var self = this;
|
||||
|
||||
// socket is a tcp connection back to the user hosting the site
|
||||
var sock = self.sockets.shift();
|
||||
|
||||
// TODO how to handle queue?
|
||||
// queue request
|
||||
if (!sock) {
|
||||
debug('no more clients, queued: %s', req.url);
|
||||
rs.pause();
|
||||
self.waiting.push([req, res, rs, ws]);
|
||||
return;
|
||||
debug('no more client, queue callback');
|
||||
return self.waiting.push(cb);
|
||||
}
|
||||
|
||||
debug('handle req: %s', req.url);
|
||||
// put the socket back
|
||||
function done() {
|
||||
if (!sock.destroyed) {
|
||||
debug('retuning socket');
|
||||
self.sockets.push(sock);
|
||||
}
|
||||
|
||||
// pipe incoming request into tcp socket
|
||||
// incoming request will close the socket when done
|
||||
// lt client should establish a new socket once request is finished
|
||||
// we do this instead of keeping socket open to make things easier
|
||||
rs.pipe(sock);
|
||||
|
||||
sock.ws = ws;
|
||||
sock.req = req;
|
||||
|
||||
// since tcp connection to upstream are kept open
|
||||
// invoke parsing so we know when the response is complete
|
||||
var parser = sock.parser;
|
||||
parser.reinitialize(HTTPParser.RESPONSE);
|
||||
parser.socket = sock;
|
||||
|
||||
// we have completed a response
|
||||
// the tcp socket is free again
|
||||
parser.onIncoming = function (res) {
|
||||
parser.onMessageComplete = function() {
|
||||
debug('ended response: %s', req.url);
|
||||
|
||||
// any request we had going on is now done
|
||||
ws.end();
|
||||
sock.end();
|
||||
|
||||
// no more forwarding
|
||||
delete sock.ws;
|
||||
delete sock.req;
|
||||
delete parser.onIncoming;
|
||||
};
|
||||
var wait = self.waiting.shift();
|
||||
debug('processing queued cb');
|
||||
if (wait) {
|
||||
return self.next_socket(cb);
|
||||
}
|
||||
};
|
||||
|
||||
rs.resume();
|
||||
debug('processing request');
|
||||
cb(sock, done);
|
||||
};
|
||||
|
||||
Proxy.prototype.proxy_upgrade = function(req, socket, head) {
|
||||
|
||||
var sock = self.sockets.shift();
|
||||
if (!sock) {
|
||||
// no available sockets to upgrade to
|
||||
// TODO queue?
|
||||
return socket.end();
|
||||
}
|
||||
|
||||
var stream = req.createRawStream();
|
||||
|
||||
sock.ws = ws;
|
||||
sock.upgraded = true;
|
||||
|
||||
stream.once('end', function() {
|
||||
delete sock.ws;
|
||||
|
||||
// when this ends, we just reset the socket to the lt client
|
||||
// this is easier than trying to figure anything else out
|
||||
sock.end();
|
||||
});
|
||||
|
||||
stream.pipe(sock);
|
||||
sock.once('end', socket.end.bind(ws));
|
||||
Proxy.prototype._done = function() {
|
||||
var self = this;
|
||||
};
|
||||
|
||||
function upstream_response(d, start, end) {
|
||||
var socket = this;
|
||||
|
||||
var ws = socket.ws;
|
||||
if (!ws) {
|
||||
return log.warn('no stream set for req:', socket.req.url);
|
||||
}
|
||||
|
||||
ws.write(d.slice(start, end));
|
||||
|
||||
if (socket.upgraded) {
|
||||
return;
|
||||
}
|
||||
|
||||
var ret = socket.parser.execute(d, start, end - start);
|
||||
if (ret instanceof Error) {
|
||||
log.error(ret);
|
||||
parsers.free(parser);
|
||||
socket.destroy(ret);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Proxy;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user