monkeyWie / proxyee

HTTP proxy server,support HTTPS&websocket.MITM impl,intercept and tamper HTTPS traffic.
MIT License
1.5k stars 566 forks source link

如何修改 响应内容 #235

Closed toohandsome closed 1 year ago

toohandsome commented 1 year ago

示例代码貌似只能追加?

HttpProxyServerConfig config =  new HttpProxyServerConfig();
//enable HTTPS support
//If not enabled, HTTPS will not be intercepted, but forwarded directly to the raw packet.
config.setHandleSsl(true);
new HttpProxyServer()
    .serverConfig(config)
    .proxyInterceptInitializer(new HttpProxyInterceptInitializer() {
      @Override
      public void init(HttpProxyInterceptPipeline pipeline) {
        pipeline.addLast(new FullResponseIntercept() {

          @Override
          public boolean match(HttpRequest httpRequest, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
            //Insert js when matching to Baidu homepage
            return HttpUtil.checkUrl(pipeline.getHttpRequest(), "^www.baidu.com$")
                && isHtml(httpRequest, httpResponse);
          }

          @Override
          public void handleResponse(HttpRequest httpRequest, FullHttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
            //Print raw packet
            System.out.println(httpResponse.toString());
            System.out.println(httpResponse.content().toString(Charset.defaultCharset()));
            //Edit response header and response body
            httpResponse.headers().set("handel", "edit head");
            httpResponse.content().writeBytes("<script>alert('hello proxyee')</script>".getBytes());
          }
        });
      }
    })
    .start(9999);

我尝试替换原始的response

 @Override
                            public void handleResponse(HttpRequest httpRequest, FullHttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
                                //Print raw packet
                                System.out.println(httpResponse.toString());
                                System.out.println(httpResponse.content().toString(Charset.defaultCharset()));
                                //Edit response header and response body
                                httpResponse.headers().set("handel", "edit head");
                                final ByteBuf byteBuf = Unpooled.wrappedBuffer("abc".getBytes());
                                FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
                                fullHttpResponse.content().writeBytes(byteBuf);
                                httpResponse = fullHttpResponse;
                                //clientChannel.writeAndFlush(fullHttpResponse);
//                                httpResponse.content().writeBytes("<script>alert('hello proxyee')</script>".getBytes());
//                                httpResponse.content().w
                            }

但是并不生效 使用 replace 也不生效,原始响应均无变化

httpResponse.replace(byteBuf);

请问可以直接修改响应内容吗?

monkeyWie commented 1 year ago

把新的response写出去就可以了:

clientChannel.writeAndFlush(fullHttpResponse);
return;
toohandsome commented 1 year ago

把新的response写出去就可以了:

clientChannel.writeAndFlush(fullHttpResponse);
return;

我解决了,先 调用一下 httpResponse.content().clear(); 就好了, clientChannel.writeAndFlush(fullHttpResponse); 示例代码里面并没有clientChannel这个变量

monkeyWie commented 1 year ago

@toohandsome pipeline.getClientChannel()也可以