Fix VPN connections change handlers

This commit is contained in:
Mygod
2018-01-13 12:13:22 +08:00
parent 57b9463a6f
commit c25b7a3f0c
4 changed files with 7 additions and 6 deletions

View File

@@ -160,7 +160,7 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C
}
Status.ACTIVE -> {
val routing = routing
assert(!routing!!.started)
check(!routing!!.started)
initRouting(ifname, routing.downstream, routing.hostAddress)
}
else -> throw RuntimeException("RepeaterService is in unexpected state when receiving onAvailable")
@@ -248,13 +248,13 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C
})
}
private fun unregisterReceiver() {
VpnListener.unregisterCallback(this)
if (receiverRegistered) {
unregisterReceiver(receiver)
receiverRegistered = false
}
}
private fun clean() {
VpnListener.unregisterCallback(this)
unregisterReceiver()
if (routing?.stop() == false)
Toast.makeText(this, "Something went wrong, please check logcat.", Toast.LENGTH_SHORT).show()

View File

@@ -41,7 +41,8 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
*/
fun rule(): Routing {
startScript.add("ip rule add from all iif $downstream lookup $upstream priority 17900")
stopScript.addFirst("ip rule del from all iif $downstream lookup $upstream priority 17900")
// by the time stopScript is called, table entry for upstream may already get removed
stopScript.addFirst("ip rule del from all iif $downstream priority 17900")
return this
}

View File

@@ -59,7 +59,7 @@ class TetheringService : Service(), VpnListener.Callback {
}
override fun onAvailable(ifname: String) {
assert(upstream == null || upstream == ifname)
check(upstream == null || upstream == ifname)
upstream = ifname
for ((downstream, value) in routings) if (value == null) {
val routing = Routing(ifname, downstream).rule().forward().dnsRedirect(app.dns)
@@ -68,7 +68,7 @@ class TetheringService : Service(), VpnListener.Callback {
}
override fun onLost(ifname: String) {
assert(upstream == null || upstream == ifname)
check(upstream == null || upstream == ifname)
upstream = null
for ((iface, routing) in routings) {
routing?.stop()

View File

@@ -31,7 +31,7 @@ object VpnListener : ConnectivityManager.NetworkCallback() {
private val available = HashMap<Network, String>()
override fun onAvailable(network: Network) {
val ifname = connectivityManager.getLinkProperties(network)?.interfaceName ?: return
available.put(network, ifname)
if (available.put(network, ifname) != null) return
debugLog(TAG, "onAvailable: $ifname")
callbacks.forEach { it.onAvailable(ifname) }
}