diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/DhcpWorkaround.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/DhcpWorkaround.kt index 9af15c6c..c2145ef1 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/DhcpWorkaround.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/DhcpWorkaround.kt @@ -34,11 +34,7 @@ object DhcpWorkaround : SharedPreferences.OnSharedPreferenceChangeListener { try { it.exec("ip rule $action iif lo uidrange 0-0 lookup local_network priority 11000") } catch (e: RoutingCommands.UnexpectedOutputException) { - if (e.result.out.isEmpty() && (e.result.exit == 2 || e.result.exit == 254) && if (enabled) { - e.result.err == "RTNETLINK answers: File exists\n" - } else { - e.result.err == "RTNETLINK answers: No such file or directory\n" - }) return@use + if (Routing.shouldSuppressIpError(e)) return@use Timber.w(IOException("Failed to tweak dhcp workaround rule", e)) SmartSnackbar.make(e).show() } 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 2caf7410..a58c82b8 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt @@ -91,11 +91,23 @@ class Routing(private val caller: Any, private val downstream: String, result.check(listOf(command), !result.out.endsWith(suffix)) if (result.out.length > suffix.length) Timber.i(result.message(listOf(command), true)) } + + fun shouldSuppressIpError(e: RoutingCommands.UnexpectedOutputException, isAdd: Boolean = true) = + e.result.out.isEmpty() && (e.result.exit == 2 || e.result.exit == 254) && e.result.err == if (isAdd) { + "RTNETLINK answers: File exists\n" + } else { + "RTNETLINK answers: No such file or directory\n" + } } - private fun RootSession.Transaction.ipRule(add: String, priority: Int, rule: String = "", del: String = add) = + private fun RootSession.Transaction.ipRule(add: String, priority: Int, rule: String = "", del: String = add) { + try { exec("ip rule add $rule iif $downstream $add priority $priority", "ip rule del $rule iif $downstream $del priority $priority") + } catch (e: RoutingCommands.UnexpectedOutputException) { + if (!shouldSuppressIpError(e)) throw e + } + } private fun RootSession.Transaction.ipRuleLookup(upstream: String, priority: Int, rule: String = "") = // by the time stopScript is called, table entry for upstream may already get removed ipRule("lookup $upstream", priority, rule, "")