monkeyWie / proxyee

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

现在请求来了都要去发送,能否判断是否自己想要的链接,不经过服务端,直接返回执行内容 #7

Closed l454124613 closed 6 years ago

l454124613 commented 6 years ago

现在请求来了都要去发送,能否判断是否自己想要的链接,不经过服务端,直接返回执行内容

monkeyWie commented 6 years ago

@l454124613 代码刚刚调整了蛮多,可以pull下最新的代码,下面是已最新代码来实现的。 思路:实现一个拦截器重写beforeRequest方法,然后拦截器不放行,进行特殊处理 demo:拦截uri为/test.html的请求然后返回一个自定义页面

pipeline.addLast(new HttpProxyIntercept() {
              @Override
              public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
                  HttpProxyInterceptPipeline pipeline) throws Exception {
                if (httpRequest.uri().equals("/test.html")) {  //匹配的uri直接返回response
                  String html = "<html><body>intercept</body><html>";
                  HttpResponse hookResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK);
                  hookResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
                  hookResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, html.getBytes().length);
                  clientChannel.writeAndFlush(hookResponse);
                  HttpContent hookContent = new DefaultLastHttpContent();
                  hookContent.content().writeBytes(html.getBytes());
                  clientChannel.writeAndFlush(hookContent );
                }else{
                  pipeline.beforeRequest(clientChannel, httpRequest);
                }
              }
            });

效果: image