From f41eaa11ca0dcca9d77f0b3dd092f2d0a59fecba Mon Sep 17 00:00:00 2001 From: tiagohm Date: Thu, 17 Oct 2024 15:55:30 -0300 Subject: [PATCH] [api]: Add libwcs for arm64; Validate checksum. Fix #513 --- .../nebulosa/api/atlas/LibWCSDownloadTask.kt | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/api/src/main/kotlin/nebulosa/api/atlas/LibWCSDownloadTask.kt b/api/src/main/kotlin/nebulosa/api/atlas/LibWCSDownloadTask.kt index 1086a6f24..2704a52dd 100644 --- a/api/src/main/kotlin/nebulosa/api/atlas/LibWCSDownloadTask.kt +++ b/api/src/main/kotlin/nebulosa/api/atlas/LibWCSDownloadTask.kt @@ -3,15 +3,18 @@ package nebulosa.api.atlas import com.sun.jna.Platform import nebulosa.api.preference.PreferenceService import nebulosa.io.transferAndCloseOutput +import nebulosa.log.e import nebulosa.log.i import nebulosa.log.loggerFor import nebulosa.wcs.LibWCS import okhttp3.OkHttpClient import okhttp3.Request +import org.apache.commons.codec.digest.DigestUtils import java.nio.file.Path import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.TimeUnit import kotlin.io.path.exists +import kotlin.io.path.inputStream import kotlin.io.path.outputStream class LibWCSDownloadTask( @@ -28,7 +31,13 @@ class LibWCSDownloadTask( override fun run() { var request = Request.Builder().get().url(VERSION_URL).build() - val libraryUrl = checkNotNull(LIBRARY_URLS[Platform.RESOURCE_PREFIX]) + val libraryUrl = LIBRARY_URLS[Platform.RESOURCE_PREFIX] + + if (libraryUrl.isNullOrBlank()) { + LOG.e("unable to download for arch {}", Platform.RESOURCE_PREFIX) + return + } + val libraryPath = Path.of("$libsPath", libraryUrl.substring(libraryUrl.lastIndexOf('/') + 1)) httpClient.newCall(request).execute().use { response -> @@ -36,7 +45,7 @@ class LibWCSDownloadTask( val newestVersion = response.body!!.string() if (newestVersion != preferenceService.getText(VERSION_KEY) || !libraryPath.exists()) { - LOG.i("LibWCS is out of date. Downloading...") + LOG.i("libwcs is out of date. Downloading...") request = Request.Builder().get().url(libraryUrl).build() @@ -47,12 +56,19 @@ class LibWCSDownloadTask( } } } else { - LOG.i("LibWCS is up to date. version={}", newestVersion) + LOG.i("libwcs is up to date. version={}", newestVersion) } } } - System.setProperty(LibWCS.PATH, "$libraryPath") + val checksum = libraryPath.inputStream().use { DigestUtils.sha256Hex(it) } + + if (checksum == LIBRARY_CHECKSUM[Platform.RESOURCE_PREFIX]) { + LOG.i("libwcs checksum is valid!") + System.setProperty(LibWCS.PATH, "$libraryPath") + } else { + LOG.e("failed to validate checksum. expected {}, got {}", LIBRARY_CHECKSUM[Platform.RESOURCE_PREFIX], checksum) + } } companion object { @@ -60,11 +76,22 @@ class LibWCSDownloadTask( const val VERSION_URL = "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/libs/wcs/VERSION.txt" const val VERSION_KEY = "LIBWCS.VERSION" + const val LINUX_X86_64 = "linux-x86-64" + const val LINUX_AARCH_64 = "linux-aarch64" + const val WIN_32_X86_64 = "win32-x86-64" + @JvmStatic private val LOG = loggerFor() @JvmStatic private val LIBRARY_URLS = mapOf( - "linux-x86-64" to "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/libs/wcs/linux-x86-64/libwcs.so", - "win32-x86-64" to "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/libs/wcs/win32-x86-64/libwcs.dll", + LINUX_X86_64 to "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/libs/wcs/$LINUX_X86_64/libwcs.so", + LINUX_AARCH_64 to "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/libs/wcs/$LINUX_AARCH_64/libwcs.so", + WIN_32_X86_64 to "https://raw.githubusercontent.com/tiagohm/nebulosa.data/main/libs/wcs/$WIN_32_X86_64/libwcs.dll", + ) + + @JvmStatic private val LIBRARY_CHECKSUM = mapOf( + LINUX_X86_64 to "ca74289426e9536eb8a38b6fe866d3bb8478400424f6652f7d9db007fee342f4", + LINUX_AARCH_64 to "8a5d14a22dcb9656b32519167a98ad2489cfd9262a4336ac3717a2eb3bf7354e", + WIN_32_X86_64 to "65ee5696485a1b2bdc5248a581bb43c947615f95051dd7efca669da475b775ab", ) } }