|
@@ -8,55 +8,73 @@ import io.legado.app.help.IntentData
|
|
|
import io.legado.app.ui.association.VerificationCodeActivity
|
|
import io.legado.app.ui.association.VerificationCodeActivity
|
|
|
import io.legado.app.ui.browser.WebViewActivity
|
|
import io.legado.app.ui.browser.WebViewActivity
|
|
|
import io.legado.app.utils.startActivity
|
|
import io.legado.app.utils.startActivity
|
|
|
-import kotlinx.coroutines.runBlocking
|
|
|
|
|
import splitties.init.appCtx
|
|
import splitties.init.appCtx
|
|
|
|
|
+import java.util.concurrent.locks.LockSupport
|
|
|
|
|
+import kotlin.time.Duration.Companion.minutes
|
|
|
|
|
|
|
|
|
|
|
|
|
object SourceVerificationHelp {
|
|
object SourceVerificationHelp {
|
|
|
|
|
|
|
|
- private var key: String = ""
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ private val waitTime = 1.minutes.inWholeNanoseconds
|
|
|
|
|
+
|
|
|
|
|
+ private fun getKey(source: BaseSource) = getKey(source.getKey())
|
|
|
|
|
+ fun getKey(sourceKey: String) = "${sourceKey}_verificationResult"
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* 获取书源验证结果
|
|
* 获取书源验证结果
|
|
|
* 图片验证码 防爬 滑动验证码 点击字符 等等
|
|
* 图片验证码 防爬 滑动验证码 点击字符 等等
|
|
|
*/
|
|
*/
|
|
|
- fun getVerificationResult(source: BaseSource?, url: String, title: String, useBrowser: Boolean): String {
|
|
|
|
|
- source ?: throw NoStackTraceException("getVerificationResult parameter source cannot be null")
|
|
|
|
|
- return runBlocking {
|
|
|
|
|
- key = "${source.getKey()}_verificationResult"
|
|
|
|
|
- CacheManager.delete(key)
|
|
|
|
|
|
|
+ fun getVerificationResult(
|
|
|
|
|
+ source: BaseSource?,
|
|
|
|
|
+ url: String,
|
|
|
|
|
+ title: String,
|
|
|
|
|
+ useBrowser: Boolean
|
|
|
|
|
+ ): String {
|
|
|
|
|
+ source
|
|
|
|
|
+ ?: throw NoStackTraceException("getVerificationResult parameter source cannot be null")
|
|
|
|
|
|
|
|
- if (!useBrowser) {
|
|
|
|
|
- appCtx.startActivity<VerificationCodeActivity> {
|
|
|
|
|
- putExtra("imageUrl", url)
|
|
|
|
|
- putExtra("sourceOrigin", source.getKey())
|
|
|
|
|
- putExtra("sourceName", source.getTag())
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- startBrowser(source, url, title, true)
|
|
|
|
|
|
|
+ val key = getKey(source)
|
|
|
|
|
+ CacheManager.delete(key)
|
|
|
|
|
+
|
|
|
|
|
+ if (!useBrowser) {
|
|
|
|
|
+ appCtx.startActivity<VerificationCodeActivity> {
|
|
|
|
|
+ putExtra("imageUrl", url)
|
|
|
|
|
+ putExtra("sourceOrigin", source.getKey())
|
|
|
|
|
+ putExtra("sourceName", source.getTag())
|
|
|
|
|
+ IntentData.put(key, Thread.currentThread())
|
|
|
}
|
|
}
|
|
|
|
|
+ } else {
|
|
|
|
|
+ startBrowser(source, url, title, true)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var waitUserInput = false
|
|
|
|
|
- while(CacheManager.get(key) == null) {
|
|
|
|
|
- if (!waitUserInput) {
|
|
|
|
|
- AppLog.putDebug("等待返回验证结果...")
|
|
|
|
|
- waitUserInput = true
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ var waitUserInput = false
|
|
|
|
|
+ while (CacheManager.get(key) == null) {
|
|
|
|
|
+ if (!waitUserInput) {
|
|
|
|
|
+ AppLog.putDebug("等待返回验证结果...")
|
|
|
|
|
+ waitUserInput = true
|
|
|
}
|
|
}
|
|
|
- CacheManager.get(key)!!.let {
|
|
|
|
|
- it.ifBlank {
|
|
|
|
|
- throw NoStackTraceException("验证结果为空")
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ LockSupport.parkNanos(this, waitTime)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return CacheManager.get(key)!!.let {
|
|
|
|
|
+ it.ifBlank {
|
|
|
|
|
+ throw NoStackTraceException("验证结果为空")
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 启动内置浏览器
|
|
* 启动内置浏览器
|
|
|
- @param saveResult 保存网页源代码到数据库
|
|
|
|
|
|
|
+ * @param saveResult 保存网页源代码到数据库
|
|
|
*/
|
|
*/
|
|
|
- fun startBrowser(source: BaseSource?, url: String, title: String, saveResult: Boolean? = false) {
|
|
|
|
|
|
|
+ fun startBrowser(
|
|
|
|
|
+ source: BaseSource?,
|
|
|
|
|
+ url: String,
|
|
|
|
|
+ title: String,
|
|
|
|
|
+ saveResult: Boolean? = false
|
|
|
|
|
+ ) {
|
|
|
source ?: throw NoStackTraceException("startBrowser parameter source cannot be null")
|
|
source ?: throw NoStackTraceException("startBrowser parameter source cannot be null")
|
|
|
- key = "${source.getKey()}_verificationResult"
|
|
|
|
|
|
|
+ val key = getKey(source)
|
|
|
appCtx.startActivity<WebViewActivity> {
|
|
appCtx.startActivity<WebViewActivity> {
|
|
|
putExtra("title", title)
|
|
putExtra("title", title)
|
|
|
putExtra("url", url)
|
|
putExtra("url", url)
|
|
@@ -64,10 +82,14 @@ object SourceVerificationHelp {
|
|
|
putExtra("sourceName", source.getTag())
|
|
putExtra("sourceName", source.getTag())
|
|
|
putExtra("sourceVerificationEnable", saveResult)
|
|
putExtra("sourceVerificationEnable", saveResult)
|
|
|
IntentData.put(url, source.getHeaderMap(true))
|
|
IntentData.put(url, source.getHeaderMap(true))
|
|
|
|
|
+ IntentData.put(key, Thread.currentThread())
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fun checkResult() {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ fun checkResult(key: String) {
|
|
|
CacheManager.get(key) ?: CacheManager.putMemory(key, "")
|
|
CacheManager.get(key) ?: CacheManager.putMemory(key, "")
|
|
|
|
|
+ val thread = IntentData.get<Thread>(key)
|
|
|
|
|
+ LockSupport.unpark(thread)
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|