lib/nproxy.js
function proxyHttps(){
  httpServer.on('connect', function(req, socket, upgradeHead){
    var hostname = req.url.split(':')[0];
    log.debug('Create internal https server for ' + hostname);
    createInternalHttpsServer(hostname, function (err, httpsServerPort) {
      if (err) {
        log.error('Failed to create internal https proxy');
        log.error(err);
        return;
      }
      // get proxy https server
      var netClient = net.createConnection(httpsServerPort);

      netClient.on('connect', function(){
        log.info('connect to https server successfully!');
        socket.write( "HTTP/1.1 200 Connection established\r\nProxy-agent: Netscape-Proxy/1.1\r\n\r\n");
      });

      socket.on('data', function(chunk){
        netClient.write(chunk);
      });
      socket.on('end', function(){
        netClient.end();
      });
      socket.on('close', function(){
        netClient.end();
      });
      socket.on('error', function(err){
        log.error('socket error ' + err.message);
        netClient.end();
      });

      netClient.on('data', function(chunk){
        socket.write(chunk);
      });
      netClient.on('end', function(){
        socket.end();
      });
      netClient.on('close', function(){
        socket.end();
      });
      netClient.on('error', function(err){
        log.error('netClient error ' + err.message);
        socket.end();
      });
    });
  });
}
function proxyHttps(){
  httpServer.on('connect', function(req, socket, upgradeHead){
    var hostname = req.url.split(':')[0];
    log.debug('Create internal https server for ' + hostname);
    createInternalHttpsServer(hostname, function (err, httpsServerPort) {
      if (err) {
        log.error('Failed to create internal https proxy');
        log.error(err);
        return;
      }
      // get proxy https server
      var netClient = net.createConnection(httpsServerPort);

      netClient.on('connect', function(){
        log.info('connect to https server successfully!');
        socket.write( "HTTP/1.1 200 Connection established\r\nProxy-agent: Netscape-Proxy/1.1\r\n\r\n");
      });

      socket.on('data', function(chunk){
        netClient.write(chunk);
      });
      socket.on('end', function(){
        netClient.end();
      });
      socket.on('close', function(){
        netClient.end();
      });
      socket.on('error', function(err){
        log.error('socket error ' + err.message);
        netClient.end();
      });

      netClient.on('data', function(chunk){
        socket.write(chunk);
      });
      netClient.on('end', function(){
        socket.end();
      });
      netClient.on('close', function(){
        socket.end();
      });
      netClient.on('error', function(err){
        log.error('netClient error ' + err.message);
        socket.end();
      });
    });
  });
}
function proxyHttps ( ) { httpServer . on ( 'connect' , function ( req , socket , upgradeHead ) { var hostname = req . url . split ( ':' ) [ 0 ] ; log . debug ( 'Create internal https server for ' + hostname ) ; createInternalHttpsServer ( hostname , function ( err , httpsServerPort ) { if ( err ) { log . error ( 'Failed to create internal https proxy' ) ; log . error ( err ) ; return ; } // get proxy https server var netClient = net . createConnection ( httpsServerPort ) ; netClient . on ( 'connect' , function ( ) { log . info ( 'connect to https server successfully!' ) ; socket . write ( "HTTP/1.1 200 Connection established\r\nProxy-agent: Netscape-Proxy/1.1\r\n\r\n" ) ; } ) ; socket . on ( 'data' , function ( chunk ) { netClient . write ( chunk ) ; } ) ; socket . on ( 'end' , function ( ) { netClient . end ( ) ; } ) ; socket . on ( 'close' , function ( ) { netClient . end ( ) ; } ) ; socket . on ( 'error' , function ( err ) { log . error ( 'socket error ' + err . message ) ; netClient . end ( ) ; } ) ; netClient . on ( 'data' , function ( chunk ) { socket . write ( chunk ) ; } ) ; netClient . on ( 'end' , function ( ) { socket . end ( ) ; } ) ; netClient . on ( 'close' , function ( ) { socket . end ( ) ; } ) ; netClient . on ( 'error' , function ( err ) { log . error ( 'netClient error ' + err . message ) ; socket . end ( ) ; } ) ; } ) ; } ) ; }
Listen the CONNECTION method and forward the https request to internal https server
Listen the CONNECTION method and forward the https request to internal https server