From 9e1fc0af17c75c94778c9e74ad531b6e0021c497 Mon Sep 17 00:00:00 2001 From: Torsten Grote <t@grobox.de> Date: Mon, 21 Mar 2022 10:21:20 -0300 Subject: [PATCH] Introduce new Tor state PUBLISHED that gets reached when we uploaded the onion service descriptor three times (need to check the actual number needed). --- .../mailbox/android/ui/MailboxViewModel.kt | 4 +-- .../java/org/briarproject/mailbox/cli/Main.kt | 2 +- .../mailbox/core/setup/QrCodeEncoder.kt | 1 - .../mailbox/core/tor/TorPlugin.java | 29 +++++++++++-------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/mailbox-android/src/main/java/org/briarproject/mailbox/android/ui/MailboxViewModel.kt b/mailbox-android/src/main/java/org/briarproject/mailbox/android/ui/MailboxViewModel.kt index 921c1b84..a0c0061f 100644 --- a/mailbox-android/src/main/java/org/briarproject/mailbox/android/ui/MailboxViewModel.kt +++ b/mailbox-android/src/main/java/org/briarproject/mailbox/android/ui/MailboxViewModel.kt @@ -80,9 +80,7 @@ class MailboxViewModel @Inject constructor( ) { ls, ts, sc -> when { ls != LifecycleState.RUNNING -> Starting(ls.name) - // TODO waiting for ACTIVE is better than not doing it but to fix #90 we need to listen for - // upload events to the hsdirs - ts != TorPlugin.State.ACTIVE -> Starting(ts.name + " TOR") + ts != TorPlugin.State.PUBLISHED -> Starting(ts.name + " TOR") sc == SetupComplete.FALSE -> { val dm = Resources.getSystem().displayMetrics val size = min(dm.widthPixels, dm.heightPixels) diff --git a/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt b/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt index 1c0c0514..f0028e49 100644 --- a/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt +++ b/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt @@ -150,7 +150,7 @@ class Main : CliktCommand( runBlocking { // wait until Tor becomes active and published the onion service torPlugin.state.takeWhile { state -> - state != TorPlugin.State.ACTIVE + state != TorPlugin.State.PUBLISHED } } qrCodeEncoder.getQrCodeBitMatrix()?.let { diff --git a/mailbox-core/src/main/java/org/briarproject/mailbox/core/setup/QrCodeEncoder.kt b/mailbox-core/src/main/java/org/briarproject/mailbox/core/setup/QrCodeEncoder.kt index 719e137a..e3a41680 100644 --- a/mailbox-core/src/main/java/org/briarproject/mailbox/core/setup/QrCodeEncoder.kt +++ b/mailbox-core/src/main/java/org/briarproject/mailbox/core/setup/QrCodeEncoder.kt @@ -73,7 +73,6 @@ class QrCodeEncoder @Inject constructor( LOG.error("Hidden service address not yet available") return null } - LOG.error(addressString) val addressBytes = Base32.decode(addressString.uppercase()) check(addressBytes.size == 35) { "$addressString not 35 bytes long" } return addressBytes.copyOfRange(0, 32) diff --git a/mailbox-core/src/main/java/org/briarproject/mailbox/core/tor/TorPlugin.java b/mailbox-core/src/main/java/org/briarproject/mailbox/core/tor/TorPlugin.java index 489f57d7..89a4e69f 100644 --- a/mailbox-core/src/main/java/org/briarproject/mailbox/core/tor/TorPlugin.java +++ b/mailbox-core/src/main/java/org/briarproject/mailbox/core/tor/TorPlugin.java @@ -76,6 +76,7 @@ import static org.briarproject.mailbox.core.tor.TorConstants.SETTINGS_NAMESPACE; import static org.briarproject.mailbox.core.tor.TorPlugin.State.ACTIVE; import static org.briarproject.mailbox.core.tor.TorPlugin.State.ENABLING; import static org.briarproject.mailbox.core.tor.TorPlugin.State.INACTIVE; +import static org.briarproject.mailbox.core.tor.TorPlugin.State.PUBLISHED; import static org.briarproject.mailbox.core.tor.TorPlugin.State.STARTING_STOPPING; import static org.briarproject.mailbox.core.util.IoUtils.copyAndClose; import static org.briarproject.mailbox.core.util.IoUtils.tryToClose; @@ -367,11 +368,11 @@ public abstract class TorPlugin s = new Settings(); } String privateKey3 = s.get(HS_PRIVATE_KEY_V3); - publishV3HiddenService(port, privateKey3); + createV3HiddenService(port, privateKey3); } @IoExecutor - private void publishV3HiddenService(String port, @Nullable String privKey) { + private void createV3HiddenService(String port, @Nullable String privKey) { LOG.info("Creating v3 hidden service"); Map<Integer, String> portLines = singletonMap(80, "127.0.0.1:" + port); Map<String, String> response; @@ -400,9 +401,6 @@ public abstract class TorPlugin s.put(HS_ADDRESS_V3, onion3); info(LOG, () -> "V3 hidden service " + scrubOnion(onion3)); - // TODO remove before release - LOG.warn("V3 hidden service: http://" + onion3 + ".onion"); - if (privKey == null) { s.put(HS_PRIVATE_KEY_V3, response.get(HS_PRIVKEY)); try { @@ -411,7 +409,6 @@ public abstract class TorPlugin logException(LOG, e, "Error while merging settings"); } } - state.setServicePublished(); } @Nullable @@ -505,6 +502,7 @@ public abstract class TorPlugin public void unrecognized(String type, String msg) { if (type.equals("HS_DESC") && msg.startsWith("UPLOADED")) { LOG.info("V3 descriptor uploaded"); + state.onServiceDescriptorUploaded(); } } @@ -599,8 +597,9 @@ public abstract class TorPlugin networkInitialised = false, networkEnabled = false, bootstrapped = false, - circuitBuilt = false, - servicePublished = false; + circuitBuilt = false; + @GuardedBy("this") + private int numServiceUploads = 0; @GuardedBy("this") @Nullable @@ -636,8 +635,8 @@ public abstract class TorPlugin return firstCircuit; } - synchronized void setServicePublished() { - servicePublished = true; + synchronized void onServiceDescriptorUploaded() { + numServiceUploads++; state.setValue(getCurrentState()); } @@ -654,8 +653,9 @@ public abstract class TorPlugin } if (!networkInitialised) return ENABLING; if (!networkEnabled) return INACTIVE; - return bootstrapped && circuitBuilt && servicePublished ? - ACTIVE : ENABLING; + if (bootstrapped && circuitBuilt) { + return (numServiceUploads >= 3) ? PUBLISHED : ACTIVE; + } else return ENABLING; } } @@ -677,6 +677,11 @@ public abstract class TorPlugin */ ACTIVE, + /** + * The plugin has published the onion service. + */ + PUBLISHED, + /** * The plugin is enabled but can't make or receive connections */ -- GitLab