Commit fd4d7f46 authored by Mygod's avatar Mygod

Make sure to clean up acl

parent 5ca1e2e4
......@@ -48,9 +48,10 @@ object LocalDnsService {
Socks5Endpoint(dns.host, if (dns.port < 0) 53 else dns.port),
DataStore.proxyAddress,
hosts,
!profile.udpdns) {
if (profile.route == Acl.ALL) null else AclMatcher().apply { init(profile.route) }
}.also {
!profile.udpdns,
if (profile.route == Acl.ALL) null else object {
suspend fun createAcl() = AclMatcher().apply { init(profile.route) }
}::createAcl).also {
servers[this] = it
}.start(InetSocketAddress(DataStore.listenAddress, DataStore.portLocalDns))
}
......
......@@ -51,7 +51,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
* Forward UDP queries to TCP.
*/
private val tcp: Boolean = false,
private val aclSpawn: suspend () -> AclMatcher? = { null }) : CoroutineScope {
aclSpawn: (suspend () -> AclMatcher)? = null) : CoroutineScope {
companion object {
private const val TAG = "LocalDnsServer"
private const val TIMEOUT = 10_000L
......@@ -84,7 +84,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
override val coroutineContext = SupervisorJob() + CoroutineExceptionHandler { _, 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 {
configureBlocking(false)
......@@ -129,7 +129,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
remote.cancel()
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)) {
true -> true.also { remote.cancel() }
false -> return@supervisorScope remote.await()
......@@ -204,5 +204,6 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
cancel()
monitor.close(scope)
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