Skip to content

Commit

Permalink
Merge pull request #96 from grote/94-ioexceptions
Browse files Browse the repository at this point in the history
Catch IOExceptions from TorWrapper
  • Loading branch information
akwizgran authored Aug 2, 2023
2 parents 2ea631b + 21fccd6 commit b6c4931
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
16 changes: 13 additions & 3 deletions app/src/main/java/org/onionshare/android/ShareManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.onionshare.android.tor.TorState
import org.onionshare.android.ui.OnionNotificationManager
import org.onionshare.android.ui.share.ShareUiState
import org.slf4j.LoggerFactory.getLogger
import java.io.IOException
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -114,8 +115,13 @@ class ShareManager @Inject constructor(
when (val zipResult = fileTask.await()) {
is ZipResult.Zipped -> {
val port = webserverManager.start(zipResult.sendPage)
torManager.publishOnionService(port)
observerTask.await()
try {
torManager.publishOnionService(port)
observerTask.await()
} catch (e: IOException) {
LOG.error("Error publishing onion service: ", e)
stopOnError(ShareUiState.ErrorStarting())
}
}

is ZipResult.Error -> {
Expand Down Expand Up @@ -149,7 +155,11 @@ class ShareManager @Inject constructor(
ShareUiState.ErrorStarting(true)
}

TorState.Stopping -> error("Still observing TorState after calling stop().")
TorState.Stopping -> {
// Somebody is stopping Tor. It wasn't us, because we would have stopped collected its state.
// So go to stopping state for now until Tor has stopped and we'll show the error below.
ShareUiState.Stopping
}

TorState.Stopped -> {
ShareUiState.ErrorStarting(errorMsg = "Tor stopped unexpectedly.")
Expand Down
24 changes: 21 additions & 3 deletions app/src/main/java/org/onionshare/android/tor/TorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ class TorManager(
LOG.info("Stopping...")
startCheckJob?.cancel()
startCheckJob = null
tor.stop()
try {
tor.stop()
} catch (e: Exception) {
LOG.warn("Error stopping Tor: ", e)
}
Intent(app, ShareService::class.java).also { intent ->
app.stopService(intent)
}
Expand All @@ -148,6 +152,7 @@ class TorManager(
else if (s == STOPPED) updateTorState(null, TorState.Stopped)
}

@Throws(IOException::class)
fun publishOnionService(port: Int) {
LOG.info("Starting hidden service...")
tor.publishHiddenService(port, 80, null)
Expand Down Expand Up @@ -191,7 +196,13 @@ class TorManager(
// try bridges we got from Moat, if any
if (!bridges.isNullOrEmpty()) {
LOG.info("Using bridges from Moat...")
useBridges(bridges)
try {
useBridges(bridges)
} catch (e: IOException) {
LOG.error("Error setting bridges: ", e)
stop()
return
}
if (waitForTorToStart()) return
} else {
LOG.info("No bridges received from Moat. Continuing...")
Expand All @@ -202,7 +213,13 @@ class TorManager(
val builtInBridges = circumventionProvider.getSuitableBridgeTypes(countryCode).flatMap { type ->
circumventionProvider.getBridges(type, countryCode, SDK_INT >= 25)
}
useBridges(builtInBridges)
try {
useBridges(builtInBridges)
} catch (e: IOException) {
LOG.error("Error setting bridges: ", e)
stop()
return
}
if (waitForTorToStart()) return
LOG.info("Could not connect to Tor")
updateTorState(TorState.Starting::class, TorState.FailedToConnect)
Expand Down Expand Up @@ -259,6 +276,7 @@ class TorManager(
}
}

@Throws(IOException::class)
private fun useBridges(bridges: List<String>) {
if (LOG.isInfoEnabled) {
LOG.info("Using bridges:")
Expand Down

0 comments on commit b6c4931

Please sign in to comment.