This commit is contained in:
yawnt 2013-09-13 23:38:12 +02:00
parent a74cd85c8a
commit e45bfd66a2
4 changed files with 88 additions and 18 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
node_modules
*.swp
cov
ttest.js
!ttest.js
notes

View File

@ -4,7 +4,7 @@ var WebsocketStream = require('../streams/websocket'),
/*!
* Array of passes.
*
* A `pass` is just a function that is executed on `req, res, options`
* A `pass` is just a function that is executed on `req, socket, options`
* so that you can easily add new checks while still keeping the base
* flexible.
*/
@ -22,13 +22,13 @@ var passes = exports;
* the `upgrade:websocket` header
*/
function checkMethodAndHeader (req, res, options) {
function checkMethodAndHeader (req, socket) {
if (req.method !== 'GET' || !req.headers.upgrade) {
req.end(); return true;
socket.destroy(); return true;
}
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
res.destroy(); return true;
socket.destroy(); return true;
}
},
@ -37,11 +37,11 @@ function checkMethodAndHeader (req, res, options) {
*
*/
function setupSocket(req, res) {
res.setTimeout(0);
res.setNoDelay(true);
function setupSocket(req, socket) {
socket.setTimeout(0);
socket.setNoDelay(true);
res.setKeepAlive(true, 0);
socket.setKeepAlive(true, 0);
},
/**
@ -49,11 +49,11 @@ function setupSocket(req, res) {
*
*/
function XHeaders(req, res, options) {
function XHeaders(req, socket, options) {
if(!options.xfwd) return;
var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
for : req.connection.remoteAddsockets || req.socket.remoteAddsockets,
port : req.connection.remotePort || req.socket.remotePort,
proto: req.connection.pair ? 'wss' : 'ws'
};
@ -70,8 +70,8 @@ function XHeaders(req, res, options) {
*
*
*/
function stream(req, res, options, head) {
req.pipe(new WebsocketStream(options, head)).pipe(res);
function stream(req, socket, options, head) {
req.pipe(new WebsocketStream(options, head)).pipe(socket);
}
] // <--

View File

@ -47,10 +47,11 @@ WebsocketStream.prototype.onFinish = function() {
WebsocketStream.prototype.onSocket = function(proxySocket) {
};
WebsocketStream.prototype.onUpgrade = function(proxyRes, proxySocket, proxyHead) {
var self = this;
this.handshake = {
headers : proxyRes.headers,
statusCode : proxyRes.statusCode
@ -63,9 +64,17 @@ WebsocketStream.prototype.onUpgrade = function(proxyRes, proxySocket, proxyHead)
proxySocket.setTimeout(0);
proxySocket.setNoDelay(true);
proxySocket.setKeepAlive(true, 0);
proxySocket.setKeepAlive(true, 0);
proxySocket.on('readable', function() {
self.read(0);
});
proxySocket.on('end', function() {
self.push(null);
});
self.emit('readable');
};
WebsocketStream.prototype.onError = function(e) {
@ -98,8 +107,8 @@ WebsocketStream.prototype._read = function(size) {
* Socket.IO specific code
*/
var sdata = chunk.toString();
sdata = sdata.substr(0, sdata.search(CRLF + CRLF));
/*var sdata = chunk.toString();
sdata = sdata.substr(0, sdata.search('\r\n\r\n'));
chunk = data.slice(Buffer.byteLength(sdata), data.length);
if (self.source.https && !self.target.https) { sdata = sdata.replace('ws:', 'wss:'); }
@ -108,7 +117,10 @@ WebsocketStream.prototype._read = function(size) {
this.push(data);
this.handshakeDone = true;
return;
return;
*/
this.push(headers);
this.push(chunk);
}
this.push(chunk);

58
ttest.js Normal file
View File

@ -0,0 +1,58 @@
var caronte = require('./'),
http = require('http'),
ws = require('ws');
var proxyTo = new ws.Server({ port: 9090 });
proxyTo.on('connection', function(ws) {
console.log('connection!');
ws.on('message', function(msg) {
console.log('received: ' + msg);
});
ws.send('derpity?');
});
/*caronte.createProxyServer({
ws : true,
target: 'http://127.0.0.1:9090'
}).listen(8000);*/
var client = new ws('ws://127.0.0.1:8000');
client.on('open', function() {
client.send('baaaka');
console.log('sent: baaaaka');
});
var srv = http.createServer(function(req, res) {
res.end('1');
}).listen(8000);
srv.on('upgrade', function(req, socket, head) {
var options = {
port: 9090,
hostname: '127.0.0.1',
headers: req.headers
}
var req = http.request(options);
req.end();
socket.on('data', function(d) {
console.log('yoo');
console.log(d);
});
var s;
req.on('socket', function(ss) {
s = ss;
});
req.on('upgrade', function(res, sock, hd) {
/*console.log(hd.toString('utf-8'));
var str = Object.keys(res.headers).map(function(i) {
return i + ": " + res.headers[i];
}).join('\r\n');
socket.write("HTTP/1.1 101 Switching Protocols\r\n" + str);
socket.write(hd);
socket.pipe(sock).pipe(socket);*/
});
});