From 77f9905b69fe75ffe21f2b062f64c99e49e33ab7 Mon Sep 17 00:00:00 2001 From: nrobi144 Date: Tue, 31 Mar 2026 16:50:21 +0300 Subject: [PATCH] feat(tor): add TorServiceStatus, ITorManager, ITorSettingsPersistence to commons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New shared types in commons/commonMain/tor/: - TorServiceStatus: clean sealed class with Off, Connecting, Active(port), Error(message). Uses data object for stateless variants. No TorControlConnection dependency (Android keeps its own version for now). - ITorManager: reactive interface with status flow + dormant/active/newIdentity - ITorSettingsPersistence: load/save interface for platform persistence Android TorServiceStatus unchanged — will migrate to commons version when RelayProxyClientConnector is refactored to use ITorManager signals. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../amethyst/commons/tor/ITorManager.kt | 40 +++++++++++++++++++ .../commons/tor/ITorSettingsPersistence.kt | 32 +++++++++++++++ .../amethyst/commons/tor/TorServiceStatus.kt | 35 ++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorManager.kt create mode 100644 commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorSettingsPersistence.kt create mode 100644 commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/TorServiceStatus.kt diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorManager.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorManager.kt new file mode 100644 index 000000000..3beb43350 --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorManager.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.tor + +import kotlinx.coroutines.flow.StateFlow + +/** + * Platform-agnostic interface for Tor daemon management. + * + * Implementations are reactive: status is derived from settings changes. + * Android uses tor-android + jtorctl. Desktop uses kmp-tor. + */ +interface ITorManager { + val status: StateFlow + val activePortOrNull: StateFlow + + suspend fun dormant() + + suspend fun active() + + suspend fun newIdentity() +} diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorSettingsPersistence.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorSettingsPersistence.kt new file mode 100644 index 000000000..03f81bbc3 --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/ITorSettingsPersistence.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.tor + +/** + * Platform-agnostic interface for persisting Tor settings. + * + * Android uses DataStore. Desktop uses java.util.prefs.Preferences. + */ +interface ITorSettingsPersistence { + fun load(): TorSettings + + fun save(settings: TorSettings) +} diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/TorServiceStatus.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/TorServiceStatus.kt new file mode 100644 index 000000000..dbe8668f7 --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/tor/TorServiceStatus.kt @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.tor + +sealed class TorServiceStatus { + data class Active( + val port: Int, + ) : TorServiceStatus() + + data object Off : TorServiceStatus() + + data object Connecting : TorServiceStatus() + + data class Error( + val message: String, + ) : TorServiceStatus() +}