Support specifying network interface

Fix #15.
This commit is contained in:
Mygod
2018-06-02 07:29:46 +08:00
parent 8e335fec1b
commit da9bf4867e
14 changed files with 326 additions and 173 deletions

View File

@@ -3,28 +3,28 @@ package be.mygod.vpnhotspot
import android.widget.Toast
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.VpnMonitor
import be.mygod.vpnhotspot.net.UpstreamMonitor
import java.net.InetAddress
import java.net.SocketException
class LocalOnlyInterfaceManager(val downstream: String, private val owner: InetAddress? = null) : VpnMonitor.Callback {
class LocalOnlyInterfaceManager(val downstream: String, private val owner: InetAddress? = null) :
UpstreamMonitor.Callback {
private var routing: Routing? = null
private var dns = emptyList<InetAddress>()
init {
app.cleanRoutings[this] = this::clean
VpnMonitor.registerCallback(this) { initRouting() }
UpstreamMonitor.registerCallback(this) { initRouting() }
}
override fun onAvailable(ifname: String, dns: List<InetAddress>) {
val routing = routing
initRouting(ifname, if (routing == null) owner else {
routing.stop()
check(routing.upstream == null)
routing.hostAddress
}, dns)
}
override fun onLost(ifname: String) {
override fun onLost() {
val routing = routing ?: return
if (!routing.stop()) app.toast(R.string.noisy_su_failure)
initRouting(null, routing.hostAddress, emptyList())
@@ -54,7 +54,7 @@ class LocalOnlyInterfaceManager(val downstream: String, private val owner: InetA
}
fun stop() {
VpnMonitor.unregisterCallback(this)
UpstreamMonitor.unregisterCallback(this)
app.cleanRoutings -= this
if (routing?.stop() == false) app.toast(R.string.noisy_su_failure)
}