forked from igrigorik/node-spdyproxy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c934be0
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
misc | ||
keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
var spdy = require('spdy'), | ||
http = require('http'), | ||
url = require('url'), | ||
net = require('net'), | ||
fs = require('fs'); | ||
|
||
process.on('uncaughtException', function(e) { | ||
console.error('Error: ' + e); | ||
}); | ||
|
||
function logRequest(req) { | ||
console.log(req.method + ' ' + req.url); | ||
for (var i in req.headers) | ||
console.log(' * ' + i + ': ' + req.headers[i]); | ||
} | ||
|
||
function handlePlain(req, res) { | ||
logRequest(req); | ||
|
||
var requestOptions = { | ||
host: req.headers.host, | ||
port: req.headers.host.split(':')[1] || 80, | ||
path: req.url, | ||
method: req.method, | ||
// headers: req.headers | ||
}; | ||
|
||
// console.log(req.url) | ||
// console.log(uri) | ||
console.log(requestOptions); | ||
|
||
var rreq = http.request(requestOptions, function(rres) { | ||
delete rres.headers['transfer-encoding']; | ||
rres.headers['x-spdy-proxy'] = 'v1.0.0'; | ||
|
||
console.log(rres.headers) | ||
|
||
res.writeHead(rres.statusCode, '', rres.headers); | ||
rres.pipe(res); | ||
}); | ||
|
||
rreq.setNoDelay(true); | ||
|
||
rreq.on('error', function(e) { | ||
console.log("Client error: " + e.message); | ||
res.writeHead(502, 'Proxy fetch failed'); | ||
res.end(); | ||
}); | ||
|
||
req.pipe(rreq); | ||
} | ||
|
||
function handleSecure(req, socket) { | ||
logRequest(req); | ||
|
||
var dest = req.headers.host.split(':'); | ||
console.log(dest) | ||
var tunnel = net.createConnection(dest[1] || 443, dest[0], function() { | ||
socket.lock(function() { | ||
var socket = this; | ||
|
||
this.framer.replyFrame( | ||
this.id, 200, "Connection established", {"Connection": "keep-alive"}, | ||
function (err, frame) { | ||
socket.connection.write(frame); | ||
socket.unlock(); | ||
|
||
tunnel.pipe(socket); | ||
socket.pipe(tunnel); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
tunnel.setNoDelay(true); | ||
|
||
tunnel.on('error', function(e) { | ||
console.log("Tunnel error: " + e); | ||
socket.lock(function() { | ||
this.framer.replyFrame( | ||
this.id, 502, "Tunnel Error", {}, | ||
function (err, frame) { | ||
socket.connection.write(frame); | ||
socket.unlock(); | ||
socket.end(); | ||
} | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
var serverOptions = { | ||
key: fs.readFileSync(__dirname + '/keys/mykey.pem'), | ||
cert: fs.readFileSync(__dirname + '/keys/mycert.pem'), | ||
ca: fs.readFileSync(__dirname + '/keys/mycsr.pem') | ||
}; | ||
|
||
var server = spdy.createServer(serverOptions); | ||
|
||
server.on("request", handlePlain); | ||
server.on("connect", handleSecure); | ||
|
||
server.listen(44300); |