Make DHCP workaround global
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package be.mygod.vpnhotspot.net
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import be.mygod.vpnhotspot.util.RootSession
|
||||
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Assuming RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000.
|
||||
* Normally this is used to forward packets from remote to local, but it works anyways.
|
||||
* It just needs to be before RULE_PRIORITY_SECURE_VPN = 12000.
|
||||
* It would be great if we can gain better understanding into why this is only needed on some of the devices but not
|
||||
* others.
|
||||
*
|
||||
* Source: https://android.googlesource.com/platform/system/netd/+/b9baf26/server/RouteController.cpp#57
|
||||
*/
|
||||
object DhcpWorkaround : SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private const val KEY_ENABLED = "service.dhcpWorkaround"
|
||||
|
||||
init {
|
||||
app.pref.registerOnSharedPreferenceChangeListener(this)
|
||||
}
|
||||
|
||||
val shouldEnable get() = app.pref.getBoolean(KEY_ENABLED, false)
|
||||
fun enable(enabled: Boolean) {
|
||||
val action = if (enabled) "add" else "del"
|
||||
try {
|
||||
RootSession.use { it.exec("ip rule $action iif lo uidrange 0-0 lookup local_network priority 11000") }
|
||||
} catch (e: RootSession.UnexpectedOutputException) {
|
||||
if (e.result.code == 2 && e.result.out.isEmpty() &&
|
||||
e.result.err.joinToString("\n") == "RTNETLINK answers: File exists") return
|
||||
Timber.w(e)
|
||||
SmartSnackbar.make(e).show()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||
if (key == KEY_ENABLED) enable(shouldEnable)
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import timber.log.Timber
|
||||
import java.net.*
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
/**
|
||||
* A transaction wrapper that helps set up routing environment.
|
||||
@@ -32,8 +31,6 @@ class Routing(val downstream: String, ownerAddress: InterfaceAddress? = null) :
|
||||
private const val RULE_PRIORITY_UPSTREAM = 17800
|
||||
private const val RULE_PRIORITY_UPSTREAM_FALLBACK = 17900
|
||||
|
||||
private val dhcpWorkaroundCounter = AtomicLong()
|
||||
|
||||
/**
|
||||
* -w <seconds> is not supported on 7.1-.
|
||||
* Fortunately there also isn't a time limit for starting a foreground service back in 7.1-.
|
||||
@@ -55,7 +52,6 @@ class Routing(val downstream: String, ownerAddress: InterfaceAddress? = null) :
|
||||
it.execQuiet("$IPTABLES -t nat -X vpnhotspot_masquerade")
|
||||
it.execQuiet("while ip rule del priority $RULE_PRIORITY_UPSTREAM; do done")
|
||||
it.execQuiet("while ip rule del priority $RULE_PRIORITY_UPSTREAM_FALLBACK; do done")
|
||||
it.execQuiet("while ip rule del iif lo uidrange 0-0 lookup local_network priority 11000; do done")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,22 +249,6 @@ class Routing(val downstream: String, ownerAddress: InterfaceAddress? = null) :
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Similarly, assuming RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000.
|
||||
* Normally this is used to forward packets from remote to local, but it works anyways. It just needs to be before
|
||||
* RULE_PRIORITY_SECURE_VPN = 12000. It would be great if we can gain better understanding into why this is only
|
||||
* needed on some of the devices but not others.
|
||||
*
|
||||
* Source: https://android.googlesource.com/platform/system/netd/+/b9baf26/server/RouteController.cpp#57
|
||||
*/
|
||||
fun dhcpWorkaround() {
|
||||
// workaround for adding multiple exact same rules
|
||||
// if somebody decides to do this 1000 times to break this, god bless you
|
||||
val priority = 11000 + dhcpWorkaroundCounter.getAndAdd(1) % 1000
|
||||
transaction.exec("ip rule add iif lo uidrange 0-0 lookup local_network priority $priority",
|
||||
"ip rule del iif lo uidrange 0-0 lookup local_network priority $priority")
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
IpNeighbourMonitor.unregisterCallback(this)
|
||||
FallbackUpstreamMonitor.unregisterCallback(fallbackUpstream)
|
||||
|
||||
Reference in New Issue
Block a user