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

Commit 8832d35

Browse files
authored
Merge pull request #74 from 05nelsonm/mn/feature/tor-setting-dormant-client-timeout
Add setting to enable torrc config option DormantClientTimeout
2 parents 4ccfd8e + 6011765 commit 8832d35

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

sampleapp/src/main/java/io/matthewnelson/sampleapp/topl_android/MyTorSettings.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ import io.matthewnelson.topl_core_base.TorSettings
7474
* */
7575
class MyTorSettings: TorSettings() {
7676

77+
override val dormantClientTimeout: Int?
78+
get() = DEFAULT__DORMANT_CLIENT_TIMEOUT
79+
7780
override val disableNetwork: Boolean
7881
get() = DEFAULT__DISABLE_NETWORK
7982

topl-core-base/src/main/java/io/matthewnelson/topl_core_base/TorSettings.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ abstract class TorSettings: BaseConsts() {
110110
* things up if you're simply looking to use a Socks5 Proxy to connect to.
111111
* */
112112
companion object {
113+
const val DEFAULT__DORMANT_CLIENT_TIMEOUT = 10
113114
const val DEFAULT__DISABLE_NETWORK = true
114115
const val DEFAULT__ENTRY_NODES = ""
115116
const val DEFAULT__EXCLUDED_NODES = ""
@@ -150,6 +151,16 @@ abstract class TorSettings: BaseConsts() {
150151
* */
151152
abstract val customTorrc: String?
152153

154+
/**
155+
* Adds to the torrc file "DormantClientTimeout <your value> minutes"
156+
*
157+
* Minimum value 10. Any value less than or equal to 9 will fall back to using the value of 10
158+
* when writing the config to the torrc file. Set `null` to disable
159+
*
160+
* See [DEFAULT__DORMANT_CLIENT_TIMEOUT]
161+
* */
162+
abstract val dormantClientTimeout: Int?
163+
153164
/**
154165
* OnionProxyManager will enable this on startup using the TorControlConnection based off
155166
* of the device's network state. Setting this to `true` is highly recommended.

topl-core/src/main/java/io/matthewnelson/topl_core/settings/TorSettingsBuilder.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,27 @@ class TorSettingsBuilder internal constructor(
318318
else
319319
this
320320

321+
fun dormantClientTimetout(minutes: Int): TorSettingsBuilder {
322+
val value = if (minutes < 10)
323+
10
324+
else
325+
minutes
326+
buffer.append("DormantClientTimeout $value minutes\n")
327+
return this
328+
}
329+
330+
@SettingsConfig
331+
fun dormantClientTimeoutFromSettings(): TorSettingsBuilder {
332+
return when (val minutes = torSettings.dormantClientTimeout) {
333+
null -> {
334+
this
335+
}
336+
else -> {
337+
dormantClientTimetout(minutes)
338+
}
339+
}
340+
}
341+
321342
fun disableNetwork(disable: Boolean): TorSettingsBuilder {
322343
val disableNetwork = if (disable) "1" else "0"
323344
buffer.append("DisableNetwork $disableNetwork\n")

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ class ServiceTorSettings internal constructor(
9898
val defaultTorSettings: TorSettings
9999
): TorSettings() {
100100

101+
override val dormantClientTimeout: Int?
102+
get() = servicePrefs.getInt(PrefKeyInt.DORMANT_CLIENT_TIMEOUT, defaultTorSettings.dormantClientTimeout)
103+
104+
fun dormantClientTimeoutSave(minutes: Int?) {
105+
if (minutes == defaultTorSettings.dormantClientTimeout)
106+
servicePrefs.remove(PrefKeyInt.DORMANT_CLIENT_TIMEOUT)
107+
else
108+
servicePrefs.putInt(PrefKeyInt.DORMANT_CLIENT_TIMEOUT, minutes)
109+
}
110+
101111
override val disableNetwork: Boolean
102112
get() = servicePrefs.getBoolean(PrefKeyBoolean.DISABLE_NETWORK, defaultTorSettings.disableNetwork)
103113

topl-service/src/main/java/io/matthewnelson/topl_service/util/ServiceConsts.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,15 @@ abstract class ServiceConsts: BaseConsts() {
210210
}
211211

212212
@StringDef(
213+
PrefKeyInt.DORMANT_CLIENT_TIMEOUT,
213214
PrefKeyInt.PROXY_PORT,
214215
PrefKeyInt.PROXY_SOCKS5_SERVER_PORT
215216
)
216217
@Retention(AnnotationRetention.SOURCE)
217218
annotation class PrefKeyInt {
218219
companion object {
219220
// Keys for returning Ints
221+
const val DORMANT_CLIENT_TIMEOUT = "DORMANT_CLIENT_TIMEOUT"
220222
const val PROXY_PORT = "PROXY_PORT"
221223
const val PROXY_SOCKS5_SERVER_PORT = "PROXY_SOCKS5_SERVER_PORT"
222224
}

topl-service/src/test/java/io/matthewnelson/test_helpers/application_provided_classes/TestTorSettings.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ import io.matthewnelson.topl_core_base.TorSettings
7070

7171
internal class TestTorSettings: TorSettings() {
7272

73+
override val dormantClientTimeout: Int?
74+
get() = DEFAULT__DORMANT_CLIENT_TIMEOUT
75+
7376
override val disableNetwork: Boolean
7477
get() = DEFAULT__DISABLE_NETWORK
7578

0 commit comments

Comments
 (0)