公司代理

时间:2016-04-03 10:04:48

标签: node.js https proxy proxy-server

我一直在尝试为HTTP和HTTPS请求创建代理服务器。 我从here开始关注此代码段。

但我支持公司代理,所以我必须稍微修改代码。对于HTTP请求,我相应更改了hostportpath

但我一直坚持配置HTTPS请求。

  // httpUserRequest handles http requests via proxy

  var server = http.createServer( httpUserRequest ).listen(port);

  // add handler for HTTPS (which issues a CONNECT to the proxy)
  server.addListener(
    'connect',
    function ( request, socketRequest, bodyhead ) {
      var url = request['url'];
      var httpVersion = request['httpVersion'];

      var hostport = getHostPortFromString( url, 443 );

      if ( debugging ){
        console.log( '  = will connect to %s:%s', hostport[0], hostport[1] );
        // console.log( request);
        console.log(hostport[0]+':443');
      }

      // set up TCP connection
      var proxySocket = new net.Socket();
      proxySocket.connect(parseInt(hostport[1]), hostport[0],
        function () {
          if (debugging)
            console.log( '  < connected to %s/%s', hostport[0], hostport[1] );

          if ( debugging )
            console.log( '  > writing head of length %d', bodyhead.length );

          proxySocket.write( bodyhead );

          // tell the caller the connection was successfully established
          socketRequest.write( "HTTP/" + httpVersion + " 200 Connection established\r\n\r\n" );
        }
      );

      proxySocket.on(
        'data',
        function ( chunk ) {
          if ( debugging )
            console.log( '  < data length = %d', chunk.length );

          socketRequest.write( chunk );
        }
      );

      proxySocket.on(
        'end',
        function () {
          if ( debugging )
            console.log( '  < end' );

          socketRequest.end();
        }
      );

      socketRequest.on(
        'data',
        function ( chunk ) {
          if ( debugging )
            console.log( '  > data length = %d', chunk.length );

          proxySocket.write( chunk );
        }
      );

      socketRequest.on(
        'end',
        function () {
          if ( debugging )
            console.log( '  > end' );

          proxySocket.end();
        }
      );

      proxySocket.on(
        'error',
        function ( err ) {
          socketRequest.write( "HTTP/" + httpVersion + " 500 Connection error\r\n\r\n" );
          if ( debugging ) {
            console.log( '  < ERR: %s', err );
          }
          socketRequest.end();
        }
      );

      socketRequest.on(
        'error',
        function ( err ) {
          if ( debugging ) {
            console.log( '  > ERR: %s', err );
          }
          proxySocket.end();
        }
      );
    }

); // HTTPS connect listener

我不断获得connect ECONNREFUSED

我尝试了几件事:

  • 做了一个简单的request得到了回复,但我不知道如何将回复转发给客户
  • 我也尝试了这个答案here,但我一直收到EACCES错误,我用sudo权限运行它,但它仍然没有捕获任何请求。(浏览器中的代理设置设置为127.0.0.1:443对于HTTPS)

所以我的问题是:

  • 我可以配置上面的套接字以使用我背后的代理服务器吗?
  • 如果我可以通过request获得回复,我该如何将其转发给客户?

由于

0 个答案:

没有答案