1package eu.siacs.conversations.http;
2
3import android.os.Build;
4import android.util.Log;
5
6import org.apache.http.conn.ssl.StrictHostnameVerifier;
7
8import java.io.IOException;
9import java.io.InputStream;
10import java.net.InetAddress;
11import java.net.InetSocketAddress;
12import java.net.Proxy;
13import java.net.UnknownHostException;
14import java.security.KeyManagementException;
15import java.security.NoSuchAlgorithmException;
16import java.util.ArrayList;
17import java.util.List;
18import java.util.concurrent.Executor;
19import java.util.concurrent.Executors;
20import java.util.concurrent.TimeUnit;
21
22import javax.net.ssl.HostnameVerifier;
23import javax.net.ssl.SSLSocketFactory;
24import javax.net.ssl.X509TrustManager;
25
26import eu.siacs.conversations.Config;
27import eu.siacs.conversations.entities.Account;
28import eu.siacs.conversations.entities.Message;
29import eu.siacs.conversations.services.AbstractConnectionManager;
30import eu.siacs.conversations.services.XmppConnectionService;
31import eu.siacs.conversations.utils.TLSSocketFactory;
32import okhttp3.HttpUrl;
33import okhttp3.OkHttpClient;
34import okhttp3.Request;
35import okhttp3.ResponseBody;
36
37public class HttpConnectionManager extends AbstractConnectionManager {
38
39 private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>();
40 private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
41
42 public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
43
44 private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient();
45
46 public HttpConnectionManager(XmppConnectionService service) {
47 super(service);
48 }
49
50 public static Proxy getProxy() {
51 final InetAddress localhost;
52 try {
53 localhost = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
54 } catch (final UnknownHostException e) {
55 throw new IllegalStateException(e);
56 }
57 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
58 return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(localhost, 9050));
59 } else {
60 return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(localhost, 8118));
61 }
62 }
63
64 public void createNewDownloadConnection(Message message) {
65 this.createNewDownloadConnection(message, false);
66 }
67
68 public void createNewDownloadConnection(final Message message, boolean interactive) {
69 synchronized (this.downloadConnections) {
70 for (HttpDownloadConnection connection : this.downloadConnections) {
71 if (connection.getMessage() == message) {
72 Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": download already in progress");
73 return;
74 }
75 }
76 final HttpDownloadConnection connection = new HttpDownloadConnection(message, this);
77 connection.init(interactive);
78 this.downloadConnections.add(connection);
79 }
80 }
81
82 public void createNewUploadConnection(final Message message, boolean delay) {
83 synchronized (this.uploadConnections) {
84 for (HttpUploadConnection connection : this.uploadConnections) {
85 if (connection.getMessage() == message) {
86 Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": upload already in progress");
87 return;
88 }
89 }
90 HttpUploadConnection connection = new HttpUploadConnection(message, Method.determine(message.getConversation().getAccount()), this);
91 connection.init(delay);
92 this.uploadConnections.add(connection);
93 }
94 }
95
96 void finishConnection(HttpDownloadConnection connection) {
97 synchronized (this.downloadConnections) {
98 this.downloadConnections.remove(connection);
99 }
100 }
101
102 void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
103 synchronized (this.uploadConnections) {
104 this.uploadConnections.remove(httpUploadConnection);
105 }
106 }
107
108 OkHttpClient buildHttpClient(final HttpUrl url, final Account account, boolean interactive) {
109 return buildHttpClient(url, account, 30, interactive);
110 }
111
112 OkHttpClient buildHttpClient(final HttpUrl url, final Account account, int readTimeout, boolean interactive) {
113 final String slotHostname = url.host();
114 final boolean onionSlot = slotHostname.endsWith(".onion");
115 final OkHttpClient.Builder builder = OK_HTTP_CLIENT.newBuilder();
116 builder.writeTimeout(30, TimeUnit.SECONDS);
117 builder.readTimeout(readTimeout, TimeUnit.SECONDS);
118 setupTrustManager(builder, interactive);
119 if (mXmppConnectionService.useTorToConnect() || account.isOnion() || onionSlot) {
120 builder.proxy(HttpConnectionManager.getProxy()).build();
121 }
122 return builder.build();
123 }
124
125 private void setupTrustManager(final OkHttpClient.Builder builder, final boolean interactive) {
126 final X509TrustManager trustManager;
127 final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
128 if (interactive) {
129 trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
130 } else {
131 trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
132 }
133 try {
134 final SSLSocketFactory sf = new TLSSocketFactory(new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG());
135 builder.sslSocketFactory(sf, trustManager);
136 builder.hostnameVerifier(hostnameVerifier);
137 } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
138 }
139 }
140
141 public static InputStream open(final String url, final boolean tor) throws IOException {
142 return open(HttpUrl.get(url), tor);
143 }
144
145 public static InputStream open(final HttpUrl httpUrl, final boolean tor) throws IOException {
146 final OkHttpClient.Builder builder = OK_HTTP_CLIENT.newBuilder();
147 if (tor) {
148 builder.proxy(HttpConnectionManager.getProxy()).build();
149 }
150 final OkHttpClient client = builder.build();
151 final Request request = new Request.Builder().get().url(httpUrl).build();
152 final ResponseBody body = client.newCall(request).execute().body();
153 if (body == null) {
154 throw new IOException("No response body found");
155 }
156 return body.byteStream();
157 }
158}