mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-16 01:00:17 +01:00
net: close the gaps found reviewing the local network gate
Treat .local names as gated before resolving, because resolution is the operation the missing permission blocks. Gather link addresses from every network so a split-tunnel VPN that falls through to the LAN still prompts, and re-run the root check when the default network changes. Retry the sync after a prompt is granted, and remember addresses that already showed the rationale so the automatic check does not nag after the form declined. Use IpPrefix instead of a hand-written matcher.
This commit is contained in:
@@ -3,8 +3,10 @@ package com.subtracks.data.net
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.net.ConnectivityManager
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.IpPrefix
|
||||||
import android.net.LinkAddress
|
import android.net.LinkAddress
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -17,44 +19,45 @@ private const val LOCAL_NETWORK_PERMISSION_SDK = 37
|
|||||||
class LocalNetworkAccess(
|
class LocalNetworkAccess(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
) {
|
) {
|
||||||
|
private val promptedAddresses = mutableSetOf<String>()
|
||||||
|
|
||||||
suspend fun requiresPermission(address: String): Boolean {
|
suspend fun requiresPermission(address: String): Boolean {
|
||||||
if (Build.VERSION.SDK_INT < LOCAL_NETWORK_PERMISSION_SDK || granted()) return false
|
if (Build.VERSION.SDK_INT < LOCAL_NETWORK_PERMISSION_SDK) return false
|
||||||
|
if (granted()) return false
|
||||||
val host = serverHost(address) ?: return false
|
val host = serverHost(address) ?: return false
|
||||||
|
if (isLocalHostname(host)) return true
|
||||||
val linkAddresses = currentLinkAddresses()
|
val linkAddresses = currentLinkAddresses()
|
||||||
if (linkAddresses.isEmpty()) return false
|
if (linkAddresses.isEmpty()) return false
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
val resolved = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList())
|
val resolved = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList())
|
||||||
resolved.any { candidate ->
|
resolved.any { candidate -> linkAddresses.any { it.contains(candidate) } }
|
||||||
linkAddresses.any { candidate.isWithinPrefix(it.address, it.prefixLength) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun markPrompted(address: String) {
|
||||||
|
promptedAddresses += normalizeServerAddress(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun wasPrompted(address: String): Boolean = normalizeServerAddress(address) in promptedAddresses
|
||||||
|
|
||||||
private fun granted(): Boolean =
|
private fun granted(): Boolean =
|
||||||
ContextCompat.checkSelfPermission(context, LOCAL_NETWORK_PERMISSION) == PackageManager.PERMISSION_GRANTED
|
ContextCompat.checkSelfPermission(context, LOCAL_NETWORK_PERMISSION) == PackageManager.PERMISSION_GRANTED
|
||||||
|
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
private fun currentLinkAddresses(): List<LinkAddress> {
|
private fun currentLinkAddresses(): List<LinkAddress> {
|
||||||
val manager = context.getSystemService(ConnectivityManager::class.java) ?: return emptyList()
|
val manager = context.getSystemService(ConnectivityManager::class.java) ?: return emptyList()
|
||||||
val network = manager.activeNetwork ?: return emptyList()
|
return manager.allNetworks.flatMap { network ->
|
||||||
return manager.getLinkProperties(network)?.linkAddresses.orEmpty()
|
manager.getLinkProperties(network)?.linkAddresses.orEmpty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun InetAddress.isWithinPrefix(
|
internal fun isLocalHostname(host: String): Boolean {
|
||||||
prefix: InetAddress,
|
val normalized = host.lowercase().trimEnd('.')
|
||||||
prefixLength: Int,
|
return normalized == "local" || normalized.endsWith(".local")
|
||||||
): Boolean {
|
|
||||||
val own = address
|
|
||||||
val network = prefix.address
|
|
||||||
if (own.size != network.size || prefixLength !in 0..(own.size * 8)) return false
|
|
||||||
|
|
||||||
val wholeBytes = prefixLength / 8
|
|
||||||
for (index in 0 until wholeBytes) {
|
|
||||||
if (own[index] != network[index]) return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val remainingBits = prefixLength % 8
|
@RequiresApi(LOCAL_NETWORK_PERMISSION_SDK)
|
||||||
if (remainingBits == 0) return true
|
private fun LinkAddress.contains(address: InetAddress): Boolean = IpPrefix(this.address, prefixLength).contains(address)
|
||||||
val mask = (0xFF shl (8 - remainingBits)) and 0xFF
|
|
||||||
return (own[wholeBytes].toInt() and mask) == (network[wholeBytes].toInt() and mask)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ import com.subtracks.data.model.Song
|
|||||||
import com.subtracks.data.repo.DownloadRepository
|
import com.subtracks.data.repo.DownloadRepository
|
||||||
import com.subtracks.data.repo.LibraryRepository
|
import com.subtracks.data.repo.LibraryRepository
|
||||||
import com.subtracks.data.repo.SourceRepository
|
import com.subtracks.data.repo.SourceRepository
|
||||||
|
import com.subtracks.data.sync.SyncManager
|
||||||
import com.subtracks.playback.PlaybackController
|
import com.subtracks.playback.PlaybackController
|
||||||
import com.subtracks.ui.components.AlbumInfoDialog
|
import com.subtracks.ui.components.AlbumInfoDialog
|
||||||
import com.subtracks.ui.components.ArtistChooserSheet
|
import com.subtracks.ui.components.ArtistChooserSheet
|
||||||
@@ -77,6 +78,7 @@ import com.subtracks.ui.components.MenuTarget
|
|||||||
import com.subtracks.ui.components.PlaylistInfoDialog
|
import com.subtracks.ui.components.PlaylistInfoDialog
|
||||||
import com.subtracks.ui.components.SongInfoDialog
|
import com.subtracks.ui.components.SongInfoDialog
|
||||||
import com.subtracks.ui.components.rememberLocalNetworkGate
|
import com.subtracks.ui.components.rememberLocalNetworkGate
|
||||||
|
import com.subtracks.ui.components.rememberNetworkRevision
|
||||||
import com.subtracks.ui.components.statusBarScrim
|
import com.subtracks.ui.components.statusBarScrim
|
||||||
import com.subtracks.ui.downloads.DownloadsRoute
|
import com.subtracks.ui.downloads.DownloadsRoute
|
||||||
import com.subtracks.ui.home.HomeListRequest
|
import com.subtracks.ui.home.HomeListRequest
|
||||||
@@ -201,9 +203,16 @@ internal fun focusPops(
|
|||||||
@Composable
|
@Composable
|
||||||
fun SubtracksRoot(root: RootViewModel = koinViewModel()) {
|
fun SubtracksRoot(root: RootViewModel = koinViewModel()) {
|
||||||
val localNetworkGate = rememberLocalNetworkGate()
|
val localNetworkGate = rememberLocalNetworkGate()
|
||||||
|
val syncManager = koinInject<SyncManager>()
|
||||||
val serverAddress by root.serverAddress.collectAsStateWithLifecycle()
|
val serverAddress by root.serverAddress.collectAsStateWithLifecycle()
|
||||||
LaunchedEffect(serverAddress) {
|
val networkRevision = rememberNetworkRevision()
|
||||||
localNetworkGate.run(address = serverAddress, onGranted = {})
|
LaunchedEffect(serverAddress, networkRevision) {
|
||||||
|
localNetworkGate.run(
|
||||||
|
address = serverAddress,
|
||||||
|
onGranted = { syncManager.requestSync() },
|
||||||
|
onNotNeeded = {},
|
||||||
|
automatic = true,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
when (val hasSource = root.hasSource.collectAsStateWithLifecycle().value) {
|
when (val hasSource = root.hasSource.collectAsStateWithLifecycle().value) {
|
||||||
null -> {
|
null -> {
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
package com.subtracks.ui.components
|
package com.subtracks.ui.components
|
||||||
|
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.LinkProperties
|
||||||
|
import android.net.Network
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.material3.AlertDialog
|
import androidx.compose.material3.AlertDialog
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TextButton
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.DisposableEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableIntStateOf
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
import com.subtracks.R
|
import com.subtracks.R
|
||||||
import com.subtracks.data.net.LOCAL_NETWORK_PERMISSION
|
import com.subtracks.data.net.LOCAL_NETWORK_PERMISSION
|
||||||
import com.subtracks.data.net.LocalNetworkAccess
|
import com.subtracks.data.net.LocalNetworkAccess
|
||||||
@@ -22,22 +29,24 @@ import org.koin.compose.koinInject
|
|||||||
class LocalNetworkGate internal constructor(
|
class LocalNetworkGate internal constructor(
|
||||||
private val scope: CoroutineScope,
|
private val scope: CoroutineScope,
|
||||||
private val checker: LocalNetworkAccess,
|
private val checker: LocalNetworkAccess,
|
||||||
private val requestPermission: (onGranted: () -> Unit, onDenied: () -> Unit) -> Unit,
|
private val requestPermission: (address: String, onGranted: () -> Unit, onDenied: () -> Unit) -> Unit,
|
||||||
) {
|
) {
|
||||||
fun run(
|
fun run(
|
||||||
address: String?,
|
address: String?,
|
||||||
onGranted: () -> Unit,
|
onGranted: () -> Unit,
|
||||||
onDenied: () -> Unit = {},
|
onDenied: () -> Unit = {},
|
||||||
|
onNotNeeded: () -> Unit = onGranted,
|
||||||
|
automatic: Boolean = false,
|
||||||
) {
|
) {
|
||||||
if (address == null) {
|
if (address == null) {
|
||||||
onGranted()
|
onNotNeeded()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
scope.launch {
|
scope.launch {
|
||||||
if (checker.requiresPermission(address)) {
|
if (!checker.requiresPermission(address) || (automatic && checker.wasPrompted(address))) {
|
||||||
requestPermission(onGranted, onDenied)
|
onNotNeeded()
|
||||||
} else {
|
} else {
|
||||||
onGranted()
|
requestPermission(address, onGranted, onDenied)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,9 +74,12 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val gate =
|
val gate =
|
||||||
LocalNetworkGate(scope, checker) { onGranted, onDenied ->
|
LocalNetworkGate(scope, checker) { address, onGranted, onDenied ->
|
||||||
|
if (pending == null) {
|
||||||
|
checker.markPrompted(address)
|
||||||
pending = PendingRequest(onGranted, onDenied)
|
pending = PendingRequest(onGranted, onDenied)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val request = pending
|
val request = pending
|
||||||
if (request != null && !awaitingSystemPrompt) {
|
if (request != null && !awaitingSystemPrompt) {
|
||||||
@@ -102,3 +114,35 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
|||||||
}
|
}
|
||||||
return gate
|
return gate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
internal fun rememberNetworkRevision(): Int {
|
||||||
|
val context = LocalContext.current
|
||||||
|
var revision by remember { mutableIntStateOf(0) }
|
||||||
|
DisposableEffect(context) {
|
||||||
|
val manager = context.getSystemService(ConnectivityManager::class.java)
|
||||||
|
val mainExecutor = ContextCompat.getMainExecutor(context)
|
||||||
|
val callback =
|
||||||
|
object : ConnectivityManager.NetworkCallback() {
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
mainExecutor.execute { revision++ }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
mainExecutor.execute { revision++ }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLinkPropertiesChanged(
|
||||||
|
network: Network,
|
||||||
|
linkProperties: LinkProperties,
|
||||||
|
) {
|
||||||
|
mainExecutor.execute { revision++ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
runCatching { manager?.registerDefaultNetworkCallback(callback) }
|
||||||
|
onDispose {
|
||||||
|
if (manager != null) runCatching { manager.unregisterNetworkCallback(callback) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return revision
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,46 +4,15 @@ import org.junit.Assert.assertEquals
|
|||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.assertFalse
|
||||||
import org.junit.Assert.assertTrue
|
import org.junit.Assert.assertTrue
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.net.InetAddress
|
|
||||||
|
|
||||||
class LocalNetworkAccessTest {
|
class LocalNetworkAccessTest {
|
||||||
@Test
|
@Test
|
||||||
fun matchesAnAddressInsideASlash24Network() {
|
fun treatsDotLocalHostnamesAsLocalNetwork() {
|
||||||
assertTrue(address("192.168.1.122").isWithinPrefix(address("192.168.1.243"), 24))
|
assertTrue(isLocalHostname("navidrome.local"))
|
||||||
}
|
assertTrue(isLocalHostname("NAVIDROME.LOCAL."))
|
||||||
|
assertTrue(isLocalHostname("local"))
|
||||||
@Test
|
assertFalse(isLocalHostname("navidrome.example.com"))
|
||||||
fun rejectsAnAddressOutsideASlash24Network() {
|
assertFalse(isLocalHostname("local.example.com"))
|
||||||
assertFalse(address("10.224.202.10").isWithinPrefix(address("192.168.22.11"), 24))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun matchesAnAddressInsideASlash8Network() {
|
|
||||||
assertTrue(address("10.224.202.10").isWithinPrefix(address("10.0.0.1"), 8))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun masksThePartialByteOfANonOctetPrefix() {
|
|
||||||
assertTrue(address("192.168.1.200").isWithinPrefix(address("192.168.1.130"), 25))
|
|
||||||
assertFalse(address("192.168.1.100").isWithinPrefix(address("192.168.1.130"), 25))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun doesNotMatchAcrossAddressFamilies() {
|
|
||||||
assertFalse(address("::1").isWithinPrefix(address("10.0.0.1"), 8))
|
|
||||||
assertFalse(address("10.0.0.1").isWithinPrefix(address("fd00::1"), 64))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun matchesAnIpv6Prefix() {
|
|
||||||
assertTrue(address("fd00:1234:5678:9abc::5").isWithinPrefix(address("fd00:1234:5678:9abc::1"), 64))
|
|
||||||
assertFalse(address("fd00:1234:5678:9abd::5").isWithinPrefix(address("fd00:1234:5678:9abc::1"), 64))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun matchesAHostRoute() {
|
|
||||||
assertTrue(address("10.0.0.5").isWithinPrefix(address("10.0.0.5"), 32))
|
|
||||||
assertFalse(address("10.0.0.6").isWithinPrefix(address("10.0.0.5"), 32))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -52,6 +21,4 @@ class LocalNetworkAccessTest {
|
|||||||
assertEquals("navidrome.example.com", serverHost("navidrome.example.com:4533"))
|
assertEquals("navidrome.example.com", serverHost("navidrome.example.com:4533"))
|
||||||
assertEquals("10.224.202.10", serverHost("http://10.224.202.10"))
|
assertEquals("10.224.202.10", serverHost("http://10.224.202.10"))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun address(value: String): InetAddress = InetAddress.getByName(value)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user