Skip to content

Commit

Permalink
Introduce a Started TorState
Browse files Browse the repository at this point in the history
This is because previously we only considered uploading the HS desc as started. However, this only happens after zipping files. If we zipping takes a long time, Tor would think it hasn't started and tries circumvention methods. Using a new Started state prevents this.
  • Loading branch information
grote committed Aug 2, 2023
1 parent 60d770f commit 49ffd78
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/org/onionshare/android/ShareManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class ShareManager @Inject constructor(
ShareUiState.Starting(zipState?.progress ?: 0, torPercent)
}

is TorState.Started -> {
ShareUiState.Starting(zipState?.progress ?: 0, 95)
}

is TorState.Published -> {
// We only create the hidden service after files have been zipped and webserver was started,
// so we are in sharing state once the first HS descriptor has been published.
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/org/onionshare/android/tor/TorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlinx.coroutines.withContext
import org.briarproject.onionwrapper.CircumventionProvider
import org.briarproject.onionwrapper.LocationUtils
import org.briarproject.onionwrapper.TorWrapper
import org.briarproject.onionwrapper.TorWrapper.TorState.CONNECTED
import org.briarproject.onionwrapper.TorWrapper.TorState.STOPPED
import org.onionshare.android.Clock
import org.onionshare.android.DefaultClock
Expand Down Expand Up @@ -143,7 +144,8 @@ class TorManager(

override fun onState(s: TorWrapper.TorState) {
LOG.info("new state: $s")
if (s == STOPPED) updateTorState(null, TorState.Stopped)
if (s == CONNECTED) updateTorState(TorState.Starting::class, TorState.Started)
else if (s == STOPPED) updateTorState(null, TorState.Stopped)
}

fun publishOnionService(port: Int) {
Expand All @@ -156,7 +158,7 @@ class TorManager(
}

override fun onHsDescriptorUpload(onion: String) {
if (updateTorState(TorState.Starting::class, TorState.Published(onion), warn = false)) {
if (updateTorState(TorState.Started::class, TorState.Published(onion), warn = false)) {
startCheckJob?.cancel()
startCheckJob = null
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/onionshare/android/tor/TorState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sealed class TorState {
val lastProgressTime: Long,
) : TorState()

object Started : TorState()

data class Published(
val onion: String,
) : TorState()
Expand Down
4 changes: 4 additions & 0 deletions app/src/test/java/org/onionshare/android/TorManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class TorManagerTest {
assertIs<TorState.Starting>(starting50) // 50%
assertEquals(50, starting50.progress)

// getting informed about CONNECTED is required now
torManager.onState(TorWrapper.TorState.CONNECTED)
assertIs<TorState.Started>(awaitItem())

torManager.onHsDescriptorUpload("foobar")
val published = awaitItem()
assertIs<TorState.Published>(published)
Expand Down

0 comments on commit 49ffd78

Please sign in to comment.