@capgo/capacitor-updater 6.51.19 → 6.51.23
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/README.md +1 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +44 -30
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +437 -351
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +51 -18
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +330 -92
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +12 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +12 -16
- package/dist/esm/history.js +7 -7
- package/dist/esm/history.js.map +1 -1
- package/dist/plugin.cjs.js +7 -7
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +7 -7
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +243 -94
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +177 -32
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +9 -1
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +4 -1
- package/package.json +6 -8
|
@@ -285,6 +285,31 @@ public class CapgoUpdater {
|
|
|
285
285
|
this.cachedKeyId = CryptoCipher.calcKeyId(publicKey);
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
+
private void requireSessionKeyForEncryptedUpdate(final String sessionKey) throws IOException {
|
|
289
|
+
if (!this.publicKey.isEmpty() && !CryptoCipher.isValidSessionKey(sessionKey)) {
|
|
290
|
+
logger.error("Public key present but no valid session key provided");
|
|
291
|
+
this.sendStats("session_key_required");
|
|
292
|
+
throw new IOException("Session key required when public key is present");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private void requireBundleChecksum(final String checksum) throws IOException {
|
|
297
|
+
if (checksum == null || checksum.isEmpty()) {
|
|
298
|
+
logger.error("No checksum provided");
|
|
299
|
+
this.sendStats("checksum_required");
|
|
300
|
+
throw new IOException("Checksum required");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
static boolean containsPathTraversalSegment(final String relativePath) {
|
|
305
|
+
for (final String segment : relativePath.split("/")) {
|
|
306
|
+
if ("..".equals(segment)) {
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
|
|
288
313
|
static File resolvePathInsideDirectory(final File baseDirectory, final String relativePath) throws IOException {
|
|
289
314
|
if (relativePath == null || relativePath.isEmpty()) {
|
|
290
315
|
throw new IOException("Invalid empty path");
|
|
@@ -292,6 +317,9 @@ public class CapgoUpdater {
|
|
|
292
317
|
if (relativePath.contains("\\") || relativePath.indexOf('\0') >= 0) {
|
|
293
318
|
throw new IOException("Invalid path separator");
|
|
294
319
|
}
|
|
320
|
+
if (containsPathTraversalSegment(relativePath)) {
|
|
321
|
+
throw new IOException("Path traversal segments are not allowed");
|
|
322
|
+
}
|
|
295
323
|
if (new File(relativePath).isAbsolute()) {
|
|
296
324
|
throw new IOException("Absolute paths are not allowed");
|
|
297
325
|
}
|
|
@@ -302,13 +330,18 @@ public class CapgoUpdater {
|
|
|
302
330
|
final String targetPath = canonicalTarget.getPath();
|
|
303
331
|
final String normalizedBasePath = basePath.endsWith(File.separator) ? basePath : basePath + File.separator;
|
|
304
332
|
|
|
305
|
-
|
|
333
|
+
// Require a strict child of the base. Equality would accept "." and wipe/write the root.
|
|
334
|
+
if (!targetPath.startsWith(normalizedBasePath)) {
|
|
306
335
|
throw new IOException("Path escapes base directory: " + relativePath);
|
|
307
336
|
}
|
|
308
337
|
|
|
309
338
|
return canonicalTarget;
|
|
310
339
|
}
|
|
311
340
|
|
|
341
|
+
static File resolveBundleDirectory(final File documentsDir, final String bundleId) throws IOException {
|
|
342
|
+
return resolvePathInsideDirectory(new File(documentsDir, bundleDirectory), bundleId);
|
|
343
|
+
}
|
|
344
|
+
|
|
312
345
|
public String getKeyId() {
|
|
313
346
|
return this.cachedKeyId;
|
|
314
347
|
}
|
|
@@ -376,11 +409,11 @@ public class CapgoUpdater {
|
|
|
376
409
|
}
|
|
377
410
|
}
|
|
378
411
|
|
|
379
|
-
private void flattenAssets(final File sourceFile, final
|
|
412
|
+
private void flattenAssets(final File sourceFile, final File destinationFile) throws IOException {
|
|
380
413
|
if (!sourceFile.exists()) {
|
|
381
414
|
throw new FileNotFoundException("Source file not found: " + sourceFile.getPath());
|
|
382
415
|
}
|
|
383
|
-
|
|
416
|
+
assertPathInsideBundleRoot(destinationFile);
|
|
384
417
|
Objects.requireNonNull(destinationFile.getParentFile()).mkdirs();
|
|
385
418
|
final String[] entries = sourceFile.list(this.filter);
|
|
386
419
|
if (entries == null || entries.length == 0) {
|
|
@@ -409,7 +442,13 @@ public class CapgoUpdater {
|
|
|
409
442
|
return;
|
|
410
443
|
}
|
|
411
444
|
|
|
412
|
-
final File bundleDir
|
|
445
|
+
final File bundleDir;
|
|
446
|
+
try {
|
|
447
|
+
bundleDir = this.getBundleDirectory(id);
|
|
448
|
+
} catch (IOException e) {
|
|
449
|
+
logger.debug("Skip delta cache population: invalid bundle id");
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
413
452
|
if (!bundleDir.exists()) {
|
|
414
453
|
logger.debug("Skip delta cache population: bundle dir missing");
|
|
415
454
|
return;
|
|
@@ -497,7 +536,10 @@ public class CapgoUpdater {
|
|
|
497
536
|
if (fileHash.isEmpty()) {
|
|
498
537
|
return "";
|
|
499
538
|
}
|
|
500
|
-
if (this.publicKey != null && !this.publicKey.isEmpty()
|
|
539
|
+
if (this.publicKey != null && !this.publicKey.isEmpty()) {
|
|
540
|
+
if (!CryptoCipher.isValidSessionKey(sessionKey)) {
|
|
541
|
+
return "";
|
|
542
|
+
}
|
|
501
543
|
try {
|
|
502
544
|
fileHash = CryptoCipher.decryptChecksum(fileHash, this.publicKey);
|
|
503
545
|
} catch (Exception e) {
|
|
@@ -833,31 +875,24 @@ public class CapgoUpdater {
|
|
|
833
875
|
String checksum = "";
|
|
834
876
|
|
|
835
877
|
try {
|
|
878
|
+
this.requireSessionKeyForEncryptedUpdate(sessionKey);
|
|
836
879
|
this.notifyDownload(id, 71);
|
|
837
880
|
downloaded = new File(this.documentsDir, dest);
|
|
838
881
|
|
|
839
882
|
if (!isManifest) {
|
|
840
|
-
String
|
|
841
|
-
|
|
842
|
-
// If public key is present but no checksum provided, refuse installation
|
|
843
|
-
if (!this.publicKey.isEmpty() && checksumDecrypted.isEmpty()) {
|
|
844
|
-
logger.error("Public key present but no checksum provided");
|
|
845
|
-
this.sendStats("checksum_required");
|
|
846
|
-
throw new IOException("Checksum required when public key is present: " + id);
|
|
847
|
-
}
|
|
883
|
+
String expectedChecksum = Objects.requireNonNullElse(checksumRes, "");
|
|
884
|
+
this.requireBundleChecksum(expectedChecksum);
|
|
848
885
|
|
|
849
|
-
if (
|
|
886
|
+
if (CryptoCipher.isValidSessionKey(sessionKey)) {
|
|
850
887
|
CryptoCipher.decryptFile(downloaded, publicKey, sessionKey);
|
|
851
|
-
|
|
852
|
-
checksum = CryptoCipher.calcChecksum(downloaded);
|
|
853
|
-
} else {
|
|
854
|
-
checksum = CryptoCipher.calcChecksum(downloaded);
|
|
888
|
+
expectedChecksum = CryptoCipher.decryptChecksum(checksumRes, publicKey);
|
|
855
889
|
}
|
|
890
|
+
checksum = CryptoCipher.calcChecksum(downloaded);
|
|
856
891
|
CryptoCipher.logChecksumInfo("Calculated checksum", checksum);
|
|
857
|
-
CryptoCipher.logChecksumInfo("Expected checksum",
|
|
858
|
-
if (
|
|
892
|
+
CryptoCipher.logChecksumInfo("Expected checksum", expectedChecksum);
|
|
893
|
+
if (!expectedChecksum.equals(checksum)) {
|
|
859
894
|
logger.error("Checksum mismatch");
|
|
860
|
-
logger.debug("Expected: " +
|
|
895
|
+
logger.debug("Expected: " + expectedChecksum + ", Got: " + checksum);
|
|
861
896
|
this.sendStats("checksum_fail");
|
|
862
897
|
throw new IOException("Checksum failed: " + id);
|
|
863
898
|
}
|
|
@@ -885,13 +920,11 @@ public class CapgoUpdater {
|
|
|
885
920
|
if (!isManifest) {
|
|
886
921
|
extractedDir = this.unzip(id, downloaded, TEMP_UNZIP_PREFIX + this.randomString());
|
|
887
922
|
this.notifyDownload(id, 91);
|
|
888
|
-
|
|
889
|
-
this.flattenAssets(extractedDir, idName);
|
|
923
|
+
this.flattenAssets(extractedDir, this.getBundleDirectory(id));
|
|
890
924
|
this.cacheBundleFilesAsync(id);
|
|
891
925
|
} else {
|
|
892
926
|
this.notifyDownload(id, 91);
|
|
893
|
-
|
|
894
|
-
this.flattenAssets(downloaded, idName);
|
|
927
|
+
this.flattenAssets(downloaded, this.getBundleDirectory(id));
|
|
895
928
|
downloaded.delete();
|
|
896
929
|
}
|
|
897
930
|
// Remove old bundle info and set new one
|
|
@@ -1110,18 +1143,44 @@ public class CapgoUpdater {
|
|
|
1110
1143
|
}
|
|
1111
1144
|
}
|
|
1112
1145
|
|
|
1146
|
+
private void assertPathInsideDocumentsDir(final File target) throws IOException {
|
|
1147
|
+
if (this.documentsDir == null) {
|
|
1148
|
+
throw new IOException("Documents directory unavailable");
|
|
1149
|
+
}
|
|
1150
|
+
final File canonicalBase = this.documentsDir.getCanonicalFile();
|
|
1151
|
+
final File canonicalTarget = target.getCanonicalFile();
|
|
1152
|
+
final String basePath = canonicalBase.getPath();
|
|
1153
|
+
final String normalizedBasePath = basePath.endsWith(File.separator) ? basePath : basePath + File.separator;
|
|
1154
|
+
final String targetPath = canonicalTarget.getPath();
|
|
1155
|
+
if (!targetPath.equals(basePath) && !targetPath.startsWith(normalizedBasePath)) {
|
|
1156
|
+
throw new IOException("Path escapes updater storage: " + target.getPath());
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
private void assertPathInsideBundleRoot(final File target) throws IOException {
|
|
1161
|
+
final File bundleRoot = new File(this.documentsDir, bundleDirectory).getCanonicalFile();
|
|
1162
|
+
final File canonicalTarget = target.getCanonicalFile();
|
|
1163
|
+
final String basePath = bundleRoot.getPath();
|
|
1164
|
+
final String normalizedBasePath = basePath.endsWith(File.separator) ? basePath : basePath + File.separator;
|
|
1165
|
+
final String targetPath = canonicalTarget.getPath();
|
|
1166
|
+
if (!targetPath.equals(basePath) && !targetPath.startsWith(normalizedBasePath)) {
|
|
1167
|
+
throw new IOException("Path escapes bundle storage: " + target.getPath());
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1113
1171
|
private void safeDelete(final File target) {
|
|
1114
1172
|
if (target == null || !target.exists()) {
|
|
1115
1173
|
return;
|
|
1116
1174
|
}
|
|
1117
1175
|
try {
|
|
1176
|
+
this.assertPathInsideDocumentsDir(target);
|
|
1118
1177
|
if (target.isDirectory()) {
|
|
1119
1178
|
this.deleteDirectory(target);
|
|
1120
1179
|
} else if (!target.delete()) {
|
|
1121
1180
|
logger.warn("Failed to delete file: " + target.getAbsolutePath());
|
|
1122
1181
|
}
|
|
1123
1182
|
} catch (IOException cleanupError) {
|
|
1124
|
-
logger.warn("
|
|
1183
|
+
logger.warn("Refusing unsafe delete for " + target.getAbsolutePath() + ": " + cleanupError.getMessage());
|
|
1125
1184
|
}
|
|
1126
1185
|
}
|
|
1127
1186
|
|
|
@@ -1395,6 +1454,15 @@ public class CapgoUpdater {
|
|
|
1395
1454
|
final JSONArray manifest,
|
|
1396
1455
|
final boolean setNext
|
|
1397
1456
|
) {
|
|
1457
|
+
try {
|
|
1458
|
+
this.requireSessionKeyForEncryptedUpdate(sessionKey);
|
|
1459
|
+
if (manifest == null) {
|
|
1460
|
+
this.requireBundleChecksum(checksum);
|
|
1461
|
+
}
|
|
1462
|
+
} catch (final IOException e) {
|
|
1463
|
+
logger.error("Download blocked: " + e.getMessage());
|
|
1464
|
+
return;
|
|
1465
|
+
}
|
|
1398
1466
|
if (!this.runDownloadGateQuiet()) {
|
|
1399
1467
|
return;
|
|
1400
1468
|
}
|
|
@@ -1425,6 +1493,8 @@ public class CapgoUpdater {
|
|
|
1425
1493
|
}
|
|
1426
1494
|
|
|
1427
1495
|
public BundleInfo download(final String url, final String version, final String sessionKey, final String checksum) throws IOException {
|
|
1496
|
+
this.requireSessionKeyForEncryptedUpdate(sessionKey);
|
|
1497
|
+
this.requireBundleChecksum(checksum);
|
|
1428
1498
|
this.runDownloadGate();
|
|
1429
1499
|
// Check for existing bundle with same version and clean up if in error state
|
|
1430
1500
|
BundleInfo existingBundle = this.getBundleInfoByName(version);
|
|
@@ -1476,6 +1546,7 @@ public class CapgoUpdater {
|
|
|
1476
1546
|
final String checksum,
|
|
1477
1547
|
final JSONArray manifest
|
|
1478
1548
|
) throws IOException {
|
|
1549
|
+
this.requireSessionKeyForEncryptedUpdate(sessionKey);
|
|
1479
1550
|
this.runDownloadGate();
|
|
1480
1551
|
if (manifest == null) {
|
|
1481
1552
|
return download(url, version, sessionKey, checksum);
|
|
@@ -1557,6 +1628,14 @@ public class CapgoUpdater {
|
|
|
1557
1628
|
|
|
1558
1629
|
public Boolean delete(final String id, final Boolean removeInfo, final boolean cancelActiveDownload) throws IOException {
|
|
1559
1630
|
synchronized (this.deleteLock) {
|
|
1631
|
+
final File bundle;
|
|
1632
|
+
try {
|
|
1633
|
+
bundle = this.getBundleDirectory(id);
|
|
1634
|
+
} catch (IOException e) {
|
|
1635
|
+
logger.error("Cannot delete bundle with invalid id");
|
|
1636
|
+
logger.debug("Bundle ID: " + id + ", Error: " + e.getMessage());
|
|
1637
|
+
return false;
|
|
1638
|
+
}
|
|
1560
1639
|
final BundleInfo deleted = this.getBundleInfo(id);
|
|
1561
1640
|
if (deleted.isBuiltin() || this.getCurrentBundleId().equals(id)) {
|
|
1562
1641
|
logger.error("Cannot delete current or builtin bundle");
|
|
@@ -1582,7 +1661,6 @@ public class CapgoUpdater {
|
|
|
1582
1661
|
return false;
|
|
1583
1662
|
}
|
|
1584
1663
|
|
|
1585
|
-
final File bundle = this.getBundleDirectory(id);
|
|
1586
1664
|
final boolean hadRegistry = this.hasStoredBundleInfo(id);
|
|
1587
1665
|
final boolean hadFolder = bundle.exists();
|
|
1588
1666
|
if (!hadRegistry && !hadFolder) {
|
|
@@ -1730,12 +1808,17 @@ public class CapgoUpdater {
|
|
|
1730
1808
|
this.editor.commit();
|
|
1731
1809
|
}
|
|
1732
1810
|
|
|
1733
|
-
private File getBundleDirectory(final String id) {
|
|
1734
|
-
return
|
|
1811
|
+
private File getBundleDirectory(final String id) throws IOException {
|
|
1812
|
+
return resolveBundleDirectory(this.documentsDir, id);
|
|
1735
1813
|
}
|
|
1736
1814
|
|
|
1737
1815
|
private boolean bundleExists(final String id) {
|
|
1738
|
-
final File bundle
|
|
1816
|
+
final File bundle;
|
|
1817
|
+
try {
|
|
1818
|
+
bundle = this.getBundleDirectory(id);
|
|
1819
|
+
} catch (IOException e) {
|
|
1820
|
+
return false;
|
|
1821
|
+
}
|
|
1739
1822
|
final BundleInfo bundleInfo = this.getBundleInfo(id);
|
|
1740
1823
|
return (
|
|
1741
1824
|
bundle.isDirectory() &&
|
|
@@ -1768,12 +1851,10 @@ public class CapgoUpdater {
|
|
|
1768
1851
|
}
|
|
1769
1852
|
|
|
1770
1853
|
void restoreResetState(final ResetState state) {
|
|
1771
|
-
final String currentBundlePath =
|
|
1772
|
-
? "public"
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
? BundleInfo.ID_BUILTIN
|
|
1776
|
-
: state.fallbackBundleId;
|
|
1854
|
+
final String currentBundlePath =
|
|
1855
|
+
state.currentBundlePath == null || state.currentBundlePath.trim().isEmpty() ? "public" : state.currentBundlePath;
|
|
1856
|
+
final String fallbackBundleId =
|
|
1857
|
+
state.fallbackBundleId == null || state.fallbackBundleId.isEmpty() ? BundleInfo.ID_BUILTIN : state.fallbackBundleId;
|
|
1777
1858
|
|
|
1778
1859
|
this.editor.putString(this.CAP_SERVER_PATH, currentBundlePath);
|
|
1779
1860
|
this.editor.putString(FALLBACK_VERSION, fallbackBundleId);
|
|
@@ -1814,7 +1895,16 @@ public class CapgoUpdater {
|
|
|
1814
1895
|
this.reset();
|
|
1815
1896
|
return true;
|
|
1816
1897
|
}
|
|
1817
|
-
final File bundle
|
|
1898
|
+
final File bundle;
|
|
1899
|
+
try {
|
|
1900
|
+
bundle = this.getBundleDirectory(id);
|
|
1901
|
+
} catch (IOException e) {
|
|
1902
|
+
logger.error("Invalid bundle id");
|
|
1903
|
+
logger.debug("Bundle ID: " + id + ", Error: " + e.getMessage());
|
|
1904
|
+
this.setBundleStatus(id, BundleStatus.ERROR);
|
|
1905
|
+
this.sendStats("set_fail", newBundle.getVersionName());
|
|
1906
|
+
return false;
|
|
1907
|
+
}
|
|
1818
1908
|
logger.info("Setting next active bundle: " + id);
|
|
1819
1909
|
if (this.bundleExists(id)) {
|
|
1820
1910
|
var currentBundleName = this.getCurrentBundle().getVersionName();
|
|
@@ -1832,8 +1922,12 @@ public class CapgoUpdater {
|
|
|
1832
1922
|
if (bundle == null || bundle.isBuiltin() || !this.bundleExists(bundle.getId())) {
|
|
1833
1923
|
return false;
|
|
1834
1924
|
}
|
|
1835
|
-
|
|
1836
|
-
|
|
1925
|
+
try {
|
|
1926
|
+
this.setCurrentBundle(this.getBundleDirectory(bundle.getId()));
|
|
1927
|
+
return true;
|
|
1928
|
+
} catch (IOException e) {
|
|
1929
|
+
return false;
|
|
1930
|
+
}
|
|
1837
1931
|
}
|
|
1838
1932
|
|
|
1839
1933
|
boolean stagePreviewFallbackReload(final BundleInfo bundle) {
|
|
@@ -1847,8 +1941,12 @@ public class CapgoUpdater {
|
|
|
1847
1941
|
if (!this.bundleExists(bundle.getId())) {
|
|
1848
1942
|
return false;
|
|
1849
1943
|
}
|
|
1850
|
-
|
|
1851
|
-
|
|
1944
|
+
try {
|
|
1945
|
+
this.setCurrentBundle(this.getBundleDirectory(bundle.getId()));
|
|
1946
|
+
return true;
|
|
1947
|
+
} catch (IOException e) {
|
|
1948
|
+
return false;
|
|
1949
|
+
}
|
|
1852
1950
|
}
|
|
1853
1951
|
|
|
1854
1952
|
void finalizePendingReload(final BundleInfo bundle, final String previousBundleName) {
|
|
@@ -2208,32 +2306,30 @@ public class CapgoUpdater {
|
|
|
2208
2306
|
.build();
|
|
2209
2307
|
|
|
2210
2308
|
// User-Agent header is automatically added by DownloadService.sharedClient interceptor
|
|
2211
|
-
DownloadService.sharedClient
|
|
2212
|
-
.
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
logger.debug("Error: " + e.getMessage());
|
|
2220
|
-
}
|
|
2309
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2310
|
+
new okhttp3.Callback() {
|
|
2311
|
+
@Override
|
|
2312
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2313
|
+
releaseRateLimitStatisticClaim();
|
|
2314
|
+
logger.error("Failed to send rate limit statistic");
|
|
2315
|
+
logger.debug("Error: " + e.getMessage());
|
|
2316
|
+
}
|
|
2221
2317
|
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
}
|
|
2318
|
+
@Override
|
|
2319
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) {
|
|
2320
|
+
// The body is unused here; closing the Response closes it.
|
|
2321
|
+
try (response) {
|
|
2322
|
+
if (response.isSuccessful()) {
|
|
2323
|
+
logger.info("Rate limit statistic sent");
|
|
2324
|
+
} else {
|
|
2325
|
+
releaseRateLimitStatisticClaim();
|
|
2326
|
+
logger.error("Error sending rate limit statistic");
|
|
2327
|
+
logger.debug("Response code: " + response.code());
|
|
2233
2328
|
}
|
|
2234
2329
|
}
|
|
2235
2330
|
}
|
|
2236
|
-
|
|
2331
|
+
}
|
|
2332
|
+
);
|
|
2237
2333
|
} catch (final Exception e) {
|
|
2238
2334
|
releaseRateLimitStatisticClaim();
|
|
2239
2335
|
logger.error("Failed to send rate limit statistic");
|
|
@@ -2247,70 +2343,47 @@ public class CapgoUpdater {
|
|
|
2247
2343
|
|
|
2248
2344
|
Request request = new Request.Builder().url(url).post(body).build();
|
|
2249
2345
|
|
|
2250
|
-
DownloadService.sharedClient
|
|
2251
|
-
.
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
callback.callback(retError);
|
|
2261
|
-
}
|
|
2346
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2347
|
+
new okhttp3.Callback() {
|
|
2348
|
+
@Override
|
|
2349
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2350
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2351
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
2352
|
+
retError.put("error", "network_error");
|
|
2353
|
+
retError.put("kind", "failed");
|
|
2354
|
+
callback.callback(retError);
|
|
2355
|
+
}
|
|
2262
2356
|
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
}
|
|
2357
|
+
@Override
|
|
2358
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2359
|
+
try (ResponseBody responseBody = response.body()) {
|
|
2360
|
+
final int statusCode = response.code();
|
|
2361
|
+
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
2362
|
+
JSONObject jsonResponse = null;
|
|
2363
|
+
if (!responseData.isEmpty()) {
|
|
2364
|
+
try {
|
|
2365
|
+
jsonResponse = new JSONObject(responseData);
|
|
2366
|
+
} catch (JSONException ignored) {
|
|
2367
|
+
// Non-JSON responses are handled as response or parse errors below.
|
|
2275
2368
|
}
|
|
2369
|
+
}
|
|
2276
2370
|
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2281
|
-
retError.put(
|
|
2282
|
-
"error",
|
|
2283
|
-
rateLimit.error.isEmpty() ? jsonResponse.optString("error", "too_many_requests") : rateLimit.error
|
|
2284
|
-
);
|
|
2285
|
-
retError.put(
|
|
2286
|
-
"message",
|
|
2287
|
-
rateLimit.message.isEmpty()
|
|
2288
|
-
? jsonResponse.optString("message", "Too many requests")
|
|
2289
|
-
: rateLimit.message
|
|
2290
|
-
);
|
|
2291
|
-
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
2292
|
-
retError.put("kind", jsonResponse.getString("kind"));
|
|
2293
|
-
} else {
|
|
2294
|
-
retError.put("kind", "failed");
|
|
2295
|
-
}
|
|
2296
|
-
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
2297
|
-
retError.put("version", jsonResponse.getString("version"));
|
|
2298
|
-
}
|
|
2299
|
-
retError.put("statusCode", statusCode);
|
|
2300
|
-
callback.callback(retError);
|
|
2301
|
-
return;
|
|
2302
|
-
}
|
|
2371
|
+
if (jsonResponse != null && (jsonResponse.has("error") || jsonResponse.has("kind"))) {
|
|
2372
|
+
if (statusCode == 429) {
|
|
2373
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2303
2374
|
Map<String, Object> retError = new HashMap<>();
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2375
|
+
retError.put(
|
|
2376
|
+
"error",
|
|
2377
|
+
rateLimit.error.isEmpty() ? jsonResponse.optString("error", "too_many_requests") : rateLimit.error
|
|
2378
|
+
);
|
|
2379
|
+
retError.put(
|
|
2380
|
+
"message",
|
|
2381
|
+
rateLimit.message.isEmpty() ? jsonResponse.optString("message", "Too many requests") : rateLimit.message
|
|
2382
|
+
);
|
|
2307
2383
|
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
2308
2384
|
retError.put("kind", jsonResponse.getString("kind"));
|
|
2309
|
-
}
|
|
2310
|
-
if (jsonResponse.has("message") && !jsonResponse.isNull("message")) {
|
|
2311
|
-
retError.put("message", jsonResponse.getString("message"));
|
|
2312
2385
|
} else {
|
|
2313
|
-
retError.put("
|
|
2386
|
+
retError.put("kind", "failed");
|
|
2314
2387
|
}
|
|
2315
2388
|
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
2316
2389
|
retError.put("version", jsonResponse.getString("version"));
|
|
@@ -2319,58 +2392,77 @@ public class CapgoUpdater {
|
|
|
2319
2392
|
callback.callback(retError);
|
|
2320
2393
|
return;
|
|
2321
2394
|
}
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
if (rateLimit.blocked) {
|
|
2326
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2327
|
-
retError.put("message", rateLimit.message);
|
|
2328
|
-
retError.put("error", rateLimit.error);
|
|
2329
|
-
retError.put("kind", "failed");
|
|
2330
|
-
retError.put("statusCode", statusCode);
|
|
2331
|
-
callback.callback(retError);
|
|
2332
|
-
return;
|
|
2395
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2396
|
+
if (jsonResponse.has("error") && !jsonResponse.isNull("error")) {
|
|
2397
|
+
retError.put("error", jsonResponse.getString("error"));
|
|
2333
2398
|
}
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2337
|
-
retError.put("message", "Server error: " + response.code());
|
|
2338
|
-
retError.put("error", "response_error");
|
|
2339
|
-
retError.put("kind", "failed");
|
|
2340
|
-
retError.put("statusCode", statusCode);
|
|
2341
|
-
callback.callback(retError);
|
|
2342
|
-
return;
|
|
2399
|
+
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
2400
|
+
retError.put("kind", jsonResponse.getString("kind"));
|
|
2343
2401
|
}
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2402
|
+
if (jsonResponse.has("message") && !jsonResponse.isNull("message")) {
|
|
2403
|
+
retError.put("message", jsonResponse.getString("message"));
|
|
2404
|
+
} else {
|
|
2405
|
+
retError.put("message", "server did not provide a message");
|
|
2347
2406
|
}
|
|
2407
|
+
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
2408
|
+
retError.put("version", jsonResponse.getString("version"));
|
|
2409
|
+
}
|
|
2410
|
+
retError.put("statusCode", statusCode);
|
|
2411
|
+
callback.callback(retError);
|
|
2412
|
+
return;
|
|
2413
|
+
}
|
|
2348
2414
|
|
|
2349
|
-
|
|
2350
|
-
|
|
2415
|
+
// Check for 429 rate limit without JSON body
|
|
2416
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2417
|
+
if (rateLimit.blocked) {
|
|
2418
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2419
|
+
retError.put("message", rateLimit.message);
|
|
2420
|
+
retError.put("error", rateLimit.error);
|
|
2421
|
+
retError.put("kind", "failed");
|
|
2422
|
+
retError.put("statusCode", statusCode);
|
|
2423
|
+
callback.callback(retError);
|
|
2424
|
+
return;
|
|
2425
|
+
}
|
|
2351
2426
|
|
|
2352
|
-
|
|
2353
|
-
while (keys.hasNext()) {
|
|
2354
|
-
String key = keys.next();
|
|
2355
|
-
if (jsonResponse.has(key)) {
|
|
2356
|
-
if ("session_key".equals(key)) {
|
|
2357
|
-
ret.put("sessionKey", jsonResponse.get(key));
|
|
2358
|
-
} else {
|
|
2359
|
-
ret.put(key, jsonResponse.get(key));
|
|
2360
|
-
}
|
|
2361
|
-
}
|
|
2362
|
-
}
|
|
2363
|
-
callback.callback(ret);
|
|
2364
|
-
} catch (JSONException e) {
|
|
2427
|
+
if (!response.isSuccessful()) {
|
|
2365
2428
|
Map<String, Object> retError = new HashMap<>();
|
|
2366
|
-
retError.put("message", "
|
|
2367
|
-
retError.put("error", "
|
|
2429
|
+
retError.put("message", "Server error: " + response.code());
|
|
2430
|
+
retError.put("error", "response_error");
|
|
2368
2431
|
retError.put("kind", "failed");
|
|
2432
|
+
retError.put("statusCode", statusCode);
|
|
2369
2433
|
callback.callback(retError);
|
|
2434
|
+
return;
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
if (jsonResponse == null) {
|
|
2438
|
+
throw new JSONException("Response is not a JSON object");
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
Map<String, Object> ret = new HashMap<>();
|
|
2442
|
+
ret.put("statusCode", statusCode);
|
|
2443
|
+
|
|
2444
|
+
Iterator<String> keys = jsonResponse.keys();
|
|
2445
|
+
while (keys.hasNext()) {
|
|
2446
|
+
String key = keys.next();
|
|
2447
|
+
if (jsonResponse.has(key)) {
|
|
2448
|
+
if ("session_key".equals(key)) {
|
|
2449
|
+
ret.put("sessionKey", jsonResponse.get(key));
|
|
2450
|
+
} else {
|
|
2451
|
+
ret.put(key, jsonResponse.get(key));
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2370
2454
|
}
|
|
2455
|
+
callback.callback(ret);
|
|
2456
|
+
} catch (JSONException e) {
|
|
2457
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2458
|
+
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
2459
|
+
retError.put("error", "parse_error");
|
|
2460
|
+
retError.put("kind", "failed");
|
|
2461
|
+
callback.callback(retError);
|
|
2371
2462
|
}
|
|
2372
2463
|
}
|
|
2373
|
-
|
|
2464
|
+
}
|
|
2465
|
+
);
|
|
2374
2466
|
}
|
|
2375
2467
|
|
|
2376
2468
|
public void getLatest(final String updateUrl, final String channel, final Callback callback) {
|
|
@@ -2553,93 +2645,91 @@ public class CapgoUpdater {
|
|
|
2553
2645
|
.put(RequestBody.create(json.toString(), MediaType.get("application/json")))
|
|
2554
2646
|
.build();
|
|
2555
2647
|
|
|
2556
|
-
DownloadService.sharedClient
|
|
2557
|
-
.
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
callback.callback(retError);
|
|
2566
|
-
}
|
|
2567
|
-
|
|
2568
|
-
@Override
|
|
2569
|
-
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2570
|
-
try (ResponseBody responseBody = response.body()) {
|
|
2571
|
-
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
2572
|
-
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2573
|
-
if (rateLimit.blocked) {
|
|
2574
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2575
|
-
retError.put("message", rateLimit.message);
|
|
2576
|
-
retError.put("error", rateLimit.error);
|
|
2577
|
-
callback.callback(retError);
|
|
2578
|
-
return;
|
|
2579
|
-
}
|
|
2648
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2649
|
+
new okhttp3.Callback() {
|
|
2650
|
+
@Override
|
|
2651
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2652
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2653
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
2654
|
+
retError.put("error", "network_error");
|
|
2655
|
+
callback.callback(retError);
|
|
2656
|
+
}
|
|
2580
2657
|
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2658
|
+
@Override
|
|
2659
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2660
|
+
try (ResponseBody responseBody = response.body()) {
|
|
2661
|
+
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
2662
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2663
|
+
if (rateLimit.blocked) {
|
|
2664
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2665
|
+
retError.put("message", rateLimit.message);
|
|
2666
|
+
retError.put("error", rateLimit.error);
|
|
2667
|
+
callback.callback(retError);
|
|
2668
|
+
return;
|
|
2669
|
+
}
|
|
2591
2670
|
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2671
|
+
if (response.code() == 400) {
|
|
2672
|
+
if (responseData.contains("channel_not_found") && !defaultChannel.isEmpty()) {
|
|
2673
|
+
Map<String, Object> ret = new HashMap<>();
|
|
2674
|
+
ret.put("channel", defaultChannel);
|
|
2675
|
+
ret.put("status", "default");
|
|
2676
|
+
logger.info("Channel get to \"" + ret);
|
|
2677
|
+
callback.callback(ret);
|
|
2597
2678
|
return;
|
|
2598
2679
|
}
|
|
2680
|
+
}
|
|
2599
2681
|
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
JSONObject jsonResponse = new JSONObject(responseData);
|
|
2682
|
+
if (!response.isSuccessful()) {
|
|
2683
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2684
|
+
retError.put("message", "Server error: " + response.code());
|
|
2685
|
+
retError.put("error", "response_error");
|
|
2686
|
+
callback.callback(retError);
|
|
2687
|
+
return;
|
|
2688
|
+
}
|
|
2608
2689
|
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2690
|
+
if (responseData.isEmpty()) {
|
|
2691
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2692
|
+
retError.put("message", "Empty response body");
|
|
2693
|
+
retError.put("error", "no_response_body");
|
|
2694
|
+
callback.callback(retError);
|
|
2695
|
+
return;
|
|
2696
|
+
}
|
|
2697
|
+
JSONObject jsonResponse = new JSONObject(responseData);
|
|
2698
|
+
|
|
2699
|
+
// Check for server-side errors first
|
|
2700
|
+
if (jsonResponse.has("error")) {
|
|
2701
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2702
|
+
retError.put("error", jsonResponse.getString("error"));
|
|
2703
|
+
if (jsonResponse.has("message")) {
|
|
2704
|
+
retError.put("message", jsonResponse.getString("message"));
|
|
2705
|
+
} else {
|
|
2706
|
+
retError.put("message", "server did not provide a message");
|
|
2620
2707
|
}
|
|
2708
|
+
callback.callback(retError);
|
|
2709
|
+
return;
|
|
2710
|
+
}
|
|
2621
2711
|
|
|
2622
|
-
|
|
2712
|
+
Map<String, Object> ret = new HashMap<>();
|
|
2623
2713
|
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
}
|
|
2714
|
+
Iterator<String> keys = jsonResponse.keys();
|
|
2715
|
+
while (keys.hasNext()) {
|
|
2716
|
+
String key = keys.next();
|
|
2717
|
+
if (jsonResponse.has(key)) {
|
|
2718
|
+
ret.put(key, jsonResponse.get(key));
|
|
2630
2719
|
}
|
|
2631
|
-
persistDefaultChannelFromResponse(ret.get("channel"), editor, defaultChannelKey);
|
|
2632
|
-
logger.info("Channel get to \"" + ret);
|
|
2633
|
-
callback.callback(ret);
|
|
2634
|
-
} catch (JSONException e) {
|
|
2635
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2636
|
-
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
2637
|
-
retError.put("error", "parse_error");
|
|
2638
|
-
callback.callback(retError);
|
|
2639
2720
|
}
|
|
2721
|
+
persistDefaultChannelFromResponse(ret.get("channel"), editor, defaultChannelKey);
|
|
2722
|
+
logger.info("Channel get to \"" + ret);
|
|
2723
|
+
callback.callback(ret);
|
|
2724
|
+
} catch (JSONException e) {
|
|
2725
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2726
|
+
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
2727
|
+
retError.put("error", "parse_error");
|
|
2728
|
+
callback.callback(retError);
|
|
2640
2729
|
}
|
|
2641
2730
|
}
|
|
2642
|
-
|
|
2731
|
+
}
|
|
2732
|
+
);
|
|
2643
2733
|
}
|
|
2644
2734
|
|
|
2645
2735
|
void persistDefaultChannelFromResponse(final Object channel, final SharedPreferences.Editor editor, final String defaultChannelKey) {
|
|
@@ -2712,85 +2802,83 @@ public class CapgoUpdater {
|
|
|
2712
2802
|
|
|
2713
2803
|
Request request = new Request.Builder().url(urlBuilder.build()).get().build();
|
|
2714
2804
|
|
|
2715
|
-
DownloadService.sharedClient
|
|
2716
|
-
.
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
callback.callback(retError);
|
|
2725
|
-
}
|
|
2805
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2806
|
+
new okhttp3.Callback() {
|
|
2807
|
+
@Override
|
|
2808
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2809
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2810
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
2811
|
+
retError.put("error", "network_error");
|
|
2812
|
+
callback.callback(retError);
|
|
2813
|
+
}
|
|
2726
2814
|
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2815
|
+
@Override
|
|
2816
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2817
|
+
try (ResponseBody responseBody = response.body()) {
|
|
2818
|
+
final String data = responseBody != null ? responseBody.string() : "";
|
|
2819
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, data);
|
|
2820
|
+
if (rateLimit.blocked) {
|
|
2821
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2822
|
+
retError.put("message", rateLimit.message);
|
|
2823
|
+
retError.put("error", rateLimit.error);
|
|
2824
|
+
callback.callback(retError);
|
|
2825
|
+
return;
|
|
2826
|
+
}
|
|
2739
2827
|
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2828
|
+
if (!response.isSuccessful()) {
|
|
2829
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2830
|
+
retError.put("message", "Server error: " + response.code());
|
|
2831
|
+
retError.put("error", "response_error");
|
|
2832
|
+
callback.callback(retError);
|
|
2833
|
+
return;
|
|
2834
|
+
}
|
|
2747
2835
|
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2836
|
+
if (data.isEmpty()) {
|
|
2837
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2838
|
+
retError.put("message", "Empty response body");
|
|
2839
|
+
retError.put("error", "no_response_body");
|
|
2840
|
+
callback.callback(retError);
|
|
2841
|
+
return;
|
|
2842
|
+
}
|
|
2755
2843
|
|
|
2756
|
-
|
|
2757
|
-
|
|
2844
|
+
try {
|
|
2845
|
+
Map<String, Object> ret = parseListChannelsResponse(data);
|
|
2758
2846
|
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2767
|
-
retError.put("error", json.getString("error"));
|
|
2768
|
-
if (json.has("message")) {
|
|
2769
|
-
retError.put("message", json.getString("message"));
|
|
2770
|
-
} else {
|
|
2771
|
-
retError.put("message", "server did not provide a message");
|
|
2772
|
-
}
|
|
2773
|
-
callback.callback(retError);
|
|
2774
|
-
return;
|
|
2775
|
-
}
|
|
2847
|
+
logger.info("Channels listed successfully");
|
|
2848
|
+
callback.callback(ret);
|
|
2849
|
+
} catch (JSONException arrayException) {
|
|
2850
|
+
// If not an array, try to parse as error object
|
|
2851
|
+
try {
|
|
2852
|
+
JSONObject json = new JSONObject(data);
|
|
2853
|
+
if (json.has("error")) {
|
|
2776
2854
|
Map<String, Object> retError = new HashMap<>();
|
|
2777
|
-
retError.put("
|
|
2778
|
-
|
|
2855
|
+
retError.put("error", json.getString("error"));
|
|
2856
|
+
if (json.has("message")) {
|
|
2857
|
+
retError.put("message", json.getString("message"));
|
|
2858
|
+
} else {
|
|
2859
|
+
retError.put("message", "server did not provide a message");
|
|
2860
|
+
}
|
|
2779
2861
|
callback.callback(retError);
|
|
2780
2862
|
return;
|
|
2781
|
-
} catch (JSONException objException) {
|
|
2782
|
-
// If neither array nor object, throw parse error
|
|
2783
|
-
arrayException.addSuppressed(objException);
|
|
2784
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2785
|
-
retError.put("message", "JSON parse error: " + arrayException.getMessage());
|
|
2786
|
-
retError.put("error", "parse_error");
|
|
2787
|
-
callback.callback(retError);
|
|
2788
2863
|
}
|
|
2864
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2865
|
+
retError.put("message", "Unexpected channels response format");
|
|
2866
|
+
retError.put("error", "parse_error");
|
|
2867
|
+
callback.callback(retError);
|
|
2868
|
+
return;
|
|
2869
|
+
} catch (JSONException objException) {
|
|
2870
|
+
// If neither array nor object, throw parse error
|
|
2871
|
+
arrayException.addSuppressed(objException);
|
|
2872
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2873
|
+
retError.put("message", "JSON parse error: " + arrayException.getMessage());
|
|
2874
|
+
retError.put("error", "parse_error");
|
|
2875
|
+
callback.callback(retError);
|
|
2789
2876
|
}
|
|
2790
2877
|
}
|
|
2791
2878
|
}
|
|
2792
2879
|
}
|
|
2793
|
-
|
|
2880
|
+
}
|
|
2881
|
+
);
|
|
2794
2882
|
}
|
|
2795
2883
|
|
|
2796
2884
|
static Map<String, Object> parseListChannelsResponse(final String data) throws JSONException {
|
|
@@ -3112,67 +3200,65 @@ public class CapgoUpdater {
|
|
|
3112
3200
|
.build();
|
|
3113
3201
|
|
|
3114
3202
|
final int eventCount = eventsToSend.size();
|
|
3115
|
-
DownloadService.sharedClient
|
|
3116
|
-
.
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3203
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
3204
|
+
new okhttp3.Callback() {
|
|
3205
|
+
@Override
|
|
3206
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
3207
|
+
if (abandonStoppedStatsFlush()) {
|
|
3208
|
+
return;
|
|
3209
|
+
}
|
|
3210
|
+
requeueStatsEvents(eventsToSend);
|
|
3211
|
+
if (logger != null) {
|
|
3212
|
+
logger.error("Failed to send stats batch");
|
|
3213
|
+
logger.debug("Error: " + e.getMessage());
|
|
3214
|
+
}
|
|
3215
|
+
statsFlushInFlight.set(false);
|
|
3216
|
+
}
|
|
3217
|
+
|
|
3218
|
+
@Override
|
|
3219
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
3220
|
+
try (ResponseBody responseBody = response.body()) {
|
|
3121
3221
|
if (abandonStoppedStatsFlush()) {
|
|
3122
3222
|
return;
|
|
3123
3223
|
}
|
|
3124
|
-
|
|
3125
|
-
if (
|
|
3126
|
-
|
|
3127
|
-
|
|
3224
|
+
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
3225
|
+
if (checkAndHandleRateLimitResponse(response, responseData).blocked) {
|
|
3226
|
+
requeueStatsEvents(eventsToSend);
|
|
3227
|
+
return;
|
|
3128
3228
|
}
|
|
3129
|
-
statsFlushInFlight.set(false);
|
|
3130
|
-
}
|
|
3131
3229
|
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
if (abandonStoppedStatsFlush()) {
|
|
3136
|
-
return;
|
|
3230
|
+
if (response.isSuccessful()) {
|
|
3231
|
+
synchronized (statsQueue) {
|
|
3232
|
+
statsInFlight.clear();
|
|
3137
3233
|
}
|
|
3138
|
-
|
|
3139
|
-
if (
|
|
3140
|
-
|
|
3141
|
-
|
|
3234
|
+
persistStatsQueue();
|
|
3235
|
+
if (logger != null) {
|
|
3236
|
+
logger.info("Stats batch sent successfully");
|
|
3237
|
+
logger.debug("Sent " + eventCount + " events");
|
|
3142
3238
|
}
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
logger.debug("Retrying later, response code: " + response.code());
|
|
3159
|
-
}
|
|
3160
|
-
} else {
|
|
3161
|
-
synchronized (statsQueue) {
|
|
3162
|
-
statsInFlight.clear();
|
|
3163
|
-
}
|
|
3164
|
-
persistStatsQueue();
|
|
3165
|
-
if (logger != null) {
|
|
3166
|
-
logger.error("Dropping stats batch after permanent error");
|
|
3167
|
-
logger.debug("Response code: " + response.code());
|
|
3168
|
-
}
|
|
3239
|
+
runStatsCallbacks(eventsToSend);
|
|
3240
|
+
} else if (isTransientStatsFailure(response.code())) {
|
|
3241
|
+
requeueStatsEvents(eventsToSend);
|
|
3242
|
+
if (logger != null) {
|
|
3243
|
+
logger.error("Error sending stats batch");
|
|
3244
|
+
logger.debug("Retrying later, response code: " + response.code());
|
|
3245
|
+
}
|
|
3246
|
+
} else {
|
|
3247
|
+
synchronized (statsQueue) {
|
|
3248
|
+
statsInFlight.clear();
|
|
3249
|
+
}
|
|
3250
|
+
persistStatsQueue();
|
|
3251
|
+
if (logger != null) {
|
|
3252
|
+
logger.error("Dropping stats batch after permanent error");
|
|
3253
|
+
logger.debug("Response code: " + response.code());
|
|
3169
3254
|
}
|
|
3170
|
-
} finally {
|
|
3171
|
-
statsFlushInFlight.set(false);
|
|
3172
3255
|
}
|
|
3256
|
+
} finally {
|
|
3257
|
+
statsFlushInFlight.set(false);
|
|
3173
3258
|
}
|
|
3174
3259
|
}
|
|
3175
|
-
|
|
3260
|
+
}
|
|
3261
|
+
);
|
|
3176
3262
|
}
|
|
3177
3263
|
|
|
3178
3264
|
private boolean abandonStoppedStatsFlush() {
|