use okhttp to fetch captcha

This commit is contained in:
Daniel Gultsch 2021-03-22 10:39:53 +01:00
parent 1cd95aefa6
commit ce7f59a76c
3 changed files with 35 additions and 7 deletions

View file

@ -4,6 +4,8 @@ import android.util.Log;
import org.apache.http.conn.ssl.StrictHostnameVerifier; import org.apache.http.conn.ssl.StrictHostnameVerifier;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
@ -28,6 +30,8 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.TLSSocketFactory; import eu.siacs.conversations.utils.TLSSocketFactory;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
public class HttpConnectionManager extends AbstractConnectionManager { public class HttpConnectionManager extends AbstractConnectionManager {
@ -121,4 +125,22 @@ public class HttpConnectionManager extends AbstractConnectionManager {
} catch (final KeyManagementException | NoSuchAlgorithmException ignored) { } 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();
}
} }

View file

@ -11,7 +11,6 @@ public class URL {
public static final List<String> WELL_KNOWN_SCHEMES = Arrays.asList("http", "https", AesGcmURL.PROTOCOL_NAME); public static final List<String> WELL_KNOWN_SCHEMES = Arrays.asList("http", "https", AesGcmURL.PROTOCOL_NAME);
public static String tryParse(String url) { public static String tryParse(String url) {
final URI uri; final URI uri;
try { try {

View file

@ -22,7 +22,6 @@ import java.net.IDN;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; 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.Message;
import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.entities.ServiceDiscoveryResult;
import eu.siacs.conversations.generator.IqGenerator; import eu.siacs.conversations.generator.IqGenerator;
import eu.siacs.conversations.http.HttpConnectionManager;
import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.MemorizingTrustManager; import eu.siacs.conversations.services.MemorizingTrustManager;
import eu.siacs.conversations.services.MessageArchiveService; 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.TagWriter;
import eu.siacs.conversations.xml.XmlReader; import eu.siacs.conversations.xml.XmlReader;
import eu.siacs.conversations.xmpp.forms.Data; 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.OnJinglePacketReceived;
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket; import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
import eu.siacs.conversations.xmpp.stanzas.AbstractAcknowledgeableStanza; import eu.siacs.conversations.xmpp.stanzas.AbstractAcknowledgeableStanza;
@ -973,11 +972,19 @@ public class XmppConnection implements Runnable {
is = null; is = null;
} }
} else { } else {
final boolean useTor = mXmppConnectionService.useTorToConnect() || account.isOnion();
try { try {
final Field field = data.getFieldByName("url"); final String url = data.getValue("url");
final URL url = field != null && field.getValue() != null ? new URL(field.getValue()) : null; final String fallbackUrl = data.getValue("captcha-fallback-url");
is = url != null ? url.openStream() : null; if (url != null) {
} catch (IOException e) { 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; is = null;
} }
} }