0

我正在使用 BrowserMob 代理,我想将流量从生产服务器重定向到测试服务器。为此,我rewriteUrl以下列方式使用 a:

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();
            }
        }
    }
}

你会注意到我取得了部分成功:

  1. 最后添加了两个//符号(这样做是为了测试)。
  2. 但是,init不会被替换platform-sandbox

要求

原文:https ://init.mycompany.com

当前:https ://init.mycompany.com

要求

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

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

回复

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

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

4

1 回答 1

1

我已经用 http 请求测试了你的代码,它运行良好(init被替换为platform-sandbox)。

该问题仅在 https 请求的情况下出现。当您请求 https URL 时,请求方法CONNECT用于建立隧道连接。目前 Browsermob 代理不支持在请求方法为CONNECT.

源代码中有更多细节。可以在代码中在这一行设置断点,通过该点调试http和https的流量,看看区别。如果使用请求方法时出现 https 流量,CONNECT则 RewriteUrlFilter 返回 null 并且重写规则不适用于此类请求。

于 2016-07-06T11:00:23.470 回答