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) { override fun onAvailable(properties: LinkProperties?) = synchronized(this@Routing) {
if (stopped) return if (stopped) return
val toRemove = subrouting.keys.toMutableSet() val toRemove = subrouting.keys.toMutableSet()
for (link in properties?.allStackedLinks ?: emptySequence()) { for (ifname in properties?.allInterfaceNames ?: emptyList()) {
val ifname = link.interfaceName if (toRemove.remove(ifname) || !upstreams.add(ifname)) continue
if (ifname == null || toRemove.remove(ifname) || !upstreams.add(ifname)) continue
try { try {
subrouting[ifname] = Subrouting(priority, ifname) subrouting[ifname] = Subrouting(priority, ifname)
} catch (e: Exception) { } 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.FallbackUpstreamMonitor
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
import be.mygod.vpnhotspot.util.SpanFormatter 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 be.mygod.vpnhotspot.util.parseNumericAddress
import timber.log.Timber 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 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 { 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) -> val charSequence get() = currentInterfaces.map { (ifname, internet) ->
if (internet) SpannableStringBuilder(ifname).apply { if (internet) SpannableStringBuilder(ifname).apply {
setSpan(StyleSpan(Typeface.BOLD), 0, length, 0) setSpan(StyleSpan(Typeface.BOLD), 0, length, 0)
@@ -36,18 +35,18 @@ class UpstreamsPreference(context: Context, attrs: AttributeSet) : Preference(co
override fun onAvailable(properties: LinkProperties?) { override fun onAvailable(properties: LinkProperties?) {
currentInterfaces = properties?.allStackedLinks?.mapNotNull { prop -> val result = mutableMapOf<String, Boolean>()
prop.interfaceName?.let { ifname -> for (route in properties?.allRoutes ?: emptyList()) {
Interface(ifname, prop.routes.any { result.compute(route.`interface` ?: continue) { _, internet ->
try { internet == true || try {
it.matches(internetV4Address) || it.matches(internetV6Address) route.matches(internetV4Address) || route.matches(internetV6Address)
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
Timber.w(e) Timber.w(e)
false false
} }
})
} }
}?.toList() ?: emptyList() }
currentInterfaces = result
onUpdate() onUpdate()
} }
} }
@@ -55,7 +54,7 @@ class UpstreamsPreference(context: Context, attrs: AttributeSet) : Preference(co
private val primary = Monitor() private val primary = Monitor()
private val fallback: Monitor = object : Monitor() { private val fallback: Monitor = object : Monitor() {
override fun onFallback() { override fun onFallback() {
currentInterfaces = listOf(Interface("<default>")) currentInterfaces = mapOf("<default>" to true)
onUpdate() 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") } private val getAllRoutes by lazy { LinkProperties::class.java.getDeclaredMethod("getAllRoutes") }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val LinkProperties.allRoutes get() = getAllRoutes.invoke(this) as List<RouteInfo> 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) { fun Context.launchUrl(url: String) {
if (app.hasTouch) try { if (app.hasTouch) try {