Commit 32e3d7ab authored by Mygod's avatar Mygod

Refine error logging

parent ea659848
...@@ -25,6 +25,7 @@ import com.github.shadowsocks.utils.printLog ...@@ -25,6 +25,7 @@ import com.github.shadowsocks.utils.printLog
import kotlinx.coroutines.* import kotlinx.coroutines.*
import org.xbill.DNS.* import org.xbill.DNS.*
import java.io.IOException import java.io.IOException
import java.lang.RuntimeException
import java.net.* import java.net.*
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.channels.DatagramChannel import java.nio.channels.DatagramChannel
...@@ -65,8 +66,8 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -65,8 +66,8 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
private val monitor = ChannelMonitor() private val monitor = ChannelMonitor()
private val job = Job() private val job = SupervisorJob()
override val coroutineContext = Dispatchers.Default + job + CoroutineExceptionHandler { _, t -> printLog(t) } override val coroutineContext = Dispatchers.Default + job
fun start(listen: SocketAddress) = DatagramChannel.open().apply { fun start(listen: SocketAddress) = DatagramChannel.open().apply {
configureBlocking(false) configureBlocking(false)
...@@ -76,8 +77,12 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -76,8 +77,12 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
val source = receive(buffer)!! val source = receive(buffer)!!
buffer.flip() buffer.flip()
launch { launch {
val reply = resolve(buffer) try {
while (isActive && send(reply, source) <= 0) monitor.wait(this@apply, SelectionKey.OP_WRITE) val reply = resolve(buffer)
while (isActive && send(reply, source) <= 0) monitor.wait(this@apply, SelectionKey.OP_WRITE)
} catch (e: RuntimeException) {
printLog(e)
}
} }
} }
} }
...@@ -98,6 +103,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -98,6 +103,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
val localResults = try { val localResults = try {
withTimeout(TIMEOUT) { withContext(Dispatchers.IO) { localResolver(host) } } withTimeout(TIMEOUT) { withContext(Dispatchers.IO) { localResolver(host) } }
} catch (_: TimeoutCancellationException) { } catch (_: TimeoutCancellationException) {
Log.w("LocalDnsServer", "Local resolving timed out, falling back to remote resolving")
return forward(packet) return forward(packet)
} catch (_: UnknownHostException) { } catch (_: UnknownHostException) {
return forward(packet) return forward(packet)
......
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