- Notifications
You must be signed in to change notification settings - Fork 615
[client-v2] SNI configuration options #2467
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
Changes from all commits
8e7a796 df12360 a65e60f 0fbe95c 659485e 07808d6 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| | @@ -129,6 +129,12 @@ | |||||||
| <version>1.18.36</version> | ||||||||
| <scope>test</scope> | ||||||||
| </dependency> | ||||||||
| <dependency> | ||||||||
| <groupId>org.slf4j</groupId> | ||||||||
| <artifactId>slf4j-simple</artifactId> | ||||||||
| <version>2.0.16</version> | ||||||||
| ||||||||
| <version>2.0.16</version> | |
| <version>2.0.16</version> | |
| <scope>test</scope> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -60,8 +60,12 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| | ||
| import javax.net.ssl.HostnameVerifier; | ||
| import javax.net.ssl.SNIHostName; | ||
| import javax.net.ssl.SSLContext; | ||
| import javax.net.ssl.SSLException; | ||
| import javax.net.ssl.SSLParameters; | ||
| import javax.net.ssl.SSLSocket; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| | @@ -256,8 +260,17 @@ public CloseableHttpClient createHttpClient(boolean initSslContext, Map<String, | |
| // Top Level builders | ||
| HttpClientBuilder clientBuilder = HttpClientBuilder.create(); | ||
| SSLContext sslContext = initSslContext ? createSSLContext(configuration) : null; | ||
| LayeredConnectionSocketFactory sslConnectionSocketFactory = sslContext == null ? new DummySSLConnectionSocketFactory() | ||
| : new SSLConnectionSocketFactory(sslContext); | ||
| LayeredConnectionSocketFactory sslConnectionSocketFactory; | ||
| if (sslContext != null) { | ||
| String socketSNI = (String)configuration.get(ClientConfigProperties.SSL_SOCKET_SNI.getKey()); | ||
| if (socketSNI != null && !socketSNI.trim().isEmpty()) { | ||
| sslConnectionSocketFactory = new CustomSSLConnectionFactory(socketSNI, sslContext, (hostname, session) -> true); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code is using a hostname verifier that always returns true ( | ||
| } else { | ||
| sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext); | ||
| } | ||
| } else { | ||
| sslConnectionSocketFactory = new DummySSLConnectionSocketFactory(); | ||
| } | ||
| // Socket configuration | ||
| SocketConfig.Builder soCfgBuilder = SocketConfig.custom(); | ||
| ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.<Integer>applyIfSet(configuration, | ||
| | @@ -834,4 +847,25 @@ public long getTime() { | |
| return count > 0 ? runningAverage / count : 0; | ||
| } | ||
| } | ||
| | ||
| public static class CustomSSLConnectionFactory extends SSLConnectionSocketFactory { | ||
| | ||
| private final SNIHostName defaultSNI; | ||
| | ||
| public CustomSSLConnectionFactory(String defaultSNI, SSLContext sslContext, HostnameVerifier hostnameVerifier) { | ||
| super(sslContext, hostnameVerifier); | ||
| this.defaultSNI = defaultSNI == null || defaultSNI.trim().isEmpty() ? null : new SNIHostName(defaultSNI); | ||
| } | ||
chernser marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| @Override | ||
| protected void prepareSocket(SSLSocket socket, HttpContext context) throws IOException { | ||
| super.prepareSocket(socket, context); | ||
| | ||
| if (defaultSNI != null) { | ||
| SSLParameters sslParams = socket.getSSLParameters(); | ||
| sslParams.setServerNames(Collections.singletonList(defaultSNI)); | ||
| socket.setSSLParameters(sslParams); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.