Remove fallback DNS

This commit is contained in:
Mygod
2019-02-04 23:30:29 +08:00
parent 9507c0c4b7
commit 8132b2766d
12 changed files with 105 additions and 134 deletions

View File

@@ -0,0 +1,60 @@
package be.mygod.vpnhotspot
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber
abstract class RoutingManager(private val caller: Any, val downstream: String) {
companion object {
private const val KEY_MASQUERADE_MODE = "service.masqueradeMode"
var masqueradeMode: Routing.MasqueradeMode
get() {
app.pref.getString(KEY_MASQUERADE_MODE, null)?.let { return Routing.MasqueradeMode.valueOf(it) }
return if (app.pref.getBoolean("service.masquerade", true)) // legacy settings
Routing.MasqueradeMode.Simple else Routing.MasqueradeMode.None
}
set(value) = app.pref.edit().putString(KEY_MASQUERADE_MODE, value.name).apply()
}
class LocalOnly(caller: Any, downstream: String) : RoutingManager(caller, downstream) {
override fun Routing.configure() {
ipForward() // local only interfaces need to enable ip_forward
forward()
masquerade(masqueradeMode)
commit(true)
}
}
var routing: Routing? = null
init {
app.onPreCleanRoutings[this] = { routing?.stop() }
app.onRoutingsCleaned[this] = { initRouting() }
}
fun initRouting() = try {
routing = Routing(caller, downstream).apply {
try {
configure()
} catch (e: Exception) {
revert()
throw e
}
}
true
} catch (e: Exception) {
SmartSnackbar.make(e).show()
Timber.w(e)
routing = null
false
}
protected abstract fun Routing.configure()
fun stop() {
app.onPreCleanRoutings -= this
app.onRoutingsCleaned -= this
routing?.revert()
}
}