HttpConnectionManager.java

  1package eu.siacs.conversations.http;
  2
  3import android.os.Build;
  4
  5import org.apache.http.conn.ssl.StrictHostnameVerifier;
  6
  7import java.io.IOException;
  8import java.net.InetAddress;
  9import java.net.InetSocketAddress;
 10import java.net.Proxy;
 11import java.security.KeyManagementException;
 12import java.security.NoSuchAlgorithmException;
 13import java.util.List;
 14import java.util.concurrent.CopyOnWriteArrayList;
 15
 16import javax.net.ssl.HostnameVerifier;
 17import javax.net.ssl.HttpsURLConnection;
 18import javax.net.ssl.SSLContext;
 19import javax.net.ssl.SSLSocketFactory;
 20import javax.net.ssl.X509TrustManager;
 21
 22import eu.siacs.conversations.entities.Message;
 23import eu.siacs.conversations.services.AbstractConnectionManager;
 24import eu.siacs.conversations.services.XmppConnectionService;
 25import eu.siacs.conversations.utils.CryptoHelper;
 26import eu.siacs.conversations.utils.SSLSocketHelper;
 27
 28public class HttpConnectionManager extends AbstractConnectionManager {
 29
 30	public HttpConnectionManager(XmppConnectionService service) {
 31		super(service);
 32	}
 33
 34	private List<HttpDownloadConnection> downloadConnections = new CopyOnWriteArrayList<>();
 35	private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>();
 36
 37	public HttpDownloadConnection createNewDownloadConnection(Message message) {
 38		return this.createNewDownloadConnection(message, false);
 39	}
 40
 41	public HttpDownloadConnection createNewDownloadConnection(Message message, boolean interactive) {
 42		HttpDownloadConnection connection = new HttpDownloadConnection(this);
 43		connection.init(message,interactive);
 44		this.downloadConnections.add(connection);
 45		return connection;
 46	}
 47
 48	public HttpUploadConnection createNewUploadConnection(Message message, boolean delay) {
 49		HttpUploadConnection connection = new HttpUploadConnection(this);
 50		connection.init(message,delay);
 51		this.uploadConnections.add(connection);
 52		return connection;
 53	}
 54
 55	public void finishConnection(HttpDownloadConnection connection) {
 56		this.downloadConnections.remove(connection);
 57	}
 58
 59	public void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
 60		this.uploadConnections.remove(httpUploadConnection);
 61	}
 62
 63	public void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) {
 64		final X509TrustManager trustManager;
 65		final HostnameVerifier hostnameVerifier;
 66		if (interactive) {
 67			trustManager = mXmppConnectionService.getMemorizingTrustManager();
 68			hostnameVerifier = mXmppConnectionService
 69					.getMemorizingTrustManager().wrapHostnameVerifier(
 70							new StrictHostnameVerifier());
 71		} else {
 72			trustManager = mXmppConnectionService.getMemorizingTrustManager()
 73					.getNonInteractive();
 74			hostnameVerifier = mXmppConnectionService
 75					.getMemorizingTrustManager()
 76					.wrapHostnameVerifierNonInteractive(
 77							new StrictHostnameVerifier());
 78		}
 79		try {
 80			final SSLContext sc = SSLSocketHelper.getSSLContext();
 81			sc.init(null, new X509TrustManager[]{trustManager},
 82					mXmppConnectionService.getRNG());
 83
 84			final SSLSocketFactory sf = sc.getSocketFactory();
 85			final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
 86					sf.getSupportedCipherSuites());
 87			if (cipherSuites.length > 0) {
 88				sc.getDefaultSSLParameters().setCipherSuites(cipherSuites);
 89
 90			}
 91
 92			connection.setSSLSocketFactory(sf);
 93			connection.setHostnameVerifier(hostnameVerifier);
 94		} catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
 95		}
 96	}
 97
 98	public Proxy getProxy() throws IOException {
 99		return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getLocalHost(), 8118));
100	}
101}