Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Sep 13, 2013
1 parent a74cd85 commit e45bfd6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,5 +1,5 @@
node_modules
*.swp
cov
ttest.js
!ttest.js
notes
24 changes: 12 additions & 12 deletions lib/caronte/passes/ws.js
Expand Up @@ -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.
*/
Expand All @@ -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;
}
},

Expand All @@ -37,23 +37,23 @@ 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);
},

/**
* Sets `x-forwarded-*` headers if specified in config.
*
*/

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'
};
Expand All @@ -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);
}

] // <--
Expand Down
22 changes: 17 additions & 5 deletions lib/caronte/streams/websocket.js
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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:'); }
Expand All @@ -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);
Expand Down
58 changes: 58 additions & 0 deletions ttest.js
@@ -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);*/
});
});

0 comments on commit e45bfd6

Please sign in to comment.