From e06ebbbb2fcd88ff6b70752dac40db30571b901d Mon Sep 17 00:00:00 2001 From: lisonge Date: Fri, 26 Jan 2024 11:37:51 +0800 Subject: [PATCH] perf: change safe remote --- .../main/kotlin/li/songe/gkd/data/SubsItem.kt | 9 +++++++++ .../li/songe/gkd/ui/component/SubsItemCard.kt | 3 +-- .../main/kotlin/li/songe/gkd/util/Constants.kt | 17 ++++++++--------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt b/app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt index 1301978dd..a0a29201e 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt @@ -12,6 +12,7 @@ import androidx.room.Update import kotlinx.coroutines.flow.Flow import li.songe.gkd.db.DbSet import li.songe.gkd.util.deleteSubscription +import li.songe.gkd.util.isSafeUrl @Entity( tableName = "subs_item", @@ -28,6 +29,14 @@ data class SubsItem( ) { + val isSafeRemote by lazy { + if (updateUrl != null) { + isSafeUrl(updateUrl) + } else { + false + } + } + suspend fun removeAssets() { deleteSubscription(id) DbSet.subsItemDao.delete(this) diff --git a/app/src/main/kotlin/li/songe/gkd/ui/component/SubsItemCard.kt b/app/src/main/kotlin/li/songe/gkd/ui/component/SubsItemCard.kt index 288d605b7..0f8af6393 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/component/SubsItemCard.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/component/SubsItemCard.kt @@ -17,7 +17,6 @@ import androidx.compose.ui.unit.sp import li.songe.gkd.data.RawSubscription import li.songe.gkd.data.SubsItem import li.songe.gkd.util.formatTimeAgo -import li.songe.gkd.util.isSafeUrl @Composable @@ -45,7 +44,7 @@ fun SubsItemCard( val sourceText = if (subsItem.id < 0) { "本地来源" - } else if (subsItem.updateUrl != null && isSafeUrl(subsItem.updateUrl)) { + } else if (subsItem.isSafeRemote) { "可信来源" } else { "未知来源" diff --git a/app/src/main/kotlin/li/songe/gkd/util/Constants.kt b/app/src/main/kotlin/li/songe/gkd/util/Constants.kt index b0281fba9..73fbde0e8 100644 --- a/app/src/main/kotlin/li/songe/gkd/util/Constants.kt +++ b/app/src/main/kotlin/li/songe/gkd/util/Constants.kt @@ -2,7 +2,6 @@ package li.songe.gkd.util import android.webkit.URLUtil import io.ktor.http.Url -import io.ktor.http.fullPath import li.songe.gkd.BuildConfig const val VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION" @@ -29,6 +28,9 @@ val GIT_COMMIT_URL = if (BuildConfig.GIT_COMMIT_ID != null) { private val safeRemoteBaseUrls = arrayOf( "https://registry.npmmirror.com/@gkd-kit/", + "https://unpkg.com/@gkd-kit/", + "https://www.unpkg.com/@gkd-kit/", + "https://github.com/gkd-kit/", "https://raw.githubusercontent.com/gkd-kit/", ) @@ -43,12 +45,9 @@ fun isSafeUrl(url: String): Boolean { } catch (e: Exception) { return false } - if (u.host == "gkd.li" || u.host.endsWith(".gkd.li")) { - return true - } else if (u.host.endsWith(".jsdelivr.net") && u.fullPath.startsWith("/npm/@gkd-kit/")) { - return true - } else if ((u.host == "unpkg.com" || u.host.endsWith(".unpkg.com")) && u.fullPath.startsWith("/@gkd-kit/")) { - return true - } - return false + return if (u.host == "s.gkd.li") { + true + } else (u.host.endsWith(".jsdelivr.net") && (u.encodedPath.startsWith("/npm/@gkd-kit/") || u.encodedPath.startsWith( + "/gh/gkd-kit/" + ))) }