Created
August 10, 2012 06:33
-
-
Save linhuiw/3311823 to your computer and use it in GitHub Desktop.
http
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
var http = require('http'); | |
var net = require('net'); | |
var url = require('url'); | |
// Create an HTTP tunneling proxy | |
var proxy = http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('okay'); | |
}); | |
proxy.on('connect', function(req, cltSocket, head) { | |
// connect to an origin server | |
var srvUrl = url.parse('http://' + req.url); | |
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() { | |
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + | |
'Proxy-agent: Node-Proxy\r\n' + | |
'\r\n'); | |
srvSocket.write(head); | |
srvSocket.pipe(cltSocket); | |
cltSocket.pipe(srvSocket); | |
}); | |
}); | |
// now that proxy is running | |
proxy.listen(1337, '127.0.0.1', function() { | |
// make a request to a tunneling proxy | |
var options = { | |
port: 1337, | |
host: '127.0.0.1', | |
method: 'CONNECT', | |
path: 'http://nodejs.org/api/http.html' | |
}; | |
var req = http.request(options); | |
req.end(); | |
req.on('connect', function(res, socket, head) { | |
console.log('got connected!'); | |
// make a request over an HTTP tunnel | |
socket.write('GET / HTTP/1.1\r\n' + | |
'Host: http://nodejs.org/api/http.html' + | |
'Connection: close\r\n' + | |
'\r\n'); | |
socket.on('data', function(chunk) { | |
console.log(chunk.toString()); | |
}); | |
socket.on('end', function() { | |
proxy.close(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment