diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 8bbae49f..20b0dfee 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -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() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt b/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt index 901562cb..d9115966 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt @@ -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 } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index b4d68b75..6f7aeba8 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -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() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/VpnListener.kt b/mobile/src/main/java/be/mygod/vpnhotspot/VpnListener.kt index 60970766..41264a52 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/VpnListener.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/VpnListener.kt @@ -31,7 +31,7 @@ object VpnListener : ConnectivityManager.NetworkCallback() { private val available = HashMap() 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) } }