HttpDownloadConnection.java

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