monkeyWie / proxyee

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

请问开启了中间人代理后,代理流式(会持续返回)接口,如何实现也流式(response输出)返回,而不是等到最后响应结束后才一次过返回一堆结果(如json) #281

Open zzmgc4 opened 1 year ago

zzmgc4 commented 1 year ago

{“result”:"1"} {“result”:"12"} {“result”:"123"}

monkeyWie commented 1 year ago

不要使用FullResponseIntercept就行了,因为要完整解码响应的话肯定是得拿到全部的报文才行。


Just don't use FullResponseIntercept, because if you want to fully decode the response, you must get all the packets.

zzmgc4 commented 1 year ago

因为需要拿到请求的报文和响应的报文做内容审查,但是又不对原来的响应速度造成影响,可以一边先流式输出,等到结束才拿到所有报文存起来麽

monkeyWie commented 1 year ago

理论上是可以的,但是需要自己实现一个类似FullResponseIntercept的拦截器,不过如果只是做记录的话很简单的,把每次过来的Content缓存住,判断是最后一个Content的时候再解码处理

zzmgc4 commented 1 year ago

pipeline.addLast(new 类似FullResponseIntercept的拦截器() { public boolean match(HttpRequest httpRequest, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) { return true; } });

如果是这样下面这段还需要吗 public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception { this.fullHttpRequest.release(); FullHttpResponse fullHttpResponse = (FullHttpResponse) httpResponse; //保存数据库 save(); pipeline.afterResponse(clientChannel, proxyChannel, httpResponse); }

monkeyWie commented 1 year ago

实现这个最基础的拦截器HttpProxyIntercept里的这个方法就行了:

public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent,
                              HttpProxyInterceptPipeline pipeline)

Just implement this method in the most basic interceptor HttpProxyIntercept:

public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent,
                              HttpProxyInterceptPipeline pipeline)
zzmgc4 commented 1 year ago

感谢大佬,我试试


Thanks guys, I'll try

zzmgc4 commented 1 year ago

是这个意思嘛 public abstract class AsynFullResponseIntercept extends HttpProxyIntercept { private List contentList = new ArrayList<>();

public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent,
                          HttpProxyInterceptPipeline pipeline) throws Exception {
    contentList.add(httpContent);

    if (httpContent instanceof LastHttpContent) {
        processContents(contentList);
        contentList.clear();

    }

    // Continue with the pipeline
    pipeline.afterResponse(clientChannel, proxyChannel, httpContent);
}

//say hello

private void processContents(List<HttpContent> contents) {
    StringBuilder stringBuilder = new StringBuilder();

    for (HttpContent content : contents) {
        ByteBuf byteBuf = content.content();
        stringBuilder.append(byteBuf.toString(CharsetUtil.UTF_8));
    }
    String result = stringBuilder.toString();
}
monkeyWie commented 1 year ago

嗯,就是这个意思,然后细节方面你再调下应该就行了


Well, that’s what it means, and then you should adjust the details