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;
26
27public class HttpConnectionManager extends AbstractConnectionManager {
28
29 public HttpConnectionManager(XmppConnectionService service) {
30 super(service);
31 }
32
33 private List<HttpDownloadConnection> downloadConnections = new CopyOnWriteArrayList<>();
34 private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>();
35
36 public HttpDownloadConnection createNewDownloadConnection(Message message) {
37 return this.createNewDownloadConnection(message, false);
38 }
39
40 public HttpDownloadConnection createNewDownloadConnection(Message message, boolean interactive) {
41 HttpDownloadConnection connection = new HttpDownloadConnection(this);
42 connection.init(message,interactive);
43 this.downloadConnections.add(connection);
44 return connection;
45 }
46
47 public HttpUploadConnection createNewUploadConnection(Message message, boolean delay) {
48 HttpUploadConnection connection = new HttpUploadConnection(this);
49 connection.init(message,delay);
50 this.uploadConnections.add(connection);
51 return connection;
52 }
53
54 public void finishConnection(HttpDownloadConnection connection) {
55 this.downloadConnections.remove(connection);
56 }
57
58 public void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
59 this.uploadConnections.remove(httpUploadConnection);
60 }
61
62 public void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) {
63 final X509TrustManager trustManager;
64 final HostnameVerifier hostnameVerifier;
65 if (interactive) {
66 trustManager = mXmppConnectionService.getMemorizingTrustManager();
67 hostnameVerifier = mXmppConnectionService
68 .getMemorizingTrustManager().wrapHostnameVerifier(
69 new StrictHostnameVerifier());
70 } else {
71 trustManager = mXmppConnectionService.getMemorizingTrustManager()
72 .getNonInteractive();
73 hostnameVerifier = mXmppConnectionService
74 .getMemorizingTrustManager()
75 .wrapHostnameVerifierNonInteractive(
76 new StrictHostnameVerifier());
77 }
78 try {
79 final SSLContext sc = SSLContext.getInstance("TLS");
80 sc.init(null, new X509TrustManager[]{trustManager},
81 mXmppConnectionService.getRNG());
82
83 final SSLSocketFactory sf = sc.getSocketFactory();
84 final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
85 sf.getSupportedCipherSuites());
86 if (cipherSuites.length > 0) {
87 sc.getDefaultSSLParameters().setCipherSuites(cipherSuites);
88
89 }
90
91 connection.setSSLSocketFactory(sf);
92 connection.setHostnameVerifier(hostnameVerifier);
93 } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
94 }
95 }
96
97 public Proxy getProxy() throws IOException {
98 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
99 return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(InetAddress.getLocalHost(), 9050));
100 } else {
101 return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getLocalHost(), 8118));
102 }
103 }
104}