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

@@ -4,16 +4,14 @@ import android.content.Intent
import android.content.IntentFilter
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.manage.TetheringFragment
import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor
import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber
import java.net.InetAddress
class TetheringService : IpNeighbourMonitoringService(), UpstreamMonitor.Callback {
class TetheringService : IpNeighbourMonitoringService() {
companion object {
const val EXTRA_ADD_INTERFACE = "interface.add"
const val EXTRA_REMOVE_INTERFACE = "interface.remove"
@@ -27,8 +25,6 @@ class TetheringService : IpNeighbourMonitoringService(), UpstreamMonitor.Callbac
private val binder = Binder()
private val routings = HashMap<String, Routing?>()
private var upstream: String? = null
private var dns: List<InetAddress> = emptyList()
private var receiverRegistered = false
private val receiver = broadcastReceiver { _, intent ->
val extras = intent.extras ?: return@broadcastReceiver
@@ -51,43 +47,36 @@ class TetheringService : IpNeighbourMonitoringService(), UpstreamMonitor.Callbac
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
app.cleanRoutings[this] = {
synchronized(routings) {
for (iface in routings.keys) routings[iface] = null
for (iface in routings.keys) routings.put(iface, null)?.stop()
updateRoutingsLocked()
}
}
IpNeighbourMonitor.registerCallback(this)
UpstreamMonitor.registerCallback(this)
}
val upstream = upstream
val disableIpv6 = app.pref.getBoolean("service.disableIpv6", false)
if (upstream != null || app.strict || disableIpv6) {
val iterator = routings.iterator()
while (iterator.hasNext()) {
val (downstream, value) = iterator.next()
if (value != null) if (value.upstream == upstream) continue else value.revert()
try {
routings[downstream] = Routing(this, upstream, downstream).apply {
try {
if (app.dhcpWorkaround) dhcpWorkaround()
// system tethering already has working forwarding rules
// so it doesn't make sense to add additional forwarding rules
rule()
// here we always enforce strict mode as fallback is handled by system which we disable
forward()
if (app.masquerade) masquerade()
if (upstream != null) dnsRedirect(dns)
if (disableIpv6) disableIpv6()
commit()
} catch (e: Exception) {
revert()
throw e
}
val iterator = routings.iterator()
while (iterator.hasNext()) {
val (downstream, value) = iterator.next()
if (value != null) continue
try {
routings[downstream] = Routing(downstream).apply {
try {
if (app.dhcpWorkaround) dhcpWorkaround()
// system tethering already has working forwarding rules
// so it doesn't make sense to add additional forwarding rules
forward()
if (app.masquerade) masquerade()
if (disableIpv6) disableIpv6()
commit()
} catch (e: Exception) {
revert()
throw e
}
} catch (e: Exception) {
Timber.w(e)
SmartSnackbar.make(e.localizedMessage).show()
iterator.remove()
}
} catch (e: Exception) {
Timber.w(e)
SmartSnackbar.make(e.localizedMessage).show()
iterator.remove()
}
}
if (routings.isEmpty()) {
@@ -113,24 +102,6 @@ class TetheringService : IpNeighbourMonitoringService(), UpstreamMonitor.Callbac
return START_NOT_STICKY
}
override fun onAvailable(ifname: String, dns: List<InetAddress>) {
if (upstream == ifname) return
upstream = ifname
this.dns = dns
synchronized(routings) { updateRoutingsLocked() }
}
override fun onLost() {
upstream = null
this.dns = emptyList()
synchronized(routings) {
for ((iface, routing) in routings) {
routing?.revert()
routings[iface] = null
}
}
}
override fun onDestroy() {
unregisterReceiver()
super.onDestroy()
@@ -141,8 +112,6 @@ class TetheringService : IpNeighbourMonitoringService(), UpstreamMonitor.Callbac
unregisterReceiver(receiver)
app.cleanRoutings -= this
IpNeighbourMonitor.unregisterCallback(this)
UpstreamMonitor.unregisterCallback(this)
upstream = null
receiverRegistered = false
}
}