Fix VPN connections change handlers
This commit is contained in:
@@ -160,7 +160,7 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C
|
|||||||
}
|
}
|
||||||
Status.ACTIVE -> {
|
Status.ACTIVE -> {
|
||||||
val routing = routing
|
val routing = routing
|
||||||
assert(!routing!!.started)
|
check(!routing!!.started)
|
||||||
initRouting(ifname, routing.downstream, routing.hostAddress)
|
initRouting(ifname, routing.downstream, routing.hostAddress)
|
||||||
}
|
}
|
||||||
else -> throw RuntimeException("RepeaterService is in unexpected state when receiving onAvailable")
|
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() {
|
private fun unregisterReceiver() {
|
||||||
VpnListener.unregisterCallback(this)
|
|
||||||
if (receiverRegistered) {
|
if (receiverRegistered) {
|
||||||
unregisterReceiver(receiver)
|
unregisterReceiver(receiver)
|
||||||
receiverRegistered = false
|
receiverRegistered = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private fun clean() {
|
private fun clean() {
|
||||||
|
VpnListener.unregisterCallback(this)
|
||||||
unregisterReceiver()
|
unregisterReceiver()
|
||||||
if (routing?.stop() == false)
|
if (routing?.stop() == false)
|
||||||
Toast.makeText(this, "Something went wrong, please check logcat.", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "Something went wrong, please check logcat.", Toast.LENGTH_SHORT).show()
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
|
|||||||
*/
|
*/
|
||||||
fun rule(): Routing {
|
fun rule(): Routing {
|
||||||
startScript.add("ip rule add from all iif $downstream lookup $upstream priority 17900")
|
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
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class TetheringService : Service(), VpnListener.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onAvailable(ifname: String) {
|
override fun onAvailable(ifname: String) {
|
||||||
assert(upstream == null || upstream == ifname)
|
check(upstream == null || upstream == ifname)
|
||||||
upstream = ifname
|
upstream = ifname
|
||||||
for ((downstream, value) in routings) if (value == null) {
|
for ((downstream, value) in routings) if (value == null) {
|
||||||
val routing = Routing(ifname, downstream).rule().forward().dnsRedirect(app.dns)
|
val routing = Routing(ifname, downstream).rule().forward().dnsRedirect(app.dns)
|
||||||
@@ -68,7 +68,7 @@ class TetheringService : Service(), VpnListener.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLost(ifname: String) {
|
override fun onLost(ifname: String) {
|
||||||
assert(upstream == null || upstream == ifname)
|
check(upstream == null || upstream == ifname)
|
||||||
upstream = null
|
upstream = null
|
||||||
for ((iface, routing) in routings) {
|
for ((iface, routing) in routings) {
|
||||||
routing?.stop()
|
routing?.stop()
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ object VpnListener : ConnectivityManager.NetworkCallback() {
|
|||||||
private val available = HashMap<Network, String>()
|
private val available = HashMap<Network, String>()
|
||||||
override fun onAvailable(network: Network) {
|
override fun onAvailable(network: Network) {
|
||||||
val ifname = connectivityManager.getLinkProperties(network)?.interfaceName ?: return
|
val ifname = connectivityManager.getLinkProperties(network)?.interfaceName ?: return
|
||||||
available.put(network, ifname)
|
if (available.put(network, ifname) != null) return
|
||||||
debugLog(TAG, "onAvailable: $ifname")
|
debugLog(TAG, "onAvailable: $ifname")
|
||||||
callbacks.forEach { it.onAvailable(ifname) }
|
callbacks.forEach { it.onAvailable(ifname) }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user