Skip to content

Commit

Permalink
Merge pull request #40 from greatfire/store-failures
Browse files Browse the repository at this point in the history
store failures in preferences
  • Loading branch information
mnbogner authored Feb 11, 2023
2 parents 0c564c2 + 5ac927e commit eacefad
Showing 1 changed file with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private const val EXTRA_PARAM_DNSTT = "org.greatfire.envoy.extra.PARAM_DNSTT"
const val ENVOY_BROADCAST_VALIDATION_SUCCEEDED = "org.greatfire.envoy.VALIDATION_SUCCEEDED"
const val ENVOY_BROADCAST_VALIDATION_CONTINUED = "org.greatfire.envoy.VALIDATION_CONTINUED"
const val ENVOY_BROADCAST_VALIDATION_FAILED = "org.greatfire.envoy.VALIDATION_FAILED"
const val ENVOY_BROADCAST_VALIDATION_BLOCKED = "org.greatfire.envoy.VALIDATION_BLOCKED"
const val ENVOY_BROADCAST_BATCH_SUCCEEDED = "org.greatfire.envoy.BATCH_SUCCEEDED"
const val ENVOY_BROADCAST_BATCH_FAILED = "org.greatfire.envoy.BATCH_FAILED"

Expand Down Expand Up @@ -187,15 +188,31 @@ class NetworkIntentService : IntentService("NetworkIntentService") {
}
}

if (urls.isNullOrEmpty()) {
val urlsToSubmit = mutableListOf<String>()
if (!urls.isNullOrEmpty()) {
urls.forEach { url ->
if (shouldSubmitUrl(url)) {
urlsToSubmit.add(url)
}
}
}

if (urlsToSubmit.isNullOrEmpty()) {
if (dnsttUrls) {
Log.w(TAG, "no additional dnstt urls submitted, cannot continue")
} else {
Log.w(TAG, "no urls submitted, fetch additional urls from dnstt")
// send broadcast if all included urls have previously been blocked
if (!urls.isNullOrEmpty()) {
Log.w(TAG, "all urls previously blocked, fetch additional urls from dnstt")
val localIntent = Intent(ENVOY_BROADCAST_VALIDATION_BLOCKED)
LocalBroadcastManager.getInstance(this@NetworkIntentService).sendBroadcast(localIntent)
} else {
Log.w(TAG, "no urls submitted, fetch additional urls from dnstt")
}
getDnsttUrls(dnsttConfig, hysteriaCert)
}
} else {
urls.forEach() { url ->
urlsToSubmit.forEach() { url ->
if (url.contains("test")) {
// no-op
} else if (url.startsWith("http")) {
Expand All @@ -211,6 +228,24 @@ class NetworkIntentService : IntentService("NetworkIntentService") {
}
}

private fun shouldSubmitUrl(url: String): Boolean {

val currentTime = System.currentTimeMillis()
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val failureTime = preferences.getLong(url + TIME_SUFFIX, 0)
val failureCount = preferences.getInt(url + COUNT_SUFFIX, 0)

if ((failureCount in 1..3 && currentTime - failureTime < ONE_HOUR_MS * failureCount)
|| (failureCount == 4 && currentTime - failureTime < ONE_DAY_MS)
|| (failureCount >= 5 && currentTime - failureTime < ONE_WEEK_MS)) {
Log.d(TAG, "time limit has not expired for url(" + failureTime + "), do not submit: " + url)
return false
} else {
Log.d(TAG, "time limit expired for url(" + failureTime + "), submit again: " + url)
return true
}
}

private fun handleBatch(hysteriaCert: String?,
dnsttConfig: List<String>?,
dnsttUrls: Boolean,
Expand Down Expand Up @@ -837,6 +872,12 @@ class NetworkIntentService : IntentService("NetworkIntentService") {
companion object {
private const val TAG = "NetworkIntentService"

private const val ONE_HOUR_MS = 3600000
private const val ONE_DAY_MS = 86400000
private const val ONE_WEEK_MS = 604800000
private const val TIME_SUFFIX = "_time"
private const val COUNT_SUFFIX = "_count"

val ioScope = CoroutineScope(Dispatchers.IO)
var dnsttFlag = false

Expand Down Expand Up @@ -972,6 +1013,7 @@ class NetworkIntentService : IntentService("NetworkIntentService") {
val editor: SharedPreferences.Editor = sharedPreferences.edit()
val json = JSONArray(this@NetworkIntentService.validUrls)
editor.putString(PREF_VALID_URLS, json.toString())
editor.putInt(originalUrl + COUNT_SUFFIX, 0)
editor.apply()

val localIntent = Intent(ENVOY_BROADCAST_VALIDATION_SUCCEEDED).apply {
Expand Down Expand Up @@ -1078,6 +1120,15 @@ class NetworkIntentService : IntentService("NetworkIntentService") {
fun handleInvalidUrl() {
handleCleanup(envoyUrl)

// store failed urls so they are not attempted again
val currentTime = System.currentTimeMillis()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this@NetworkIntentService)
val failureCount = sharedPreferences.getInt(originalUrl + COUNT_SUFFIX, 0)
val editor: SharedPreferences.Editor = sharedPreferences.edit()
editor.putLong(originalUrl + TIME_SUFFIX, currentTime)
editor.putInt(originalUrl + COUNT_SUFFIX, failureCount + 1)
editor.apply()

// broadcast intent with invalid urls so application can handle errors
this@NetworkIntentService.invalidUrls.add(envoyUrl)
val localIntent = Intent(ENVOY_BROADCAST_VALIDATION_FAILED).apply {
Expand Down

0 comments on commit eacefad

Please sign in to comment.