Skip to content

Commit

Permalink
Xfani:add new preference to ignore ssl verify issue. (#319)
Browse files Browse the repository at this point in the history
* xfani: format source info for aniyomi

Tmporary disable ssl check for site certificate issue.

* xfani: add preference to ignore ssl verify issue.
  • Loading branch information
AlphaBoom authored Oct 21, 2024
1 parent 80f3773 commit 4dc9e2e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/zh/xfani/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
extName = 'Xfani'
extClass = '.Xfani'
extVersionCode = 1
extVersionCode = 2
}

apply from: "$rootDir/common.gradle"
103 changes: 85 additions & 18 deletions src/zh/xfani/src/eu/kanade/tachiyomi/animeextension/zh/xfani/Xfani.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package eu.kanade.tachiyomi.animeextension.zh.xfani

import android.annotation.SuppressLint
import android.app.Application
import android.content.SharedPreferences
import android.widget.Toast
import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreferenceCompat
import eu.kanade.tachiyomi.animesource.ConfigurableAnimeSource
import eu.kanade.tachiyomi.animesource.model.AnimeFilter
import eu.kanade.tachiyomi.animesource.model.AnimeFilterList
Expand All @@ -19,12 +22,20 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLHandshakeException
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

class Xfani : AnimeHttpSource(), ConfigurableAnimeSource {
override val baseUrl: String
Expand All @@ -41,11 +52,53 @@ class Xfani : AnimeHttpSource(), ConfigurableAnimeSource {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
private val numberRegex = Regex("\\d+")
private fun OkHttpClient.Builder.ignoreAllSSLErrors(): OkHttpClient.Builder {
val naiveTrustManager =
@SuppressLint("CustomX509TrustManager")
object : X509TrustManager {
override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) =
Unit

override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) =
Unit
}

val insecureSocketFactory = SSLContext.getInstance("TLSv1.2").apply {
val trustAllCerts = arrayOf<TrustManager>(naiveTrustManager)
init(null, trustAllCerts, SecureRandom())
}.socketFactory

sslSocketFactory(insecureSocketFactory, naiveTrustManager)
hostnameVerifier { _, _ -> true }
return this
}

override val client: OkHttpClient
get() = if (preferences.getBoolean(PREF_KEY_IGNORE_SSL_ERROR, false)) {
network.client.newBuilder().ignoreAllSSLErrors().build()
} else {
network.client.newBuilder().addInterceptor(::checkSSLErrorInterceptor).build()
}

private val selectedVideoSource
get() = preferences.getString(PREF_KEY_VIDEO_SOURCE, DEFAULT_VIDEO_SOURCE)!!.toInt()

override fun animeDetailsParse(response: Response): SAnime = SAnime.create()
private fun checkSSLErrorInterceptor(chain: Interceptor.Chain): Response {
try {
return chain.proceed(chain.request())
} catch (e: SSLHandshakeException) {
throw SSLHandshakeException("SSL证书验证异常,可以尝试在设置中忽略SSL验证问题。")
}
}

override fun animeDetailsParse(response: Response): SAnime {
val jsoup = response.asJsoup()
return SAnime.create().apply {
description = jsoup.select("#height_limit.text").text()
title = jsoup.select(".slide-info-title").text()
}
}

override fun episodeListParse(response: Response): List<SEpisode> {
val jsoup = response.asJsoup()
Expand All @@ -61,7 +114,7 @@ class Xfani : AnimeHttpSource(), ConfigurableAnimeSource {
url = it.attr("href")
episode_number = numberRegex.find(name)?.value?.toFloat() ?: -1F
}
}
}.sortedByDescending { it.episode_number }
}

override fun videoListParse(response: Response): List<Video> {
Expand Down Expand Up @@ -92,9 +145,9 @@ class Xfani : AnimeHttpSource(), ConfigurableAnimeSource {
url = "/bangumi/${it.vodId}.html"
thumbnail_url = it.vodPicThumb.ifEmpty { it.vodPic }
title = it.vodName
author = it.vodActor
author = it.vodActor.replace(",,,", "")
description = it.vodBlurb
genre = it.vodClass
genre = it.vodClass.replace(",", ", ")
}
}
return AnimesPage(
Expand Down Expand Up @@ -184,24 +237,38 @@ class Xfani : AnimeHttpSource(), ConfigurableAnimeSource {
}

override fun setupPreferenceScreen(screen: PreferenceScreen) {
screen.addPreference(
ListPreference(screen.context).apply {
key = PREF_KEY_VIDEO_SOURCE
title = "请设置首选视频源线路"
entries = arrayOf("主线-1", "主线-2", "备用-1")
entryValues = arrayOf("0", "1", "2")
setDefaultValue(DEFAULT_VIDEO_SOURCE)
summary = "当前选择:${entries[selectedVideoSource]}"
setOnPreferenceChangeListener { _, newValue ->
summary = "当前选择 ${entries[(newValue as String).toInt()]}"
true
}
},
)
screen.apply {
addPreference(
ListPreference(screen.context).apply {
key = PREF_KEY_VIDEO_SOURCE
title = "请设置首选视频源线路"
entries = arrayOf("主线-1", "主线-2", "备用-1")
entryValues = arrayOf("0", "1", "2")
setDefaultValue(DEFAULT_VIDEO_SOURCE)
summary = "当前选择:${entries[selectedVideoSource]}"
setOnPreferenceChangeListener { _, newValue ->
summary = "当前选择 ${entries[(newValue as String).toInt()]}"
true
}
},
)
addPreference(
SwitchPreferenceCompat(screen.context).apply {
key = PREF_KEY_IGNORE_SSL_ERROR
title = "忽略SSL证书校验"
setDefaultValue(false)
setOnPreferenceChangeListener { _, _ ->
Toast.makeText(screen.context, "重启应用后生效", Toast.LENGTH_SHORT).show()
true
}
},
)
}
}

companion object {
const val PREF_KEY_VIDEO_SOURCE = "PREF_KEY_VIDEO_SOURCE"
const val PREF_KEY_IGNORE_SSL_ERROR = "PREF_KEY_IGNORE_SSL_ERROR"

const val DEFAULT_VIDEO_SOURCE = "0"

Expand Down

0 comments on commit 4dc9e2e

Please sign in to comment.