Various bugfix for Wi-Fi direct mode

This commit is contained in:
Mygod
2018-01-05 01:33:03 +08:00
parent 7e30ddf26d
commit 62449a98ea
4 changed files with 37 additions and 26 deletions

View File

@@ -20,7 +20,7 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
class InterfaceNotFoundException : IOException()
val hostAddress: String
val hostAddress: InetAddress
private val subnetPrefixLength: Short
private val startScript = LinkedList<String>()
private val stopScript = LinkedList<String>()
@@ -28,19 +28,28 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
val address = NetworkInterface.getByName(downstream)?.interfaceAddresses
?.singleOrNull { if (ownerAddress == null) it.address is Inet4Address else it.address == ownerAddress }
?: throw InterfaceNotFoundException()
hostAddress = address.address.hostAddress
hostAddress = address.address
subnetPrefixLength = address.networkPrefixLength
}
fun p2pRule(): Routing {
// clear suffix bits
val address = hostAddress.address
var done = subnetPrefixLength.toInt()
while (done < address.size shl 3) {
val index = done shr 3
address[index] = (address[index].toInt() and (0x7f00 shr (done and 7))).toByte()
done = (index + 1) shl 3
}
val hostAddress = InetAddress.getByAddress(address).hostAddress
startScript.add("echo 1 >/proc/sys/net/ipv4/ip_forward") // Wi-Fi direct doesn't enable ip_forward
startScript.add("ip route add default dev $upstream scope link table 62")
startScript.add("ip route add $hostAddress/$subnetPrefixLength dev $downstream scope link table 62")
startScript.add("ip route add broadcast 255.255.255.255 dev $downstream scope link table 62")
startScript.add("ip rule add iif $downstream lookup 62")
stopScript.addFirst("ip route del default dev $upstream scope link table 62")
stopScript.addFirst("ip route del $hostAddress/$subnetPrefixLength dev $downstream scope link table 62")
stopScript.addFirst("ip route del broadcast 255.255.255.255 dev $downstream scope link table 62")
// removing each rule may fail if downstream is already removed
stopScript.addFirst("ip route flush table 62")
stopScript.addFirst("ip rule del iif $downstream lookup 62")
return this
}
@@ -65,6 +74,7 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
}
fun dnsRedirect(dns: String): Routing {
val hostAddress = hostAddress.hostAddress
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
stopScript.addFirst("iptables -t nat -D PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")