mirror of
https://github.com/SagerNet/sing-box-for-android.git
synced 2025-04-03 20:07:38 +03:00
Fix local DNS resolver
This commit is contained in:
parent
bda0bb69d5
commit
da91616d4b
3 changed files with 24 additions and 17 deletions
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue