monkeyWie / proxyee

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

重写父类方法afterResponse 不会被触发 #249

Closed xujimu closed 1 year ago

xujimu commented 1 year ago

package com.example.demo;

import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptInitializer; import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptPipeline; import com.github.monkeywie.proxyee.intercept.common.FullResponseIntercept; import com.github.monkeywie.proxyee.proxy.ProxyType; import com.github.monkeywie.proxyee.server.HttpProxyServer; import com.github.monkeywie.proxyee.server.HttpProxyServerConfig; import com.github.monkeywie.proxyee.server.auth.BasicHttpProxyAuthenticationProvider; import com.github.monkeywie.proxyee.server.auth.HttpAuthContext; import com.github.monkeywie.proxyee.server.auth.model.BasicHttpToken; import com.github.monkeywie.proxyee.server.auth.model.HttpToken; import com.github.monkeywie.proxyee.util.HttpUtil; import io.netty.channel.Channel; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpContent; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import org.bouncycastle.asn1.x509.GeneralName; import org.bouncycastle.asn1.x509.GeneralNames; import org.bouncycastle.asn1.x509.X509Extensions; import org.bouncycastle.jce.X509Principal; import org.bouncycastle.x509.X509V3CertificateGenerator; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.graphql.GraphQlProperties; import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigInteger; import java.nio.charset.Charset; import java.security.PublicKey; import java.security.cert.X509Certificate; import java.util.Date;

@SpringBootTest class DemoApplicationTests {

public static HttpProxyServer httpProxyServer ;

@Test
void contextLoads() {

    HttpProxyServerConfig config =  new HttpProxyServerConfig();
    config.setHandleSsl(true);

    config.setAuthenticationProvider(new BasicHttpProxyAuthenticationProvider() {
        @Override
        protected BasicHttpToken authenticate(String usr, String pwd) {
            if ("1".equals(usr) && "1".equals(pwd)) {
                System.out.println("通过");
                return new BasicHttpToken(usr, pwd);
            }
            return null;

        }
    });
    httpProxyServer = new HttpProxyServer();
    httpProxyServer.serverConfig(config);
    httpProxyServer.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 true;
                }

                @Override
                public void handleResponse(HttpRequest httpRequest, FullHttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
                    //Print raw packet

                }
                @Override
                public void beforeConnect(Channel clientChannel, HttpProxyInterceptPipeline pipeline) throws Exception {
                    pipeline.beforeConnect(clientChannel);
                }

                @Override
                public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception {
                    pipeline.beforeRequest(clientChannel, httpRequest);
                }
                @Override
                public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) throws Exception {
                    pipeline.afterResponse(clientChannel, proxyChannel, httpContent);
                }

            });
        }
    });

    httpProxyServer.start(9999);

}

}

beforeConnect 和 beforeRequest都会触发 afterResponse却不会这是为何?

我现在的需求是在代理请求完毕后 进行鉴权 如果鉴权不通过就直接返回一个自定义页面 想通过afterResponse 但是确实没有触发 handleResponse方法好像没有办法使用HttpToken token = HttpAuthContext.getToken(clientChannel);