Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: JavaScript  |  size: 2.66 KB  |  views: 60  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var http = require('http');
  2. var url = require('url');
  3. var winston = require('winston');
  4. var logger = new (winston.Logger)({
  5.     transports: [
  6.       new (winston.transports.Console)(),
  7.       new (winston.transports.File)({ filename: 'proxy.log', json: false })
  8.     ]
  9.   });
  10. var base_url;
  11.  
  12. http.createServer(function (clientRequest, clientResponse) {
  13.   logger.info('ENTERING createServer');
  14.  
  15.   function makeProxyRequest(clientRequestUrl) {
  16.     logger.info('ENTERING makeProxyRequest');
  17.    
  18.     // hack!
  19.     clientRequestUrl.protocol = 'http:';
  20.    
  21.         if (typeof(clientRequestUrl.hostname) === "undefined") {
  22.           clientRequestUrl.hostname = base_url.hostname;
  23.         }
  24.    
  25.     logger.info('client url: ' + url.format(clientRequestUrl));
  26.  
  27.     proxy = http.request(clientRequestUrl, proxyResponseHandler);
  28.     proxy.on('error', function(e) {
  29.           logger.error('problem with proxy request to '
  30.                     + clientRequestUrl.host + '/' + clientRequestUrl.path
  31.                     + ': ' + JSON.stringify(e));
  32.       clientResponse.end();
  33.     });
  34.     proxy.end();
  35.     logger.info('EXITING makeProxyRequest');
  36.   }
  37.  
  38.   function proxyResponseHandler(proxyResponse) {
  39.     logger.info('ENTERING proxyResponseHandler');
  40.     logger.info('status: ' + proxyResponse.statusCode);
  41.    
  42.     switch(proxyResponse.statusCode) {
  43.           case 301:
  44.           case 302:        
  45.                 var new_url = url.parse(proxyResponse.headers['location']);
  46.                 if(typeof(new_url.host) !== 'undefined') {
  47.                   base_url = new_url;
  48.                 }  
  49.                 logger.info('redirect received: ' + JSON.stringify(new_url));
  50.                 makeProxyRequest(new_url);             
  51.           break;
  52.      
  53.       case 200:
  54.             for(header in proxyResponse.headers) {
  55.           logger.info(' ' + header + ': ' + proxyResponse.headers[header]);
  56.           clientResponse.setHeader(header, proxyResponse.headers[header]);
  57.         }
  58.  
  59.         clientResponse.writeHead(proxyResponse.statusCode);
  60.  
  61.         proxyResponse.on('data', function(data) {
  62.           clientResponse.write(data);
  63.         });
  64.         proxyResponse.on('end', function() {
  65.           logger.info('end from origin');
  66.           clientResponse.end();
  67.         });
  68.           break;  
  69.         }
  70.     logger.info('EXITING proxyResponseHandler');
  71.   }
  72.   //////////////////////// Execution starts here //////////////////////////////
  73.   var tmp_url = url.parse(clientRequest.url.substring(1));
  74.   logger.info(JSON.stringify(tmp_url));
  75.   logger.info(tmp_url.host);
  76.   if (typeof(tmp_url.host) !== 'undefined' && typeof(base_url) === 'undefined' ) {
  77.     base_url = tmp_url;
  78.   }
  79.   makeProxyRequest(tmp_url);
  80.  
  81.   logger.info('EXITING createServer');
  82. }).listen(8124);
  83.  
  84. console.log('Server running at http://127.0.0.1:8124/');