Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现当没有传入证书时只拦截和处理 http 请求,跳过所有 https 请求。 #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpInterceptorFactory> interceptors) {
return defaultConfig().newBuilder()
.setVirtualGatewayFactory(new HttpVirtualGatewayFactory(jks, interceptors))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -58,10 +61,10 @@ 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)) {
if ((mType == TYPE_INVALID) || (mType == TYPE_WHITELIST) || (mType == TYPE_HTTPS && sslEngineFactory == null)) {
chain.processFinal(buffer);
return;
}
Expand All @@ -71,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 | NullPointerException 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpInterceptorFactory> factories) {
this.mJKS = jks;
this.mFactories = factories;
Expand All @@ -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<HttpInterceptorFactory> factories) {
return new HttpVirtualGatewayFactory(authority, factories);
}
Expand Down