HttpConnectionManager.java

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