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