Avoid using greylist api

This commit is contained in:
Mygod
2020-09-12 03:35:03 +08:00
parent b5ae2fb5d2
commit 642f93b9f7
3 changed files with 16 additions and 29 deletions

View File

@@ -14,7 +14,7 @@ import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.net.monitor.FallbackUpstreamMonitor
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
import be.mygod.vpnhotspot.util.SpanFormatter
import be.mygod.vpnhotspot.util.allStackedLinks
import be.mygod.vpnhotspot.util.allRoutes
import be.mygod.vpnhotspot.util.parseNumericAddress
import timber.log.Timber
@@ -25,9 +25,8 @@ class UpstreamsPreference(context: Context, attrs: AttributeSet) : Preference(co
private val internetV6Address = parseNumericAddress("2001:4860:4860::8888")
}
private data class Interface(val ifname: String, val internet: Boolean = true)
private open inner class Monitor : UpstreamMonitor.Callback {
protected var currentInterfaces = emptyList<Interface>()
protected var currentInterfaces = emptyMap<String, Boolean>()
val charSequence get() = currentInterfaces.map { (ifname, internet) ->
if (internet) SpannableStringBuilder(ifname).apply {
setSpan(StyleSpan(Typeface.BOLD), 0, length, 0)
@@ -36,18 +35,18 @@ class UpstreamsPreference(context: Context, attrs: AttributeSet) : Preference(co
override fun onAvailable(properties: LinkProperties?) {
currentInterfaces = properties?.allStackedLinks?.mapNotNull { prop ->
prop.interfaceName?.let { ifname ->
Interface(ifname, prop.routes.any {
try {
it.matches(internetV4Address) || it.matches(internetV6Address)
} catch (e: RuntimeException) {
Timber.w(e)
false
}
})
val result = mutableMapOf<String, Boolean>()
for (route in properties?.allRoutes ?: emptyList()) {
result.compute(route.`interface` ?: continue) { _, internet ->
internet == true || try {
route.matches(internetV4Address) || route.matches(internetV6Address)
} catch (e: RuntimeException) {
Timber.w(e)
false
}
}
}?.toList() ?: emptyList()
}
currentInterfaces = result
onUpdate()
}
}
@@ -55,7 +54,7 @@ class UpstreamsPreference(context: Context, attrs: AttributeSet) : Preference(co
private val primary = Monitor()
private val fallback: Monitor = object : Monitor() {
override fun onFallback() {
currentInterfaces = listOf(Interface("<default>"))
currentInterfaces = mapOf("<default>" to true)
onUpdate()
}
}