Remove fallback upstream monitor

This option has been deprecated. As a replacement, if one wants to forbid fallback communication, a global routed VPN can be used instead.
This commit is contained in:
Mygod
2019-02-01 21:11:49 +08:00
parent f1973116ca
commit 99e721bf7e
7 changed files with 19 additions and 83 deletions

View File

@@ -11,7 +11,6 @@ import androidx.preference.SwitchPreference
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
import be.mygod.vpnhotspot.net.monitor.FallbackUpstreamMonitor
import be.mygod.vpnhotspot.net.monitor.IpMonitor
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
import be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat
@@ -132,24 +131,23 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
override fun onDisplayPreferenceDialog(preference: Preference) {
when (preference.key) {
UpstreamMonitor.KEY, FallbackUpstreamMonitor.KEY ->
AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat().apply {
setArguments(preference.key, try {
NetworkInterface.getNetworkInterfaces().asSequence()
.filter {
try {
it.isUp && !it.isLoopback && it.interfaceAddresses.isNotEmpty()
} catch (_: SocketException) {
false
}
UpstreamMonitor.KEY -> AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat().apply {
setArguments(preference.key, try {
NetworkInterface.getNetworkInterfaces().asSequence()
.filter {
try {
it.isUp && !it.isLoopback && it.interfaceAddresses.isNotEmpty()
} catch (_: SocketException) {
false
}
.map { it.name }.sorted().toList().toTypedArray()
} catch (e: SocketException) {
Timber.d(e)
emptyArray<String>()
})
setTargetFragment(this@SettingsPreferenceFragment, 0)
}.show(fragmentManager ?: return, preference.key)
}
.map { it.name }.sorted().toList().toTypedArray()
} catch (e: SocketException) {
Timber.d(e)
emptyArray<String>()
})
setTargetFragment(this@SettingsPreferenceFragment, 0)
}.show(fragmentManager ?: return, preference.key)
else -> super.onDisplayPreferenceDialog(preference)
}
}

View File

@@ -3,7 +3,7 @@ package be.mygod.vpnhotspot.net
import android.os.Build
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.net.monitor.FallbackUpstreamMonitor
import be.mygod.vpnhotspot.net.monitor.DefaultNetworkMonitor
import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor
import be.mygod.vpnhotspot.net.monitor.TrafficRecorder
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
@@ -283,16 +283,14 @@ class Routing(val downstream: String, ownerAddress: InterfaceAddress? = null) :
fun stop() {
IpNeighbourMonitor.unregisterCallback(this)
FallbackUpstreamMonitor.unregisterCallback(fallbackUpstream)
DefaultNetworkMonitor.unregisterCallback(fallbackUpstream)
UpstreamMonitor.unregisterCallback(upstream)
}
fun commit(localOnly: Boolean = false) {
transaction.commit()
Timber.i("Started routing for $downstream")
if (localOnly || masqueradeMode != MasqueradeMode.Netd) {
FallbackUpstreamMonitor.registerCallback(fallbackUpstream)
}
if (localOnly || masqueradeMode != MasqueradeMode.Netd) DefaultNetworkMonitor.registerCallback(fallbackUpstream)
UpstreamMonitor.registerCallback(upstream)
IpNeighbourMonitor.registerCallback(this)
}

View File

@@ -1,41 +0,0 @@
package be.mygod.vpnhotspot.net.monitor
import android.content.SharedPreferences
import be.mygod.vpnhotspot.App.Companion.app
abstract class FallbackUpstreamMonitor private constructor() : UpstreamMonitor() {
companion object : SharedPreferences.OnSharedPreferenceChangeListener {
const val KEY = "service.upstream.fallback"
init {
app.pref.registerOnSharedPreferenceChangeListener(this)
}
private fun generateMonitor(): UpstreamMonitor {
val upstream = app.pref.getString(KEY, null)
return if (upstream.isNullOrEmpty()) DefaultNetworkMonitor else InterfaceMonitor(upstream)
}
private var monitor = generateMonitor()
fun registerCallback(callback: Callback) = synchronized(this) { monitor.registerCallback(callback) }
fun unregisterCallback(callback: Callback) = synchronized(this) { monitor.unregisterCallback(callback) }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == KEY) synchronized(this) {
val old = monitor
val callbacks = synchronized(old) {
val callbacks = old.callbacks.toList()
old.callbacks.clear()
old.destroyLocked()
callbacks
}
val new = generateMonitor()
monitor = new
for (callback in callbacks) {
callback.onLost()
new.registerCallback(callback)
}
}
}
}
}