diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkListener.kt b/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkListener.kt index f89eb79..9fb45f6 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkListener.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkListener.kt @@ -34,7 +34,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.channels.actor import kotlinx.coroutines.runBlocking -import java.net.UnknownHostException object DefaultNetworkListener { private sealed class NetworkMessage { @@ -104,7 +103,7 @@ object DefaultNetworkListener { suspend fun get() = if (fallback) @TargetApi(23) { Application.connectivity.activeNetwork - ?: throw UnknownHostException() // failed to listen, return current if available + ?: error("missing default network") // failed to listen, return current if available } else NetworkMessage.Get().run { networkActor.send(this) response.await() diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkMonitor.kt b/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkMonitor.kt index 0e04e3c..3d116b8 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkMonitor.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/DefaultNetworkMonitor.kt @@ -27,6 +27,14 @@ object DefaultNetworkMonitor { DefaultNetworkListener.stop(this) } + suspend fun require(): Network { + val network = defaultNetwork + if (network != null) { + return network + } + return DefaultNetworkListener.get() + } + fun setListener(listener: InterfaceUpdateListener?) { this.listener = listener checkDefaultInterfaceUpdate(defaultNetwork) diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/LocalResolver.kt b/app/src/main/java/io/nekohasekai/sfa/bg/LocalResolver.kt index 8e37553..5db814e 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/LocalResolver.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/LocalResolver.kt @@ -27,6 +27,7 @@ object LocalResolver : LocalDNSTransport { @RequiresApi(Build.VERSION_CODES.Q) override fun exchange(ctx: ExchangeContext, message: ByteArray) { return runBlocking { + val defaultNetwork = DefaultNetworkMonitor.require() suspendCoroutine { continuation -> val signal = CancellationSignal() ctx.onCancel(signal::cancel) @@ -52,7 +53,7 @@ object LocalResolver : LocalDNSTransport { } } DnsResolver.getInstance().rawQuery( - DefaultNetworkMonitor.defaultNetwork, + defaultNetwork, message, DnsResolver.FLAG_NO_RETRY, Dispatchers.IO.asExecutor(), @@ -64,8 +65,9 @@ object LocalResolver : LocalDNSTransport { } override fun lookup(ctx: ExchangeContext, network: String, domain: String) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - return runBlocking { + return runBlocking { + val defaultNetwork = DefaultNetworkMonitor.require() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { suspendCoroutine { continuation -> val signal = CancellationSignal() ctx.onCancel(signal::cancel) @@ -99,7 +101,7 @@ object LocalResolver : LocalDNSTransport { } if (type != null) { DnsResolver.getInstance().query( - DefaultNetworkMonitor.defaultNetwork, + defaultNetwork, domain, type, DnsResolver.FLAG_NO_RETRY, @@ -109,7 +111,7 @@ object LocalResolver : LocalDNSTransport { ) } else { DnsResolver.getInstance().query( - DefaultNetworkMonitor.defaultNetwork, + defaultNetwork, domain, DnsResolver.FLAG_NO_RETRY, Dispatchers.IO.asExecutor(), @@ -118,17 +120,15 @@ object LocalResolver : LocalDNSTransport { ) } } + } else { + val answer = try { + defaultNetwork.getAllByName(domain) + } catch (e: UnknownHostException) { + ctx.errorCode(RCODE_NXDOMAIN) + return@runBlocking + } + ctx.success(answer.mapNotNull { it.hostAddress }.joinToString("\n")) } - } else { - val underlyingNetwork = - DefaultNetworkMonitor.defaultNetwork ?: error("upstream network not found") - val answer = try { - underlyingNetwork.getAllByName(domain) - } catch (e: UnknownHostException) { - ctx.errorCode(RCODE_NXDOMAIN) - return - } - ctx.success(answer.mapNotNull { it.hostAddress }.joinToString("\n")) } } } \ No newline at end of file