FG
๐Ÿ’ป Software๐Ÿ“ก Networking

Secure Website Problem

Fresh3 days ago
Mar 14, 20260 views
Confidence Score54%
54%

Problem

I am trying to create a simple reverse proxy. Firefox Browser -> Node Proxy -> Proxy 1 (LimeProxies.com) -> Target Firefox Browser -> Node Proxy -> Proxy 2 (LImeProxies.com, or some other provider) -> Target Firefox Browser -> Node Proxy -> Proxy 3 (LImeProxies.com, or some other provider) -> Target Very straight forward. The problem comes in with requesting a secure website, "https://bing.com" for example as compared to "http://bing.com", which has no problems. [code block] When I request a secure website, jp.process doesn't even fire. Through research, I found that server.on('connect', function(){}); was required. By adding this, I was able to see the request for a secure website "https://bing.com". [code block] I've tried both the ws and web methods, with no luck. It attempts to proxy the request, but it fails for some reason through a timeout without any helpful error message. I've tried dozens of proxies, even SOCKS proxies for the WS method, but I cannot get the request to process successfully. If I put something like this in the connect method, it works, but the request obviously comes from the nodejs server instead of a 3rd party proxy. [code block] I am just trying to create a simple reverse proxy, but have been unable to do so for weeks now. Any help is greatly appreciated. Thanks!

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Implement HTTPS Proxying with CONNECT Method

Medium Risk

The issue arises because HTTPS requests require a different handling mechanism than HTTP requests. When a client attempts to connect to an HTTPS site, it uses the CONNECT method to establish a tunnel through the proxy. If this method is not properly implemented, the request will fail, leading to timeouts as the server cannot establish a secure connection to the target site.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Set Up the CONNECT Method Handler

    Implement the CONNECT method in your Node.js proxy server to handle HTTPS requests. This allows the proxy to establish a tunnel to the target server.

    javascript
    server.on('connect', (req, socket, head) => {
      const targetUrl = req.url.split(':');
      const targetHost = targetUrl[0];
      const targetPort = targetUrl[1] || 443;
    
      const proxySocket = net.connect(targetPort, targetHost, () => {
        socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
        socket.pipe(proxySocket);
        proxySocket.pipe(socket);
      });
    
      proxySocket.on('error', (err) => {
        console.error('Proxy Socket Error:', err);
        socket.end();
      });
    });
  2. 2

    Handle Incoming Requests

    Ensure that your server can handle both HTTP and HTTPS requests by checking the request protocol and routing them accordingly. For HTTPS, use the CONNECT method, and for HTTP, use the standard request handling.

    javascript
    server.on('request', (req, res) => {
      // Handle HTTP requests normally
    });
  3. 3

    Test with Different Proxies

    Test the proxy setup with various HTTPS sites to ensure that the CONNECT method is functioning correctly. Use tools like Postman or curl to verify that the requests are being proxied as expected.

    bash
    curl -x http://your-proxy:port https://bing.com
  4. 4

    Implement Error Handling

    Add error handling to manage potential issues when connecting to the target server or during data transmission. This will help in diagnosing problems more effectively.

    javascript
    proxySocket.on('error', (err) => {
      console.error('Error connecting to target:', err);
      res.writeHead(502);
      res.end('Bad Gateway');
    });
  5. 5

    Monitor and Log Requests

    Implement logging for incoming requests and responses to monitor the behavior of the proxy. This will help in troubleshooting and ensuring that requests are processed correctly.

    javascript
    server.on('request', (req, res) => {
      console.log(`Request: ${req.method} ${req.url}`);
    });

Validation

To confirm the fix worked, send HTTPS requests through the proxy and check the logs for successful connections. Use tools like curl or Postman to verify that the requests return the expected responses from the target site.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

proxyhttpnode.js