-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_spdy.js
97 lines (85 loc) · 2.57 KB
/
proxy_spdy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* SPDY Proxy by Patrizio Tufarolo
*/
var net = require('net'),
http = require('http'),
fs = require('fs'),
url = require('url');
function plainHandler(req,res,host,path,port) {
var options = {
'host':host,
'port':port,
'method': req.method,
'agent':req.agent,
'path':path,
'headers':req.headers
};
var proxyRequest = http.request(options,
function (proxyResponse) {
res.writeHead(proxyResponse.statusCode,proxyResponse.headers);
proxyResponse.pipe(res);
res.pipe(proxyResponse);
}
);
proxyRequest.on('error',function(error) { res.writeHead(200); res.write("<h1>500 error</h1><p>" + error + "</p>"); res.end(); });
req.pipe(proxyRequest);
res.on('close', function() {
proxyRequest.abort();
});
}
function spdyHandler(req,res,host,path,port) {
var options = {
host: host,
port: port,
};
var tunnel = net.createConnection(options, function() {
synReply(res, 200, 'Connection established',
{
'Connection': 'keep-alive',
'Proxy-Agent': 'PattPatel SPDY Proxy'
},
function() {
tunnel.pipe(socket);
socket.pipe(tunnel);
}
);
});
tunnel.setNoDelay(true);
tunnel.on('error', function(e) {
console.log("SPDY Tunnel error: ".red + e);
synReply(socket, 500, "SPDY Tunnel Error", {}, function() {
socket.end();
});
});
}
function synReply(socket, code, reason, headers, callback) {
try {
if(socket._lock){
socket._lock(function() {
var socket = this;
this._spdyState.framer.replyFrame(this._spdyState.id, code, reason, headers,
function (err, frame) {
socket.connection.write(frame);
socket._unlock();
callback.call();
}
);
});
} else {
var statusLine = 'HTTP/1.1 ' + code + ' ' + reason + '\r\n';
var headerLines = '';
for(head in headers){
headerLines += head + ': ' + headers[head] + '\r\n';
}
socket.write(statusLine + headerLines + '\r\n', 'UTF-8', callback);
}
} catch(error) {
callback.call();
}
}
function requestHandler(req,res,host,port) {
var path = req.headers.path || url.parse(req.url).path;
req.method == 'CONNECT' ? spdyHandler(req, res, host, port) : plainHandler(req, res, host, path, port);
}
// Functions which will be available to external callers
exports.requestHandler = requestHandler;