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