diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt index bd0e3b31..d90cfca8 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt @@ -85,16 +85,11 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener { when (intent.action) { WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> if (intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, 0) == - WifiP2pManager.WIFI_P2P_STATE_DISABLED) clean() // group may be enabled by other apps + WifiP2pManager.WIFI_P2P_STATE_DISABLED) clean() // ignore P2P enabled WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> { - val info = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO) - val net = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO) - val group = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP) - if (routing == null) onGroupCreated(info, group) - this.group = group - binder.data?.onGroupChanged() - showNotification(group) - Log.d(TAG, "${intent.action}: $info, $net, $group") + onP2pConnectionChanged(intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO), + intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO), + intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP)) } WIFI_AP_STATE_CHANGED_ACTION -> if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0) != WIFI_AP_STATE_ENABLED) clean() @@ -214,6 +209,16 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener { startForeground(1, builder.build()) } + private fun onP2pConnectionChanged(info: WifiP2pInfo, net: NetworkInfo?, group: WifiP2pGroup) { + if (routing == null) onGroupCreated(info, group) else if (!group.isGroupOwner) { // P2P shutdown + clean() + return + } + this.group = group + binder.data?.onGroupChanged() + showNotification(group) + Log.d(TAG, "P2P connection changed: $info\n$net\n$group") + } private fun onGroupCreated(info: WifiP2pInfo, group: WifiP2pGroup) { val owner = info.groupOwnerAddress val downstream = group.`interface` diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt b/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt index 0a228f0e..29f90f68 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt @@ -41,7 +41,6 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress startScript.add("ip route add ${InetAddress.getByAddress(address).hostAddress}/$subnetPrefixLength dev $downstream scope link table 62") startScript.add("ip route add broadcast 255.255.255.255 dev $downstream scope link table 62") startScript.add("ip rule add iif $downstream lookup 62") - stopScript.addFirst("ip route del default dev $upstream scope link table 62") // removing each rule may fail if downstream is already removed stopScript.addFirst("ip route flush table 62") stopScript.addFirst("ip rule del iif $downstream lookup 62")