Skip to content

Commit f1e4795

Browse files
fix(crash): Add rate limit to crash reporting
To prevent spamming the crash report endpoint, a 4-hour cooldown period has been implemented. This is achieved by storing the timestamp of the last successful report in SharedPreferences. Subsequent crash reports will only be sent if the current time is at least 4 hours after the last one was sent.
1 parent 079b04b commit f1e4795

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

app/src/main/java/com/github/droidworksstudio/mlauncher/CrashReportActivity.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.net.Uri
88
import android.os.Bundle
99
import android.util.Base64
1010
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.core.content.edit
1112
import androidx.core.net.toUri
1213
import androidx.lifecycle.lifecycleScope
1314
import com.github.droidworksstudio.common.getLocalizedString
@@ -65,8 +66,25 @@ class CrashReportActivity : AppCompatActivity() {
6566
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
6667
}
6768

69+
private fun getLastSent(): Long {
70+
val prefs = getSharedPreferences("crash_report", MODE_PRIVATE)
71+
return prefs.getLong("last_sent", 0L)
72+
}
73+
74+
private fun setLastSent(time: Long) {
75+
val prefs = getSharedPreferences("crash_report", MODE_PRIVATE)
76+
prefs.edit { putLong("last_sent", time) }
77+
}
78+
79+
6880
private fun sendCrashReportNative() {
81+
val cooldownMs = 14_400_000L // 4 hours
82+
val now = System.currentTimeMillis()
83+
val lastSent = getLastSent()
84+
val canSend = now - lastSent >= cooldownMs
85+
6986
lifecycleScope.launch(Dispatchers.IO) {
87+
if (!canSend) return@launch
7088
try {
7189
val crashFileUri: Uri? = intent.getStringExtra("crash_log_uri")?.toUri()
7290
val crashFileUris: List<Uri> = crashFileUri?.let { listOf(it) } ?: emptyList()
@@ -118,6 +136,7 @@ class CrashReportActivity : AppCompatActivity() {
118136
val responseMessage = responseStream.bufferedReader().use { it.readText() }
119137

120138
if (responseCode in 200..299) {
139+
setLastSent(now)
121140
println("Crash report sent successfully: $responseMessage")
122141
} else {
123142
println("Failed to send crash report: $responseCode $responseMessage")

0 commit comments

Comments
 (0)