From f3fee49f8c4f16857e861dbcadb9749abc527f02 Mon Sep 17 00:00:00 2001 From: Js0n Date: Sat, 22 Feb 2025 15:18:00 +0800 Subject: [PATCH] feat: properly set OkHttp header (#2463) properly send `User-Agent` and `Accept-Language` to `updateJson` server, module developers can check `User-Agent` whether to send update to user and set user preferred language for changelog. also added cache to reduce update server load --- .../java/com/rifsxd/ksunext/KernelSUApplication.kt | 13 +++++++++++++ .../java/com/rifsxd/ksunext/ui/screen/Module.kt | 4 ++-- .../java/com/rifsxd/ksunext/ui/util/Downloader.kt | 3 ++- .../rifsxd/ksunext/ui/viewmodel/ModuleViewModel.kt | 8 +++----- .../ksunext/ui/viewmodel/TemplateViewModel.kt | 13 +++---------- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/manager/app/src/main/java/com/rifsxd/ksunext/KernelSUApplication.kt b/manager/app/src/main/java/com/rifsxd/ksunext/KernelSUApplication.kt index 809d9927..57e5a81e 100644 --- a/manager/app/src/main/java/com/rifsxd/ksunext/KernelSUApplication.kt +++ b/manager/app/src/main/java/com/rifsxd/ksunext/KernelSUApplication.kt @@ -6,12 +6,25 @@ import coil.Coil import coil.ImageLoader import me.zhanghai.android.appiconloader.coil.AppIconFetcher import me.zhanghai.android.appiconloader.coil.AppIconKeyer +import okhttp3.Cache +import okhttp3.OkHttpClient import java.io.File +import java.util.Locale lateinit var ksuApp: KernelSUApplication class KernelSUApplication : Application() { + val okhttpClient = + OkHttpClient.Builder().cache(Cache(File(cacheDir, "okhttp"), 10 * 1024 * 1024)) + .addInterceptor { block -> + block.proceed( + block.request().newBuilder() + .header("User-Agent", "KernelSU/${BuildConfig.VERSION_CODE}") + .header("Accept-Language", Locale.getDefault().toLanguageTag()).build() + ) + }.build() + override fun onCreate() { super.onCreate() ksuApp = this diff --git a/manager/app/src/main/java/com/rifsxd/ksunext/ui/screen/Module.kt b/manager/app/src/main/java/com/rifsxd/ksunext/ui/screen/Module.kt index 3a2d4021..51ed78c4 100644 --- a/manager/app/src/main/java/com/rifsxd/ksunext/ui/screen/Module.kt +++ b/manager/app/src/main/java/com/rifsxd/ksunext/ui/screen/Module.kt @@ -92,6 +92,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import com.rifsxd.ksunext.Natives import com.rifsxd.ksunext.R +import com.rifsxd.ksunext.ksuApp import com.rifsxd.ksunext.ui.component.ConfirmResult import com.rifsxd.ksunext.ui.component.rememberConfirmDialog import com.rifsxd.ksunext.ui.component.rememberLoadingDialog @@ -107,7 +108,6 @@ import com.rifsxd.ksunext.ui.util.uninstallModule import com.rifsxd.ksunext.ui.util.restoreModule import com.rifsxd.ksunext.ui.viewmodel.ModuleViewModel import com.rifsxd.ksunext.ui.webui.WebUIActivity -import okhttp3.OkHttpClient @OptIn(ExperimentalMaterial3Api::class) @Destination @@ -352,7 +352,7 @@ private fun ModuleList( val changelogResult = loadingDialog.withLoading { withContext(Dispatchers.IO) { runCatching { - OkHttpClient().newCall( + ksuApp.okhttpClient.newCall( okhttp3.Request.Builder().url(changelogUrl).build() ).execute().body!!.string() } diff --git a/manager/app/src/main/java/com/rifsxd/ksunext/ui/util/Downloader.kt b/manager/app/src/main/java/com/rifsxd/ksunext/ui/util/Downloader.kt index 85d9dfcc..70c6af34 100644 --- a/manager/app/src/main/java/com/rifsxd/ksunext/ui/util/Downloader.kt +++ b/manager/app/src/main/java/com/rifsxd/ksunext/ui/util/Downloader.kt @@ -11,6 +11,7 @@ import android.os.Environment import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.core.content.ContextCompat +import com.rifsxd.ksunext.ksuApp import com.rifsxd.ksunext.ui.util.module.LatestVersionInfo /** @@ -67,7 +68,7 @@ fun checkNewVersion(): LatestVersionInfo { // default null value if failed val defaultValue = LatestVersionInfo() runCatching { - okhttp3.OkHttpClient().newCall(okhttp3.Request.Builder().url(url).build()).execute() + ksuApp.okhttpClient.newCall(okhttp3.Request.Builder().url(url).build()).execute() .use { response -> if (!response.isSuccessful) { return defaultValue diff --git a/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/ModuleViewModel.kt b/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/ModuleViewModel.kt index 087cba76..ab363d67 100644 --- a/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/ModuleViewModel.kt +++ b/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/ModuleViewModel.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import java.text.Collator import java.util.Locale +import com.rifsxd.ksunext.ksuApp import com.rifsxd.ksunext.ui.util.HanziToPinyin import com.rifsxd.ksunext.ui.util.listModules import com.rifsxd.ksunext.ui.util.overlayFsAvailable @@ -144,11 +145,8 @@ class ModuleViewModel : ViewModel() { val result = kotlin.runCatching { val url = m.updateJson Log.i(TAG, "checkUpdate url: $url") - val response = okhttp3.OkHttpClient() - .newCall( - okhttp3.Request.Builder() - .url(url) - .build() + val response = ksuApp.okhttpClient.newCall( + okhttp3.Request.Builder().url(url).build() ).execute() Log.d(TAG, "checkUpdate code: ${response.code}") if (response.isSuccessful) { diff --git a/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/TemplateViewModel.kt b/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/TemplateViewModel.kt index 9c5d3041..b7a134b3 100644 --- a/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/TemplateViewModel.kt +++ b/manager/app/src/main/java/com/rifsxd/ksunext/ui/viewmodel/TemplateViewModel.kt @@ -11,18 +11,17 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.parcelize.Parcelize import com.rifsxd.ksunext.Natives +import com.rifsxd.ksunext.ksuApp import com.rifsxd.ksunext.profile.Capabilities import com.rifsxd.ksunext.profile.Groups import com.rifsxd.ksunext.ui.util.getAppProfileTemplate import com.rifsxd.ksunext.ui.util.listAppProfileTemplates import com.rifsxd.ksunext.ui.util.setAppProfileTemplate -import okhttp3.OkHttpClient import okhttp3.Request import org.json.JSONArray import org.json.JSONObject import java.text.Collator import java.util.Locale -import java.util.concurrent.TimeUnit /** @@ -138,13 +137,7 @@ class TemplateViewModel : ViewModel() { private fun fetchRemoteTemplates() { runCatching { - val client: OkHttpClient = OkHttpClient.Builder() - .connectTimeout(5, TimeUnit.SECONDS) - .writeTimeout(5, TimeUnit.SECONDS) - .readTimeout(10, TimeUnit.SECONDS) - .build() - - client.newCall( + ksuApp.okhttpClient.newCall( Request.Builder().url(TEMPLATE_INDEX_URL).build() ).execute().use { response -> if (!response.isSuccessful) { @@ -155,7 +148,7 @@ private fun fetchRemoteTemplates() { 0.until(remoteTemplateIds.length()).forEach { i -> val id = remoteTemplateIds.getString(i) Log.i(TAG, "fetch template: $id") - val templateJson = client.newCall( + val templateJson = ksuApp.okhttpClient.newCall( Request.Builder().url(TEMPLATE_URL.format(id)).build() ).runCatching { execute().use { response ->