Skip to content

Instantly share code, notes, and snippets.

@darcyliu darcyliu/proxy.js
Created May 10, 2017

Embed
What would you like to do?
Simple HTTP Proxy
var http = require('http');
var net = require('net');
var url = require('url');
function onRequest(request, response) {
console.log('request: ' + request.url);
var u = url.parse(request.url);
var options = {
hostname: u.hostname,
port: u.port || 80,
path: u.path,
method: request.method,
headers: request.headers
};
var proxy = http.request(options, function(res) {
response.writeHead(res.statusCode, res.headers);
res.pipe(response, {
end: true
});
}).on('error', function(e) {
response.end();
});;
request.pipe(proxy, {
end: true
});
}
function onConnect(request, response) {
console.log('request: ' + request.url);
var u = url.parse('https://' + request.url);
var client = net.connect(u.port, u.hostname, function() {
response.write('HTTP/1.1 200 Connection Established\r\n\r\n');
client.pipe(response);
}).on('error', function(e) {
response.end();
});
response.pipe(client);
}
http.createServer()
.on('request', onRequest)
.on('connect', onConnect)
.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.