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.net.URL;
12import java.security.KeyManagementException;
13import java.security.NoSuchAlgorithmException;
14import java.util.List;
15import java.util.concurrent.CopyOnWriteArrayList;
16
17import javax.net.ssl.HostnameVerifier;
18import javax.net.ssl.HttpsURLConnection;
19import javax.net.ssl.SSLSocketFactory;
20import javax.net.ssl.X509TrustManager;
21
22import eu.siacs.conversations.Config;
23import eu.siacs.conversations.entities.Account;
24import eu.siacs.conversations.entities.Message;
25import eu.siacs.conversations.services.AbstractConnectionManager;
26import eu.siacs.conversations.services.XmppConnectionService;
27import eu.siacs.conversations.utils.TLSSocketFactory;
28import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
29
30public class HttpConnectionManager extends AbstractConnectionManager {
31
32 public HttpConnectionManager(XmppConnectionService service) {
33 super(service);
34 }
35
36 private List<HttpDownloadConnection> downloadConnections = new CopyOnWriteArrayList<>();
37 private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>();
38
39 public HttpDownloadConnection createNewDownloadConnection(Message message) {
40 return this.createNewDownloadConnection(message, false);
41 }
42
43 public HttpDownloadConnection createNewDownloadConnection(Message message, boolean interactive) {
44 HttpDownloadConnection connection = new HttpDownloadConnection(this);
45 connection.init(message,interactive);
46 this.downloadConnections.add(connection);
47 return connection;
48 }
49
50 public void createNewUploadConnection(Message message, boolean delay) {
51 HttpUploadConnection connection = new HttpUploadConnection(Method.determine(message.getConversation().getAccount()), this);
52 connection.init(message,delay);
53 this.uploadConnections.add(connection);
54 }
55
56 public boolean checkConnection(Message message) {
57 final Account account = message.getConversation().getAccount();
58 final URL url = message.getFileParams().url;
59 if (url.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME) && account.getStatus() != Account.State.ONLINE) {
60 return false;
61 }
62 return mXmppConnectionService.hasInternetConnection();
63 }
64
65 public void finishConnection(HttpDownloadConnection connection) {
66 this.downloadConnections.remove(connection);
67 }
68
69 public void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
70 this.uploadConnections.remove(httpUploadConnection);
71 }
72
73 public void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) {
74 final X509TrustManager trustManager;
75 final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
76 if (interactive) {
77 trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
78 } else {
79 trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
80 }
81 try {
82 final SSLSocketFactory sf = new TLSSocketFactory(new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG());
83 connection.setSSLSocketFactory(sf);
84 connection.setHostnameVerifier(hostnameVerifier);
85 } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
86 }
87 }
88
89 public static Proxy getProxy() throws IOException {
90 return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127,0,0,1}), 8118));
91 }
92}