From a77beb39db1a8453390bae172d92a26979ff9c8b Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 13 Jan 2018 19:17:00 +0800 Subject: [PATCH] Create more noise if SU fails --- .../be/mygod/vpnhotspot/RepeaterService.kt | 12 ++++++----- .../be/mygod/vpnhotspot/TetheringService.kt | 21 ++++++++++++++----- mobile/src/main/res/values/strings.xml | 1 + 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 14a0753b..7a3cf890 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -181,7 +181,7 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C VpnListener.registerCallback(this) return START_NOT_STICKY } - private fun startFailure(msg: String?, group: WifiP2pGroup? = null) { + private fun startFailure(msg: CharSequence?, group: WifiP2pGroup? = null) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() showNotification() if (group != null) removeGroup() else clean() @@ -227,13 +227,15 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C Status.ACTIVE -> { val routing = routing check(!routing!!.started) - initRouting(ifname, routing.downstream, routing.hostAddress) + if (!initRouting(ifname, routing.downstream, routing.hostAddress)) + Toast.makeText(this, getText(R.string.noisy_su_failure), Toast.LENGTH_SHORT).show() } else -> throw RuntimeException("RepeaterService is in unexpected state when receiving onAvailable") } } override fun onLost(ifname: String) { - routing?.stop() + if (routing?.stop() == false) + Toast.makeText(this, getText(R.string.noisy_su_failure), Toast.LENGTH_SHORT).show() upstream = null } @@ -267,7 +269,7 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C receiverRegistered = true try { if (initRouting(upstream!!, downstream, owner)) doStart(group) - else startFailure("Something went wrong, please check logcat.", group) + else startFailure(getText(R.string.noisy_su_failure), group) } catch (e: Routing.InterfaceNotFoundException) { startFailure(e.message, group) return @@ -323,7 +325,7 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C VpnListener.unregisterCallback(this) unregisterReceiver() if (routing?.stop() == false) - Toast.makeText(this, "Something went wrong, please check logcat.", Toast.LENGTH_SHORT).show() + Toast.makeText(this, getText(R.string.noisy_su_failure), Toast.LENGTH_SHORT).show() routing = null status = Status.IDLE stopForeground(true) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index 4ba09429..6ef112a4 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -3,6 +3,7 @@ package be.mygod.vpnhotspot import android.app.Service import android.content.Intent import android.support.v4.content.LocalBroadcastManager +import android.widget.Toast import be.mygod.vpnhotspot.App.Companion.app class TetheringService : Service(), VpnListener.Callback { @@ -26,7 +27,8 @@ class TetheringService : Service(), VpnListener.Callback { private val receiver = broadcastReceiver { _, intent -> val remove = routings.keys - NetUtils.getTetheredIfaces(intent.extras) if (remove.isEmpty()) return@broadcastReceiver - for (iface in remove) routings.remove(iface)?.stop() + val failed = remove.any { routings.remove(it)?.stop() == false } + if (failed) Toast.makeText(this, getText(R.string.noisy_su_failure), Toast.LENGTH_SHORT).show() updateRoutings() } @@ -38,10 +40,16 @@ class TetheringService : Service(), VpnListener.Callback { } else { val upstream = upstream if (upstream != null) { + var failed = false for ((downstream, value) in routings) if (value == null) { val routing = Routing(upstream, downstream).rule().forward().dnsRedirect(app.dns) - if (routing.start()) routings[downstream] = routing else routing.stop() + if (routing.start()) routings[downstream] = routing else { + failed = true + routing.stop() + routings.remove(downstream) + } } + if (failed) Toast.makeText(this, getText(R.string.noisy_su_failure), Toast.LENGTH_SHORT).show() } else if (!receiverRegistered) { registerReceiver(receiver, intentFilter(NetUtils.ACTION_TETHER_STATE_CHANGED)) VpnListener.registerCallback(this) @@ -56,7 +64,8 @@ class TetheringService : Service(), VpnListener.Callback { if (intent != null) { // otw service is recreated after being killed val iface = intent.getStringExtra(EXTRA_ADD_INTERFACE) if (iface != null) routings.put(iface, null) - routings.remove(intent.getStringExtra(EXTRA_REMOVE_INTERFACE))?.stop() + if (routings.remove(intent.getStringExtra(EXTRA_REMOVE_INTERFACE))?.stop() == false) + Toast.makeText(this, getText(R.string.noisy_su_failure), Toast.LENGTH_SHORT).show() } else active.forEach { routings.put(it, null) } updateRoutings() return START_STICKY @@ -71,10 +80,12 @@ class TetheringService : Service(), VpnListener.Callback { override fun onLost(ifname: String) { check(upstream == null || upstream == ifname) upstream = null + var failed = false for ((iface, routing) in routings) { - routing?.stop() + if (routing?.stop() == false) failed = true routings[iface] = null } + if (failed) Toast.makeText(this, getText(R.string.noisy_su_failure), Toast.LENGTH_SHORT).show() } override fun onDestroy() { @@ -82,7 +93,7 @@ class TetheringService : Service(), VpnListener.Callback { super.onDestroy() } - fun unregisterReceiver() { + private fun unregisterReceiver() { if (receiverRegistered) { unregisterReceiver(receiver) VpnListener.unregisterCallback(this) diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index f95c9c49..f794def8 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -8,4 +8,5 @@ Fatal: Downstream interface not found system tethering first.]]> + Something went wrong, please check logcat.