[6a3a178] | 1 | var http = require('http'),
|
---|
| 2 | https = require('https'),
|
---|
| 3 | url = require('url');
|
---|
| 4 |
|
---|
| 5 | var port = 1234,
|
---|
| 6 | log = true,
|
---|
| 7 | request_auth = false;
|
---|
| 8 |
|
---|
| 9 | http.createServer(function(request, response) {
|
---|
| 10 |
|
---|
| 11 | console.log(request.headers);
|
---|
| 12 | console.log("Got request: " + request.url);
|
---|
| 13 | console.log("Forwarding request to " + request.headers['host']);
|
---|
| 14 |
|
---|
| 15 | if (request_auth) {
|
---|
| 16 | if (!request.headers['proxy-authorization']) {
|
---|
| 17 | response.writeHead(407, {'Proxy-Authenticate': 'Basic realm="proxy.com"'})
|
---|
| 18 | return response.end('Hello.');
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | var remote = url.parse(request.url);
|
---|
| 23 | var protocol = remote.protocol == 'https:' ? https : http;
|
---|
| 24 |
|
---|
| 25 | var opts = {
|
---|
| 26 | host: request.headers['host'],
|
---|
| 27 | port: remote.port || (remote.protocol == 'https:' ? 443 : 80),
|
---|
| 28 | method: request.method,
|
---|
| 29 | path: remote.pathname,
|
---|
| 30 | headers: request.headers
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | var proxy_request = protocol.request(opts, function(proxy_response){
|
---|
| 34 |
|
---|
| 35 | proxy_response.on('data', function(chunk) {
|
---|
| 36 | if (log) console.log(chunk.toString());
|
---|
| 37 | response.write(chunk, 'binary');
|
---|
| 38 | });
|
---|
| 39 | proxy_response.on('end', function() {
|
---|
| 40 | response.end();
|
---|
| 41 | });
|
---|
| 42 |
|
---|
| 43 | response.writeHead(proxy_response.statusCode, proxy_response.headers);
|
---|
| 44 | });
|
---|
| 45 |
|
---|
| 46 | request.on('data', function(chunk) {
|
---|
| 47 | if (log) console.log(chunk.toString());
|
---|
| 48 | proxy_request.write(chunk, 'binary');
|
---|
| 49 | });
|
---|
| 50 |
|
---|
| 51 | request.on('end', function() {
|
---|
| 52 | proxy_request.end();
|
---|
| 53 | });
|
---|
| 54 |
|
---|
| 55 | }).listen(port);
|
---|
| 56 |
|
---|
| 57 | process.on('uncaughtException', function(err){
|
---|
| 58 | console.log('Uncaught exception!');
|
---|
| 59 | console.log(err);
|
---|
| 60 | });
|
---|
| 61 |
|
---|
| 62 | console.log("Proxy server listening on port " + port);
|
---|