Refine cleaning routing rules

This commit is contained in:
Mygod
2018-12-19 15:32:16 +08:00
parent 7cf8a37b64
commit d8ccd47bac
5 changed files with 19 additions and 14 deletions

View File

@@ -58,5 +58,6 @@ class App : Application() {
val masquerade get() = pref.getBoolean("service.masquerade", true)
val dhcpWorkaround get() = pref.getBoolean("service.dhcpWorkaround", false)
val cleanRoutings = Event0()
val onPreCleanRoutings = Event0()
val onRoutingsCleaned = Event0()
}

View File

@@ -10,14 +10,13 @@ class LocalOnlyInterfaceManager(val downstream: String) {
private var routing: Routing? = null
init {
app.cleanRoutings[this] = this::clean
app.onPreCleanRoutings[this] = { routing?.stop() }
app.onRoutingsCleaned[this] = this::clean
initRouting()
}
private fun clean() {
val routing = routing ?: return
routing.stop()
initRouting(routing.hostAddress)
initRouting((routing ?: return).hostAddress)
}
private fun initRouting(owner: InterfaceAddress? = null) {
@@ -42,7 +41,8 @@ class LocalOnlyInterfaceManager(val downstream: String) {
}
fun stop() {
app.cleanRoutings -= this
app.onPreCleanRoutings -= this
app.onRoutingsCleaned -= this
routing?.revert()
}
}

View File

@@ -40,6 +40,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
boot.isChecked = BootReceiver.enabled
} else boot.parent!!.removePreference(boot)
findPreference("service.clean").setOnPreferenceClickListener {
app.onPreCleanRoutings()
val cleaned = try {
Routing.clean()
true
@@ -48,7 +49,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
SmartSnackbar.make(e).show()
false
}
if (cleaned) app.cleanRoutings()
if (cleaned) app.onRoutingsCleaned()
true
}
findPreference("misc.logcat").setOnPreferenceClickListener {

View File

@@ -45,12 +45,10 @@ class TetheringService : IpNeighbourMonitoringService() {
if (!receiverRegistered) {
receiverRegistered = true
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
app.cleanRoutings[this] = {
synchronized(routings) {
for (iface in routings.keys) routings.put(iface, null)?.stop()
updateRoutingsLocked()
}
app.onPreCleanRoutings[this] = {
synchronized(routings) { for (iface in routings.keys) routings.put(iface, null)?.stop() }
}
app.onRoutingsCleaned[this] = { synchronized(routings) { updateRoutingsLocked() } }
IpNeighbourMonitor.registerCallback(this)
}
val disableIpv6 = app.pref.getBoolean("service.disableIpv6", false)
@@ -110,7 +108,8 @@ class TetheringService : IpNeighbourMonitoringService() {
private fun unregisterReceiver() {
if (receiverRegistered) {
unregisterReceiver(receiver)
app.cleanRoutings -= this
app.onPreCleanRoutings -= this
app.onRoutingsCleaned -= this
IpNeighbourMonitor.unregisterCallback(this)
receiverRegistered = false
}

View File

@@ -61,13 +61,17 @@ class Subrouting(private val parent: Routing, priority: Int, val upstream: Strin
private val subroutes = HashMap<InetAddress, Subroute>()
init {
Timber.d("Subrouting initialized from %s to %s", parent.downstream, upstream)
IpNeighbourMonitor.registerCallback(this)
}
/**
* Unregister client listener. This should be always called even after clean.
*/
override fun close() = IpNeighbourMonitor.unregisterCallback(this)
override fun close() {
IpNeighbourMonitor.unregisterCallback(this)
Timber.d("Subrouting closed from %s to %s", parent.downstream, upstream)
}
override fun onIpNeighbourAvailable(neighbours: List<IpNeighbour>) = synchronized(parent) {
val toRemove = HashSet(subroutes.keys)