HttpConnectionManager.java

  1package eu.siacs.conversations.http;
  2
  3import android.util.Log;
  4
  5import org.apache.http.conn.ssl.StrictHostnameVerifier;
  6
  7import java.net.InetAddress;
  8import java.net.InetSocketAddress;
  9import java.net.Proxy;
 10import java.net.UnknownHostException;
 11import java.security.KeyManagementException;
 12import java.security.NoSuchAlgorithmException;
 13import java.util.ArrayList;
 14import java.util.List;
 15import java.util.concurrent.Executor;
 16import java.util.concurrent.Executors;
 17import java.util.concurrent.TimeUnit;
 18
 19import javax.net.ssl.HostnameVerifier;
 20import javax.net.ssl.SSLSocketFactory;
 21import javax.net.ssl.X509TrustManager;
 22
 23import eu.siacs.conversations.Config;
 24import eu.siacs.conversations.entities.Account;
 25import eu.siacs.conversations.entities.Message;
 26import eu.siacs.conversations.services.AbstractConnectionManager;
 27import eu.siacs.conversations.services.XmppConnectionService;
 28import eu.siacs.conversations.utils.TLSSocketFactory;
 29import okhttp3.HttpUrl;
 30import okhttp3.OkHttpClient;
 31
 32public class HttpConnectionManager extends AbstractConnectionManager {
 33
 34    private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>();
 35    private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
 36
 37    public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
 38
 39    public HttpConnectionManager(XmppConnectionService service) {
 40        super(service);
 41    }
 42
 43    public static Proxy getProxy() {
 44        try {
 45            return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 9050));
 46        } catch (final UnknownHostException e) {
 47            throw new IllegalStateException(e);
 48        }
 49    }
 50
 51    public void createNewDownloadConnection(Message message) {
 52        this.createNewDownloadConnection(message, false);
 53    }
 54
 55    public void createNewDownloadConnection(final Message message, boolean interactive) {
 56        synchronized (this.downloadConnections) {
 57            for(HttpDownloadConnection connection : this.downloadConnections) {
 58                if (connection.getMessage() == message) {
 59                    Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": download already in progress");
 60                    return;
 61                }
 62            }
 63            final HttpDownloadConnection connection = new HttpDownloadConnection(message, this);
 64            connection.init(interactive);
 65            this.downloadConnections.add(connection);
 66        }
 67    }
 68
 69    public void createNewUploadConnection(final Message message, boolean delay) {
 70        synchronized (this.uploadConnections) {
 71            for (HttpUploadConnection connection : this.uploadConnections) {
 72                if (connection.getMessage() == message) {
 73                    Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": upload already in progress");
 74                    return;
 75                }
 76            }
 77            HttpUploadConnection connection = new HttpUploadConnection(message, Method.determine(message.getConversation().getAccount()), this);
 78            connection.init(delay);
 79            this.uploadConnections.add(connection);
 80        }
 81    }
 82
 83    void finishConnection(HttpDownloadConnection connection) {
 84        synchronized (this.downloadConnections) {
 85            this.downloadConnections.remove(connection);
 86        }
 87    }
 88
 89    void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
 90        synchronized (this.uploadConnections) {
 91            this.uploadConnections.remove(httpUploadConnection);
 92        }
 93    }
 94
 95    OkHttpClient buildHttpClient(final HttpUrl url, final Account account, boolean interactive) {
 96        final String slotHostname = url.host();
 97        final boolean onionSlot = slotHostname.endsWith(".onion");
 98        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
 99        //builder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS));
100        builder.writeTimeout(30, TimeUnit.SECONDS);
101        builder.readTimeout(30, TimeUnit.SECONDS);
102        setupTrustManager(builder, interactive);
103        if (mXmppConnectionService.useTorToConnect() || account.isOnion() || onionSlot) {
104            builder.proxy(HttpConnectionManager.getProxy()).build();
105        }
106        return builder.build();
107    }
108
109    private void setupTrustManager(final OkHttpClient.Builder builder, final boolean interactive) {
110        final X509TrustManager trustManager;
111        final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
112        if (interactive) {
113            trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
114        } else {
115            trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
116        }
117        try {
118            final SSLSocketFactory sf = new TLSSocketFactory(new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG());
119            builder.sslSocketFactory(sf, trustManager);
120            builder.hostnameVerifier(hostnameVerifier);
121        } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
122        }
123    }
124}