feat: Diagnosedaten senden (Opt-out, Zahnrad-Knopf, einmaliger Hinweis)
Eigenes kleines Log (Abstuerze per UncaughtExceptionHandler, Jam- und PC-Verbindungsfehler), gefiltert an POST /api/diag des Playtube-Dienstes, kurz nach dem Start hoechstens alle 6 Stunden. Einstellungsdialog mit Schalter, "Jetzt senden" und Installations-Kennung. Unit-Tests DiagReportTest. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package de.fojadrachi.playtube
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
/**
|
||||
* Reine Logik fuer Diagnoseberichte (ohne Android, mit Unit-Tests): Logzeilen filtern und
|
||||
* den Bericht fuer `POST /api/diag` (playtube-jam) bauen. Versand und Log-Datei: [Diagnostics].
|
||||
*
|
||||
* Nicht gesendet werden Cookies, Tokens, Anmeldedaten, Suchanfragen, Songtitel, Video-IDs,
|
||||
* E-Mail-Adressen und URL-Parameter.
|
||||
*/
|
||||
object DiagReport {
|
||||
const val MAX_LINES = 300
|
||||
const val MAX_LINE_CHARS = 480
|
||||
|
||||
private val COOKIE = Regex("(?i)\\b(cookie|set-cookie|authorization)\\b.*")
|
||||
private val BEARER = Regex("(?i)\\bbearer\\s+\\S+")
|
||||
private val SECRET = Regex("(?i)\\b(\\w*token|\\w*key|secret|password|passwort|sapisid)(\\s*[\"']?\\s*[:=]\\s*[\"']?)[^\\s\"'&,;]+")
|
||||
private val EMAIL = Regex("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}")
|
||||
private val URL_QUERY = Regex("(?i)(https?://[^\\s?#\"']+)[?#][^\\s\"']*")
|
||||
private val WORD = Regex("[A-Za-z0-9_-]+")
|
||||
private val CONTROL = Regex("[\\u0000-\\u0008\\u000b-\\u001f\\u007f\\u202a-\\u202e\\u2066-\\u2069]")
|
||||
|
||||
/** YouTube-Video-ID: 11 Zeichen, Gross- und Kleinbuchstaben gemischt plus Ziffer, `_` oder `-`. */
|
||||
fun looksLikeVideoId(word: String): Boolean =
|
||||
word.length == 11 &&
|
||||
word.all { it.isLetterOrDigit() && it.code < 128 || it == '_' || it == '-' } &&
|
||||
word.any { it in 'A'..'Z' } && word.any { it in 'a'..'z' } &&
|
||||
word.any { it.isDigit() || it == '_' || it == '-' }
|
||||
|
||||
private fun looksLikeSecret(word: String): Boolean =
|
||||
word.length >= 20 && word.any { it.isDigit() } && word.any { it.isLetter() }
|
||||
|
||||
fun sanitize(line: String): String {
|
||||
var text = CONTROL.replace(line, " ")
|
||||
text = COOKIE.replace(text) { "${it.groupValues[1].lowercase()} [entfernt]" }
|
||||
text = BEARER.replace(text, "Bearer [entfernt]")
|
||||
text = SECRET.replace(text) { "${it.groupValues[1]}${it.groupValues[2]}[entfernt]" }
|
||||
text = EMAIL.replace(text, "[e-mail]")
|
||||
text = URL_QUERY.replace(text) { "${it.groupValues[1]}?[…]" }
|
||||
text = WORD.replace(text) {
|
||||
when {
|
||||
looksLikeVideoId(it.value) -> "[video]"
|
||||
looksLikeSecret(it.value) -> "[id]"
|
||||
else -> it.value
|
||||
}
|
||||
}
|
||||
return text.trim().take(MAX_LINE_CHARS)
|
||||
}
|
||||
|
||||
data class Collected(val lines: List<String>, val latest: Long)
|
||||
|
||||
/** Zeilen `[secs] text` nach `since` (Unix-Sekunden), gefiltert, hoechstens [MAX_LINES] neueste. */
|
||||
fun collect(logText: String, since: Long): Collected {
|
||||
var latest = since
|
||||
val lines = ArrayList<String>()
|
||||
for (raw in logText.lineSequence()) {
|
||||
if (!raw.startsWith("[")) continue
|
||||
val end = raw.indexOf("] ")
|
||||
if (end < 0) continue
|
||||
val secs = raw.substring(1, end).toLongOrNull() ?: continue
|
||||
if (secs <= since) continue
|
||||
latest = maxOf(latest, secs)
|
||||
lines += "[$secs] ${sanitize(raw.substring(end + 2))}"
|
||||
}
|
||||
return Collected(lines.takeLast(MAX_LINES), latest)
|
||||
}
|
||||
|
||||
fun isError(line: String): Boolean {
|
||||
val lower = line.lowercase()
|
||||
return listOf("absturz", "exception", "error", "fehler", "fehlgeschlagen", "failed").any { it in lower }
|
||||
}
|
||||
|
||||
fun build(installId: String, version: String, androidVersion: String, device: String, lines: List<String>, note: String = ""): JSONObject =
|
||||
JSONObject()
|
||||
.put("app", "android")
|
||||
.put("version", version)
|
||||
.put("platform", "android-$androidVersion")
|
||||
.put("os", device)
|
||||
.put("installId", installId)
|
||||
.put("lines", JSONArray(lines))
|
||||
.put("errors", JSONArray(lines.filter(::isError)))
|
||||
.put("note", note)
|
||||
.put("includesTitles", false)
|
||||
}
|
||||
Reference in New Issue
Block a user