8 changed files with 127 additions and 105 deletions
@ -0,0 +1,66 @@ |
|||||
|
package qianmu.container.socket; |
||||
|
|
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.net.InetAddress; |
||||
|
import java.net.Socket; |
||||
|
import java.net.UnknownHostException; |
||||
|
import javax.net.ssl.SSLSocket; |
||||
|
import javax.net.ssl.SSLSocketFactory; |
||||
|
|
||||
|
public class CustomSSLSocketFactory extends SSLSocketFactory { |
||||
|
private final SSLSocketFactory delegate; |
||||
|
|
||||
|
public CustomSSLSocketFactory(SSLSocketFactory delegate) { |
||||
|
this.delegate = delegate; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String[] getDefaultCipherSuites() { |
||||
|
return delegate.getDefaultCipherSuites(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String[] getSupportedCipherSuites() { |
||||
|
return delegate.getSupportedCipherSuites(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { |
||||
|
Socket socket = delegate.createSocket(s, host, port, autoClose); |
||||
|
return wrapSocket(socket); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Socket createSocket(String host, int port) throws IOException, UnknownHostException { |
||||
|
Socket socket = delegate.createSocket(host, port); |
||||
|
return wrapSocket(socket); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { |
||||
|
Socket socket = delegate.createSocket(host, port, localHost, localPort); |
||||
|
return wrapSocket(socket); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Socket createSocket(InetAddress host, int port) throws IOException { |
||||
|
Socket socket = delegate.createSocket(host, port); |
||||
|
return wrapSocket(socket); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { |
||||
|
Socket socket = delegate.createSocket(address, port, localAddress, localPort); |
||||
|
return wrapSocket(socket); |
||||
|
} |
||||
|
|
||||
|
// 关键:包装 Socket,确保使用我们的 TrustManager 逻辑
|
||||
|
private Socket wrapSocket(Socket socket) { |
||||
|
if (socket instanceof SSLSocket) { |
||||
|
// 可在此处强制设置 TLS 版本等参数(如 Android 14 要求的 TLSv1.2+)
|
||||
|
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.2", "TLSv1.3"}); |
||||
|
} |
||||
|
return socket; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue