Merge pull request #1876 from Xwite/master

cookiejar选项
This commit is contained in:
kunfei 2022-05-13 08:11:46 +08:00 committed by GitHub
commit 24f9c7e193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 53 additions and 6 deletions

View File

@ -10,7 +10,8 @@
@Json: json规则,直接写时以$.开头可省略@Json
: regex规则,不可省略,只可以用在书籍列表和目录列表
```
* CookieJar
> 启用后会自动保存每次返回头中的Set-Cookie中的值适用于验证码图片一类需要session的网站
* 登录UI
> 不使用内置webView登录网站需要使用`登录URL`规则实现登录逻辑,可使用`登录检查JS`检查登录结果
```

View File

@ -11,6 +11,11 @@
* 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。
* 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源!
**2022/05/13**
* 书源编辑添加cookieJar选项
**2022/05/11**
* 修复替换报错的bug

View File

@ -20,7 +20,7 @@ val appDb by lazy {
}
@Database(
version = 48,
version = 49,
exportSchema = true,
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class,
ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class,
@ -32,7 +32,8 @@ val appDb by lazy {
AutoMigration(from = 44, to = 45),
AutoMigration(from = 45, to = 46),
AutoMigration(from = 46, to = 47),
AutoMigration(from = 47, to = 48)
AutoMigration(from = 47, to = 48),
AutoMigration(from = 48, to = 49)
]
)
abstract class AppDatabase : RoomDatabase() {

View File

@ -21,6 +21,7 @@ interface BaseSource : JsExtensions {
var loginUrl: String? // 登录地址
var loginUi: String? // 登录UI
var header: String? // 请求头
var enabledCookieJar: Boolean? //启用cookieJar
fun getTag(): String

View File

@ -38,6 +38,9 @@ data class BookSource(
var enabled: Boolean = true,
// 启用发现
var enabledExplore: Boolean = true,
// 启用okhttp CookieJAr 自动保存每次请求的cookie
@ColumnInfo(defaultValue = "0")
override var enabledCookieJar: Boolean? = false,
// 并发率
override var concurrentRate: String? = null,
// 请求头

View File

@ -24,6 +24,8 @@ data class HttpTTS(
override var loginUrl: String? = null,
override var loginUi: String? = null,
override var header: String? = null,
@ColumnInfo(defaultValue = "0")
override var enabledCookieJar: Boolean? = false,
var loginCheckJs: String? = null,
@ColumnInfo(defaultValue = "0")
var lastUpdateTime: Long = System.currentTimeMillis()

View File

@ -1,6 +1,7 @@
package io.legado.app.data.entities
import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
@ -19,6 +20,8 @@ data class RssSource(
var sourceGroup: String? = null,
var sourceComment: String? = null,
var enabled: Boolean = true,
@ColumnInfo(defaultValue = "0")
override var enabledCookieJar: Boolean? = false,
override var concurrentRate: String? = null, //并发率
override var header: String? = null, // 请求头
override var loginUrl: String? = null, // 登录地址

View File

@ -1,9 +1,11 @@
package io.legado.app.help.http
import io.legado.app.constant.AppConst
import io.legado.app.help.CacheManager
import io.legado.app.help.config.AppConfig
import io.legado.app.help.http.cronet.CronetInterceptor
import io.legado.app.help.http.cronet.CronetLoader
import io.legado.app.utils.NetworkUtils
import okhttp3.*
import java.net.InetSocketAddress
import java.net.Proxy
@ -23,7 +25,10 @@ val cookieJar by lazy {
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookies.forEach {
CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}")
//CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}")
//临时保存 书源启用cookie选项再添加到数据库
val domain = NetworkUtils.getSubDomain(url.toString())
CacheManager.putMemory("${domain}_cookieJar", "${it.name}=${it.value}")
}
}

View File

@ -150,6 +150,7 @@ object BookCover {
override var loginUrl: String? = null,
override var loginUi: String? = null,
override var header: String? = null,
override var enabledCookieJar: Boolean? = false,
) : BaseSource {
override fun getTag(): String {

View File

@ -69,6 +69,7 @@ class AnalyzeUrl(
private var retry: Int = 0
private var useWebView: Boolean = false
private var webJs: String? = null
private val enabledCookieJar = source?.enabledCookieJar ?: false
init {
if (!mUrl.isDataUrl()) {
@ -519,11 +520,19 @@ class AnalyzeUrl(
}
/**
*设置cookie urlOption的优先级大于书源保存的cookie
*设置cookie 优先级
* urlOption临时cookie > 数据库cookie = okhttp CookieJar保存在内存中的cookie
*@param tag 书源url 缺省为传入的url
*/
private fun setCookie(tag: String?) {
val cookie = CookieStore.getCookie(tag ?: url)
val domain = NetworkUtils.getSubDomain(tag ?: url)
//书源启用保存cookie时 添加内存中的cookie到数据库
if (enabledCookieJar) {
CacheManager.getFromMemory("${domain}_cookieJar")?.let {
CookieStore.replaceCookie(domain, it)
}
}
val cookie = CookieStore.getCookie(domain)
if (cookie.isNotEmpty()) {
val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "")

View File

@ -190,6 +190,7 @@ class BookSourceEditActivity :
source?.let {
binding.cbIsEnable.isChecked = it.enabled
binding.cbIsEnableFind.isChecked = it.enabledExplore
binding.cbIsEnableCookie.isChecked = it.enabledCookieJar ?: false
binding.spType.setSelection(
when (it.bookSourceType) {
BookType.file -> 3
@ -296,6 +297,7 @@ class BookSourceEditActivity :
val source = viewModel.bookSource?.copy() ?: BookSource()
source.enabled = binding.cbIsEnable.isChecked
source.enabledExplore = binding.cbIsEnableFind.isChecked
source.enabledCookieJar = binding.cbIsEnableCookie.isChecked
source.bookSourceType = when (binding.spType.selectedItemPosition) {
3 -> BookType.file
2 -> BookType.image

View File

@ -33,6 +33,13 @@
android:checked="true"
android:text="@string/discovery" />
<io.legado.app.lib.theme.view.ThemeCheckBox
android:id="@+id/cb_is_enable_cookie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/auto_save_cookie" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View File

@ -978,5 +978,6 @@
<string name="import_tts">导入TTS</string>
<string name="import_theme">导入主题</string>
<string name="import_txt_toc_rule">导入txt目录规则</string>
<string name="auto_save_cookie">CookieJar</string>
<!-- string end -->
</resources>

View File

@ -981,5 +981,6 @@
<string name="import_tts">导入TTS</string>
<string name="import_theme">导入主题</string>
<string name="import_txt_toc_rule">导入txt目录规则</string>
<string name="auto_save_cookie">CookieJar</string>
<!-- string end -->
</resources>

View File

@ -981,5 +981,6 @@
<string name="import_tts">导入TTS</string>
<string name="import_theme">导入主题</string>
<string name="import_txt_toc_rule">导入txt目录规则</string>
<string name="auto_save_cookie">CookieJar</string>
<!-- string end -->
</resources>

View File

@ -978,5 +978,6 @@
<string name="import_tts">导入TTS</string>
<string name="import_theme">导入主题</string>
<string name="import_txt_toc_rule">导入txt目录规则</string>
<string name="auto_save_cookie">CookieJar</string>
<!-- string end -->
</resources>

View File

@ -980,5 +980,6 @@
<string name="import_tts">导入TTS</string>
<string name="import_theme">导入主题</string>
<string name="import_txt_toc_rule">导入txt目录规则</string>
<string name="auto_save_cookie">CookieJar</string>
<!-- string end -->
</resources>

View File

@ -980,5 +980,6 @@
<string name="import_tts">导入TTS</string>
<string name="import_theme">导入主题</string>
<string name="import_txt_toc_rule">导入txt目录规则</string>
<string name="auto_save_cookie">CookieJar</string>
<!-- string end -->
</resources>

View File

@ -981,5 +981,6 @@
<string name="import_tts">导入TTS</string>
<string name="import_theme">导入主题</string>
<string name="import_txt_toc_rule">导入txt目录规则</string>
<string name="auto_save_cookie">CookieJar</string>
<!-- string end -->
</resources>