@capgo/capacitor-updater 6.51.21 → 6.53.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapgoCapacitorUpdater.podspec +0 -3
- package/Package.swift +3 -11
- package/README.md +202 -67
- package/android/build.gradle +2 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +610 -177
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +909 -196
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +53 -19
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +226 -106
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +210 -58
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +325 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +63 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
- package/dist/docs.json +394 -27
- package/dist/esm/definitions.d.ts +220 -7
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +104 -52
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +562 -187
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +162 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +564 -109
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +11 -2
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +204 -93
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +75 -5
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +53 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +9 -3
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/package.json +7 -3
|
@@ -15,7 +15,6 @@ import java.io.*;
|
|
|
15
15
|
import java.io.FileInputStream;
|
|
16
16
|
import java.net.HttpURLConnection;
|
|
17
17
|
import java.net.MalformedURLException;
|
|
18
|
-
import java.net.URL;
|
|
19
18
|
import java.nio.channels.FileChannel;
|
|
20
19
|
import java.security.MessageDigest;
|
|
21
20
|
import java.util.ArrayList;
|
|
@@ -35,7 +34,9 @@ import java.util.function.BooleanSupplier;
|
|
|
35
34
|
import java.util.function.LongConsumer;
|
|
36
35
|
import okhttp3.Call;
|
|
37
36
|
import okhttp3.Callback;
|
|
37
|
+
import okhttp3.CookieJar;
|
|
38
38
|
import okhttp3.Dispatcher;
|
|
39
|
+
import okhttp3.HttpUrl;
|
|
39
40
|
import okhttp3.Interceptor;
|
|
40
41
|
import okhttp3.MediaType;
|
|
41
42
|
import okhttp3.OkHttpClient;
|
|
@@ -50,6 +51,14 @@ import org.json.JSONObject;
|
|
|
50
51
|
|
|
51
52
|
public class DownloadService extends Worker {
|
|
52
53
|
|
|
54
|
+
/** A refused HTTPS to HTTP redirect. Permanent for the given URL, so downloads must not retry it. */
|
|
55
|
+
static final class BlockedRedirectException extends IOException {
|
|
56
|
+
|
|
57
|
+
BlockedRedirectException() {
|
|
58
|
+
super("Blocked HTTPS to HTTP redirect; set allowHttpsToHttpRedirect to true to allow it");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
53
62
|
static final class DownloadRetryException extends RuntimeException {
|
|
54
63
|
|
|
55
64
|
DownloadRetryException(String message) {
|
|
@@ -126,6 +135,8 @@ public class DownloadService extends Worker {
|
|
|
126
135
|
// Match CapgoUpdater.timeout / responseTimeout default (20s). OkHttp's 10s
|
|
127
136
|
// defaults were unused by the plugin config and aborted slow manifest GETs.
|
|
128
137
|
private static volatile int httpTimeoutMs = 20_000;
|
|
138
|
+
// Off by default: a redirect must never downgrade updater traffic from HTTPS to plain HTTP.
|
|
139
|
+
private static volatile boolean allowHttpsToHttpRedirect = false;
|
|
129
140
|
private static String currentAppId = "unknown";
|
|
130
141
|
private static String currentPluginVersion = "unknown";
|
|
131
142
|
private static String currentVersionOs = "unknown";
|
|
@@ -138,6 +149,9 @@ public class DownloadService extends Worker {
|
|
|
138
149
|
sharedClient = new OkHttpClient.Builder()
|
|
139
150
|
.dispatcher(dispatcher)
|
|
140
151
|
.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
|
|
152
|
+
// Never share cookies with Capgo endpoints: the app's java.net.CookieHandler default is
|
|
153
|
+
// Capacitor's WebView cookie manager, and it must not see (or be fed by) plugin traffic.
|
|
154
|
+
.cookieJar(CookieJar.NO_COOKIES)
|
|
141
155
|
.connectTimeout(httpTimeoutMs, TimeUnit.MILLISECONDS)
|
|
142
156
|
.readTimeout(httpTimeoutMs, TimeUnit.MILLISECONDS)
|
|
143
157
|
.writeTimeout(httpTimeoutMs, TimeUnit.MILLISECONDS)
|
|
@@ -147,9 +161,33 @@ public class DownloadService extends Worker {
|
|
|
147
161
|
Request requestWithUserAgent = originalRequest.newBuilder().header("User-Agent", userAgent).build();
|
|
148
162
|
return chain.proceed(requestWithUserAgent);
|
|
149
163
|
})
|
|
164
|
+
.addNetworkInterceptor((chain) -> {
|
|
165
|
+
Response response = chain.proceed(chain.request());
|
|
166
|
+
if (!allowHttpsToHttpRedirect && isHttpsToHttpRedirect(response)) {
|
|
167
|
+
response.close();
|
|
168
|
+
throw new BlockedRedirectException();
|
|
169
|
+
}
|
|
170
|
+
return response;
|
|
171
|
+
})
|
|
150
172
|
.build();
|
|
151
173
|
}
|
|
152
174
|
|
|
175
|
+
static void setAllowHttpsToHttpRedirect(boolean allow) {
|
|
176
|
+
allowHttpsToHttpRedirect = allow;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static boolean isHttpsToHttpRedirect(Response response) {
|
|
180
|
+
if (!response.isRedirect() || !response.request().isHttps()) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
String location = response.header("Location");
|
|
184
|
+
if (location == null) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
HttpUrl target = response.request().url().resolve(location);
|
|
188
|
+
return target != null && !target.isHttps();
|
|
189
|
+
}
|
|
190
|
+
|
|
153
191
|
static int httpTimeoutMs() {
|
|
154
192
|
return httpTimeoutMs;
|
|
155
193
|
}
|
|
@@ -239,8 +277,21 @@ public class DownloadService extends Worker {
|
|
|
239
277
|
}
|
|
240
278
|
|
|
241
279
|
private Result createFailureResult(String error) {
|
|
242
|
-
|
|
243
|
-
|
|
280
|
+
return createFailureResult(error, null, null, false);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private Result createFailureResult(String error, String version, String dest, boolean isManifest) {
|
|
284
|
+
Data.Builder builder = new Data.Builder().putString(ERROR, error);
|
|
285
|
+
if (version != null) {
|
|
286
|
+
builder.putString(VERSION, version);
|
|
287
|
+
}
|
|
288
|
+
if (dest != null) {
|
|
289
|
+
builder.putString(FILEDEST, dest);
|
|
290
|
+
}
|
|
291
|
+
if (isManifest) {
|
|
292
|
+
builder.putBoolean(IS_MANIFEST, true);
|
|
293
|
+
}
|
|
294
|
+
return Result.failure(builder.build());
|
|
244
295
|
}
|
|
245
296
|
|
|
246
297
|
private Result createSuccessResult(String dest, String version, String sessionKey, String checksum, boolean isManifest) {
|
|
@@ -363,6 +414,38 @@ public class DownloadService extends Worker {
|
|
|
363
414
|
return seenTargets.add(targetFile.getCanonicalPath());
|
|
364
415
|
}
|
|
365
416
|
|
|
417
|
+
private static final class ManifestDownloadTask {
|
|
418
|
+
|
|
419
|
+
final String fileName;
|
|
420
|
+
final String finalFileHash;
|
|
421
|
+
final String downloadUrl;
|
|
422
|
+
final boolean isBrotli;
|
|
423
|
+
final File targetFile;
|
|
424
|
+
final File builtinFile;
|
|
425
|
+
final File cacheFile;
|
|
426
|
+
final File legacyCacheFile;
|
|
427
|
+
|
|
428
|
+
ManifestDownloadTask(
|
|
429
|
+
final String fileName,
|
|
430
|
+
final String finalFileHash,
|
|
431
|
+
final String downloadUrl,
|
|
432
|
+
final boolean isBrotli,
|
|
433
|
+
final File targetFile,
|
|
434
|
+
final File builtinFile,
|
|
435
|
+
final File cacheFile,
|
|
436
|
+
final File legacyCacheFile
|
|
437
|
+
) {
|
|
438
|
+
this.fileName = fileName;
|
|
439
|
+
this.finalFileHash = finalFileHash;
|
|
440
|
+
this.downloadUrl = downloadUrl;
|
|
441
|
+
this.isBrotli = isBrotli;
|
|
442
|
+
this.targetFile = targetFile;
|
|
443
|
+
this.builtinFile = builtinFile;
|
|
444
|
+
this.cacheFile = cacheFile;
|
|
445
|
+
this.legacyCacheFile = legacyCacheFile;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
366
449
|
static boolean tryCopyBuiltinAsset(final AssetManager assets, final String fileName, final File dest, final String expectedHash) {
|
|
367
450
|
if (assets == null || fileName == null || dest == null) {
|
|
368
451
|
return false;
|
|
@@ -399,16 +482,16 @@ public class DownloadService extends Worker {
|
|
|
399
482
|
@NonNull
|
|
400
483
|
@Override
|
|
401
484
|
public Result doWork() {
|
|
485
|
+
final String documentsDir = getInputData().getString(DOCDIR);
|
|
486
|
+
final String dest = getInputData().getString(FILEDEST);
|
|
487
|
+
final String version = getInputData().getString(VERSION);
|
|
488
|
+
final boolean isManifest = getInputData().getBoolean(IS_MANIFEST, false);
|
|
402
489
|
try {
|
|
403
490
|
String url = getInputData().getString(URL);
|
|
404
491
|
String id = getInputData().getString(ID);
|
|
405
|
-
String documentsDir = getInputData().getString(DOCDIR);
|
|
406
|
-
String dest = getInputData().getString(FILEDEST);
|
|
407
|
-
String version = getInputData().getString(VERSION);
|
|
408
492
|
String sessionKey = getInputData().getString(SESSIONKEY);
|
|
409
493
|
String checksum = getInputData().getString(CHECKSUM);
|
|
410
494
|
String publicKey = getInputData().getString(PUBLIC_KEY);
|
|
411
|
-
boolean isManifest = getInputData().getBoolean(IS_MANIFEST, false);
|
|
412
495
|
|
|
413
496
|
logger.debug("doWork isManifest: " + isManifest);
|
|
414
497
|
|
|
@@ -418,13 +501,15 @@ public class DownloadService extends Worker {
|
|
|
418
501
|
handleManifestDownload(id, documentsDir, dest, version, sessionKey, publicKey, manifest);
|
|
419
502
|
return createSuccessResult(dest, version, sessionKey, checksum, true);
|
|
420
503
|
} else if (isStopped()) {
|
|
421
|
-
|
|
504
|
+
CapgoUpdater.deleteManifestStagingFolderAt(documentsDir != null ? new File(documentsDir) : null, dest, logger);
|
|
505
|
+
return createFailureResult("download_cancelled", version, dest, true);
|
|
422
506
|
} else {
|
|
423
507
|
logger.error("Manifest is null");
|
|
424
|
-
|
|
508
|
+
CapgoUpdater.deleteManifestStagingFolderAt(documentsDir != null ? new File(documentsDir) : null, dest, logger);
|
|
509
|
+
return createFailureResult("Manifest is null", version, dest, true);
|
|
425
510
|
}
|
|
426
511
|
} else {
|
|
427
|
-
handleSingleFileDownload(url, id, documentsDir, dest, version, sessionKey, checksum);
|
|
512
|
+
handleSingleFileDownload(url, id, documentsDir, dest, version, sessionKey, checksum, publicKey);
|
|
428
513
|
return createSuccessResult(dest, version, sessionKey, checksum, false);
|
|
429
514
|
}
|
|
430
515
|
} catch (DownloadRetryException e) {
|
|
@@ -433,7 +518,10 @@ public class DownloadService extends Worker {
|
|
|
433
518
|
}
|
|
434
519
|
return Result.retry();
|
|
435
520
|
} catch (Exception e) {
|
|
436
|
-
|
|
521
|
+
if (isManifest) {
|
|
522
|
+
CapgoUpdater.deleteManifestStagingFolderAt(documentsDir != null ? new File(documentsDir) : null, dest, logger);
|
|
523
|
+
}
|
|
524
|
+
return createFailureResult(e.getMessage(), version, dest, isManifest);
|
|
437
525
|
}
|
|
438
526
|
}
|
|
439
527
|
|
|
@@ -515,6 +603,12 @@ public class DownloadService extends Worker {
|
|
|
515
603
|
try {
|
|
516
604
|
logger.debug("handleManifestDownload");
|
|
517
605
|
|
|
606
|
+
if (publicKey != null && !publicKey.isEmpty() && !CryptoCipher.isValidSessionKey(sessionKey)) {
|
|
607
|
+
logger.error("Public key present but no valid session key provided");
|
|
608
|
+
sendStatsAsync("session_key_required", version);
|
|
609
|
+
throw new IOException("Session key required when public key is present");
|
|
610
|
+
}
|
|
611
|
+
|
|
518
612
|
// Send stats for manifest download start
|
|
519
613
|
sendStatsAsync("download_manifest_start", version);
|
|
520
614
|
|
|
@@ -535,9 +629,7 @@ public class DownloadService extends Worker {
|
|
|
535
629
|
int totalFiles = manifest.length();
|
|
536
630
|
final AtomicLong completedFiles = new AtomicLong(0);
|
|
537
631
|
final AtomicBoolean hasError = new AtomicBoolean(false);
|
|
538
|
-
|
|
539
|
-
ExecutorService executor = Executors.newFixedThreadPool(Math.min(MANIFEST_MAX_CONCURRENT_FILES, Math.max(1, totalFiles)));
|
|
540
|
-
List<Future<?>> futures = new ArrayList<>();
|
|
632
|
+
final List<ManifestDownloadTask> tasks = new ArrayList<>();
|
|
541
633
|
final Set<String> seenTargets = new HashSet<>();
|
|
542
634
|
|
|
543
635
|
for (int i = 0; i < totalFiles; i++) {
|
|
@@ -552,7 +644,13 @@ public class DownloadService extends Worker {
|
|
|
552
644
|
continue;
|
|
553
645
|
}
|
|
554
646
|
|
|
555
|
-
if (publicKey != null && !publicKey.isEmpty()
|
|
647
|
+
if (publicKey != null && !publicKey.isEmpty()) {
|
|
648
|
+
if (!CryptoCipher.isValidSessionKey(sessionKey)) {
|
|
649
|
+
logger.error("Public key present but no valid session key provided");
|
|
650
|
+
sendStatsAsync("session_key_required", version);
|
|
651
|
+
hasError.set(true);
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
556
654
|
try {
|
|
557
655
|
fileHash = CryptoCipher.decryptChecksum(fileHash, publicKey);
|
|
558
656
|
} catch (Exception e) {
|
|
@@ -592,35 +690,56 @@ public class DownloadService extends Worker {
|
|
|
592
690
|
final File legacyCacheFile =
|
|
593
691
|
isBrotli && cacheFile != null ? new File(cacheFolder, finalFileHash + "_" + new File(fileName).getName()) : null;
|
|
594
692
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
693
|
+
tasks.add(
|
|
694
|
+
new ManifestDownloadTask(
|
|
695
|
+
fileName,
|
|
696
|
+
finalFileHash,
|
|
697
|
+
downloadUrl,
|
|
698
|
+
isBrotli,
|
|
699
|
+
targetFile,
|
|
700
|
+
builtinFile,
|
|
701
|
+
cacheFile,
|
|
702
|
+
legacyCacheFile
|
|
703
|
+
)
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (hasError.get()) {
|
|
708
|
+
throw new IOException("Manifest contains invalid or duplicate file paths");
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
for (final ManifestDownloadTask task : tasks) {
|
|
712
|
+
if (!Objects.requireNonNull(task.targetFile.getParentFile()).exists() && !task.targetFile.getParentFile().mkdirs()) {
|
|
713
|
+
logger.error("Failed to create parent directory for: " + task.targetFile.getAbsolutePath());
|
|
714
|
+
throw new IOException("Failed to create parent directory for: " + task.targetFile.getAbsolutePath());
|
|
600
715
|
}
|
|
716
|
+
}
|
|
601
717
|
|
|
602
|
-
|
|
718
|
+
ExecutorService executor = Executors.newFixedThreadPool(Math.min(MANIFEST_MAX_CONCURRENT_FILES, Math.max(1, totalFiles)));
|
|
719
|
+
List<Future<?>> futures = new ArrayList<>();
|
|
720
|
+
|
|
721
|
+
for (final ManifestDownloadTask task : tasks) {
|
|
603
722
|
Future<?> future = executor.submit(() -> {
|
|
604
723
|
try {
|
|
605
|
-
if (tryCopyBuiltinAsset(assets, fileName, targetFile, finalFileHash)) {
|
|
606
|
-
logger.debug("using builtin asset " + fileName);
|
|
607
|
-
} else if (tryCopyBuiltinFile(builtinFile, targetFile, finalFileHash)) {
|
|
608
|
-
logger.debug("using builtin file " + fileName);
|
|
724
|
+
if (tryCopyBuiltinAsset(assets, task.fileName, task.targetFile, task.finalFileHash)) {
|
|
725
|
+
logger.debug("using builtin asset " + task.fileName);
|
|
726
|
+
} else if (tryCopyBuiltinFile(task.builtinFile, task.targetFile, task.finalFileHash)) {
|
|
727
|
+
logger.debug("using builtin file " + task.fileName);
|
|
609
728
|
} else if (
|
|
610
|
-
tryCopyFromCache(cacheFile, targetFile, finalFileHash) ||
|
|
611
|
-
(legacyCacheFile != null && tryCopyFromCache(legacyCacheFile, targetFile, finalFileHash))
|
|
729
|
+
tryCopyFromCache(task.cacheFile, task.targetFile, task.finalFileHash) ||
|
|
730
|
+
(task.legacyCacheFile != null && tryCopyFromCache(task.legacyCacheFile, task.targetFile, task.finalFileHash))
|
|
612
731
|
) {
|
|
613
|
-
logger.debug("already cached " + fileName);
|
|
732
|
+
logger.debug("already cached " + task.fileName);
|
|
614
733
|
} else {
|
|
615
734
|
downloadAndVerify(
|
|
616
|
-
downloadUrl,
|
|
617
|
-
targetFile,
|
|
618
|
-
cacheFile,
|
|
619
|
-
finalFileHash,
|
|
735
|
+
task.downloadUrl,
|
|
736
|
+
task.targetFile,
|
|
737
|
+
task.cacheFile,
|
|
738
|
+
task.finalFileHash,
|
|
620
739
|
sessionKey,
|
|
621
740
|
publicKey,
|
|
622
|
-
|
|
623
|
-
fileName
|
|
741
|
+
task.isBrotli,
|
|
742
|
+
task.fileName
|
|
624
743
|
);
|
|
625
744
|
}
|
|
626
745
|
|
|
@@ -628,8 +747,8 @@ public class DownloadService extends Worker {
|
|
|
628
747
|
int percent = calcTotalPercent(completed, totalFiles);
|
|
629
748
|
setProgress(percent);
|
|
630
749
|
} catch (Exception e) {
|
|
631
|
-
logger.error("Error processing file: " + fileName + " " + e.getMessage());
|
|
632
|
-
sendStatsAsync("download_manifest_file_fail", version + ":" + fileName);
|
|
750
|
+
logger.error("Error processing file: " + task.fileName + " " + e.getMessage());
|
|
751
|
+
sendStatsAsync("download_manifest_file_fail", version + ":" + task.fileName);
|
|
633
752
|
hasError.set(true);
|
|
634
753
|
}
|
|
635
754
|
});
|
|
@@ -676,8 +795,20 @@ public class DownloadService extends Worker {
|
|
|
676
795
|
String dest,
|
|
677
796
|
String version,
|
|
678
797
|
String sessionKey,
|
|
679
|
-
String checksum
|
|
798
|
+
String checksum,
|
|
799
|
+
String publicKey
|
|
680
800
|
) {
|
|
801
|
+
if (publicKey != null && !publicKey.isEmpty() && !CryptoCipher.isValidSessionKey(sessionKey)) {
|
|
802
|
+
logger.error("Public key present but no valid session key provided");
|
|
803
|
+
sendStatsAsync("session_key_required", version);
|
|
804
|
+
throw new RuntimeException("Session key required when public key is present");
|
|
805
|
+
}
|
|
806
|
+
if (checksum == null || checksum.isEmpty()) {
|
|
807
|
+
logger.error("No checksum provided");
|
|
808
|
+
sendStatsAsync("checksum_required", version);
|
|
809
|
+
throw new RuntimeException("Checksum required");
|
|
810
|
+
}
|
|
811
|
+
|
|
681
812
|
// Send stats for zip download start
|
|
682
813
|
sendStatsAsync("download_zip_start", version);
|
|
683
814
|
|
|
@@ -693,20 +824,16 @@ public class DownloadService extends Worker {
|
|
|
693
824
|
throw new RuntimeException("insufficient_disk_space");
|
|
694
825
|
}
|
|
695
826
|
|
|
696
|
-
|
|
827
|
+
Response response = null;
|
|
697
828
|
InputStream inputStream = null;
|
|
698
829
|
BufferedReader reader = null;
|
|
699
830
|
BufferedWriter writer = null;
|
|
700
831
|
|
|
701
832
|
try {
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
// responseTimeout cannot shrink large-bundle downloads.
|
|
707
|
-
int zipTimeoutMs = Math.max(httpTimeoutMs, 60_000);
|
|
708
|
-
httpConn.setConnectTimeout(zipTimeoutMs);
|
|
709
|
-
httpConn.setReadTimeout(zipTimeoutMs);
|
|
833
|
+
HttpUrl zipUrl = url != null ? HttpUrl.parse(url) : null;
|
|
834
|
+
if (zipUrl == null) {
|
|
835
|
+
throw new MalformedURLException("Expected an http or https URL");
|
|
836
|
+
}
|
|
710
837
|
|
|
711
838
|
// Reading progress file (if exist)
|
|
712
839
|
long downloadedBytes = 0;
|
|
@@ -735,15 +862,16 @@ public class DownloadService extends Worker {
|
|
|
735
862
|
throw new DownloadRetryException("download_stopped");
|
|
736
863
|
}
|
|
737
864
|
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
int responseCode = httpConn.getResponseCode();
|
|
865
|
+
response = executeZipRequest(zipUrl, downloadedBytes);
|
|
866
|
+
int responseCode = response.code();
|
|
743
867
|
|
|
744
868
|
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) {
|
|
745
|
-
|
|
746
|
-
|
|
869
|
+
ResponseBody responseBody = response.body();
|
|
870
|
+
if (responseBody == null) {
|
|
871
|
+
throw new IOException("Response body is null");
|
|
872
|
+
}
|
|
873
|
+
ZipWritePlan writePlan = planZipResumeWrite(responseCode, downloadedBytes, response.header("Content-Range"));
|
|
874
|
+
long responseBodyLength = responseBody.contentLength();
|
|
747
875
|
long contentLength = shouldAppendHttpBody(writePlan.statusCode, writePlan.writeOffset)
|
|
748
876
|
? responseBodyLength >= 0
|
|
749
877
|
? responseBodyLength + writePlan.writeOffset
|
|
@@ -756,7 +884,7 @@ public class DownloadService extends Worker {
|
|
|
756
884
|
}
|
|
757
885
|
|
|
758
886
|
try {
|
|
759
|
-
inputStream =
|
|
887
|
+
inputStream = responseBody.byteStream();
|
|
760
888
|
|
|
761
889
|
if (writePlan.writeOffset == 0 && downloadedBytes == 0) {
|
|
762
890
|
writer = new BufferedWriter(new FileWriter(infoFile));
|
|
@@ -767,7 +895,7 @@ public class DownloadService extends Worker {
|
|
|
767
895
|
|
|
768
896
|
final int[] lastNotifiedPercent = { 0 };
|
|
769
897
|
final long totalContentLength = contentLength;
|
|
770
|
-
String contentRangeHeader =
|
|
898
|
+
String contentRangeHeader = response.header("Content-Range");
|
|
771
899
|
writeHttpBody(tempFile, inputStream, writePlan.statusCode, writePlan.writeOffset, this::isStopped, (written) -> {
|
|
772
900
|
int percent = calcTotalPercent(written, totalContentLength);
|
|
773
901
|
if (percent >= lastNotifiedPercent[0] + 10) {
|
|
@@ -835,6 +963,9 @@ public class DownloadService extends Worker {
|
|
|
835
963
|
} catch (MalformedURLException e) {
|
|
836
964
|
logger.error("Invalid download URL: " + e.getMessage());
|
|
837
965
|
throw new RuntimeException("invalid_url: " + e.getMessage());
|
|
966
|
+
} catch (BlockedRedirectException e) {
|
|
967
|
+
logger.error("Download error: " + e.getMessage());
|
|
968
|
+
throw new RuntimeException("blocked_redirect: " + e.getMessage());
|
|
838
969
|
} catch (IOException e) {
|
|
839
970
|
logger.error("Download error: " + e.getMessage());
|
|
840
971
|
throw new DownloadRetryException(e.getMessage(), e);
|
|
@@ -843,14 +974,35 @@ public class DownloadService extends Worker {
|
|
|
843
974
|
throw new RuntimeException(e.getMessage());
|
|
844
975
|
} finally {
|
|
845
976
|
// Ensure connection is closed
|
|
846
|
-
if (
|
|
977
|
+
if (response != null) {
|
|
847
978
|
try {
|
|
848
|
-
|
|
979
|
+
response.close();
|
|
849
980
|
} catch (Exception ignored) {}
|
|
850
981
|
}
|
|
851
982
|
}
|
|
852
983
|
}
|
|
853
984
|
|
|
985
|
+
/**
|
|
986
|
+
* Zip bundle request on the shared client (no cookies, same User-Agent), resuming from
|
|
987
|
+
* {@code downloadedBytes} with a Range header. The caller closes the response.
|
|
988
|
+
*/
|
|
989
|
+
static Response executeZipRequest(HttpUrl url, long downloadedBytes) throws IOException {
|
|
990
|
+
// Zip can stall longer than a JSON API call; keep a floor so
|
|
991
|
+
// responseTimeout cannot shrink large-bundle downloads.
|
|
992
|
+
int zipTimeoutMs = Math.max(httpTimeoutMs, 60_000);
|
|
993
|
+
OkHttpClient zipClient = sharedClient
|
|
994
|
+
.newBuilder()
|
|
995
|
+
.connectTimeout(zipTimeoutMs, TimeUnit.MILLISECONDS)
|
|
996
|
+
.readTimeout(zipTimeoutMs, TimeUnit.MILLISECONDS)
|
|
997
|
+
.writeTimeout(zipTimeoutMs, TimeUnit.MILLISECONDS)
|
|
998
|
+
.build();
|
|
999
|
+
Request.Builder builder = new Request.Builder().url(url);
|
|
1000
|
+
if (downloadedBytes > 0) {
|
|
1001
|
+
builder.header("Range", "bytes=" + downloadedBytes + "-");
|
|
1002
|
+
}
|
|
1003
|
+
return zipClient.newCall(builder.build()).execute();
|
|
1004
|
+
}
|
|
1005
|
+
|
|
854
1006
|
static boolean isRetryableHttpStatus(int responseCode) {
|
|
855
1007
|
return responseCode >= 500 || responseCode == 408 || responseCode == 429;
|
|
856
1008
|
}
|
|
@@ -1061,7 +1213,7 @@ public class DownloadService extends Worker {
|
|
|
1061
1213
|
}
|
|
1062
1214
|
}
|
|
1063
1215
|
|
|
1064
|
-
boolean needDecrypt = publicKey != null && !publicKey.isEmpty() &&
|
|
1216
|
+
boolean needDecrypt = publicKey != null && !publicKey.isEmpty() && CryptoCipher.isValidSessionKey(sessionKey);
|
|
1065
1217
|
File source = partial;
|
|
1066
1218
|
if (needDecrypt) {
|
|
1067
1219
|
workFile = new File(cacheFolder, "work_" + UUID.randomUUID() + "_" + targetFile.getName() + ".tmp");
|