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.ArrayList;
15import java.util.List;
16import java.util.concurrent.Executor;
17import java.util.concurrent.Executors;
18
19import javax.net.ssl.HostnameVerifier;
20import javax.net.ssl.HttpsURLConnection;
21import javax.net.ssl.SSLSocketFactory;
22import javax.net.ssl.X509TrustManager;
23
24import eu.siacs.conversations.Config;
25import eu.siacs.conversations.entities.Account;
26import eu.siacs.conversations.entities.Message;
27import eu.siacs.conversations.services.AbstractConnectionManager;
28import eu.siacs.conversations.services.XmppConnectionService;
29import eu.siacs.conversations.utils.TLSSocketFactory;
30
31public class HttpConnectionManager extends AbstractConnectionManager {
32
33 private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>();
34 private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
35
36 public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
37
38 public HttpConnectionManager(XmppConnectionService service) {
39 super(service);
40 }
41
42 public static Proxy getProxy() throws IOException {
43 return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 9050));
44 }
45
46 public void createNewDownloadConnection(Message message) {
47 this.createNewDownloadConnection(message, false);
48 }
49
50 public void createNewDownloadConnection(final Message message, boolean interactive) {
51 synchronized (this.downloadConnections) {
52 for(HttpDownloadConnection connection : this.downloadConnections) {
53 if (connection.getMessage() == message) {
54 Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": download already in progress");
55 return;
56 }
57 }
58 final HttpDownloadConnection connection = new HttpDownloadConnection(message, this);
59 connection.init(interactive);
60 this.downloadConnections.add(connection);
61 }
62 }
63
64 public void createNewUploadConnection(final Message message, boolean delay) {
65 synchronized (this.uploadConnections) {
66 for (HttpUploadConnection connection : this.uploadConnections) {
67 if (connection.getMessage() == message) {
68 Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": upload already in progress");
69 return;
70 }
71 }
72 HttpUploadConnection connection = new HttpUploadConnection(message, Method.determine(message.getConversation().getAccount()), this);
73 connection.init(delay);
74 this.uploadConnections.add(connection);
75 }
76 }
77
78 public boolean checkConnection(Message message) {
79 final Account account = message.getConversation().getAccount();
80 final URL url = message.getFileParams().url;
81 if (url.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME) && account.getStatus() != Account.State.ONLINE) {
82 return false;
83 }
84 return mXmppConnectionService.hasInternetConnection();
85 }
86
87 void finishConnection(HttpDownloadConnection connection) {
88 synchronized (this.downloadConnections) {
89 this.downloadConnections.remove(connection);
90 }
91 }
92
93 void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
94 synchronized (this.uploadConnections) {
95 this.uploadConnections.remove(httpUploadConnection);
96 }
97 }
98
99 void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) {
100 final X509TrustManager trustManager;
101 final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
102 if (interactive) {
103 trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
104 } else {
105 trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
106 }
107 try {
108 final SSLSocketFactory sf = new TLSSocketFactory(new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG());
109 connection.setSSLSocketFactory(sf);
110 connection.setHostnameVerifier(hostnameVerifier);
111 } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
112 }
113 }
114}