给httpTts添加登录功能

pull/1336/head
gedoor 4 years ago
parent 40773b6244
commit 287096affa
  1. 1513
      app/schemas/io.legado.app.data.AppDatabase/41.json
  2. 2
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  3. 13
      app/src/main/java/io/legado/app/data/DatabaseMigrations.kt
  4. 22
      app/src/main/java/io/legado/app/data/entities/BaseSource.kt
  5. 4
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  6. 38
      app/src/main/java/io/legado/app/data/entities/HttpTTS.kt
  7. 4
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  8. 2
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  9. 10
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  10. 2
      app/src/main/java/io/legado/app/ui/login/RuleUiLoginDialog.kt
  11. 6
      app/src/main/java/io/legado/app/ui/login/WebViewLoginFragment.kt

File diff suppressed because it is too large Load Diff

@ -16,7 +16,7 @@ val appDb by lazy {
} }
@Database( @Database(
version = 40, version = 41,
exportSchema = true, exportSchema = true,
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class,
ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class,

@ -36,7 +36,8 @@ object DatabaseMigrations {
migration_36_37, migration_36_37,
migration_37_38, migration_37_38,
migration_38_39, migration_38_39,
migration_39_40 migration_39_40,
migration_40_41
) )
} }
@ -318,4 +319,14 @@ object DatabaseMigrations {
database.execSQL("ALTER TABLE `chapters` ADD `isPay` INTEGER NOT NULL DEFAULT 0") database.execSQL("ALTER TABLE `chapters` ADD `isPay` INTEGER NOT NULL DEFAULT 0")
} }
} }
private val migration_40_41 = object : Migration(40, 41) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE `httpTTS` ADD `loginUrl` TEXT")
database.execSQL("ALTER TABLE `httpTTS` ADD `loginUi` TEXT")
database.execSQL("ALTER TABLE `httpTTS` ADD `loginCheckJs` TEXT")
database.execSQL("ALTER TABLE `httpTTS` ADD `header` TEXT")
database.execSQL("ALTER TABLE `httpTTS` ADD `concurrentRate` TEXT")
}
}
} }

@ -24,9 +24,9 @@ interface BaseSource : JsExtensions {
var loginUi: List<RowUi>? // 登录UI var loginUi: List<RowUi>? // 登录UI
var header: String? // 请求头 var header: String? // 请求头
fun getName(): String fun getTag(): String
fun getStoreUrl(): String fun getKey(): String
fun getLoginJs(): String? { fun getLoginJs(): String? {
val loginJs = loginUrl val loginJs = loginUrl
@ -68,7 +68,7 @@ interface BaseSource : JsExtensions {
* 获取用于登录的头部信息 * 获取用于登录的头部信息
*/ */
fun getLoginHeader(): String? { fun getLoginHeader(): String? {
return CacheManager.get("loginHeader_${getStoreUrl()}") return CacheManager.get("loginHeader_${getKey()}")
} }
fun getLoginHeaderMap(): Map<String, String>? { fun getLoginHeaderMap(): Map<String, String>? {
@ -80,7 +80,7 @@ interface BaseSource : JsExtensions {
* 保存登录头部信息,map格式,访问时自动添加 * 保存登录头部信息,map格式,访问时自动添加
*/ */
fun putLoginHeader(header: String) { fun putLoginHeader(header: String) {
CacheManager.put("loginHeader_${getStoreUrl()}", header) CacheManager.put("loginHeader_${getKey()}", header)
} }
/** /**
@ -90,7 +90,7 @@ interface BaseSource : JsExtensions {
fun getLoginInfo(): String? { fun getLoginInfo(): String? {
try { try {
val key = AppConst.androidId.encodeToByteArray(0, 8) val key = AppConst.androidId.encodeToByteArray(0, 8)
val cache = CacheManager.get("userInfo_${getStoreUrl()}") ?: return null val cache = CacheManager.get("userInfo_${getKey()}") ?: return null
val encodeBytes = Base64.decode(cache, Base64.DEFAULT) val encodeBytes = Base64.decode(cache, Base64.DEFAULT)
val decodeBytes = EncoderUtils.decryptAES(encodeBytes, key) val decodeBytes = EncoderUtils.decryptAES(encodeBytes, key)
?: return null ?: return null
@ -113,7 +113,7 @@ interface BaseSource : JsExtensions {
val key = (AppConst.androidId).encodeToByteArray(0, 8) val key = (AppConst.androidId).encodeToByteArray(0, 8)
val encodeBytes = EncoderUtils.encryptAES(info.toByteArray(), key) val encodeBytes = EncoderUtils.encryptAES(info.toByteArray(), key)
val encodeStr = Base64.encodeToString(encodeBytes, Base64.DEFAULT) val encodeStr = Base64.encodeToString(encodeBytes, Base64.DEFAULT)
CacheManager.put("userInfo_${getStoreUrl()}", encodeStr) CacheManager.put("userInfo_${getKey()}", encodeStr)
true true
} catch (e: Exception) { } catch (e: Exception) {
e.printOnDebug() e.printOnDebug()
@ -122,19 +122,19 @@ interface BaseSource : JsExtensions {
} }
fun removeLoginInfo() { fun removeLoginInfo() {
CacheManager.delete("userInfo_${getStoreUrl()}") CacheManager.delete("userInfo_${getKey()}")
} }
fun setVariable(variable: String?) { fun setVariable(variable: String?) {
if (variable != null) { if (variable != null) {
CacheManager.put("sourceVariable_${getStoreUrl()}", variable) CacheManager.put("sourceVariable_${getKey()}", variable)
} else { } else {
CacheManager.delete("sourceVariable_${getStoreUrl()}") CacheManager.delete("sourceVariable_${getKey()}")
} }
} }
fun getVariable(): String? { fun getVariable(): String? {
return CacheManager.get("sourceVariable_${getStoreUrl()}") return CacheManager.get("sourceVariable_${getKey()}")
} }
/** /**
@ -145,7 +145,7 @@ interface BaseSource : JsExtensions {
val bindings = SimpleBindings() val bindings = SimpleBindings()
bindings["java"] = this bindings["java"] = this
bindings["source"] = this bindings["source"] = this
bindings["baseUrl"] = getStoreUrl() bindings["baseUrl"] = getKey()
bindings["cookie"] = CookieStore bindings["cookie"] = CookieStore
bindings["cache"] = CacheManager bindings["cache"] = CacheManager
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings)

@ -44,11 +44,11 @@ data class BookSource(
var ruleContent: ContentRule? = null // 正文页规则 var ruleContent: ContentRule? = null // 正文页规则
) : Parcelable, BaseSource { ) : Parcelable, BaseSource {
override fun getName(): String { override fun getTag(): String {
return bookSourceName return bookSourceName
} }
override fun getStoreUrl(): String { override fun getKey(): String {
return bookSourceUrl return bookSourceUrl
} }

@ -2,11 +2,45 @@ package io.legado.app.data.entities
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import io.legado.app.data.entities.rule.RowUi
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray
@TypeConverters(HttpTTS.Converters::class)
@Entity(tableName = "httpTTS") @Entity(tableName = "httpTTS")
data class HttpTTS( data class HttpTTS(
@PrimaryKey @PrimaryKey
val id: Long = System.currentTimeMillis(), val id: Long = System.currentTimeMillis(),
var name: String = "", var name: String = "",
var url: String = "" var url: String = "",
) override var concurrentRate: String? = null,
override var loginUrl: String? = null,
override var loginUi: List<RowUi>? = null,
override var header: String? = null,
var loginCheckJs: String? = null,
) : BaseSource {
override fun getTag(): String {
return name
}
override fun getKey(): String {
return md5Encode(url)
}
override fun getSource(): BaseSource {
return this
}
class Converters {
@TypeConverter
fun loginUiRuleToString(loginUi: List<RowUi>?): String = GSON.toJson(loginUi)
@TypeConverter
fun stringToLoginRule(json: String?): List<RowUi>? = GSON.fromJsonArray(json)
}
}

@ -45,11 +45,11 @@ data class RssSource(
var customOrder: Int = 0 var customOrder: Int = 0
) : Parcelable, BaseSource { ) : Parcelable, BaseSource {
override fun getName(): String { override fun getTag(): String {
return sourceName return sourceName
} }
override fun getStoreUrl(): String { override fun getKey(): String {
return sourceUrl return sourceUrl
} }

@ -457,7 +457,7 @@ interface JsExtensions {
*/ */
fun log(msg: String): String { fun log(msg: String): String {
getSource()?.let { getSource()?.let {
Debug.log(it.getStoreUrl(), msg) Debug.log(it.getKey(), msg)
} ?: Debug.log(msg) } ?: Debug.log(msg)
return msg return msg
} }

@ -276,9 +276,9 @@ class AnalyzeUrl(
if (concurrentRate.isNullOrEmpty()) { if (concurrentRate.isNullOrEmpty()) {
return return
} }
val fetchRecord = accessTime[source.getStoreUrl()] val fetchRecord = accessTime[source.getKey()]
if (fetchRecord == null) { if (fetchRecord == null) {
accessTime[source.getStoreUrl()] = FetchRecord(System.currentTimeMillis(), 1) accessTime[source.getKey()] = FetchRecord(System.currentTimeMillis(), 1)
return return
} }
val waitTime = synchronized(fetchRecord) { val waitTime = synchronized(fetchRecord) {
@ -328,7 +328,7 @@ class AnalyzeUrl(
return StrResponse(url, StringUtils.byteToHexString(getByteArray())) return StrResponse(url, StringUtils.byteToHexString(getByteArray()))
} }
judgmentConcurrent() judgmentConcurrent()
setCookie(source?.getStoreUrl()) setCookie(source?.getKey())
if (useWebView) { if (useWebView) {
val params = AjaxWebView.AjaxParams(url) val params = AjaxWebView.AjaxParams(url)
params.headerMap = headerMap params.headerMap = headerMap
@ -336,7 +336,7 @@ class AnalyzeUrl(
params.javaScript = webJs ?: jsStr params.javaScript = webJs ?: jsStr
params.sourceRegex = sourceRegex params.sourceRegex = sourceRegex
params.postData = body?.toByteArray() params.postData = body?.toByteArray()
params.tag = source?.getStoreUrl() params.tag = source?.getKey()
return getWebViewSrc(params) return getWebViewSrc(params)
} }
return getProxyClient(proxy).newCallStrResponse(retry) { return getProxyClient(proxy).newCallStrResponse(retry) {
@ -360,7 +360,7 @@ class AnalyzeUrl(
*/ */
suspend fun getByteArray(): ByteArray { suspend fun getByteArray(): ByteArray {
judgmentConcurrent() judgmentConcurrent()
setCookie(source?.getStoreUrl()) setCookie(source?.getKey())
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
return getProxyClient(proxy).newCall(retry) { return getProxyClient(proxy).newCall(retry) {
addHeaders(headerMap) addHeaders(headerMap)

@ -48,7 +48,7 @@ class RuleUiLoginDialog : BaseDialogFragment() {
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
binding.toolBar.setBackgroundColor(primaryColor) binding.toolBar.setBackgroundColor(primaryColor)
val source = viewModel.source ?: return val source = viewModel.source ?: return
binding.toolBar.title = getString(R.string.login_source, source.getName()) binding.toolBar.title = getString(R.string.login_source, source.getTag())
val loginInfo = source.getLoginInfoMap() val loginInfo = source.getLoginInfoMap()
val loginUi = source.loginUi val loginUi = source.loginUi
loginUi?.forEachIndexed { index, rowUi -> loginUi?.forEachIndexed { index, rowUi ->

@ -28,7 +28,7 @@ class WebViewLoginFragment : BaseFragment(R.layout.fragment_web_view_login) {
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
viewModel.source?.let { viewModel.source?.let {
binding.titleBar.title = getString(R.string.login_source, it.getName()) binding.titleBar.title = getString(R.string.login_source, it.getTag())
initWebView(it) initWebView(it)
} }
} }
@ -64,13 +64,13 @@ class WebViewLoginFragment : BaseFragment(R.layout.fragment_web_view_login) {
binding.webView.webViewClient = object : WebViewClient() { binding.webView.webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
val cookie = cookieManager.getCookie(url) val cookie = cookieManager.getCookie(url)
CookieStore.setCookie(source.getStoreUrl(), cookie) CookieStore.setCookie(source.getKey(), cookie)
super.onPageStarted(view, url, favicon) super.onPageStarted(view, url, favicon)
} }
override fun onPageFinished(view: WebView?, url: String?) { override fun onPageFinished(view: WebView?, url: String?) {
val cookie = cookieManager.getCookie(url) val cookie = cookieManager.getCookie(url)
CookieStore.setCookie(source.getStoreUrl(), cookie) CookieStore.setCookie(source.getKey(), cookie)
if (checking) { if (checking) {
activity?.finish() activity?.finish()
} }

Loading…
Cancel
Save