Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

spdy proxy hello world

  • Loading branch information...
commit c934be0bf85be235ae70ce66551541d3ce75dfd0 0 parents
Ilya Grigorik authored
Showing with 105 additions and 0 deletions.
  1. +2 −0  .gitignore
  2. +103 −0 server.js
2  .gitignore
@@ -0,0 +1,2 @@
+misc
+keys
103 server.js
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.