Skip to content

Commit

Permalink
perf: change safe remote
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jan 26, 2024
1 parent e8b342c commit e06ebbb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
9 changes: 9 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
"未知来源"
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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/",
)
Expand All @@ -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/"
)))
}

0 comments on commit e06ebbb

Please sign in to comment.