From 4c743a515f60f2274fde0cb0fbfa918f6c0c9491 Mon Sep 17 00:00:00 2001 From: xausky Date: Thu, 17 Oct 2019 13:26:21 +0800 Subject: [PATCH 1/4] Skip https when jks is empty. --- .../github/megatronking/netbare/NetBareConfig.java | 4 ++-- .../netbare/http/HttpSniffInterceptor.java | 7 +++++-- .../netbare/http/HttpVirtualGateway.java | 14 ++++++++------ .../netbare/http/HttpVirtualGatewayFactory.java | 4 ++-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/netbare-core/src/main/java/com/github/megatronking/netbare/NetBareConfig.java b/netbare-core/src/main/java/com/github/megatronking/netbare/NetBareConfig.java index 85530fa..0240a5d 100644 --- a/netbare-core/src/main/java/com/github/megatronking/netbare/NetBareConfig.java +++ b/netbare-core/src/main/java/com/github/megatronking/netbare/NetBareConfig.java @@ -75,11 +75,11 @@ public Builder newBuilder() { /** * Create a default config using {@link HttpVirtualGatewayFactory} for HTTP protocol. * - * @param jks JSK instance, not null. + * @param jks JSK instance, skip https when jks is empty. * @param interceptors A collection of {@link HttpInterceptorFactory}. * @return A NetBare config instance. */ - public static NetBareConfig defaultHttpConfig(@NonNull JKS jks, + public static NetBareConfig defaultHttpConfig(JKS jks, List interceptors) { return defaultConfig().newBuilder() .setVirtualGatewayFactory(new HttpVirtualGatewayFactory(jks, interceptors)) diff --git a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java index 165c77a..f8fbaea 100644 --- a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java +++ b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java @@ -19,6 +19,7 @@ import com.github.megatronking.netbare.NetBareLog; import com.github.megatronking.netbare.ssl.SSLCodec; +import com.github.megatronking.netbare.ssl.SSLEngineFactory; import com.github.megatronking.netbare.ssl.SSLWhiteList; import java.io.IOException; @@ -40,11 +41,13 @@ private static final int TYPE_WHITELIST = 4; private final HttpSession mSession; + private final SSLEngineFactory sslEngineFactory; private int mType; - /* package */ HttpSniffInterceptor(HttpSession session) { + /* package */ HttpSniffInterceptor(HttpSession session, SSLEngineFactory sslEngineFactory) { this.mSession = session; + this.sslEngineFactory = sslEngineFactory; } @Override @@ -61,7 +64,7 @@ protected void intercept(@NonNull HttpRequestChain chain, @NonNull ByteBuffer bu if (mType == TYPE_HTTPS) { mSession.isHttps = true; } - if ((mType == TYPE_INVALID) || (mType == TYPE_WHITELIST)) { + if ((mType == TYPE_INVALID) || (mType == TYPE_WHITELIST) || (mType == TYPE_HTTPS && sslEngineFactory == null)) { chain.processFinal(buffer); return; } diff --git a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java index 86f9afe..e628f7c 100644 --- a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java +++ b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java @@ -57,18 +57,20 @@ this.mHttpZygoteRequest = new HttpZygoteRequest(request, sessionFactory); this.mHttpZygoteResponse = new HttpZygoteResponse(response, sessionFactory); - SSLEngineFactory sslEngineFactory; - try { - sslEngineFactory = SSLEngineFactory.get(jks); - } catch (GeneralSecurityException | IOException e) { - sslEngineFactory = null; + SSLEngineFactory sslEngineFactory = null; + if (jks != null){ + try { + sslEngineFactory = SSLEngineFactory.get(jks); + } catch (GeneralSecurityException | IOException e) { + //Ignore + } } // Add default interceptors. HttpSSLCodecInterceptor codecInterceptor = new HttpSSLCodecInterceptor(sslEngineFactory, request, response); this.mInterceptors = new ArrayList<>(8); - mInterceptors.add(new HttpSniffInterceptor(sessionFactory.create(session.id))); + mInterceptors.add(new HttpSniffInterceptor(sessionFactory.create(session.id), sslEngineFactory)); mInterceptors.add(codecInterceptor); mInterceptors.add(new Http2SniffInterceptor(codecInterceptor)); mInterceptors.add(new Http2DecodeInterceptor(codecInterceptor, mHttpZygoteRequest, mHttpZygoteResponse)); diff --git a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGatewayFactory.java b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGatewayFactory.java index d415f16..aa0d860 100644 --- a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGatewayFactory.java +++ b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGatewayFactory.java @@ -45,7 +45,7 @@ public class HttpVirtualGatewayFactory implements VirtualGatewayFactory { * @param factories a collection of {@link HttpInterceptorFactory}. * @return A instance of {@link HttpVirtualGatewayFactory}. */ - public HttpVirtualGatewayFactory(@NonNull JKS jks, + public HttpVirtualGatewayFactory(JKS jks, @NonNull List factories) { this.mJKS = jks; this.mFactories = factories; @@ -63,7 +63,7 @@ public VirtualGateway create(Session session, Request request, Response response * @param factories a collection of {@link HttpInterceptorFactory}. * @return A instance of {@link HttpVirtualGatewayFactory}. */ - public static VirtualGatewayFactory create(@NonNull JKS authority, + public static VirtualGatewayFactory create(JKS authority, @NonNull List factories) { return new HttpVirtualGatewayFactory(authority, factories); } From b7dcd4c76c9440d381517400b50742b43b80f17c Mon Sep 17 00:00:00 2001 From: xausky Date: Thu, 17 Oct 2019 14:06:32 +0800 Subject: [PATCH 2/4] fix NullPointerException. --- .../github/megatronking/netbare/http/HttpVirtualGateway.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java index e628f7c..c0379f8 100644 --- a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java +++ b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGateway.java @@ -61,7 +61,7 @@ if (jks != null){ try { sslEngineFactory = SSLEngineFactory.get(jks); - } catch (GeneralSecurityException | IOException e) { + } catch (GeneralSecurityException | IOException | NullPointerException e) { //Ignore } } From 16d37796887d77cf5f046ca4f6e10fab7defc219 Mon Sep 17 00:00:00 2001 From: xausky Date: Thu, 17 Oct 2019 14:23:52 +0800 Subject: [PATCH 3/4] fix response skip https. --- .../github/megatronking/netbare/http/HttpSniffInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java index f8fbaea..00962a5 100644 --- a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java +++ b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java @@ -74,7 +74,7 @@ protected void intercept(@NonNull HttpRequestChain chain, @NonNull ByteBuffer bu @Override protected void intercept(@NonNull HttpResponseChain chain, @NonNull ByteBuffer buffer, int index) throws IOException { - if ((mType == TYPE_INVALID) || (mType == TYPE_WHITELIST)) { + if ((mType == TYPE_INVALID) || (mType == TYPE_WHITELIST) || (mType == TYPE_HTTPS && sslEngineFactory == null)) { chain.processFinal(buffer); return; } From de26a155c07ffe17916ab76ac8fd03164cd26b62 Mon Sep 17 00:00:00 2001 From: xausky Date: Mon, 21 Oct 2019 15:39:39 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BA=AFHttp=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/megatronking/netbare/http/HttpSniffInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java index 00962a5..917c82e 100644 --- a/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java +++ b/netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSniffInterceptor.java @@ -61,7 +61,7 @@ protected void intercept(@NonNull HttpRequestChain chain, @NonNull ByteBuffer bu mType = chain.request().host() == null ? TYPE_INVALID : verifyHttpType(buffer); } } - if (mType == TYPE_HTTPS) { + if (mType == TYPE_HTTPS && sslEngineFactory != null) { mSession.isHttps = true; } if ((mType == TYPE_INVALID) || (mType == TYPE_WHITELIST) || (mType == TYPE_HTTPS && sslEngineFactory == null)) {