Skip to content

Commit

Permalink
fix(plugin-apipost): 忽略证书验证功能错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed Dec 25, 2022
1 parent 9750f2b commit 3ded7d2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
build/
/.idea
out/
/ToolsFx.properties
/local.properties
ToolsFx.properties
local.properties
/tools/*/
dict/
*.json
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ tasks.withType<Jar>().forEach {

dependencies {
implementation(project(":plugin-lib"))
implementation("org.bouncycastle:bcprov-jdk18on:${rootProject.extra["bouncycastle_version"]}")
api("org.bouncycastle:bcprov-jdk18on:${rootProject.extra["bouncycastle_version"]}")
implementation("com.google.zxing:javase:${rootProject.extra["zxing_version"]}")
api("com.google.code.gson:gson:2.10")
implementation("org.openjdk.nashorn:nashorn-core:15.4")
Expand Down
36 changes: 35 additions & 1 deletion app/src/test/kotlin/me/leon/Ocr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,50 @@ import kotlin.test.*
import me.leon.ext.ocr.BaiduOcr
import me.leon.ext.toBase64
import org.junit.Test
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.X509TrustManager

@Ignore
//@Ignore
class Ocr {

@Test
fun urlOcr() {
fixSsl()
val data = "https://wx1.sinaimg.cn/mw2000/7736d59fly1gzpm3yc7m6j20j80ip755.jpg"
assertTrue(BaiduOcr.ocr(data).contains("你们别看我"))

}
private fun fixSsl() {
val trustManagers =
arrayOf(
object : X509TrustManager {
override fun checkClientTrusted(
chain: Array<out X509Certificate>?,
authType: String?
) {
// nop
}

override fun checkServerTrusted(
chain: Array<out X509Certificate>?,
authType: String?
) {
// nop
}

override fun getAcceptedIssuers(): Array<X509Certificate>? {
return null
}
}
)
val sc = SSLContext.getInstance("TLSv1.2")
sc.init(null, trustManagers, SecureRandom())
HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
HttpsURLConnection.setDefaultHostnameVerifier { _, _ -> true }
}
@Test
fun base64Ocr() {
assertTrue(
Expand Down
2 changes: 1 addition & 1 deletion plugin-apipost/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "me.leon.toolsfx"
version = "1.6.0"
version = "1.6.1"

plugins {
`java-library`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import me.leon.toolsfx.plugin.table.EditingCell
import tornadofx.*

class ApiPostView : PluginFragment("ApiPost") {
override val version = "v1.6.0"
override val date: String = "2022-12-19"
override val version = "v1.6.1"
override val date: String = "2022-12-25"
override val author = "Leon406"
override val description = "ApiPost"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object HttpUrlUtil {
fun verifySSL(enable: Boolean = true) {
val sc = SSLContext.getInstance("TLS")
HttpsURLConnection.setDefaultHostnameVerifier { _, _ -> true }
sc.init(null, if (enable) emptyArray() else arrayOf(ALL_TRUST_MANAGER), null)
sc.init(null, if (enable) null else arrayOf(ALL_TRUST_MANAGER), null)
HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
}

Expand Down
4 changes: 4 additions & 0 deletions plugin-apipost/src/test/kotlin/me/leon/JbLicenseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ class JbLicenseTest {
@Test
fun check() {
checkUrl("https://35.188.104.230").also { println(it) }
HttpUrlUtil.get("https://www.baidu.com/").also { println(it.data.length) }
verifySSL(false)
checkUrl("https://35.188.104.230").also { println(it) }
HttpUrlUtil.get("https://www.baidu.com/").also { println(it.data.length) }
verifySSL(true)
checkUrl("https://35.188.104.230").also { println(it) }
HttpUrlUtil.get("https://www.baidu.com/").also { println(it.data.length) }
verifySSL(false)
checkUrl("https://35.188.104.230").also { println(it) }
HttpUrlUtil.get("https://www.baidu.com/").also { println(it.data.length) }
}
}
3 changes: 3 additions & 0 deletions plugin-apipost/src/test/kotlin/me/leon/plugin/HttpTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.io.File
import java.util.Base64
import me.leon.toolsfx.plugin.net.*
import org.junit.Test
import java.security.Security

class HttpTest {

Expand Down Expand Up @@ -163,6 +164,8 @@ class HttpTest {

@Test
fun cert() {

HttpUrlUtil.verifySSL(true)
TrustManager.parseFromCertification("$httpConfigPath/baidu.cer")
HttpUrlUtil.get("https://www.baidu.com")
// error cer
Expand Down

0 comments on commit 3ded7d2

Please sign in to comment.