2

I have been trying to create proxy server for both HTTP and HTTPS requests. I followed this code snippet from here.

But I'm behind a corporate proxy, so I have to modify the code a bit. For HTTP requests, I changed the host, port and path, accordingly.

But I'm stuck at configuring the HTTPS requests.

  // 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

I keep getting connect ECONNREFUSED

I tried a couple of things:

  • Did a simple request and got the response, but I don't know how to forward the response to the client
  • I also tried this answer here, but I keep getting EACCES error, I ran it with sudo permissions, but still it doesn't catch any request.(Proxy Settings in browser set to 127.0.0.1:443 for HTTPS)

So my questions are:

  • Can I configure the socket above to use the proxy server I'm behind?
  • If I can get the response via a request, how can I forward it to the client?

Thanks

Community
  • 1
  • 1
Samuel Bushi
  • 341
  • 3
  • 16

0 Answers0