HttpConnectionManager.java

 1package eu.siacs.conversations.http;
 2
 3import android.util.Log;
 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.SSLSocketFactory;
19import javax.net.ssl.X509TrustManager;
20
21import eu.siacs.conversations.Config;
22import eu.siacs.conversations.entities.Account;
23import eu.siacs.conversations.entities.Message;
24import eu.siacs.conversations.services.AbstractConnectionManager;
25import eu.siacs.conversations.services.XmppConnectionService;
26import eu.siacs.conversations.utils.TLSSocketFactory;
27import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
28
29public class HttpConnectionManager extends AbstractConnectionManager {
30
31	public HttpConnectionManager(XmppConnectionService service) {
32		super(service);
33	}
34
35	private List<HttpDownloadConnection> downloadConnections = new CopyOnWriteArrayList<>();
36	private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>();
37
38	public HttpDownloadConnection createNewDownloadConnection(Message message) {
39		return this.createNewDownloadConnection(message, false);
40	}
41
42	public HttpDownloadConnection createNewDownloadConnection(Message message, boolean interactive) {
43		HttpDownloadConnection connection = new HttpDownloadConnection(this);
44		connection.init(message,interactive);
45		this.downloadConnections.add(connection);
46		return connection;
47	}
48
49	public void createNewUploadConnection(Message message, boolean delay) {
50		HttpUploadConnection connection = new HttpUploadConnection(Method.determine(message.getConversation().getAccount()), this);
51		connection.init(message,delay);
52		this.uploadConnections.add(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 = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
66		if (interactive) {
67			trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
68		} else {
69			trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
70		}
71		try {
72			final SSLSocketFactory sf = new TLSSocketFactory(new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG());
73			connection.setSSLSocketFactory(sf);
74			connection.setHostnameVerifier(hostnameVerifier);
75		} catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
76		}
77	}
78
79	public static Proxy getProxy() throws IOException {
80		return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127,0,0,1}), 8118));
81	}
82}