native-update 3.4.2 → 4.0.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/AI-INTEGRATION-GUIDE.md +26 -16
- package/NativeUpdate.podspec +2 -2
- package/README.md +23 -20
- package/android/build.gradle +37 -33
- package/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/android/proguard-rules.pro +0 -3
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +45 -49
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +12 -10
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +12 -21
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +3 -34
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +206 -72
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +74 -37
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +169 -179
- package/android/variables.gradle +2 -2
- package/cli/.yarn/install-state.gz +0 -0
- package/cli/commands/bundle-verify.js +4 -1
- package/cli/commands/keys-generate.js +12 -46
- package/cli/index.js +4 -7
- package/cli/package.json +2 -1
- package/dist/esm/core/config.d.ts +3 -0
- package/dist/esm/core/config.js +17 -0
- package/dist/esm/core/plugin-manager.js +4 -2
- package/dist/esm/core/security.js +2 -5
- package/dist/esm/core/update-check.d.ts +2 -0
- package/dist/esm/core/update-check.js +5 -3
- package/dist/esm/definitions.d.ts +43 -2
- package/dist/esm/live-update/delta-processor.d.ts +6 -20
- package/dist/esm/live-update/delta-processor.js +75 -119
- package/dist/esm/plugin.d.ts +0 -3
- package/dist/esm/plugin.js +172 -12
- package/dist/esm/security/crypto.js +2 -5
- package/dist/esm/web.d.ts +1 -9
- package/dist/esm/web.js +4 -56
- package/dist/plugin.cjs +1 -1
- package/dist/plugin.esm.js +1 -1
- package/dist/plugin.js +2 -2
- package/docs/CHANGELOG.md +42 -0
- package/docs/KNOWN_LIMITATIONS.md +4 -3
- package/docs/MIGRATION.md +32 -3
- package/docs/server-requirements.md +40 -29
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +6 -2
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +21 -30
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +269 -61
- package/ios/Plugin/NativeUpdatePlugin.m +8 -1
- package/ios/Plugin/NativeUpdatePlugin.swift +71 -34
- package/ios/Plugin/Security/SecurityManager.swift +100 -99
- package/package.json +25 -14
|
@@ -1,43 +1,41 @@
|
|
|
1
1
|
package com.aoneahsan.nativeupdate
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
-
import android.content.pm.PackageManager
|
|
5
4
|
import androidx.work.*
|
|
6
5
|
import com.getcapacitor.JSObject
|
|
7
|
-
import com.getcapacitor.Plugin
|
|
8
6
|
import com.getcapacitor.PluginCall
|
|
9
|
-
import com.getcapacitor.PluginMethod
|
|
10
|
-
import com.getcapacitor.annotation.CapacitorPlugin
|
|
11
7
|
import java.util.concurrent.TimeUnit
|
|
12
8
|
import org.json.JSONObject
|
|
13
9
|
import org.json.JSONArray
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
class BackgroundUpdatePlugin : Plugin() {
|
|
11
|
+
class BackgroundUpdatePlugin(private val context: Context) {
|
|
17
12
|
|
|
18
13
|
private lateinit var notificationManager: BackgroundNotificationManager
|
|
19
14
|
private var backgroundUpdateConfig: BackgroundUpdateConfig? = null
|
|
20
15
|
private var backgroundUpdateStatus: BackgroundUpdateStatus = BackgroundUpdateStatus()
|
|
16
|
+
private var eventListener: ((String, JSObject) -> Unit)? = null
|
|
21
17
|
|
|
22
18
|
companion object {
|
|
23
19
|
private const val WORK_NAME = "background_update_work"
|
|
24
20
|
private const val WORK_TAG = "capacitor_native_update"
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
init {
|
|
24
|
+
notificationManager = BackgroundNotificationManager(context) { event, data ->
|
|
25
|
+
eventListener?.invoke(event, data)
|
|
26
|
+
}
|
|
30
27
|
notificationManager.createNotificationChannel()
|
|
31
|
-
|
|
32
|
-
// Register this plugin instance with the manager
|
|
33
28
|
BackgroundUpdateManager.registerBackgroundUpdatePlugin(this)
|
|
34
29
|
}
|
|
30
|
+
|
|
31
|
+
fun setEventListener(listener: (String, JSObject) -> Unit) {
|
|
32
|
+
eventListener = listener
|
|
33
|
+
}
|
|
35
34
|
|
|
36
35
|
fun notifyBackgroundUpdateListeners(event: String, data: JSObject) {
|
|
37
|
-
|
|
36
|
+
eventListener?.invoke(event, data)
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
@PluginMethod
|
|
41
39
|
fun enableBackgroundUpdates(call: PluginCall) {
|
|
42
40
|
try {
|
|
43
41
|
val configData = call.data
|
|
@@ -58,25 +56,21 @@ class BackgroundUpdatePlugin : Plugin() {
|
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
58
|
|
|
61
|
-
@PluginMethod
|
|
62
59
|
fun disableBackgroundUpdates(call: PluginCall) {
|
|
63
60
|
cancelAllBackgroundWork()
|
|
64
61
|
call.resolve()
|
|
65
62
|
}
|
|
66
63
|
|
|
67
|
-
@PluginMethod
|
|
68
64
|
fun getBackgroundUpdateStatus(call: PluginCall) {
|
|
69
65
|
call.resolve(backgroundUpdateStatus.toJSObject())
|
|
70
66
|
}
|
|
71
67
|
|
|
72
|
-
@PluginMethod
|
|
73
68
|
fun scheduleBackgroundCheck(call: PluginCall) {
|
|
74
69
|
val interval = call.getLong("interval") ?: (24 * 60 * 60 * 1000L) // Default 24 hours
|
|
75
70
|
scheduleBackgroundWork(interval)
|
|
76
71
|
call.resolve()
|
|
77
72
|
}
|
|
78
73
|
|
|
79
|
-
@PluginMethod
|
|
80
74
|
fun triggerBackgroundCheck(call: PluginCall) {
|
|
81
75
|
val workRequest = OneTimeWorkRequestBuilder<BackgroundUpdateWorker>()
|
|
82
76
|
.setInputData(createWorkData())
|
|
@@ -103,20 +97,17 @@ class BackgroundUpdatePlugin : Plugin() {
|
|
|
103
97
|
call.resolve(JSObject().put("triggered", true))
|
|
104
98
|
}
|
|
105
99
|
|
|
106
|
-
@PluginMethod
|
|
107
100
|
fun setNotificationPreferences(call: PluginCall) {
|
|
108
101
|
val preferences = call.data
|
|
109
102
|
notificationManager.setPreferences(preferences)
|
|
110
103
|
call.resolve()
|
|
111
104
|
}
|
|
112
105
|
|
|
113
|
-
@PluginMethod
|
|
114
106
|
fun getNotificationPermissions(call: PluginCall) {
|
|
115
107
|
val status = notificationManager.getPermissionStatus()
|
|
116
108
|
call.resolve(status.toJSObject())
|
|
117
109
|
}
|
|
118
110
|
|
|
119
|
-
@PluginMethod
|
|
120
111
|
fun requestNotificationPermissions(call: PluginCall) {
|
|
121
112
|
val granted = notificationManager.requestPermissions()
|
|
122
113
|
call.resolve(JSObject().put("granted", granted))
|
|
@@ -349,4 +340,4 @@ data class LatestVersion(
|
|
|
349
340
|
version?.let { obj.put("version", it) }
|
|
350
341
|
return obj
|
|
351
342
|
}
|
|
352
|
-
}
|
|
343
|
+
}
|
|
@@ -2,7 +2,6 @@ package com.aoneahsan.nativeupdate
|
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import androidx.work.*
|
|
5
|
-
import com.getcapacitor.Bridge
|
|
6
5
|
import com.getcapacitor.JSObject
|
|
7
6
|
import com.google.gson.Gson
|
|
8
7
|
import kotlinx.coroutines.Dispatchers
|
|
@@ -122,17 +121,7 @@ class BackgroundUpdateWorker(
|
|
|
122
121
|
if (appUpdatePlugin != null) {
|
|
123
122
|
// Use actual plugin to check for updates
|
|
124
123
|
appUpdatePlugin.getAppUpdateInfoAsync()
|
|
125
|
-
} else
|
|
126
|
-
// Fallback to basic implementation
|
|
127
|
-
val currentVersion = getCurrentAppVersion()
|
|
128
|
-
val availableVersion = getAvailableAppVersion()
|
|
129
|
-
|
|
130
|
-
AppUpdateStatus(
|
|
131
|
-
updateAvailable = availableVersion != currentVersion,
|
|
132
|
-
currentVersion = currentVersion,
|
|
133
|
-
availableVersion = if (availableVersion != currentVersion) availableVersion else null
|
|
134
|
-
)
|
|
135
|
-
}
|
|
124
|
+
} else null
|
|
136
125
|
} catch (e: Exception) {
|
|
137
126
|
android.util.Log.e(TAG, "App update check failed", e)
|
|
138
127
|
null
|
|
@@ -147,15 +136,7 @@ class BackgroundUpdateWorker(
|
|
|
147
136
|
if (liveUpdatePlugin != null) {
|
|
148
137
|
// Use actual plugin to check for updates
|
|
149
138
|
liveUpdatePlugin.getLatestVersionAsync()
|
|
150
|
-
} else
|
|
151
|
-
// Fallback to basic implementation
|
|
152
|
-
val hasUpdate = checkForLiveUpdateAvailable()
|
|
153
|
-
|
|
154
|
-
LatestVersion(
|
|
155
|
-
available = hasUpdate,
|
|
156
|
-
version = if (hasUpdate) "2.0.0" else null
|
|
157
|
-
)
|
|
158
|
-
}
|
|
139
|
+
} else null
|
|
159
140
|
} catch (e: Exception) {
|
|
160
141
|
android.util.Log.e(TAG, "Live update check failed", e)
|
|
161
142
|
null
|
|
@@ -172,18 +153,6 @@ class BackgroundUpdateWorker(
|
|
|
172
153
|
}
|
|
173
154
|
}
|
|
174
155
|
|
|
175
|
-
private fun getAvailableAppVersion(): String {
|
|
176
|
-
// This would typically check Google Play Store API
|
|
177
|
-
// For demo purposes, we'll return a mock version
|
|
178
|
-
return "1.0.1"
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private fun checkForLiveUpdateAvailable(): Boolean {
|
|
182
|
-
// This would typically check the live update server
|
|
183
|
-
// For demo purposes, we'll return false
|
|
184
|
-
return false
|
|
185
|
-
}
|
|
186
|
-
|
|
187
156
|
private fun sendNotification(result: BackgroundCheckResult) {
|
|
188
157
|
try {
|
|
189
158
|
val plugin = getBackgroundUpdatePlugin()
|
|
@@ -264,4 +233,4 @@ class BackgroundUpdateWorker(
|
|
|
264
233
|
null
|
|
265
234
|
}
|
|
266
235
|
}
|
|
267
|
-
}
|
|
236
|
+
}
|
|
@@ -2,18 +2,18 @@ package com.aoneahsan.nativeupdate
|
|
|
2
2
|
|
|
3
3
|
import android.app.Activity
|
|
4
4
|
import android.content.Context
|
|
5
|
+
import android.content.pm.ApplicationInfo
|
|
6
|
+
import android.util.Base64
|
|
5
7
|
import com.getcapacitor.JSObject
|
|
6
8
|
import com.getcapacitor.PluginCall
|
|
7
9
|
import kotlinx.coroutines.*
|
|
8
10
|
import okhttp3.*
|
|
9
|
-
import okhttp3.tls.HandshakeCertificates
|
|
10
11
|
import java.io.File
|
|
11
12
|
import java.io.FileOutputStream
|
|
12
13
|
import java.io.IOException
|
|
13
14
|
import java.security.MessageDigest
|
|
14
15
|
import java.util.concurrent.TimeUnit
|
|
15
16
|
import java.util.zip.ZipFile
|
|
16
|
-
import javax.net.ssl.X509TrustManager
|
|
17
17
|
|
|
18
18
|
class LiveUpdatePlugin(
|
|
19
19
|
private val activity: Activity,
|
|
@@ -31,11 +31,18 @@ class LiveUpdatePlugin(
|
|
|
31
31
|
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
|
32
32
|
private lateinit var okHttpClient: OkHttpClient
|
|
33
33
|
private val securityManager = SecurityManager(context)
|
|
34
|
-
private val activeDownloads =
|
|
35
|
-
private var downloadManager: android.app.DownloadManager? = null
|
|
34
|
+
private val activeDownloads = java.util.concurrent.ConcurrentHashMap<String, Call>()
|
|
36
35
|
|
|
37
36
|
/** Hard cap on uncompressed bundle size (zip-bomb defense). */
|
|
38
|
-
private val maxUncompressedBundleBytes: Long
|
|
37
|
+
private val maxUncompressedBundleBytes: Long
|
|
38
|
+
get() = config?.optLong("maxUncompressedBundleSize", 500L * 1024 * 1024)
|
|
39
|
+
?.coerceIn(1L, 2L * 1024 * 1024 * 1024)
|
|
40
|
+
?: 500L * 1024 * 1024
|
|
41
|
+
|
|
42
|
+
private val maxDownloadBytes: Long
|
|
43
|
+
get() = config?.optLong("maxBundleSize", 100L * 1024 * 1024)
|
|
44
|
+
?.coerceIn(1L, 2L * 1024 * 1024 * 1024)
|
|
45
|
+
?: 100L * 1024 * 1024
|
|
39
46
|
|
|
40
47
|
/**
|
|
41
48
|
* The app's signing-certificate fingerprints (SHA-256, SHA-1) as lowercase
|
|
@@ -68,7 +75,6 @@ class LiveUpdatePlugin(
|
|
|
68
75
|
init {
|
|
69
76
|
// Initialize OkHttp with default settings
|
|
70
77
|
okHttpClient = createOkHttpClient()
|
|
71
|
-
downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as? android.app.DownloadManager
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
private fun createOkHttpClient(): OkHttpClient {
|
|
@@ -98,12 +104,19 @@ class LiveUpdatePlugin(
|
|
|
98
104
|
okHttpClient = createOkHttpClient()
|
|
99
105
|
|
|
100
106
|
// Validate configuration
|
|
101
|
-
val serverUrl = config.
|
|
102
|
-
if (serverUrl != null && !
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
val serverUrl = config.optString("serverUrl").takeIf { it.isNotBlank() }
|
|
108
|
+
if (serverUrl != null && !securityManager.validateUrl(serverUrl)) {
|
|
109
|
+
throw IllegalArgumentException("Server URL must be an allowed HTTPS URL")
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
val requireSignature = config.optBoolean("requireSignature", true)
|
|
113
|
+
if (requireSignature && config.optString("publicKey").isBlank()) {
|
|
114
|
+
throw IllegalArgumentException("publicKey is required when requireSignature is enabled")
|
|
115
|
+
}
|
|
116
|
+
if (!requireSignature && !unsignedDevelopmentUpdatesAllowed()) {
|
|
117
|
+
throw IllegalArgumentException(
|
|
118
|
+
"Unsigned updates are allowed only in debuggable builds with allowUnsignedDevelopmentUpdates enabled"
|
|
119
|
+
)
|
|
107
120
|
}
|
|
108
121
|
}
|
|
109
122
|
|
|
@@ -179,14 +192,15 @@ class LiveUpdatePlugin(
|
|
|
179
192
|
return@launch
|
|
180
193
|
}
|
|
181
194
|
|
|
182
|
-
if (!
|
|
183
|
-
call.reject("INSECURE_URL", "Download URL must
|
|
195
|
+
if (!securityManager.validateUrl(url)) {
|
|
196
|
+
call.reject("INSECURE_URL", "Download URL must be an allowed HTTPS URL")
|
|
184
197
|
return@launch
|
|
185
198
|
}
|
|
186
199
|
|
|
187
200
|
downloadDir.mkdirs()
|
|
188
201
|
|
|
189
|
-
val downloadedFile =
|
|
202
|
+
val downloadedFile = tryApplyDelta(call, downloadDir, bundleId)
|
|
203
|
+
?: downloadBundle(url, downloadDir, bundleId)
|
|
190
204
|
|
|
191
205
|
if (!verifyChecksum(downloadedFile, checksum)) {
|
|
192
206
|
call.reject("CHECKSUM_ERROR", "Bundle checksum validation failed")
|
|
@@ -194,17 +208,21 @@ class LiveUpdatePlugin(
|
|
|
194
208
|
}
|
|
195
209
|
|
|
196
210
|
val signature = call.getString("signature")
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
211
|
+
val publicKey = config?.getString("publicKey")
|
|
212
|
+
val requireSignature = config?.getBool("requireSignature") ?: true
|
|
213
|
+
val signatureAlgorithm = call.getString("signatureAlgorithm") ?: "RSA-SHA256"
|
|
214
|
+
if (signatureAlgorithm != "RSA-SHA256") {
|
|
215
|
+
call.reject("SIGNATURE_ERROR", "Unsupported signature algorithm")
|
|
216
|
+
return@launch
|
|
217
|
+
}
|
|
218
|
+
if (requireSignature && signature.isNullOrBlank()) {
|
|
219
|
+
call.reject("SIGNATURE_ERROR", "Bundle signature is required")
|
|
220
|
+
return@launch
|
|
221
|
+
}
|
|
222
|
+
if (!signature.isNullOrBlank()) {
|
|
223
|
+
if (publicKey.isNullOrBlank() || !securityManager.verifySignature(downloadedFile, signature, publicKey)) {
|
|
224
|
+
call.reject("SIGNATURE_ERROR", "Bundle signature validation failed")
|
|
225
|
+
return@launch
|
|
208
226
|
}
|
|
209
227
|
}
|
|
210
228
|
|
|
@@ -231,6 +249,7 @@ class LiveUpdatePlugin(
|
|
|
231
249
|
bundleInfo.put("verified", true)
|
|
232
250
|
if (signature != null) {
|
|
233
251
|
bundleInfo.put("signature", signature)
|
|
252
|
+
bundleInfo.put("signatureAlgorithm", "RSA-SHA256")
|
|
234
253
|
}
|
|
235
254
|
|
|
236
255
|
saveBundleInfo(bundleInfo)
|
|
@@ -365,6 +384,11 @@ class LiveUpdatePlugin(
|
|
|
365
384
|
result.put("url", latestVersion.getString("downloadUrl"))
|
|
366
385
|
result.put("notes", latestVersion.getString("releaseNotes"))
|
|
367
386
|
result.put("checksum", latestVersion.getString("checksum"))
|
|
387
|
+
latestVersion.optString("signature", "")
|
|
388
|
+
.takeIf { it.isNotEmpty() }
|
|
389
|
+
?.let { result.put("signature", it) }
|
|
390
|
+
result.put("signatureAlgorithm", latestVersion.optString("signatureAlgorithm", "RSA-SHA256"))
|
|
391
|
+
latestVersion.optJSONObject("delta")?.let { result.put("delta", it) }
|
|
368
392
|
result.put("size", latestVersion.optLong("size", 0))
|
|
369
393
|
result.put("mandatory", latestVersion.optBoolean("mandatory", false))
|
|
370
394
|
call.resolve(result)
|
|
@@ -390,17 +414,12 @@ class LiveUpdatePlugin(
|
|
|
390
414
|
}
|
|
391
415
|
|
|
392
416
|
fun setUpdateUrl(call: PluginCall) {
|
|
393
|
-
|
|
417
|
+
call.getString("url") ?: run {
|
|
394
418
|
call.reject("URL is required")
|
|
395
419
|
return
|
|
396
420
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
call.reject("INSECURE_URL", "Update URL must use HTTPS")
|
|
400
|
-
return
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
config?.put("serverUrl", url)
|
|
421
|
+
|
|
422
|
+
android.util.Log.w("NativeUpdate", "setUpdateUrl() is deprecated and ignored; the update URL is locked after initialize()")
|
|
404
423
|
call.resolve()
|
|
405
424
|
}
|
|
406
425
|
|
|
@@ -433,6 +452,33 @@ class LiveUpdatePlugin(
|
|
|
433
452
|
}
|
|
434
453
|
}
|
|
435
454
|
}
|
|
455
|
+
|
|
456
|
+
fun cancelDownload(call: PluginCall) {
|
|
457
|
+
val bundleId = call.getString("bundleId") ?: run {
|
|
458
|
+
call.reject("Bundle ID is required")
|
|
459
|
+
return
|
|
460
|
+
}
|
|
461
|
+
activeDownloads.remove(bundleId)?.cancel()
|
|
462
|
+
call.resolve()
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
fun cancelAllDownloads(call: PluginCall) {
|
|
466
|
+
activeDownloads.values.forEach(Call::cancel)
|
|
467
|
+
activeDownloads.clear()
|
|
468
|
+
call.resolve()
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
fun isDownloading(call: PluginCall) {
|
|
472
|
+
val bundleId = call.getString("bundleId") ?: run {
|
|
473
|
+
call.reject("Bundle ID is required")
|
|
474
|
+
return
|
|
475
|
+
}
|
|
476
|
+
call.resolve(JSObject().put("isDownloading", activeDownloads.containsKey(bundleId)))
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
fun getActiveDownloadCount(call: PluginCall) {
|
|
480
|
+
call.resolve(JSObject().put("count", activeDownloads.size))
|
|
481
|
+
}
|
|
436
482
|
|
|
437
483
|
/**
|
|
438
484
|
* The app's client identity for API-key client restrictions: applicationId
|
|
@@ -542,45 +588,131 @@ class LiveUpdatePlugin(
|
|
|
542
588
|
}
|
|
543
589
|
}
|
|
544
590
|
|
|
545
|
-
private suspend fun downloadBundle(
|
|
591
|
+
private suspend fun downloadBundle(
|
|
592
|
+
url: String,
|
|
593
|
+
downloadDir: File,
|
|
594
|
+
bundleId: String,
|
|
595
|
+
filename: String = "bundle.zip"
|
|
596
|
+
): File {
|
|
546
597
|
return withContext(Dispatchers.IO) {
|
|
547
598
|
val request = Request.Builder()
|
|
548
599
|
.url(url)
|
|
549
600
|
.build()
|
|
550
601
|
|
|
551
|
-
val
|
|
552
|
-
|
|
553
|
-
|
|
602
|
+
val networkCall = okHttpClient.newCall(request)
|
|
603
|
+
activeDownloads[bundleId] = networkCall
|
|
604
|
+
try {
|
|
605
|
+
networkCall.execute().use { response ->
|
|
606
|
+
if (!response.isSuccessful) throw IOException("Download failed: ${response.code}")
|
|
607
|
+
|
|
608
|
+
val file = File(downloadDir, filename)
|
|
609
|
+
val totalBytes = response.body.contentLength()
|
|
610
|
+
if (totalBytes > maxDownloadBytes) throw IOException("Bundle exceeds maximum download size")
|
|
611
|
+
var downloadedBytes = 0L
|
|
612
|
+
|
|
613
|
+
response.body.byteStream().use { input ->
|
|
614
|
+
FileOutputStream(file).use { output ->
|
|
615
|
+
val buffer = ByteArray(8192)
|
|
616
|
+
while (true) {
|
|
617
|
+
val read = input.read(buffer)
|
|
618
|
+
if (read < 0) break
|
|
619
|
+
output.write(buffer, 0, read)
|
|
620
|
+
downloadedBytes += read
|
|
621
|
+
if (downloadedBytes > maxDownloadBytes) {
|
|
622
|
+
throw IOException("Bundle exceeds maximum download size")
|
|
623
|
+
}
|
|
624
|
+
if (totalBytes > 0) {
|
|
625
|
+
progressListener?.invoke(JSObject().apply {
|
|
626
|
+
put("percent", ((downloadedBytes * 100) / totalBytes).toInt())
|
|
627
|
+
put("bytesDownloaded", downloadedBytes)
|
|
628
|
+
put("totalBytes", totalBytes)
|
|
629
|
+
put("bundleId", bundleId)
|
|
630
|
+
})
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
file
|
|
636
|
+
}
|
|
637
|
+
} finally {
|
|
638
|
+
activeDownloads.remove(bundleId)
|
|
554
639
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
private suspend fun tryApplyDelta(call: PluginCall, directory: File, bundleId: String): File? {
|
|
644
|
+
if (config?.optBoolean("enableDeltaUpdates", true) == false) return null
|
|
645
|
+
val delta = call.getObject("delta") ?: return null
|
|
646
|
+
if (delta.optString("format") != "NUDELTA/1" ||
|
|
647
|
+
delta.optString("fromVersion") != getCurrentVersion()) return null
|
|
648
|
+
return try {
|
|
649
|
+
val activeId = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE)
|
|
650
|
+
.getString(ACTIVE_BUNDLE_KEY, null) ?: return null
|
|
651
|
+
val basePath = getBundleInfoById(activeId)?.optString("path", "") ?: return null
|
|
652
|
+
val base = File(basePath)
|
|
653
|
+
if (!base.isFile) return null
|
|
654
|
+
val patchUrl = delta.getString("patchUrl") ?: return null
|
|
655
|
+
if (!securityManager.validateUrl(patchUrl)) return null
|
|
656
|
+
val patchChecksum = delta.getString("patchChecksum") ?: return null
|
|
657
|
+
val targetChecksum = delta.getString("targetChecksum") ?: return null
|
|
658
|
+
val patch = downloadBundle(patchUrl, directory, bundleId, "patch.nudelta.json")
|
|
659
|
+
if (!verifyChecksum(patch, patchChecksum)) return null
|
|
660
|
+
val document = JSObject(patch.readText(Charsets.UTF_8))
|
|
661
|
+
if (document.optString("format") != "NUDELTA/1" ||
|
|
662
|
+
document.optString("fromVersion") != delta.optString("fromVersion") ||
|
|
663
|
+
document.optString("targetChecksum") != targetChecksum ||
|
|
664
|
+
document.optString("baseChecksum").isBlank() ||
|
|
665
|
+
!verifyChecksum(base, document.optString("baseChecksum"))) return null
|
|
666
|
+
val targetSize = document.getLong("targetSize") ?: return null
|
|
667
|
+
if (targetSize <= 0 || targetSize > maxDownloadBytes) return null
|
|
668
|
+
val output = File(directory, "bundle.zip")
|
|
669
|
+
java.io.RandomAccessFile(base, "r").use { source ->
|
|
670
|
+
FileOutputStream(output).use { destination ->
|
|
671
|
+
var written = 0L
|
|
672
|
+
val operations = document.getJSONArray("operations") ?: return null
|
|
673
|
+
if (operations.length() == 0 || operations.length() > 100_000) return null
|
|
674
|
+
for (index in 0 until operations.length()) {
|
|
675
|
+
val operation = operations.getJSONObject(index)
|
|
676
|
+
when (operation.getString("op")) {
|
|
677
|
+
"copy" -> {
|
|
678
|
+
val offset = operation.getLong("offset")
|
|
679
|
+
val length = operation.getLong("length")
|
|
680
|
+
if (offset < 0 || length <= 0 || length > source.length() ||
|
|
681
|
+
offset > source.length() - length) return null
|
|
682
|
+
source.seek(offset)
|
|
683
|
+
var remaining = length
|
|
684
|
+
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
|
685
|
+
while (remaining > 0) {
|
|
686
|
+
val read = source.read(buffer, 0, minOf(buffer.size.toLong(), remaining).toInt())
|
|
687
|
+
if (read <= 0) return null
|
|
688
|
+
destination.write(buffer, 0, read)
|
|
689
|
+
remaining -= read
|
|
690
|
+
}
|
|
691
|
+
written += length
|
|
692
|
+
}
|
|
693
|
+
"literal" -> {
|
|
694
|
+
val bytes = Base64.decode(operation.getString("data"), Base64.DEFAULT)
|
|
695
|
+
destination.write(bytes)
|
|
696
|
+
written += bytes.size
|
|
697
|
+
}
|
|
698
|
+
else -> return null
|
|
578
699
|
}
|
|
700
|
+
if (written > targetSize) return null
|
|
579
701
|
}
|
|
702
|
+
if (written != targetSize) return null
|
|
580
703
|
}
|
|
581
704
|
}
|
|
582
|
-
|
|
583
|
-
|
|
705
|
+
if (!verifyChecksum(output, targetChecksum)) {
|
|
706
|
+
output.delete()
|
|
707
|
+
null
|
|
708
|
+
} else {
|
|
709
|
+
patch.delete()
|
|
710
|
+
output
|
|
711
|
+
}
|
|
712
|
+
} catch (_: Exception) {
|
|
713
|
+
File(directory, "patch.nudelta.json").delete()
|
|
714
|
+
File(directory, "bundle.zip").delete()
|
|
715
|
+
null
|
|
584
716
|
}
|
|
585
717
|
}
|
|
586
718
|
|
|
@@ -671,7 +803,8 @@ class LiveUpdatePlugin(
|
|
|
671
803
|
}
|
|
672
804
|
|
|
673
805
|
val calculatedChecksum = digest.digest().joinToString("") { "%02x".format(it) }
|
|
674
|
-
return
|
|
806
|
+
return expectedChecksum.matches(Regex("^[a-fA-F0-9]{64}$")) &&
|
|
807
|
+
calculatedChecksum.equals(expectedChecksum, ignoreCase = true)
|
|
675
808
|
}
|
|
676
809
|
|
|
677
810
|
/**
|
|
@@ -816,9 +949,7 @@ class LiveUpdatePlugin(
|
|
|
816
949
|
fun cleanup() {
|
|
817
950
|
// Clean up any resources
|
|
818
951
|
// Cancel any ongoing downloads
|
|
819
|
-
activeDownloads.values.forEach
|
|
820
|
-
downloadManager?.remove(downloadId)
|
|
821
|
-
}
|
|
952
|
+
activeDownloads.values.forEach(Call::cancel)
|
|
822
953
|
activeDownloads.clear()
|
|
823
954
|
}
|
|
824
955
|
|
|
@@ -914,11 +1045,9 @@ class LiveUpdatePlugin(
|
|
|
914
1045
|
|
|
915
1046
|
val signature = bundleInfo.optString("signature", "")
|
|
916
1047
|
if (signature.isNotEmpty()) {
|
|
917
|
-
val publicKey = config?.
|
|
918
|
-
?: config?.getString("publicKey")
|
|
1048
|
+
val publicKey = config?.getString("publicKey")
|
|
919
1049
|
if (!publicKey.isNullOrEmpty()) {
|
|
920
|
-
|
|
921
|
-
if (!securityManager.verifySignature(bytes, signature, publicKey)) {
|
|
1050
|
+
if (!securityManager.verifySignature(bundleFile, signature, publicKey)) {
|
|
922
1051
|
rollbackToPrevious("bundle signature mismatch on boot", activeBundleId)
|
|
923
1052
|
return
|
|
924
1053
|
}
|
|
@@ -926,6 +1055,11 @@ class LiveUpdatePlugin(
|
|
|
926
1055
|
}
|
|
927
1056
|
}
|
|
928
1057
|
|
|
1058
|
+
private fun unsignedDevelopmentUpdatesAllowed(): Boolean {
|
|
1059
|
+
val debuggable = context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
|
|
1060
|
+
return debuggable && config?.optBoolean("allowUnsignedDevelopmentUpdates", false) == true
|
|
1061
|
+
}
|
|
1062
|
+
|
|
929
1063
|
private fun rollbackToPrevious(reason: String, fromBundleId: String) {
|
|
930
1064
|
val prefs = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE)
|
|
931
1065
|
val previous = prefs.getString(PREVIOUS_BUNDLE_KEY, null)
|
|
@@ -992,4 +1126,4 @@ class LiveUpdatePlugin(
|
|
|
992
1126
|
null
|
|
993
1127
|
}
|
|
994
1128
|
}
|
|
995
|
-
}
|
|
1129
|
+
}
|