mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
Wake the tunnel reader only for packets and on stop
This commit is contained in:
@@ -124,7 +124,14 @@ class DohVpnService : VpnService() {
|
|||||||
stopSelf()
|
stopSelf()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
session = Session(tun, DohClient(this, provider), ::onSessionFailed).also { it.start() }
|
val next = try {
|
||||||
|
Session(tun, DohClient(this, provider), ::onSessionFailed)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
// A tunnel nobody reads would swallow every DNS lookup on the device
|
||||||
|
runCatching { tun.close() }
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
session = next.also { it.start() }
|
||||||
runCatching { previous?.stop() }
|
runCatching { previous?.stop() }
|
||||||
|
|
||||||
_active.value = provider
|
_active.value = provider
|
||||||
@@ -210,6 +217,10 @@ private class Session(
|
|||||||
private val pool = Executors.newFixedThreadPool(WORKERS)
|
private val pool = Executors.newFixedThreadPool(WORKERS)
|
||||||
private val cache = DnsCache()
|
private val cache = DnsCache()
|
||||||
private val output = FileOutputStream(tun.fileDescriptor)
|
private val output = FileOutputStream(tun.fileDescriptor)
|
||||||
|
|
||||||
|
// stop() closes the write end, which wakes the reader at once. Otherwise the reader would have
|
||||||
|
// to wake up on a timer to notice that the session ended, also while the device sleeps.
|
||||||
|
private val wakeup = ParcelFileDescriptor.createPipe()
|
||||||
private val reader = Thread(::readLoop, "tun-reader")
|
private val reader = Thread(::readLoop, "tun-reader")
|
||||||
|
|
||||||
fun start() = reader.start()
|
fun start() = reader.start()
|
||||||
@@ -217,6 +228,7 @@ private class Session(
|
|||||||
fun stop() {
|
fun stop() {
|
||||||
if (!alive.getAndSet(false)) return
|
if (!alive.getAndSet(false)) return
|
||||||
pool.shutdownNow()
|
pool.shutdownNow()
|
||||||
|
runCatching { wakeup[1].close() }
|
||||||
runCatching { tun.close() }
|
runCatching { tun.close() }
|
||||||
closer.execute {
|
closer.execute {
|
||||||
try {
|
try {
|
||||||
@@ -230,16 +242,28 @@ private class Session(
|
|||||||
private fun readLoop() {
|
private fun readLoop() {
|
||||||
val input = FileInputStream(tun.fileDescriptor)
|
val input = FileInputStream(tun.fileDescriptor)
|
||||||
val buf = ByteArray(MTU)
|
val buf = ByteArray(MTU)
|
||||||
val pollFd = StructPollfd().apply {
|
val tunFd = StructPollfd().apply {
|
||||||
fd = tun.fileDescriptor
|
fd = tun.fileDescriptor
|
||||||
events = OsConstants.POLLIN.toShort()
|
events = OsConstants.POLLIN.toShort()
|
||||||
}
|
}
|
||||||
|
val wakeupFd = StructPollfd().apply {
|
||||||
|
fd = wakeup[0].fileDescriptor
|
||||||
|
events = OsConstants.POLLIN.toShort()
|
||||||
|
}
|
||||||
|
val fds = arrayOf(tunFd, wakeupFd)
|
||||||
|
|
||||||
var badPackets = 0
|
var badPackets = 0
|
||||||
|
|
||||||
|
try {
|
||||||
while (alive.get()) {
|
while (alive.get()) {
|
||||||
val n = try {
|
val n = try {
|
||||||
if (Os.poll(arrayOf(pollFd), 500) <= 0) continue
|
// Sleeps until a packet arrives or stop() closes the pipe
|
||||||
|
if (Os.poll(fds, -1) <= 0) continue
|
||||||
|
if (wakeupFd.revents.toInt() != 0) {
|
||||||
|
// Only stop() touches the pipe, so this ends the loop
|
||||||
|
readerFailed(null)
|
||||||
|
return
|
||||||
|
}
|
||||||
input.read(buf)
|
input.read(buf)
|
||||||
} catch (e: ErrnoException) {
|
} catch (e: ErrnoException) {
|
||||||
if (e.errno == OsConstants.EINTR) continue
|
if (e.errno == OsConstants.EINTR) continue
|
||||||
@@ -268,6 +292,9 @@ private class Session(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
runCatching { wakeup[0].close() }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop() closes the tunnel, which also ends the loop; only an unexpected end is a failure
|
// stop() closes the tunnel, which also ends the loop; only an unexpected end is a failure
|
||||||
|
|||||||
Reference in New Issue
Block a user