From 899540c39b4496eef9d607d665120e06ee0856bf Mon Sep 17 00:00:00 2001 From: SpiReCZ Date: Sat, 15 Oct 2022 16:36:22 +0200 Subject: [PATCH] fix torrc overrides were not propagated, support to obtain control port that was set manually, disable checking for port in stdout if it was already propagated outside, minor optimizations from IDE suggestions --- .../berndpruenster/netlayer/tor/TorContext.kt | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/tor.native/src/main/kotlin/org/berndpruenster/netlayer/tor/TorContext.kt b/tor.native/src/main/kotlin/org/berndpruenster/netlayer/tor/TorContext.kt index dfd28d6..7d838cf 100644 --- a/tor.native/src/main/kotlin/org/berndpruenster/netlayer/tor/TorContext.kt +++ b/tor.native/src/main/kotlin/org/berndpruenster/netlayer/tor/TorContext.kt @@ -79,8 +79,8 @@ class Torrc @Throws(IOException::class) internal constructor(defaults: InputStre private val rc = LinkedHashMap() init { - overrides?.forEach { rc.put(it.key, it.value.trim()) } parse(defaults)?.forEach { rc.put(it.key, it.value.trim()) } + overrides?.forEach { rc.put(it.key, it.value.trim()) } } internal val inputStream: InputStream @@ -130,29 +130,36 @@ abstract class TorContext @Throws(IOException::class) protected constructor(val private val EVENTS = listOf("CIRC", "WARN", "ERR") private fun parseBootstrap(inputStream: InputStream, latch: CountDownLatch, port: AtomicReference) { - Thread({ - Thread.currentThread().name = "NFO" - BufferedReader(inputStream.reader()).use { reader -> - reader.forEachLine { - logger?.debug { it } - if (it.contains("Control listener listening on port ")) { - port.set(Integer.parseInt(it.substring(it.lastIndexOf(" ") + 1, it.length - 1))) - latch.countDown() - } - } - } - }).start() + Thread { + Thread.currentThread().name = "NFO" + BufferedReader(inputStream.reader()).use { reader -> + reader.forEachLine { + logger?.debug { it } + if (latch.count > 0L) { + if (it.contains("Control listener listening on port ")) { + // Automatic port + port.set(Integer.parseInt(it.substring(it.lastIndexOf(" ") + 1, it.length - 1))) + latch.countDown() + } else if (it.contains("Opened Control listener connection")) { + // Manual port + port.set(Integer.parseInt(it.substring(it.lastIndexOf(":") + 1, it.length).trim())) + latch.countDown() + } + } + } + } + }.start() } private fun forwardErr(inputStream: InputStream) { - Thread({ - Thread.currentThread().name = "ERR" - BufferedReader(inputStream.reader()).use { reader -> - reader.forEachLine { - logger?.error { it } - } - } - }).start() + Thread { + Thread.currentThread().name = "ERR" + BufferedReader(inputStream.reader()).use { reader -> + reader.forEachLine { + logger?.error { it } + } + } + }.start() } }