From ce7f59a76c474533af61f4a6c0c18e9e118bb491 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 22 Mar 2021 10:39:53 +0100 Subject: [PATCH] use okhttp to fetch captcha --- .../http/HttpConnectionManager.java | 22 +++++++++++++++++++ .../java/eu/siacs/conversations/http/URL.java | 1 - .../conversations/xmpp/XmppConnection.java | 19 +++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java index 64b49f369..8186040c9 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java @@ -4,6 +4,8 @@ import android.util.Log; import org.apache.http.conn.ssl.StrictHostnameVerifier; +import java.io.IOException; +import java.io.InputStream; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Proxy; @@ -28,6 +30,8 @@ import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.TLSSocketFactory; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.ResponseBody; public class HttpConnectionManager extends AbstractConnectionManager { @@ -121,4 +125,22 @@ public class HttpConnectionManager extends AbstractConnectionManager { } catch (final KeyManagementException | NoSuchAlgorithmException ignored) { } } + + public static InputStream open(final String url, final boolean tor) throws IOException { + return open(HttpUrl.get(url), tor); + } + + public static InputStream open(final HttpUrl httpUrl, final boolean tor) throws IOException { + final OkHttpClient.Builder builder = new OkHttpClient.Builder(); + if (tor) { + builder.proxy(HttpConnectionManager.getProxy()).build(); + } + final OkHttpClient client = builder.build(); + final Request request = new Request.Builder().get().url(httpUrl).build(); + final ResponseBody body = client.newCall(request).execute().body(); + if (body == null) { + throw new IOException("No response body found"); + } + return body.byteStream(); + } } diff --git a/src/main/java/eu/siacs/conversations/http/URL.java b/src/main/java/eu/siacs/conversations/http/URL.java index f51391561..e294ed8a0 100644 --- a/src/main/java/eu/siacs/conversations/http/URL.java +++ b/src/main/java/eu/siacs/conversations/http/URL.java @@ -11,7 +11,6 @@ public class URL { public static final List WELL_KNOWN_SCHEMES = Arrays.asList("http", "https", AesGcmURL.PROTOCOL_NAME); - public static String tryParse(String url) { final URI uri; try { diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 42afba49f..6baa65256 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -22,7 +22,6 @@ import java.net.IDN; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; -import java.net.URL; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; @@ -69,6 +68,7 @@ import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.generator.IqGenerator; +import eu.siacs.conversations.http.HttpConnectionManager; import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.services.MemorizingTrustManager; import eu.siacs.conversations.services.MessageArchiveService; @@ -87,7 +87,6 @@ import eu.siacs.conversations.xml.Tag; import eu.siacs.conversations.xml.TagWriter; import eu.siacs.conversations.xml.XmlReader; import eu.siacs.conversations.xmpp.forms.Data; -import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived; import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket; import eu.siacs.conversations.xmpp.stanzas.AbstractAcknowledgeableStanza; @@ -973,11 +972,19 @@ public class XmppConnection implements Runnable { is = null; } } else { + final boolean useTor = mXmppConnectionService.useTorToConnect() || account.isOnion(); try { - final Field field = data.getFieldByName("url"); - final URL url = field != null && field.getValue() != null ? new URL(field.getValue()) : null; - is = url != null ? url.openStream() : null; - } catch (IOException e) { + final String url = data.getValue("url"); + final String fallbackUrl = data.getValue("captcha-fallback-url"); + if (url != null) { + is = HttpConnectionManager.open(url, useTor); + } else if (fallbackUrl != null) { + is = HttpConnectionManager.open(fallbackUrl, useTor); + } else { + is = null; + } + } catch (final IOException e) { + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": unable to fetch captcha", e); is = null; } }