Commit fd4d7f46 authored by Mygod's avatar Mygod

Make sure to clean up acl

parent 5ca1e2e4
...@@ -48,9 +48,10 @@ object LocalDnsService { ...@@ -48,9 +48,10 @@ object LocalDnsService {
Socks5Endpoint(dns.host, if (dns.port < 0) 53 else dns.port), Socks5Endpoint(dns.host, if (dns.port < 0) 53 else dns.port),
DataStore.proxyAddress, DataStore.proxyAddress,
hosts, hosts,
!profile.udpdns) { !profile.udpdns,
if (profile.route == Acl.ALL) null else AclMatcher().apply { init(profile.route) } if (profile.route == Acl.ALL) null else object {
}.also { suspend fun createAcl() = AclMatcher().apply { init(profile.route) }
}::createAcl).also {
servers[this] = it servers[this] = it
}.start(InetSocketAddress(DataStore.listenAddress, DataStore.portLocalDns)) }.start(InetSocketAddress(DataStore.listenAddress, DataStore.portLocalDns))
} }
......
...@@ -51,7 +51,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -51,7 +51,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
* Forward UDP queries to TCP. * Forward UDP queries to TCP.
*/ */
private val tcp: Boolean = false, private val tcp: Boolean = false,
private val aclSpawn: suspend () -> AclMatcher? = { null }) : CoroutineScope { aclSpawn: (suspend () -> AclMatcher)? = null) : CoroutineScope {
companion object { companion object {
private const val TAG = "LocalDnsServer" private const val TAG = "LocalDnsServer"
private const val TIMEOUT = 10_000L private const val TIMEOUT = 10_000L
...@@ -84,7 +84,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -84,7 +84,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
override val coroutineContext = SupervisorJob() + CoroutineExceptionHandler { _, t -> override val coroutineContext = SupervisorJob() + CoroutineExceptionHandler { _, t ->
if (t is IOException) Crashlytics.log(Log.WARN, TAG, t.message) else printLog(t) if (t is IOException) Crashlytics.log(Log.WARN, TAG, t.message) else printLog(t)
} }
private val acl = async { aclSpawn() } private val acl = aclSpawn?.let { async { it() } }
suspend fun start(listen: SocketAddress) = DatagramChannel.open().run { suspend fun start(listen: SocketAddress) = DatagramChannel.open().run {
configureBlocking(false) configureBlocking(false)
...@@ -129,7 +129,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -129,7 +129,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
remote.cancel() remote.cancel()
return@supervisorScope cookDnsResponse(request, hostsResults) return@supervisorScope cookDnsResponse(request, hostsResults)
} }
val acl = acl.await() ?: return@supervisorScope remote.await() val acl = acl?.await() ?: return@supervisorScope remote.await()
val useLocal = when (acl.shouldBypass(host)) { val useLocal = when (acl.shouldBypass(host)) {
true -> true.also { remote.cancel() } true -> true.also { remote.cancel() }
false -> return@supervisorScope remote.await() false -> return@supervisorScope remote.await()
...@@ -204,5 +204,6 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -204,5 +204,6 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
cancel() cancel()
monitor.close(scope) monitor.close(scope)
coroutineContext[Job]!!.also { job -> scope.launch { job.join() } } coroutineContext[Job]!!.also { job -> scope.launch { job.join() } }
acl?.also { scope.launch { it.await().close() } }
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment