mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(stores): log why a GOG cloud sync failed and stage uploads in the cache folder (#2596)
* fix(stores): log why a GOG cloud sync failed and stage uploads in the cache folder * fix(stores): strip URL credentials and bound the error body in GOG cloud failure details * fix(stores): send Galaxy's communication-service User-Agent to GOG cloud storage GOG cloud storage now answers every list with HTTP 400 invalid_version for the bare "GOG Galaxy" value, so uploads and downloads failed for every game. List, HEAD, GET and PUT now identify as GOGGalaxyCommunicationService/2.0.13.27 like gogdl and Lutris, and PUT carries the same value as X-Object-Meta-User-Agent. * fix(stores): bound cloud list errors and verify test requests * fix(stores): preserve cloud error classification
This commit is contained in:
1 parent
7254a88391
commit
a7032530da
5 files changed
+320
-56
No files matched your search
@@ -25,9 +25,11 @@ object CloudSaves {
|
||||
private const val PARALLEL = 6
|
||||
private const val CLOUD_LAYOUT = 2
|
||||
|
||||
class Result(val result: String, val files: Int, val bytes: Long, val reason: String, val conflicts: Int = 0) {
|
||||
class Result(val result: String, val files: Int, val bytes: Long, val reason: String, val conflicts: Int = 0, val detail: String = "") {
|
||||
val ok: Boolean get() = result != "failed"
|
||||
val line: String get() = "result=$result files=$files bytes=$bytes reason=$reason" + if (conflicts > 0) " conflicts=$conflicts" else ""
|
||||
val line: String get() = "result=$result files=$files bytes=$bytes reason=$reason" +
|
||||
(if (conflicts > 0) " conflicts=$conflicts" else "") +
|
||||
if (detail.isNotEmpty()) " detail=\"$detail\"" else ""
|
||||
}
|
||||
|
||||
class Status(val supported: Boolean, val folder: File?, val lastSync: Long, val reason: String, val conflicts: List<String> = emptyList())
|
||||
@@ -126,7 +128,7 @@ object CloudSaves {
|
||||
)
|
||||
if (picked == null) GogConfig("", "") else GogConfig(picked.name, picked.template)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "gog remote-config: ${e.javaClass.simpleName}")
|
||||
Log.w(TAG, "gog remote-config: ${cloudFailureDetail(e)}")
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -166,7 +168,7 @@ object CloudSaves {
|
||||
val galaxy = GogAuth.validToken(context)
|
||||
val clientId = gogClientId(context, id, folder)
|
||||
if (userId.isEmpty() || galaxy == null || clientId == null) null
|
||||
else GogCloud(userId, clientId, gogScopedToken(context, id, clientId) ?: galaxy, gogLocationName(context, id, folder))
|
||||
else GogCloud(userId, clientId, gogScopedToken(context, id, clientId) ?: galaxy, gogLocationName(context, id, folder), tempDir = context.cacheDir)
|
||||
}
|
||||
Store.EPIC -> {
|
||||
val token = EpicCredentialStore.getValidAccessToken(context)
|
||||
@@ -312,7 +314,7 @@ object CloudSaves {
|
||||
rememberNoCloud(prefs(context, store), id)
|
||||
Result("skipped", 0, 0, "no-cloud-saves")
|
||||
} catch (e: Exception) {
|
||||
Result("failed", 0, 0, e.javaClass.simpleName)
|
||||
Result("failed", 0, 0, e.javaClass.simpleName, detail = cloudFailureDetail(e))
|
||||
}
|
||||
return done(store, id, way, result)
|
||||
}
|
||||
|
||||
@@ -59,6 +59,69 @@ private fun httpDate(s: String?): Long = try {
|
||||
SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).apply { timeZone = TimeZone.getTimeZone("GMT") }.parse(s!!.trim())?.time ?: -1L
|
||||
} catch (e: Exception) { -1L }
|
||||
|
||||
private val SECRET_PARAM = Regex("(?i)(token|secret|code|password|authorization)([\"']?\\s*[:=]\\s*[\"']?)[^\\s&\"',;}]+")
|
||||
private val BEARER = Regex("(?i)bearer\\s+\\S+")
|
||||
private val URL_TEXT = Regex("(?i)https?://(?:[^/\\s?#@]*@)?([^/\\s?#@]+)[^\\s]*")
|
||||
private val LONG_TOKEN = Regex("[A-Za-z0-9._~+/=-]{32,}")
|
||||
private val SPACES = Regex("\\s+")
|
||||
private const val DETAIL_MAX = 400
|
||||
|
||||
internal fun scrubCloudText(text: String, longTokens: Boolean = true): String = text
|
||||
.replace(BEARER, "Bearer <redacted>")
|
||||
.replace(URL_TEXT) { it.groupValues[1] }
|
||||
.replace(SECRET_PARAM) { it.groupValues[1] + it.groupValues[2] + "<redacted>" }
|
||||
.let { if (longTokens) it.replace(LONG_TOKEN, "<redacted>") else it }
|
||||
.replace(SPACES, " ")
|
||||
.trim()
|
||||
|
||||
internal fun cloudFailureDetail(e: Throwable): String {
|
||||
val parts = ArrayList<String>()
|
||||
var t: Throwable? = e
|
||||
while (t != null && parts.size < 3) {
|
||||
val message = t.message?.let { scrubCloudText(it, longTokens = false) }.orEmpty()
|
||||
parts += if (message.isEmpty()) t.javaClass.simpleName else "${t.javaClass.simpleName}: $message"
|
||||
t = t.cause?.takeIf { it !== t }
|
||||
}
|
||||
return parts.joinToString(" <- ").take(DETAIL_MAX)
|
||||
}
|
||||
|
||||
private const val SNIPPET_READ_MAX = 4096
|
||||
private const val SNIPPET_MAX = 120
|
||||
|
||||
private data class CloudErrorPrefix(val text: String, val truncated: Boolean) {
|
||||
fun snippet(): String {
|
||||
val safe = if (truncated) text.substring(0, text.indexOfLast { it.isWhitespace() }.coerceAtLeast(0)) else text
|
||||
return scrubCloudText(safe).take(SNIPPET_MAX)
|
||||
}
|
||||
}
|
||||
|
||||
private fun cloudErrorPrefix(input: InputStream?): CloudErrorPrefix {
|
||||
if (input == null) return CloudErrorPrefix("", false)
|
||||
return input.use { stream ->
|
||||
val buffer = ByteArray(SNIPPET_READ_MAX + 1)
|
||||
var read = 0
|
||||
while (read < buffer.size) {
|
||||
val n = stream.read(buffer, read, buffer.size - read)
|
||||
if (n < 0) break
|
||||
read += n
|
||||
}
|
||||
CloudErrorPrefix(buffer.decodeToString(0, minOf(read, SNIPPET_READ_MAX)), read > SNIPPET_READ_MAX)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun cloudErrorSnippet(input: InputStream?): String =
|
||||
cloudErrorPrefix(input).snippet()
|
||||
|
||||
private fun HttpURLConnection.errorSnippet(): String =
|
||||
runCatching { cloudErrorSnippet(errorStream) }.getOrDefault("")
|
||||
|
||||
private fun httpFailure(step: String, name: String?, code: Int, error: String): java.io.IOException =
|
||||
java.io.IOException(buildString {
|
||||
append("GOG cloud ").append(step).append(" HTTP ").append(code)
|
||||
if (name != null) append(" file=").append(name)
|
||||
if (error.isNotEmpty()) append(" body=").append(error)
|
||||
})
|
||||
|
||||
internal fun isGzip(data: ByteArray) =
|
||||
data.size >= 10 && data[0] == 0x1f.toByte() && data[1] == 0x8b.toByte() && data[2] == 0x08.toByte()
|
||||
|
||||
@@ -183,6 +246,8 @@ private fun gzipFile(src: File, dst: File): GzipFile {
|
||||
}
|
||||
}
|
||||
|
||||
internal const val GOG_CLOUD_UA = "GOGGalaxyCommunicationService/2.0.13.27 (Windows_32bit) dont_sync_marker/true installation_source/gog"
|
||||
|
||||
/** GOG Galaxy parses `X-Object-Meta-LocalLastModified`; objects without it fail as "invalid data". */
|
||||
private val GOG_STAMP = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx", Locale.US)
|
||||
|
||||
@@ -195,9 +260,10 @@ class GogCloud(
|
||||
private val token: String,
|
||||
private val location: String? = null,
|
||||
baseUrl: String? = null,
|
||||
private val tempDir: File? = null,
|
||||
) : CloudTransport {
|
||||
private val base = baseUrl ?: "https://cloudstorage.gog.com/v1/$userId/$clientId"
|
||||
private val ua = "GOG Galaxy"
|
||||
private val ua = GOG_CLOUD_UA
|
||||
private var prefix: String? = location?.let { "$it/" }
|
||||
|
||||
private fun key(name: String) = URLEncoder.encode(name, "UTF-8").replace("+", "%20")
|
||||
@@ -209,10 +275,10 @@ class GogCloud(
|
||||
val code = c.responseCode
|
||||
if (code == 404) { c.disconnect(); return emptyList() }
|
||||
if (code !in 200..299) {
|
||||
val err = runCatching { c.errorStream?.readBytes()?.decodeToString().orEmpty() }.getOrDefault("")
|
||||
val err = runCatching { cloudErrorPrefix(c.errorStream) }.getOrDefault(CloudErrorPrefix("", false))
|
||||
c.disconnect()
|
||||
if ("not_enabled_for_client" in err || "disabled" in err) throw NoCloudSaves()
|
||||
throw java.io.IOException("GOG cloud list HTTP $code")
|
||||
if ("not_enabled_for_client" in err.text || "disabled" in err.text) throw NoCloudSaves()
|
||||
throw httpFailure("list", null, code, err.snippet())
|
||||
}
|
||||
val text = c.body().decodeToString().trim()
|
||||
c.disconnect()
|
||||
@@ -255,12 +321,20 @@ class GogCloud(
|
||||
override fun get(file: CloudFile): Pair<ByteArray, Long> {
|
||||
val c = open("$base/${cloudKey(file.name)}", "GET", token, ua)
|
||||
c.setRequestProperty("Accept-Encoding", "identity")
|
||||
if (c.responseCode !in 200..299) { val code = c.responseCode; c.disconnect(); throw java.io.IOException("GOG cloud get HTTP $code") }
|
||||
if (c.responseCode !in 200..299) {
|
||||
val code = c.responseCode
|
||||
val err = c.errorSnippet()
|
||||
c.disconnect()
|
||||
throw httpFailure("get", file.name, code, err)
|
||||
}
|
||||
val modified = httpDate(c.getHeaderField("Last-Modified"))
|
||||
val encoding = c.getHeaderField("Content-Encoding")
|
||||
val data = c.body()
|
||||
c.disconnect()
|
||||
return gogDecodeCloudBody(data, encoding) to modified
|
||||
val data = try { c.body() } finally { c.disconnect() }
|
||||
return try {
|
||||
gogDecodeCloudBody(data, encoding) to modified
|
||||
} catch (e: java.io.IOException) {
|
||||
throw java.io.IOException("GOG cloud get file=${file.name} bytes=${data.size} encoding=$encoding", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(files: Map<String, ByteArray>, modifiedMs: Map<String, Long>): Map<String, Long> = files.mapValues { (name, data) ->
|
||||
@@ -269,7 +343,11 @@ class GogCloud(
|
||||
}
|
||||
|
||||
override fun putFiles(files: Map<String, File>, modifiedMs: Map<String, Long>): Map<String, CloudPut> = files.mapValues { (name, file) ->
|
||||
val compressed = File.createTempFile("gog-cloud-", ".gz")
|
||||
val compressed = try {
|
||||
File.createTempFile("gog-cloud-", ".gz", tempDir?.apply { mkdirs() })
|
||||
} catch (e: java.io.IOException) {
|
||||
throw java.io.IOException("GOG cloud put file=$name temp file", e)
|
||||
}
|
||||
try {
|
||||
val gz = gzipFile(file, compressed)
|
||||
val modified = putBody(name, compressed.inputStream(), gz.length, gz.etag, modifiedMs[name] ?: file.lastModified())
|
||||
@@ -287,10 +365,16 @@ class GogCloud(
|
||||
c.setRequestProperty("Content-Encoding", "gzip")
|
||||
c.setRequestProperty("Etag", etag)
|
||||
c.setRequestProperty("X-Object-Meta-LocalLastModified", gogStamp(modified))
|
||||
c.setRequestProperty("X-Object-Meta-User-Agent", ua)
|
||||
c.setFixedLengthStreamingMode(length)
|
||||
c.outputStream.use { out -> source.use { it.copyTo(out, 64 * 1024) } }
|
||||
try {
|
||||
c.outputStream.use { out -> source.use { it.copyTo(out, 64 * 1024) } }
|
||||
} catch (e: java.io.IOException) {
|
||||
val code = runCatching { c.responseCode }.getOrNull()
|
||||
throw java.io.IOException("GOG cloud put file=$name bytes=$length" + (code?.let { " HTTP $it" } ?: ""), e)
|
||||
}
|
||||
val code = c.responseCode
|
||||
if (code !in 200..299) throw java.io.IOException("GOG cloud put HTTP $code")
|
||||
if (code !in 200..299) throw httpFailure("put", name, code, c.errorSnippet())
|
||||
return CloudFile(name, -1L).also { details(it) }.modifiedMs
|
||||
} finally {
|
||||
c.disconnect()
|
||||
|
||||
Reference in new issue
Block a user