Git Product home page Git Product logo

Comments (5)

monkeyWie avatar monkeyWie commented on May 18, 2024

这个bug是什么情况才会出现呢,看起来是栈内存溢出了,估计是有无限递归的情况

from proxyee.

l454124613 avatar l454124613 commented on May 18, 2024

使用方法如下:
new Thread(new Runnable() {
@OverRide
public void run() {
new HttpProxyServer()
.proxyInterceptInitializer(new HttpProxyInterceptInitializer() {
@OverRide
public void init(HttpProxyInterceptPipeline pipeline) {
pipeline.addLast(new CertDownIntercept());
pipeline.addLast(new HttpProxyIntercept() {
@OverRide
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
HttpProxyInterceptPipeline pipeline) throws Exception {

                       AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
                            //转到下一个拦截器处理

//----以下是截取uri提取部分,和代理无关
map.put(clientChannel.id().asShortText(),httpRequest.uri());
if(lt.size()>0){
for (int i = 0; i <lt.size() ; i++) {
String tm= httpRequest.uri();
String ta=lt.get(i).getValue();
if(ta.contains("(")&&ta.contains(")")){
int t1=ta.indexOf("(");
int t2=ta.lastIndexOf(")");
String tb=ta.substring(t1,t2+1);
String re11=ta.replace(tb,"tmppmt112233qq");
ta= re11.replace(".","\.").replace("?","\?").replace("+","\+");
tb=tb.substring(1,t2-t1);
ta= ta.replace("tmppmt112233qq",tb);

                                    }else {
                                        ta=ta.replace(".","\\.").replace("?","\\?").replace("+","\\+");
                                    }

//----以上是截取uri提取部分,和代理无关

                                    if(httpRequest.uri().matches(ta)){
                                        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);
                                        pipeline.getDefault().afterResponse(clientChannel, null , hookResponse, pipeline);
                                        HttpContent hookContent = new DefaultLastHttpContent();
                                        hookContent.content().writeBytes(html.getBytes());
                                        pipeline.getDefault().afterResponse(clientChannel, null, hookContent, pipeline);

                                    }else {
                                        pipeline.beforeRequest(clientChannel, httpRequest);
                                    }

                                }
                            }

                        }

                        @Override
                        public void afterResponse(Channel clientChannel, Channel proxyChannel,
                                                  HttpResponse httpResponse,
                                                  HttpProxyInterceptPipeline pipeline) throws Exception {
                            //拦截响应,添加一个响应头
                            //httpResponse.headers().add("intercept", "test");
                            if(map.containsKey(proxyChannel.id().asShortText())){
                                map.remove(proxyChannel.id().asShortText());
                            }
                            pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
                        }
                    });
                }
            }).start(8102);

}

}).start();

}

from proxyee.

monkeyWie avatar monkeyWie commented on May 18, 2024

你是在for循环里调用了pipeline.beforeRequest(clientChannel, httpRequest)吗,这个是处理完逻辑之后再调用,使用下一个handler来处理的

from proxyee.

monkeyWie avatar monkeyWie commented on May 18, 2024

不经过服务器,直接本地拦截可以写的更简单点

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);
                  //直接输出http响应体至客户端,不走后面的转发handler了
                  clientChannel.writeAndFlush(hookResponse);
                  HttpContent hookContent = new DefaultLastHttpContent();
                  hookContent.content().writeBytes(html.getBytes());
                  clientChannel.writeAndFlush(hookContent );
                }else{
                  pipeline.beforeRequest(clientChannel, httpRequest);
                }
              }
            });

from proxyee.

l454124613 avatar l454124613 commented on May 18, 2024

成功啦,谢谢大神了,果然多次handle 了

from proxyee.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.