Skip to content

Commit e45bfd6

Browse files
committed
stuff
1 parent a74cd85 commit e45bfd6

File tree

4 files changed

+88
-18
lines changed

4 files changed

+88
-18
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node_modules
22
*.swp
33
cov
4-
ttest.js
4+
!ttest.js
55
notes

lib/caronte/passes/ws.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var WebsocketStream = require('../streams/websocket'),
44
/*!
55
* Array of passes.
66
*
7-
* A `pass` is just a function that is executed on `req, res, options`
7+
* A `pass` is just a function that is executed on `req, socket, options`
88
* so that you can easily add new checks while still keeping the base
99
* flexible.
1010
*/
@@ -22,13 +22,13 @@ var passes = exports;
2222
* the `upgrade:websocket` header
2323
*/
2424

25-
function checkMethodAndHeader (req, res, options) {
25+
function checkMethodAndHeader (req, socket) {
2626
if (req.method !== 'GET' || !req.headers.upgrade) {
27-
req.end(); return true;
27+
socket.destroy(); return true;
2828
}
2929

3030
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
31-
res.destroy(); return true;
31+
socket.destroy(); return true;
3232
}
3333
},
3434

@@ -37,23 +37,23 @@ function checkMethodAndHeader (req, res, options) {
3737
*
3838
*/
3939

40-
function setupSocket(req, res) {
41-
res.setTimeout(0);
42-
res.setNoDelay(true);
40+
function setupSocket(req, socket) {
41+
socket.setTimeout(0);
42+
socket.setNoDelay(true);
4343

44-
res.setKeepAlive(true, 0);
44+
socket.setKeepAlive(true, 0);
4545
},
4646

4747
/**
4848
* Sets `x-forwarded-*` headers if specified in config.
4949
*
5050
*/
5151

52-
function XHeaders(req, res, options) {
52+
function XHeaders(req, socket, options) {
5353
if(!options.xfwd) return;
5454

5555
var values = {
56-
for : req.connection.remoteAddress || req.socket.remoteAddress,
56+
for : req.connection.remoteAddsockets || req.socket.remoteAddsockets,
5757
port : req.connection.remotePort || req.socket.remotePort,
5858
proto: req.connection.pair ? 'wss' : 'ws'
5959
};
@@ -70,8 +70,8 @@ function XHeaders(req, res, options) {
7070
*
7171
*
7272
*/
73-
function stream(req, res, options, head) {
74-
req.pipe(new WebsocketStream(options, head)).pipe(res);
73+
function stream(req, socket, options, head) {
74+
req.pipe(new WebsocketStream(options, head)).pipe(socket);
7575
}
7676

7777
] // <--

lib/caronte/streams/websocket.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ WebsocketStream.prototype.onFinish = function() {
4747

4848
WebsocketStream.prototype.onSocket = function(proxySocket) {
4949

50-
5150
};
5251

5352
WebsocketStream.prototype.onUpgrade = function(proxyRes, proxySocket, proxyHead) {
53+
var self = this;
54+
5455
this.handshake = {
5556
headers : proxyRes.headers,
5657
statusCode : proxyRes.statusCode
@@ -63,9 +64,17 @@ WebsocketStream.prototype.onUpgrade = function(proxyRes, proxySocket, proxyHead)
6364
proxySocket.setTimeout(0);
6465
proxySocket.setNoDelay(true);
6566

66-
proxySocket.setKeepAlive(true, 0);
67+
proxySocket.setKeepAlive(true, 0);
68+
69+
proxySocket.on('readable', function() {
70+
self.read(0);
71+
});
6772

73+
proxySocket.on('end', function() {
74+
self.push(null);
75+
});
6876

77+
self.emit('readable');
6978
};
7079

7180
WebsocketStream.prototype.onError = function(e) {
@@ -98,8 +107,8 @@ WebsocketStream.prototype._read = function(size) {
98107
* Socket.IO specific code
99108
*/
100109

101-
var sdata = chunk.toString();
102-
sdata = sdata.substr(0, sdata.search(CRLF + CRLF));
110+
/*var sdata = chunk.toString();
111+
sdata = sdata.substr(0, sdata.search('\r\n\r\n'));
103112
chunk = data.slice(Buffer.byteLength(sdata), data.length);
104113
105114
if (self.source.https && !self.target.https) { sdata = sdata.replace('ws:', 'wss:'); }
@@ -108,7 +117,10 @@ WebsocketStream.prototype._read = function(size) {
108117
this.push(data);
109118
110119
this.handshakeDone = true;
111-
return;
120+
return;
121+
*/
122+
this.push(headers);
123+
this.push(chunk);
112124
}
113125

114126
this.push(chunk);

ttest.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var caronte = require('./'),
2+
http = require('http'),
3+
ws = require('ws');
4+
5+
var proxyTo = new ws.Server({ port: 9090 });
6+
7+
proxyTo.on('connection', function(ws) {
8+
console.log('connection!');
9+
ws.on('message', function(msg) {
10+
console.log('received: ' + msg);
11+
});
12+
ws.send('derpity?');
13+
});
14+
15+
/*caronte.createProxyServer({
16+
ws : true,
17+
target: 'http://127.0.0.1:9090'
18+
}).listen(8000);*/
19+
20+
21+
var client = new ws('ws://127.0.0.1:8000');
22+
client.on('open', function() {
23+
client.send('baaaka');
24+
console.log('sent: baaaaka');
25+
});
26+
27+
28+
var srv = http.createServer(function(req, res) {
29+
res.end('1');
30+
}).listen(8000);
31+
32+
srv.on('upgrade', function(req, socket, head) {
33+
var options = {
34+
port: 9090,
35+
hostname: '127.0.0.1',
36+
headers: req.headers
37+
}
38+
var req = http.request(options);
39+
req.end();
40+
socket.on('data', function(d) {
41+
console.log('yoo');
42+
console.log(d);
43+
});
44+
var s;
45+
req.on('socket', function(ss) {
46+
s = ss;
47+
});
48+
req.on('upgrade', function(res, sock, hd) {
49+
/*console.log(hd.toString('utf-8'));
50+
var str = Object.keys(res.headers).map(function(i) {
51+
return i + ": " + res.headers[i];
52+
}).join('\r\n');
53+
socket.write("HTTP/1.1 101 Switching Protocols\r\n" + str);
54+
55+
socket.write(hd);
56+
socket.pipe(sock).pipe(socket);*/
57+
});
58+
});

0 commit comments

Comments
 (0)