From d8ccd47bace14cc3033937ff21024f1f4981c100 Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 19 Dec 2018 15:32:16 +0800 Subject: [PATCH] Refine cleaning routing rules --- mobile/src/main/java/be/mygod/vpnhotspot/App.kt | 3 ++- .../be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt | 10 +++++----- .../be/mygod/vpnhotspot/SettingsPreferenceFragment.kt | 3 ++- .../main/java/be/mygod/vpnhotspot/TetheringService.kt | 11 +++++------ .../main/java/be/mygod/vpnhotspot/net/Subrouting.kt | 6 +++++- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt index c32bd001..8126d3e1 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt @@ -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() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt index 976bafaf..96b36dcc 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt @@ -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() } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt index fb525fc2..b1c5ac78 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt @@ -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 { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index e91e7c0e..4a6a1e55 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -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 } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/Subrouting.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/Subrouting.kt index cbe1d374..64ca85ca 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/Subrouting.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/Subrouting.kt @@ -61,13 +61,17 @@ class Subrouting(private val parent: Routing, priority: Int, val upstream: Strin private val subroutes = HashMap() 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) = synchronized(parent) { val toRemove = HashSet(subroutes.keys)