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

@@ -185,9 +185,8 @@ class Routing(private val caller: Any, private val downstream: String) : IpNeigh
override fun onAvailable(properties: LinkProperties?) = synchronized(this@Routing) {
if (stopped) return
val toRemove = subrouting.keys.toMutableSet()
for (link in properties?.allStackedLinks ?: emptySequence()) {
val ifname = link.interfaceName
if (ifname == null || toRemove.remove(ifname) || !upstreams.add(ifname)) continue
for (ifname in properties?.allInterfaceNames ?: emptyList()) {
if (toRemove.remove(ifname) || !upstreams.add(ifname)) continue
try {
subrouting[ifname] = Subrouting(priority, ifname)
} catch (e: Exception) {

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()
}
}

View File

@@ -117,17 +117,6 @@ val LinkProperties.allInterfaceNames get() = getAllInterfaceNames.invoke(this) a
private val getAllRoutes by lazy { LinkProperties::class.java.getDeclaredMethod("getAllRoutes") }
@Suppress("UNCHECKED_CAST")
val LinkProperties.allRoutes get() = getAllRoutes.invoke(this) as List<RouteInfo>
private val getStackedLinks by lazy { LinkProperties::class.java.getDeclaredMethod("getStackedLinks") }
@Suppress("UNCHECKED_CAST")
private val LinkProperties.stackedLinks get() = getStackedLinks.invoke(this) as List<LinkProperties>
private suspend fun SequenceScope<LinkProperties>.yieldRec(prop: LinkProperties) {
yield(prop)
for (link in prop.stackedLinks) yieldRec(link)
}
val LinkProperties.allStackedLinks get() = let {
sequence { yieldRec(it) }
}
fun Context.launchUrl(url: String) {
if (app.hasTouch) try {