Skip to content

Instantly share code, notes, and snippets.

@vvb2060
Created September 1, 2024 10:38
Show Gist options
  • Save vvb2060/d51fefce4a269d6becfe298d7b588ff1 to your computer and use it in GitHub Desktop.
Save vvb2060/d51fefce4a269d6becfe298d7b588ff1 to your computer and use it in GitHub Desktop.
disable SNI for okhttp 5.x
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.net.ssl.SNIHostName;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class NoSNISSLSocketFactory extends SSLSocketFactory {
private final SSLSocketFactory factory;
public NoSNISSLSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
var context = SSLContext.getInstance("TLS");
context.init(null, null, null);
factory = context.getSocketFactory();
}
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
if (false) {
var socket = (SSLSocket) factory.createSocket(s, host, port, autoClose);
var params = socket.getSSLParameters();
params.setServerNames(List.of(new SNIHostName("null")));
socket.setSSLParameters(params);
return socket;
} else {
var address = s.getInetAddress().getHostAddress();
return factory.createSocket(s, address, port, autoClose);
}
}
public Socket createSocket(String host, int port) throws IOException {
return factory.createSocket(host, port);
}
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
return factory.createSocket(host, port, localHost, localPort);
}
public Socket createSocket(InetAddress host, int port) throws IOException {
return factory.createSocket(host, port);
}
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return factory.createSocket(address, port, localAddress, localPort);
}
public String[] getDefaultCipherSuites() {
return factory.getDefaultCipherSuites();
}
public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment