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