1package eu.siacs.conversations.http;
2
3import android.os.PowerManager;
4import android.support.annotation.Nullable;
5import android.util.Log;
6
7import com.google.common.io.ByteStreams;
8
9import java.io.BufferedInputStream;
10import java.io.FileInputStream;
11import java.io.IOException;
12import java.io.InputStream;
13import java.io.OutputStream;
14import java.net.HttpURLConnection;
15import java.net.MalformedURLException;
16import java.net.URL;
17import java.util.concurrent.CancellationException;
18
19import javax.net.ssl.HttpsURLConnection;
20import javax.net.ssl.SSLHandshakeException;
21
22import eu.siacs.conversations.Config;
23import eu.siacs.conversations.R;
24import eu.siacs.conversations.entities.Account;
25import eu.siacs.conversations.entities.DownloadableFile;
26import eu.siacs.conversations.entities.Message;
27import eu.siacs.conversations.entities.Transferable;
28import eu.siacs.conversations.persistance.FileBackend;
29import eu.siacs.conversations.services.AbstractConnectionManager;
30import eu.siacs.conversations.services.XmppConnectionService;
31import eu.siacs.conversations.utils.CryptoHelper;
32import eu.siacs.conversations.utils.FileWriterException;
33import eu.siacs.conversations.utils.WakeLockHelper;
34import eu.siacs.conversations.xmpp.OnIqPacketReceived;
35import eu.siacs.conversations.xmpp.stanzas.IqPacket;
36import rocks.xmpp.addr.Jid;
37
38public class HttpDownloadConnection implements Transferable {
39
40 private HttpConnectionManager mHttpConnectionManager;
41 private XmppConnectionService mXmppConnectionService;
42
43 private URL mUrl;
44 private final Message message;
45 private DownloadableFile file;
46 private int mStatus = Transferable.STATUS_UNKNOWN;
47 private boolean acceptedAutomatically = false;
48 private int mProgress = 0;
49 private final boolean mUseTor;
50 private boolean canceled = false;
51 private Method method = Method.HTTP_UPLOAD;
52
53 HttpDownloadConnection(Message message, HttpConnectionManager manager) {
54 this.message = message;
55 this.mHttpConnectionManager = manager;
56 this.mXmppConnectionService = manager.getXmppConnectionService();
57 this.mUseTor = mXmppConnectionService.useTorToConnect();
58 }
59
60 @Override
61 public boolean start() {
62 if (mXmppConnectionService.hasInternetConnection()) {
63 if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) {
64 checkFileSize(true);
65 } else {
66 download(true);
67 }
68 return true;
69 } else {
70 return false;
71 }
72 }
73
74 public void init(boolean interactive) {
75 this.message.setTransferable(this);
76 try {
77 final Message.FileParams fileParams = message.getFileParams();
78 if (message.hasFileOnRemoteHost()) {
79 mUrl = CryptoHelper.toHttpsUrl(fileParams.url);
80 } else if (message.isOOb() && fileParams.url != null && fileParams.size > 0) {
81 mUrl = fileParams.url;
82 } else {
83 mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody().split("\n")[0]));
84 }
85 final AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(mUrl.getPath());
86 if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
87 this.message.setEncryption(Message.ENCRYPTION_PGP);
88 } else if (message.getEncryption() != Message.ENCRYPTION_OTR
89 && message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
90 this.message.setEncryption(Message.ENCRYPTION_NONE);
91 }
92 final String ext;
93 if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
94 ext = extension.secondary;
95 } else {
96 ext = extension.main;
97 }
98 message.setRelativeFilePath(message.getUuid() + (ext != null ? ("." + ext) : ""));
99 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
100 this.file = new DownloadableFile(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + message.getUuid());
101 Log.d(Config.LOGTAG, "create temporary OMEMO encrypted file: " + this.file.getAbsolutePath() + "(" + message.getMimeType() + ")");
102 } else {
103 this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
104 }
105 final String reference = mUrl.getRef();
106 if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {
107 this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
108 }
109
110 if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
111 this.message.setEncryption(Message.ENCRYPTION_NONE);
112 }
113 method = mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME) ? Method.P1_S3 : Method.HTTP_UPLOAD;
114 long knownFileSize = message.getFileParams().size;
115 if (knownFileSize > 0 && interactive && method != Method.P1_S3) {
116 this.file.setExpectedSize(knownFileSize);
117 download(true);
118 } else {
119 checkFileSize(interactive);
120 }
121 } catch (MalformedURLException e) {
122 this.cancel();
123 }
124 }
125
126 private void download(boolean interactive) {
127 new Thread(new FileDownloader(interactive)).start();
128 }
129
130 private void checkFileSize(boolean interactive) {
131 new Thread(new FileSizeChecker(interactive)).start();
132 }
133
134 @Override
135 public void cancel() {
136 this.canceled = true;
137 mHttpConnectionManager.finishConnection(this);
138 message.setTransferable(null);
139 if (message.isFileOrImage()) {
140 message.setDeleted(true);
141 }
142 mHttpConnectionManager.updateConversationUi(true);
143 }
144
145 private void decryptOmemoFile() {
146 final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
147
148 if (outputFile.getParentFile().mkdirs()) {
149 Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath());
150 }
151
152 try {
153 outputFile.createNewFile();
154 final InputStream is = new FileInputStream(this.file);
155
156 outputFile.setKey(this.file.getKey());
157 outputFile.setIv(this.file.getIv());
158 final OutputStream os = AbstractConnectionManager.createOutputStream(outputFile, false, true);
159
160 ByteStreams.copy(is, os);
161
162 FileBackend.close(is);
163 FileBackend.close(os);
164
165 if (!file.delete()) {
166 Log.w(Config.LOGTAG,"unable to delete temporary OMEMO encrypted file " + file.getAbsolutePath());
167 }
168
169 message.setRelativeFilePath(outputFile.getPath());
170 } catch (IOException e) {
171 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
172 mXmppConnectionService.updateMessage(message);
173 }
174 }
175
176 private void finish() throws Exception {
177 message.setTransferable(null);
178 mHttpConnectionManager.finishConnection(this);
179 boolean notify = acceptedAutomatically && !message.isRead();
180 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
181 notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
182 }
183 mHttpConnectionManager.updateConversationUi(true);
184 final boolean notifyAfterScan = notify;
185 mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> {
186 if (notifyAfterScan) {
187 mXmppConnectionService.getNotificationService().push(message);
188 }
189 });
190 }
191
192 private void decryptIfNeeded() {
193 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
194 decryptOmemoFile();
195 }
196 }
197
198 private void changeStatus(int status) {
199 this.mStatus = status;
200 mHttpConnectionManager.updateConversationUi(true);
201 }
202
203 private void showToastForException(Exception e) {
204 if (e instanceof java.net.UnknownHostException) {
205 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
206 } else if (e instanceof java.net.ConnectException) {
207 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
208 } else if (e instanceof FileWriterException) {
209 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
210 } else if (!(e instanceof CancellationException)) {
211 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
212 }
213 }
214
215 private void updateProgress(long i) {
216 this.mProgress = (int) i;
217 mHttpConnectionManager.updateConversationUi(false);
218 }
219
220 @Override
221 public int getStatus() {
222 return this.mStatus;
223 }
224
225 @Override
226 public long getFileSize() {
227 if (this.file != null) {
228 return this.file.getExpectedSize();
229 } else {
230 return 0;
231 }
232 }
233
234 @Override
235 public int getProgress() {
236 return this.mProgress;
237 }
238
239 public Message getMessage() {
240 return message;
241 }
242
243 private class FileSizeChecker implements Runnable {
244
245 private final boolean interactive;
246
247 FileSizeChecker(boolean interactive) {
248 this.interactive = interactive;
249 }
250
251
252 @Override
253 public void run() {
254 if (mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME)) {
255 retrieveUrl();
256 } else {
257 check();
258 }
259 }
260
261 private void retrieveUrl() {
262 changeStatus(STATUS_CHECKING);
263 final Account account = message.getConversation().getAccount();
264 IqPacket request = mXmppConnectionService.getIqGenerator().requestP1S3Url(Jid.of(account.getJid().getDomain()), mUrl.getHost());
265 mXmppConnectionService.sendIqPacket(message.getConversation().getAccount(), request, (a, packet) -> {
266 if (packet.getType() == IqPacket.TYPE.RESULT) {
267 String download = packet.query().getAttribute("download");
268 if (download != null) {
269 try {
270 mUrl = new URL(download);
271 check();
272 return;
273 } catch (MalformedURLException e) {
274 //fallthrough
275 }
276 }
277 }
278 Log.d(Config.LOGTAG,"unable to retrieve actual download url");
279 retrieveFailed(null);
280 });
281 }
282
283 private void retrieveFailed(@Nullable Exception e) {
284 changeStatus(STATUS_OFFER_CHECK_FILESIZE);
285 if (interactive) {
286 if (e != null) {
287 showToastForException(e);
288 }
289 } else {
290 HttpDownloadConnection.this.acceptedAutomatically = false;
291 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
292 }
293 cancel();
294 }
295
296 private void check() {
297 long size;
298 try {
299 size = retrieveFileSize();
300 } catch (Exception e) {
301 Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
302 retrieveFailed(e);
303 return;
304 }
305 final Message.FileParams fileParams = message.getFileParams();
306 FileBackend.updateFileParams(message, fileParams.url, size);
307 message.setOob(true);
308 mXmppConnectionService.databaseBackend.updateMessage(message, true);
309 file.setExpectedSize(size);
310 message.resetFileParams();
311 if (mHttpConnectionManager.hasStoragePermission()
312 && size <= mHttpConnectionManager.getAutoAcceptFileSize()
313 && mXmppConnectionService.isDataSaverDisabled()) {
314 HttpDownloadConnection.this.acceptedAutomatically = true;
315 download(interactive);
316 } else {
317 changeStatus(STATUS_OFFER);
318 HttpDownloadConnection.this.acceptedAutomatically = false;
319 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
320 }
321 }
322
323 private long retrieveFileSize() throws IOException {
324 try {
325 Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive));
326 changeStatus(STATUS_CHECKING);
327 HttpURLConnection connection;
328 final String hostname = mUrl.getHost();
329 final boolean onion = hostname != null && hostname.endsWith(".onion");
330 if (mUseTor || message.getConversation().getAccount().isOnion() || onion) {
331 connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
332 } else {
333 connection = (HttpURLConnection) mUrl.openConnection();
334 }
335 if (method == Method.P1_S3) {
336 connection.setRequestMethod("GET");
337 connection.addRequestProperty("Range","bytes=0-0");
338 } else {
339 connection.setRequestMethod("HEAD");
340 }
341 connection.setUseCaches(false);
342 Log.d(Config.LOGTAG, "url: " + connection.getURL().toString());
343 connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
344 if (connection instanceof HttpsURLConnection) {
345 mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
346 }
347 connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
348 connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
349 connection.connect();
350 String contentLength;
351 if (method == Method.P1_S3) {
352 String contentRange = connection.getHeaderField("Content-Range");
353 String[] contentRangeParts = contentRange == null ? new String[0] : contentRange.split("/");
354 if (contentRangeParts.length != 2) {
355 contentLength = null;
356 } else {
357 contentLength = contentRangeParts[1];
358 }
359 } else {
360 contentLength = connection.getHeaderField("Content-Length");
361 }
362 connection.disconnect();
363 if (contentLength == null) {
364 throw new IOException("no content-length found in HEAD response");
365 }
366 return Long.parseLong(contentLength, 10);
367 } catch (IOException e) {
368 Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
369 throw e;
370 } catch (NumberFormatException e) {
371 throw new IOException();
372 }
373 }
374
375 }
376
377 private class FileDownloader implements Runnable {
378
379 private final boolean interactive;
380
381 private OutputStream os;
382
383 public FileDownloader(boolean interactive) {
384 this.interactive = interactive;
385 }
386
387 @Override
388 public void run() {
389 try {
390 changeStatus(STATUS_DOWNLOADING);
391 download();
392 decryptIfNeeded();
393 updateImageBounds();
394 finish();
395 } catch (SSLHandshakeException e) {
396 changeStatus(STATUS_OFFER);
397 } catch (Exception e) {
398 if (interactive) {
399 showToastForException(e);
400 } else {
401 HttpDownloadConnection.this.acceptedAutomatically = false;
402 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
403 }
404 cancel();
405 }
406 }
407
408 private void download() throws Exception {
409 InputStream is = null;
410 HttpURLConnection connection = null;
411 PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid());
412 try {
413 wakeLock.acquire();
414 if (mUseTor || message.getConversation().getAccount().isOnion()) {
415 connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
416 } else {
417 connection = (HttpURLConnection) mUrl.openConnection();
418 }
419 if (connection instanceof HttpsURLConnection) {
420 mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
421 }
422 connection.setUseCaches(false);
423 connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
424 final long expected = file.getExpectedSize();
425 final boolean tryResume = file.exists() && file.getSize() > 0 && file.getSize() < expected;
426 long resumeSize = 0;
427
428 if (tryResume) {
429 resumeSize = file.getSize();
430 Log.d(Config.LOGTAG, "http download trying resume after " + resumeSize + " of " + expected);
431 connection.setRequestProperty("Range", "bytes=" + resumeSize + "-");
432 }
433 connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
434 connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
435 connection.connect();
436 is = new BufferedInputStream(connection.getInputStream());
437 final String contentRange = connection.getHeaderField("Content-Range");
438 boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
439 long transmitted = 0;
440 if (tryResume && serverResumed) {
441 Log.d(Config.LOGTAG, "server resumed");
442 transmitted = file.getSize();
443 updateProgress(Math.round(((double) transmitted / expected) * 100));
444 os = AbstractConnectionManager.createOutputStream(file, true, false);
445 if (os == null) {
446 throw new FileWriterException();
447 }
448 } else {
449 long reportedContentLengthOnGet;
450 try {
451 reportedContentLengthOnGet = Long.parseLong(connection.getHeaderField("Content-Length"));
452 } catch (NumberFormatException | NullPointerException e) {
453 reportedContentLengthOnGet = 0;
454 }
455 if (expected != reportedContentLengthOnGet) {
456 Log.d(Config.LOGTAG, "content-length reported on GET (" + reportedContentLengthOnGet + ") did not match Content-Length reported on HEAD (" + expected + ")");
457 }
458 file.getParentFile().mkdirs();
459 if (!file.exists() && !file.createNewFile()) {
460 throw new FileWriterException();
461 }
462 os = AbstractConnectionManager.createOutputStream(file, false, false);
463 }
464 int count;
465 byte[] buffer = new byte[4096];
466 while ((count = is.read(buffer)) != -1) {
467 transmitted += count;
468 try {
469 os.write(buffer, 0, count);
470 } catch (IOException e) {
471 throw new FileWriterException();
472 }
473 updateProgress(Math.round(((double) transmitted / expected) * 100));
474 if (canceled) {
475 throw new CancellationException();
476 }
477 }
478 try {
479 os.flush();
480 } catch (IOException e) {
481 throw new FileWriterException();
482 }
483 } catch (CancellationException | IOException e) {
484 Log.d(Config.LOGTAG, "http download failed " + e.getMessage());
485 throw e;
486 } finally {
487 FileBackend.close(os);
488 FileBackend.close(is);
489 if (connection != null) {
490 connection.disconnect();
491 }
492 WakeLockHelper.release(wakeLock);
493 }
494 }
495
496 private void updateImageBounds() {
497 final boolean privateMessage = message.isPrivateMessage();
498 message.setType(privateMessage ? Message.TYPE_PRIVATE_FILE : Message.TYPE_FILE);
499 final URL url;
500 final String ref = mUrl.getRef();
501 if (method == Method.P1_S3) {
502 url = message.getFileParams().url;
503 } else if (ref != null && AesGcmURLStreamHandler.IV_KEY.matcher(ref).matches()) {
504 url = CryptoHelper.toAesGcmUrl(mUrl);
505 } else {
506 url = mUrl;
507 }
508 mXmppConnectionService.getFileBackend().updateFileParams(message, url);
509 mXmppConnectionService.updateMessage(message);
510 }
511
512 }
513}