run Tor connection via hostname only if extended connection ui is visible

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/entities/Account.java    |  5 +
src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java | 17 ++++
src/main/res/menu/message_context.xml                         |  5 
3 files changed, 22 insertions(+), 5 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/entities/Account.java 🔗

@@ -317,6 +317,11 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable
         return server != null && server.endsWith(".onion");
     }
 
+    public boolean isDirectToOnion() {
+        final var hostname = Strings.nullToEmpty(this.hostname).trim();
+        return isOnion() && (hostname.isEmpty() || hostname.endsWith(".onion"));
+    }
+
     public int getPort() {
         return this.port;
     }

src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java 🔗

@@ -304,8 +304,9 @@ public class XmppConnection implements Runnable {
             Socket localSocket;
             shouldAuthenticate = !account.isOptionSet(Account.OPTION_REGISTER);
             this.changeStatus(Account.State.CONNECTING);
-            final boolean useTor = mXmppConnectionService.useTorToConnect() || account.isOnion();
+            final boolean useTorSetting = mXmppConnectionService.useTorToConnect();
             final boolean extended = mXmppConnectionService.showExtendedConnectionOptions();
+            final boolean useTor = useTorSetting || account.isOnion();
             // TODO collapse Tor usage into normal connection code path
             if (useTor) {
                 final var seeOtherHost = this.seeOtherHostResolverResult;
@@ -323,7 +324,15 @@ public class XmppConnection implements Runnable {
                                     Resolver.fromHardCoded(
                                             account.getServer(), Resolver.XMPP_PORT_STARTTLS));
                 } else {
-                    viaTor = Iterables.getOnlyElement(Resolver.fromHardCoded(hostname, port));
+                    if (useTorSetting || extended) {
+                        // if the hostname configuration is showing we can take it
+                        viaTor = Iterables.getOnlyElement(Resolver.fromHardCoded(hostname, port));
+                    } else {
+                        viaTor =
+                                Iterables.getOnlyElement(
+                                        Resolver.fromHardCoded(
+                                                account.getServer(), Resolver.XMPP_PORT_STARTTLS));
+                    }
                     this.verifiedHostname = hostname;
                 }
 
@@ -1567,7 +1576,9 @@ public class XmppConnection implements Runnable {
     }
 
     private boolean isSecure() {
-        return features.encryptionEnabled || Config.ALLOW_NON_TLS_CONNECTIONS || account.isOnion();
+        return (features.encryptionEnabled && this.socket instanceof SSLSocket)
+                || Config.ALLOW_NON_TLS_CONNECTIONS
+                || account.isDirectToOnion();
     }
 
     private void authenticate(final SaslMechanism.Version version) throws IOException {

src/main/res/menu/message_context.xml 🔗

@@ -55,9 +55,10 @@
         android:id="@+id/send_again"
         android:title="@string/send_again"
         android:visible="false" />
-    <item android:id="@+id/send_again_as_p2p"
+    <item
+        android:id="@+id/send_again_as_p2p"
         android:title="@string/retry_with_p2p"
-        android:visible="false"/>
+        android:visible="false" />
     <item
         android:id="@+id/download_file"
         android:title="@string/download_x_file"