0

I'm using BrowserMob Proxy and I want to redirect traffic from my production server to a test server. For that I'm using a rewriteUrl in the followin manner:

public class Main {

    public static void main(String[] args) {

        BrowserMobProxyServer server = new BrowserMobProxyServer();
        server.setTrustAllServers(true);

        server.start(8888);
        server.rewriteUrl("(https?://)init.mycompany.com(.*)","$1platform-sandbox.mycompany.com$2//");

        if (server.isStarted()) {
            System.out.println("Server started on " + server.getClientBindAddress() + ":" + server.getPort());
        }

        server.addRequestFilter(new RequestFilter() {
            @Override
            public HttpResponse filterRequest(io.netty.handler.codec.http.HttpRequest httpRequest, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) {
                System.out.println("REQUEST");
                System.out.println("Original: " + httpMessageInfo.getOriginalUrl());
                System.out.println("Current: " + httpMessageInfo.getUrl());
                return null;
            }
        });

        server.addResponseFilter(new ResponseFilter() {
            @Override
            public void filterResponse(HttpResponse httpResponse, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) {
                System.out.println("RESPONSE");
                System.out.println("Original: " + httpMessageInfo.getOriginalUrl());
                System.out.println("Current: " + httpMessageInfo.getUrl());
                if (httpMessageInfo.getOriginalUrl() != httpMessageInfo.getUrl()) {
                    System.out.printf("DIFFERENT!!!!");
                }
            }
        });

        server.newHar("test");
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

You'll notice that I have a partial success:

  1. The two // symbols are added in the end (did that for testing).
  2. However, init is not replaced by platform-sandbox

REQUEST

Original: https://init.mycompany.com

Current: https://init.mycompany.com

REQUEST

Original: https://init.mycompany.com/sdk/v6.4.6?applicationKey=42a8c1f5

Current: https://init.mycompany.com/sdk/v6.4.6?applicationKey=42a8c1f5

RESPONSE

Original: https://init.mycompany.com/sdk/v6.4.6?applicationKey=42a8c1f5

Current: https://init.mycompany.com/sdk/v6.4.6?applicationKey=42a8c1f5//

Yotam
  • 7,564
  • 13
  • 42
  • 66

1 Answers1

1

I have tested your code with http requests and it works good (init is replaced with platform-sandbox).

The problem appears only in case of https requests. When you request https URL the request method CONNECT is used to establish a tunnel connection. Currently the Browsermob Proxy doesn't support rewriting of URLs if request method is CONNECT.

There are more details in the source code. You can set breakpoint on this line in the code and debug http and https traffic through this point to see the difference. In case of https traffic when request method CONNECT is used the RewriteUrlFilter returns null and rewriting rules are not applied to such requests.

Eugene Maysyuk
  • 1,639
  • 18
  • 18