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