diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt index feb76fa6..b53e45f8 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyInterfaceManager.kt @@ -6,10 +6,10 @@ import be.mygod.vpnhotspot.net.Routing import be.mygod.vpnhotspot.net.UpstreamMonitor import com.crashlytics.android.Crashlytics import java.net.InetAddress +import java.net.InterfaceAddress import java.net.SocketException -class LocalOnlyInterfaceManager(val downstream: String, private val owner: InetAddress? = null) : - UpstreamMonitor.Callback { +class LocalOnlyInterfaceManager(val downstream: String) : UpstreamMonitor.Callback { private var routing: Routing? = null private var dns = emptyList() @@ -20,7 +20,7 @@ class LocalOnlyInterfaceManager(val downstream: String, private val owner: InetA override fun onAvailable(ifname: String, dns: List) { val routing = routing - initRouting(ifname, if (routing == null) owner else { + initRouting(ifname, if (routing == null) null else { routing.stop() routing.hostAddress }, dns) @@ -37,7 +37,7 @@ class LocalOnlyInterfaceManager(val downstream: String, private val owner: InetA initRouting(routing.upstream, routing.hostAddress, dns) } - private fun initRouting(upstream: String? = null, owner: InetAddress? = this.owner, + private fun initRouting(upstream: String? = null, owner: InterfaceAddress? = null, dns: List = this.dns) { try { val routing = Routing(upstream, downstream, owner) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index f6b5d597..acfc55e1 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -226,15 +226,15 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere } else if (routingManager != null) { this.group = group showNotification(group) - } else doStart(group, info.groupOwnerAddress) + } else doStart(group) } /** * startService Step 3 */ - private fun doStart(group: WifiP2pGroup, ownerAddress: InetAddress? = null) { + private fun doStart(group: WifiP2pGroup) { this.group = group check(routingManager == null) - routingManager = LocalOnlyInterfaceManager(group.`interface`!!, ownerAddress) + routingManager = LocalOnlyInterfaceManager(group.`interface`!!) status = Status.ACTIVE showNotification(group) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt index 7a025baf..a1b60706 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt @@ -5,13 +5,10 @@ import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.util.debugLog import be.mygod.vpnhotspot.util.noisySu -import java.net.Inet4Address -import java.net.InetAddress -import java.net.NetworkInterface -import java.net.SocketException +import java.net.* import java.util.* -class Routing(val upstream: String?, private val downstream: String, ownerAddress: InetAddress? = null) { +class Routing(val upstream: String?, private val downstream: String, ownerAddress: InterfaceAddress? = null) { companion object { /** * -w is not supported on 7.1-. @@ -37,8 +34,8 @@ class Routing(val upstream: String?, private val downstream: String, ownerAddres override val message: String get() = app.getString(R.string.exception_interface_not_found) } - val hostAddress = ownerAddress ?: NetworkInterface.getByName(downstream)?.inetAddresses?.asSequence() - ?.singleOrNull { it is Inet4Address } ?: throw InterfaceNotFoundException() + val hostAddress = ownerAddress ?: NetworkInterface.getByName(downstream)?.interfaceAddresses?.asSequence() + ?.singleOrNull { it.address is Inet4Address } ?: throw InterfaceNotFoundException() private val startScript = LinkedList() private val stopScript = LinkedList() var started = false @@ -91,15 +88,16 @@ class Routing(val upstream: String?, private val downstream: String, ownerAddres } fun masquerade(strict: Boolean = true): Routing { + val hostSubnet = "${hostAddress.address.hostAddress}/${hostAddress.networkPrefixLength}" startScript.add("quiet $IPTABLES -t nat -N vpnhotspot_masquerade 2>/dev/null") // note: specifying -i wouldn't work for POSTROUTING if (strict) { check(upstream != null) - startScript.add("$IPTABLES -t nat -A vpnhotspot_masquerade -o $upstream -j MASQUERADE") - stopScript.addFirst("$IPTABLES -t nat -D vpnhotspot_masquerade -o $upstream -j MASQUERADE") + startScript.add("$IPTABLES -t nat -A vpnhotspot_masquerade -s $hostSubnet -o $upstream -j MASQUERADE") + stopScript.addFirst("$IPTABLES -t nat -D vpnhotspot_masquerade -s $hostSubnet -o $upstream -j MASQUERADE") } else { - startScript.add("$IPTABLES -t nat -A vpnhotspot_masquerade -j MASQUERADE") - stopScript.addFirst("$IPTABLES -t nat -D vpnhotspot_masquerade -j MASQUERADE") + startScript.add("$IPTABLES -t nat -A vpnhotspot_masquerade -s $hostSubnet -j MASQUERADE") + stopScript.addFirst("$IPTABLES -t nat -D vpnhotspot_masquerade -s $hostSubnet -j MASQUERADE") } startScript.add("$IPTABLES -t nat -I POSTROUTING -j vpnhotspot_masquerade") stopScript.addFirst("$IPTABLES -t nat -D POSTROUTING -j vpnhotspot_masquerade") @@ -107,7 +105,7 @@ class Routing(val upstream: String?, private val downstream: String, ownerAddres } fun dnsRedirect(dnses: List): Routing { - val hostAddress = hostAddress.hostAddress + val hostAddress = hostAddress.address.hostAddress val dns = dnses.firstOrNull { it is Inet4Address }?.hostAddress ?: app.dns debugLog("Routing", "Using $dns from ($dnses)") startScript.add("$IPTABLES -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")