Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 8028152

Browse files
committed
implement updating the application's TorServiceEventBroadcaster with Tor address information
1 parent c75b767 commit 8028152

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

topl-service/src/main/java/io/matthewnelson/topl_service/service/components/onionproxy/ServiceEventBroadcaster.kt

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
package io.matthewnelson.topl_service.service.components.onionproxy
6868

6969
import io.matthewnelson.topl_core.OnionProxyManager
70+
import io.matthewnelson.topl_core.listener.BaseEventListener
7071
import io.matthewnelson.topl_core_base.EventBroadcaster
7172
import io.matthewnelson.topl_service.TorServiceController
7273
import io.matthewnelson.topl_service.service.BaseService
@@ -205,29 +206,48 @@ internal class ServiceEventBroadcaster(private val torService: BaseService): Eve
205206
private fun isBootstrappingComplete(): Boolean =
206207
bootstrapProgress == "Bootstrapped 100%"
207208

209+
@Volatile
210+
private var controlPort: String? = null
211+
@Volatile
212+
private var httpTunnelPort: String? = null
213+
@Volatile
214+
private var socksPort: String? = null
215+
208216
override fun broadcastNotice(msg: String) {
209217

210218
when {
219+
// ServiceActionProcessor
220+
msg.contains(ServiceActionProcessor::class.java.simpleName) -> {
221+
handleServiceActionProcessorMsg(msg)
222+
}
211223
// BOOTSTRAPPED
212-
// NOTICE|BaseEventListener|Bootstrapped 5% (conn): Connecting to a relay
213224
msg.contains("Bootstrapped") -> {
214225
handleBootstrappedMsg(msg)
215226
}
227+
// Control Port
228+
msg.contains("Successfully connected to Control Port:") -> {
229+
handleControlPortMsg(msg)
230+
}
231+
// Http Tunnel Port
232+
msg.contains("Opened HTTP tunnel listener on ") -> {
233+
handleHttpTunnelPortMsg(msg)
234+
}
235+
// Socks Port
236+
msg.contains("Opened Socks listener on ") -> {
237+
handleSocksPortMsg(msg)
238+
}
216239
// NEWNYM
217240
msg.contains(TorControlCommands.SIGNAL_NEWNYM) -> {
218241
handleNewNymMsg(msg)
219242
}
220-
// ServiceActionProcessor
221-
msg.contains(ServiceActionProcessor::class.java.simpleName) -> {
222-
handleServiceActionProcessorMsg(msg)
223-
}
224243
}
225244

226245
TorServiceController.appEventBroadcaster?.let {
227246
scopeMain.launch { it.broadcastNotice(msg) }
228247
}
229248
}
230249

250+
// NOTICE|BaseEventListener|Bootstrapped 5% (conn): Connecting to a relay
231251
private fun handleBootstrappedMsg(msg: String) {
232252
val msgSplit = msg.split(" ")
233253
msgSplit.elementAtOrNull(2)?.let {
@@ -237,6 +257,7 @@ internal class ServiceEventBroadcaster(private val torService: BaseService): Eve
237257
torService.updateNotificationContentText(bootstrapped)
238258

239259
if (bootstrapped == "Bootstrapped 100%") {
260+
updateAppEventBroadcasterWithPortInfo()
240261
torService.updateNotificationIcon(NotificationImage.ENABLED)
241262
torService.updateNotificationProgress(true, 100)
242263
torService.updateNotificationProgress(false, null)
@@ -257,6 +278,24 @@ internal class ServiceEventBroadcaster(private val torService: BaseService): Eve
257278
}
258279
}
259280

281+
// NOTICE|OnionProxyManager|Successfully connected to Control Port: 44201
282+
private fun handleControlPortMsg(msg: String) {
283+
val port = msg.split(":")[1].trim()
284+
controlPort = "127.0.0.1:$port"
285+
}
286+
287+
// NOTICE|BaseEventListener|Opened HTTP tunnel listener on 127.0.0.1:37397
288+
private fun handleHttpTunnelPortMsg(msg: String) {
289+
val port = msg.split(":")[1].trim()
290+
httpTunnelPort = "127.0.0.1:$port"
291+
}
292+
293+
// NOTICE|BaseEventListener|Opened Socks listener on 127.0.0.1:9051
294+
private fun handleSocksPortMsg(msg: String) {
295+
val port = msg.split(":")[1].trim()
296+
socksPort = "127.0.0.1:$port"
297+
}
298+
260299
private fun handleNewNymMsg(msg: String) {
261300
val msgToShow: String? =
262301
when {
@@ -300,6 +339,16 @@ internal class ServiceEventBroadcaster(private val torService: BaseService): Eve
300339
}
301340
}
302341

342+
private fun updateAppEventBroadcasterWithPortInfo() {
343+
TorServiceController.appEventBroadcaster?.let {
344+
scopeMain.launch {
345+
it.broadcastControlPortAddress(controlPort)
346+
it.broadcastHttpPortAddress(httpTunnelPort)
347+
it.broadcastSocksPortAddress(socksPort)
348+
}
349+
}
350+
}
351+
303352
/**
304353
* Display a message in the notification's ContentText space for the defined
305354
* [delayMilliSeconds], after which (if Tor is connected), publish to the Notification's
@@ -335,6 +384,10 @@ internal class ServiceEventBroadcaster(private val torService: BaseService): Eve
335384
override fun broadcastTorState(@TorState state: String, @TorNetworkState networkState: String) {
336385
if (torState == TorState.ON && state != torState) {
337386
bootstrapProgress = ""
387+
controlPort = null
388+
httpTunnelPort = null
389+
socksPort = null
390+
updateAppEventBroadcasterWithPortInfo()
338391
torService.removeNotificationActions()
339392
}
340393

0 commit comments

Comments
 (0)