Create more noise if SU fails

This commit is contained in:
Mygod
2018-01-13 19:17:00 +08:00
parent d278c5daab
commit a77beb39db
3 changed files with 24 additions and 10 deletions

View File

@@ -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)

View File

@@ -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)