|
| 1 | +package features.subscription |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | +import androidx.core.net.toUri |
| 6 | +import app.AppState |
| 7 | +import app.SubscriptionGroupState |
| 8 | +import data.AndroidAppStateStore |
| 9 | +import features.proxy.server.usecase.ProxyServerListSubscriptionUpdateResult |
| 10 | +import features.proxy.server.usecase.withUpdatedSubscriptionServers |
| 11 | +import features.subscription.usecase.toSubscriptionFetchOptions |
| 12 | +import features.subscription.usecase.updateSubscriptions |
| 13 | + |
| 14 | +internal data class V2rayNgInstallConfig( |
| 15 | + val name: String, |
| 16 | + val url: String, |
| 17 | +) |
| 18 | + |
| 19 | +internal class SubscriptionInstallConfigUseCase( |
| 20 | + private val stateStore: AndroidAppStateStore, |
| 21 | + private val subscriptionFetchUseCase: SubscriptionFetchUseCase, |
| 22 | +) { |
| 23 | + suspend fun install(config: V2rayNgInstallConfig): ProxyServerListSubscriptionUpdateResult { |
| 24 | + val group = stateStore.addSubscriptionGroup(config) |
| 25 | + val result = updateSubscriptions( |
| 26 | + groups = listOf(group), |
| 27 | + subscriptionFetchUseCase = subscriptionFetchUseCase, |
| 28 | + fetchOptions = { stateStore.state.value.toSubscriptionFetchOptions(it) }, |
| 29 | + ) |
| 30 | + if (result.updates.isNotEmpty()) { |
| 31 | + stateStore.update { state -> |
| 32 | + state.withUpdatedSubscriptionServers( |
| 33 | + updates = result.updates, |
| 34 | + updatedAtMillis = result.updatedAtMillis, |
| 35 | + ) |
| 36 | + } |
| 37 | + } |
| 38 | + return result |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +internal fun Intent.toV2rayNgInstallConfigOrNull(): V2rayNgInstallConfig? { |
| 43 | + if (action != Intent.ACTION_VIEW) return null |
| 44 | + return data?.toV2rayNgInstallConfigOrNull() |
| 45 | +} |
| 46 | + |
| 47 | +internal fun Uri.isV2rayNgInstallConfigUri(): Boolean { |
| 48 | + return scheme.equals(V2rayNgScheme, ignoreCase = true) && |
| 49 | + isHierarchical && |
| 50 | + host.equals(V2rayNgInstallConfigHost, ignoreCase = true) |
| 51 | +} |
| 52 | + |
| 53 | +private fun Uri.toV2rayNgInstallConfigOrNull(): V2rayNgInstallConfig? { |
| 54 | + if (!isV2rayNgInstallConfigUri()) return null |
| 55 | + val name = getQueryParameter("name")?.trim().orEmpty() |
| 56 | + val url = getQueryParameter("url")?.trim().orEmpty() |
| 57 | + if (name.isBlank() || !url.isValidSubscriptionUrl()) return null |
| 58 | + return V2rayNgInstallConfig( |
| 59 | + name = name, |
| 60 | + url = url, |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +private fun AndroidAppStateStore.addSubscriptionGroup(config: V2rayNgInstallConfig): SubscriptionGroupState { |
| 65 | + var savedGroup: SubscriptionGroupState? = null |
| 66 | + update { state -> |
| 67 | + val group = state.newSubscriptionGroup(config) |
| 68 | + savedGroup = group |
| 69 | + state.copy( |
| 70 | + subscriptionGroups = state.subscriptionGroups + group, |
| 71 | + nextSubscriptionGroupId = state.nextSubscriptionGroupId + 1, |
| 72 | + ) |
| 73 | + } |
| 74 | + return checkNotNull(savedGroup) |
| 75 | +} |
| 76 | + |
| 77 | +private fun AppState.newSubscriptionGroup(config: V2rayNgInstallConfig): SubscriptionGroupState { |
| 78 | + return SubscriptionGroupState( |
| 79 | + id = nextSubscriptionGroupId, |
| 80 | + name = config.name, |
| 81 | + url = config.url, |
| 82 | + userAgent = DefaultSubscriptionUserAgent, |
| 83 | + updateInterval = "", |
| 84 | + updateViaProxy = false, |
| 85 | + enabled = true, |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +private fun String.isValidSubscriptionUrl(): Boolean { |
| 90 | + val uri = runCatching { toUri() }.getOrNull() ?: return false |
| 91 | + val scheme = uri.scheme?.lowercase() ?: return false |
| 92 | + return uri.isHierarchical && |
| 93 | + scheme in SubscriptionUrlSchemes && |
| 94 | + uri.host?.isNotBlank() == true |
| 95 | +} |
| 96 | + |
| 97 | +private const val V2rayNgScheme = "v2rayng" |
| 98 | +private const val V2rayNgInstallConfigHost = "install-config" |
| 99 | +private val SubscriptionUrlSchemes = setOf("http", "https") |
0 commit comments