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

View File

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