Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.movtery.zalithlauncher.game.account.microsoft.getTokenResponse
import com.movtery.zalithlauncher.game.account.microsoft.microsoftAuthAsync
import com.movtery.zalithlauncher.game.account.microsoft.toLocal
import com.movtery.zalithlauncher.ui.screens.content.elements.MicrosoftLoginOperation
import com.movtery.zalithlauncher.path.URL_USER_AGENT
import com.movtery.zalithlauncher.utils.copyText
import com.movtery.zalithlauncher.utils.logging.Logger
import com.movtery.zalithlauncher.utils.network.toLocal
Expand Down Expand Up @@ -385,6 +386,7 @@ fun tryGetFullServerUrl(baseUrl: String): String {
(URL(url).openConnection() as HttpURLConnection).apply {
connectTimeout = 5000
readTimeout = 5000
setRequestProperty("User-Agent", URL_USER_AGENT)
}

var conn: HttpURLConnection? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package com.movtery.zalithlauncher.game.account.wardrobe

import com.google.gson.JsonObject
import com.movtery.zalithlauncher.path.createOkHttpClient
import com.movtery.zalithlauncher.path.createRequestBuilder
import com.movtery.zalithlauncher.utils.GSON
import com.movtery.zalithlauncher.utils.logging.Logger
import com.movtery.zalithlauncher.utils.network.fetchStringFromUrl
import com.movtery.zalithlauncher.utils.string.decodeBase64
import okhttp3.Request
import java.io.File
import java.io.FileOutputStream

Expand Down Expand Up @@ -52,9 +52,7 @@ abstract class WardrobeDownloader {
if (!exists()) mkdirs()
}

val request = Request.Builder()
.url(url)
.build()
val request = createRequestBuilder(url).build()

mClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ private val CURSEFORGE_INTERCEPTOR = Interceptor { chain ->
chain.proceed(request)
}

/**
* An [Interceptor] that ensures the [URL_USER_AGENT] header is present on every request.
*
* If a request already carries a User-Agent header (set by [createRequestBuilder] or
* similar), this interceptor is a no-op — avoiding duplicate headers.
*/
private val USER_AGENT_INTERCEPTOR = Interceptor { chain ->
val request = chain.request()
if (request.header("User-Agent") != null) {
chain.proceed(request)
} else {
val newRequest = request.newBuilder()
.header("User-Agent", URL_USER_AGENT)
.build()
chain.proceed(newRequest)
}
}

fun createRequestBuilder(url: String): Request.Builder {
return createRequestBuilder(url, null)
}
Expand All @@ -133,6 +151,7 @@ fun createOkHttpClientBuilder(action: (OkHttpClient.Builder) -> Unit = { }): OkH
return OkHttpClient.Builder()
.callTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
.addInterceptor(CURSEFORGE_INTERCEPTOR)
.addInterceptor(USER_AGENT_INTERCEPTOR)
.apply(action)
}

Expand All @@ -152,6 +171,7 @@ val DOWNLOAD_OKHTTP_CLIENT: OkHttpClient by lazy {
.writeTimeout(30, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.addInterceptor(CURSEFORGE_INTERCEPTOR)
.addInterceptor(USER_AGENT_INTERCEPTOR)
.build()
// 注意:不设置 callTimeout,因为文件大小差异极大
// 协程层的 withTimeout 提供整体兜底保护
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.movtery.zalithlauncher.R
import com.movtery.zalithlauncher.context.COPY_LABEL_LINK
import com.movtery.zalithlauncher.path.DOWNLOAD_OKHTTP_CLIENT
import com.movtery.zalithlauncher.path.URL_USER_AGENT
import com.movtery.zalithlauncher.path.createOkHttpClient
import com.movtery.zalithlauncher.path.createRequestBuilder
import com.movtery.zalithlauncher.ui.theme.showThemed
Expand All @@ -50,7 +49,6 @@ import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import okhttp3.Call
import okhttp3.Request
import org.apache.commons.io.FileUtils
import java.io.BufferedOutputStream
import java.io.File
Expand Down Expand Up @@ -120,10 +118,7 @@ fun downloadFileWithHttp(
try {
outputFile.ensureParentDirectory()

val request = Request.Builder()
.url(url)
.header("User-Agent", URL_USER_AGENT)
.build()
val request = createRequestBuilder(url).build()

DOWNLOAD_OKHTTP_CLIENT
.newCall(request)
Expand Down