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 HttpUploadConnection createNewUploadConnection(Message message, boolean delay) {
50 HttpUploadConnection connection = new HttpUploadConnection(this);
51 connection.init(message,delay);
52 this.uploadConnections.add(connection);
53 return connection;
54 }
55
56 public void finishConnection(HttpDownloadConnection connection) {
57 this.downloadConnections.remove(connection);
58 }
59
60 public void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
61 this.uploadConnections.remove(httpUploadConnection);
62 }
63
64 public void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) {
65 final X509TrustManager trustManager;
66 final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
67 if (interactive) {
68 trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
69 } else {
70 trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
71 }
72 try {
73 final SSLSocketFactory sf = new TLSSocketFactory(new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG());
74 connection.setSSLSocketFactory(sf);
75 connection.setHostnameVerifier(hostnameVerifier);
76 } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
77 }
78 }
79
80 public static Proxy getProxy() throws IOException {
81 return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127,0,0,1}), 8118));
82 }
83}