Replace strict mode with fallback upstream interface

Fixes #40. Apparently we can no longer take advantage of default network rules set by Android system since Android 9.0 thanks to this commit: 758627c4d9
This commit is contained in:
Mygod
2018-10-03 13:02:28 +08:00
parent 8e3567954e
commit 8e09e8cd8a
26 changed files with 573 additions and 361 deletions

View File

@@ -1,54 +1,33 @@
package be.mygod.vpnhotspot
import android.content.Context
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber
import java.net.InetAddress
import java.net.InterfaceAddress
class LocalOnlyInterfaceManager(private val owner: Context, val downstream: String) : UpstreamMonitor.Callback {
class LocalOnlyInterfaceManager(val downstream: String) {
private var routing: Routing? = null
private var dns = emptyList<InetAddress>()
init {
app.cleanRoutings[this] = this::clean
UpstreamMonitor.registerCallback(this) { initRouting() }
}
override fun onAvailable(ifname: String, dns: List<InetAddress>) {
val routing = routing
initRouting(ifname, if (routing == null) null else {
routing.revert()
routing.hostAddress
}, dns)
}
override fun onLost() {
val routing = routing ?: return
routing.revert()
initRouting(null, routing.hostAddress, emptyList())
initRouting()
}
private fun clean() {
val routing = routing ?: return
routing.stop()
initRouting(routing.upstream, routing.hostAddress, dns)
initRouting(routing.hostAddress)
}
private fun initRouting(upstream: String? = null, owner: InterfaceAddress? = null,
dns: List<InetAddress> = this.dns) {
this.dns = dns
try {
routing = Routing(this.owner, upstream, downstream, owner, app.strict).apply {
private fun initRouting(owner: InterfaceAddress? = null) {
routing = try {
Routing(downstream, owner).apply {
try {
if (app.dhcpWorkaround) dhcpWorkaround()
ipForward() // local only interfaces need to enable ip_forward
rule()
forward()
if (app.masquerade) masquerade()
dnsRedirect(dns)
commit()
} catch (e: Exception) {
revert()
@@ -58,12 +37,11 @@ class LocalOnlyInterfaceManager(private val owner: Context, val downstream: Stri
} catch (e: Exception) {
SmartSnackbar.make(e.localizedMessage).show()
Timber.w(e)
routing = null
null
}
}
fun stop() {
UpstreamMonitor.unregisterCallback(this)
app.cleanRoutings -= this
routing?.revert()
}