1

NodeJS、Angular、およびノー​​ド Webkit を使用してプロキシを作成しようとしています。HTTP サーバーがすべての要求および応答データを取得しています。ただし、 https.createServer 呼び出しは私の要求ハンドラーを呼び出していません。私はいくつかの異なることを試しましたが、何も https が処理されるようには見えません。

    App.factory('proxy', ['$rootScope', 'http', 'https', 'fs', 'net', 'urlParser', function($rootScope, http, https, fs, net, urlParser){
 'use strict';

  var intercept = false;

  /*
  * Handle requests dependent on protocol
  * input: (obj)request, (obj)response, (str) type
  */
  var handle_req = function(req, res, type){
    console.log('listening for '+type);
    /*
    * Options used in the http(s).request method
    */
    var req_options = {
      hostname: req.headers.host,
      port: (type === "https" ? 443 : 80),
      path: urlParser.getPath(req.url),
      method: req.method,
      agent: false,
      headers: req.headers
    };
    /*
    * Handle requests and all events that may happen with response
    */
    var proxy_request = (type === "https" ? https : http).request(req_options, function(response){
      res.writeHead(response.statusCode, response.headers);

      response.on('data',function(chunk){
        if(type === 'https')
          res.stdout.write(chunk);  
        else
          res.write(chunk);
      });

      response.on('end', function(){
        $rootScope.$broadcast('Proxy.response', response.headers);
        res.end();
      });

      response.on('close', function(){
        response.connection.end();
      });
    });
    /*
    * Other functions to listen and handle on the request portion.
    */
    req.on('data', function(chunk){
      proxy_request.write(chunk, 'binary');
      $rootScope.$broadcast('Proxy.request', req.headers);
    });

    req.on('end', function(){
      proxy_request.end();
    });

    req.on('close', function(){
      proxy_request.end();
    });

    req.on('error', function(){
      proxy_request.end();
      console.log("Proxy request errored out.");
    });

  };
  /*
  * Options for https listening
  */
  var https_opts = {
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.crt')
  };

  /*
  * Create HTTPS server
  */
  var httpsProxy = https.createServer(https_opts, function(req, res){
    handle_req(req, res, "https");
  });

  /*
  * Create HTTP server
  */
  var httpProxy = http.createServer(function(req, res){
    handle_req(req, res, "http");
  });
    /*
    * Handle upgrade from HTTP to HTTPS 
    */
  httpProxy.on('upgrade',function(req, socket, upgradeHead){
    console.log('upgrading');
    var proxy = net.createConnection(8000, 'localhost');

    proxy.on('connect', function(){
      socket.write("HTTP/1.0 200 Connection Established");
    });

    proxy.on('data', function(chunk){
      socket.write(chunk);
    });
    socket.on('data', function(chunk){
        proxy.write(chunk);
    });

    proxy.on('end', function(){
      socket.end();
    });
    socket.on('end', function(){
      proxy.end()
    });

    proxy.on('close', function(){
      socket.end();
    });
    socket.on('close', function(){
      proxy.end();
    });

    proxy.on('error', function(){
      socket.end();
    });
    socket.on('error', function(){
      proxy.end();
    });

  });

  httpProxy.on('error',function(){
    console.log("Error on server");
  });


  return {
    start: function() {
      httpProxy.listen(8080);
      httpsProxy.listen(8000);
      console.log("Proxy listening");
    },
    stop: function() {
      httpProxy.close();
      httpsProxy.close();
      console.log("Proxy has stopped listening")
    },
    getResponse: function(){
      Proxy.emit('Proxy.request.forward');
    },
    sendResponse: function(){
      Proxy.emit('Proxy.response.forward');
    },
    toggleIntercept: function(){
      intercept = !intercept;
    }
  }
}]);
4

0 に答える 0