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 masquerade get() = pref.getBoolean("service.masquerade", true)
val dhcpWorkaround get() = pref.getBoolean("service.dhcpWorkaround", false) 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 private var routing: Routing? = null
init { init {
app.cleanRoutings[this] = this::clean app.onPreCleanRoutings[this] = { routing?.stop() }
app.onRoutingsCleaned[this] = this::clean
initRouting() initRouting()
} }
private fun clean() { private fun clean() {
val routing = routing ?: return initRouting((routing ?: return).hostAddress)
routing.stop()
initRouting(routing.hostAddress)
} }
private fun initRouting(owner: InterfaceAddress? = null) { private fun initRouting(owner: InterfaceAddress? = null) {
@@ -42,7 +41,8 @@ class LocalOnlyInterfaceManager(val downstream: String) {
} }
fun stop() { fun stop() {
app.cleanRoutings -= this app.onPreCleanRoutings -= this
app.onRoutingsCleaned -= this
routing?.revert() routing?.revert()
} }
} }

View File

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

View File

@@ -45,12 +45,10 @@ class TetheringService : IpNeighbourMonitoringService() {
if (!receiverRegistered) { if (!receiverRegistered) {
receiverRegistered = true receiverRegistered = true
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
app.cleanRoutings[this] = { app.onPreCleanRoutings[this] = {
synchronized(routings) { synchronized(routings) { for (iface in routings.keys) routings.put(iface, null)?.stop() }
for (iface in routings.keys) routings.put(iface, null)?.stop()
updateRoutingsLocked()
}
} }
app.onRoutingsCleaned[this] = { synchronized(routings) { updateRoutingsLocked() } }
IpNeighbourMonitor.registerCallback(this) IpNeighbourMonitor.registerCallback(this)
} }
val disableIpv6 = app.pref.getBoolean("service.disableIpv6", false) val disableIpv6 = app.pref.getBoolean("service.disableIpv6", false)
@@ -110,7 +108,8 @@ class TetheringService : IpNeighbourMonitoringService() {
private fun unregisterReceiver() { private fun unregisterReceiver() {
if (receiverRegistered) { if (receiverRegistered) {
unregisterReceiver(receiver) unregisterReceiver(receiver)
app.cleanRoutings -= this app.onPreCleanRoutings -= this
app.onRoutingsCleaned -= this
IpNeighbourMonitor.unregisterCallback(this) IpNeighbourMonitor.unregisterCallback(this)
receiverRegistered = false 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>() private val subroutes = HashMap<InetAddress, Subroute>()
init { init {
Timber.d("Subrouting initialized from %s to %s", parent.downstream, upstream)
IpNeighbourMonitor.registerCallback(this) IpNeighbourMonitor.registerCallback(this)
} }
/** /**
* Unregister client listener. This should be always called even after clean. * 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) { override fun onIpNeighbourAvailable(neighbours: List<IpNeighbour>) = synchronized(parent) {
val toRemove = HashSet(subroutes.keys) val toRemove = HashSet(subroutes.keys)