diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt index 2e5a78c2..01ab5ed5 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt @@ -53,9 +53,9 @@ class Routing(val downstream: String, ownerAddress: InterfaceAddress? = null) { } fun RootSession.Transaction.iptablesAdd(content: String, table: String = "filter") = - exec("$IPTABLES -t $table -A $content", "$IPTABLES -t $table -D $content") + exec("$IPTABLES -t $table -A $content", "$IPTABLES -t $table -D $content", true) fun RootSession.Transaction.iptablesInsert(content: String, table: String = "filter") = - exec("$IPTABLES -t $table -I $content", "$IPTABLES -t $table -D $content") + exec("$IPTABLES -t $table -I $content", "$IPTABLES -t $table -D $content", true) } class InterfaceNotFoundException : SocketException() { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/RootSession.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/RootSession.kt index ab9048c9..2c545ce5 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/RootSession.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/RootSession.kt @@ -58,12 +58,11 @@ class RootSession : AutoCloseable { class UnexpectedOutputException(msg: String) : RuntimeException(msg) private fun checkOutput(command: String, result: Shell.Result, out: Boolean = result.out.isNotEmpty(), - err: Boolean = result.err.isNotEmpty()) { - if (result.isSuccess && !out && !err) return + err: Boolean = result.err.isNotEmpty()): String { val msg = StringBuilder("$command exited with ${result.code}") if (out) result.out.forEach { msg.append("\n$it") } if (err) result.err.forEach { msg.append("\nE $it") } - throw UnexpectedOutputException(msg.toString()) + if (!result.isSuccess || out || err) throw UnexpectedOutputException(msg.toString()) else return msg.toString() } private val shell = Shell.newInstance("su") @@ -102,6 +101,11 @@ class RootSession : AutoCloseable { }).exec() } fun exec(command: String) = checkOutput(command, execQuiet(command)) + fun execWithWait(command: String) { + val result = execQuiet(command) + val message = checkOutput(command, result, err = false) + if (result.err.isNotEmpty()) Timber.i(message) + } fun execOutUnjoined(command: String): List { val result = execQuiet(command) checkOutput(command, result, false) @@ -115,9 +119,9 @@ class RootSession : AutoCloseable { inner class Transaction { private val revertCommands = LinkedList() - fun exec(command: String, revert: String? = null) { + fun exec(command: String, revert: String? = null, wait: Boolean = false) { if (revert != null) revertCommands.addFirst(revert) // add first just in case exec fails - this@RootSession.exec(command) + if (wait) this@RootSession.execWithWait(command) else this@RootSession.exec(command) } fun execQuiet(command: String) = this@RootSession.execQuiet(command)