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