





















@@ -13,6 +13,7 @@ import java.security.SecureRandom
1313import java.security.cert.CertificateException
1414import java.security.cert.X509Certificate
1515import java.util.Locale
16+import java.util.concurrent.atomic.AtomicReference
1617import javax.net.ssl.HttpsURLConnection
1718import javax.net.ssl.HostnameVerifier
1819import javax.net.ssl.SSLContext
@@ -106,18 +107,25 @@ suspend fun probeGatewayTlsFingerprint(
106107if (port !in 1..65535) return GatewayTlsProbeResult(failure = GatewayTlsProbeFailure.ENDPOINT_UNREACHABLE)
107108108109return withContext(Dispatchers.IO) {
109-val trustAll =
110- @SuppressLint("CustomX509TrustManager", "TrustAllX509TrustManager")
110+val fingerprintRef = AtomicReference<String?>(null)
111+val probeTrustManager =
112+ @SuppressLint("CustomX509TrustManager")
111113object : X509TrustManager {
112- @SuppressLint("TrustAllX509TrustManager")
113-override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
114- @SuppressLint("TrustAllX509TrustManager")
115-override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
114+override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
115+throw CertificateException("gateway TLS probe does not accept client certificates")
116+ }
117+118+override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
119+if (chain.isEmpty()) throw CertificateException("empty certificate chain")
120+ fingerprintRef.set(sha256Hex(chain[0].encoded))
121+throw CertificateException("gateway TLS probe captured fingerprint")
122+ }
123+116124override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
117125 }
118126119127val context = SSLContext.getInstance("TLS")
120- context.init(null, arrayOf(trustAll), SecureRandom())
128+ context.init(null, arrayOf(probeTrustManager), SecureRandom())
121129122130val socket = (context.socketFactory.createSocket() as SSLSocket)
123131try {
@@ -141,6 +149,7 @@ suspend fun probeGatewayTlsFingerprint(
141149?: return@withContext GatewayTlsProbeResult(failure = GatewayTlsProbeFailure.TLS_UNAVAILABLE)
142150GatewayTlsProbeResult(fingerprintSha256 = sha256Hex(cert.encoded))
143151 } catch (err: Throwable) {
152+ fingerprintRef.get()?.let { return@withContext GatewayTlsProbeResult(fingerprintSha256 = it) }
144153val failure =
145154when (err) {
146155is SSLException,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。