Improve: patch all dns to core

This commit is contained in:
kr328 2021-11-21 19:55:52 +08:00
parent c173114368
commit 53aa718674
2 changed files with 29 additions and 29 deletions

View File

@ -6,7 +6,7 @@ import android.os.Build
import androidx.core.content.getSystemService
import com.github.kr328.clash.common.log.Log
import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.service.util.resolveDns
import com.github.kr328.clash.service.util.resolvePrimaryDns
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.trySendBlocking
@ -48,45 +48,46 @@ class NetworkObserveModule(service: Service) : Module<Network?>(service) {
}
try {
var current: Network? = null
val networks = mutableSetOf<Network>()
while (true) {
val action = actions.receive()
when (action.type) {
val resolveDefault = when (action.type) {
Action.Type.Available -> {
networks.add(action.network)
true
}
Action.Type.Lost -> {
networks.remove(action.network)
true
}
Action.Type.Changed -> {
if (current == action.network) {
val dns = connectivity.resolveDns(action.network)
Clash.notifyDnsChanged(dns)
Log.d("Current network changed: ${action.network}: $dns")
}
continue
false
}
}
current = networks.maxByOrNull {
connectivity.getNetworkCapabilities(it)?.let { cap ->
TRANSPORT_PRIORITY.indexOfFirst { cap.hasTransport(it) }
} ?: -1
val dns = networks.mapNotNull {
connectivity.resolvePrimaryDns(it)
}
val dns = connectivity.resolveDns(current)
Clash.notifyDnsChanged(dns)
enqueueEvent(current)
Log.d("DNS: $dns")
Log.d("Available network changed: $current of $networks: $dns")
if (resolveDefault) {
val network = networks.maxByOrNull { net ->
connectivity.getNetworkCapabilities(net)?.let { cap ->
TRANSPORT_PRIORITY.indexOfFirst { cap.hasTransport(it) }
} ?: -1
}
enqueueEvent(network)
Log.d("Network: $network of $networks")
}
}
} finally {
withContext(NonCancellable) {
@ -105,19 +106,19 @@ class NetworkObserveModule(service: Service) : Module<Network?>(service) {
private val TRANSPORT_PRIORITY = sequence {
yield(NetworkCapabilities.TRANSPORT_CELLULAR)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
if (Build.VERSION.SDK_INT >= 27) {
yield(NetworkCapabilities.TRANSPORT_LOWPAN)
}
yield(NetworkCapabilities.TRANSPORT_BLUETOOTH)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= 26) {
yield(NetworkCapabilities.TRANSPORT_WIFI_AWARE)
}
yield(NetworkCapabilities.TRANSPORT_WIFI)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (Build.VERSION.SDK_INT >= 31) {
yield(NetworkCapabilities.TRANSPORT_USB)
}

View File

@ -3,9 +3,8 @@ package com.github.kr328.clash.service.util
import android.net.ConnectivityManager
import android.net.Network
fun ConnectivityManager.resolveDns(network: Network?): List<String> {
return network?.run(this::getLinkProperties)
?.dnsServers
?.map { it.asSocketAddressText(53) }
?: emptyList()
}
fun ConnectivityManager.resolvePrimaryDns(network: Network?): String? {
val properties = getLinkProperties(network) ?: return null
return properties.dnsServers.firstOrNull()?.asSocketAddressText(53)
}