Refine hostAddress

This commit is contained in:
Mygod
2018-01-05 08:13:29 +08:00
parent 2a235c89af
commit dd25fe6654
2 changed files with 8 additions and 14 deletions

View File

@@ -87,7 +87,7 @@ class MainActivity : AppCompatActivity(), ServiceConnection, Toolbar.OnMenuItemC
} }
holder.binding.device = device holder.binding.device = device
holder.binding.ipAddress = when (position) { holder.binding.ipAddress = when (position) {
0 -> binder?.service?.routing?.hostAddress?.hostAddress 0 -> binder?.service?.routing?.hostAddress?.address?.hostAddress
else -> arpCache[device?.deviceAddress] else -> arpCache[device?.deviceAddress]
} }
holder.binding.executePendingBindings() holder.binding.executePendingBindings()

View File

@@ -20,31 +20,25 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
class InterfaceNotFoundException : IOException() class InterfaceNotFoundException : IOException()
val hostAddress: InetAddress val hostAddress = NetworkInterface.getByName(downstream)?.interfaceAddresses
private val subnetPrefixLength: Short
private val startScript = LinkedList<String>()
private val stopScript = LinkedList<String>()
init {
val address = NetworkInterface.getByName(downstream)?.interfaceAddresses
?.singleOrNull { if (ownerAddress == null) it.address is Inet4Address else it.address == ownerAddress } ?.singleOrNull { if (ownerAddress == null) it.address is Inet4Address else it.address == ownerAddress }
?: throw InterfaceNotFoundException() ?: throw InterfaceNotFoundException()
hostAddress = address.address private val startScript = LinkedList<String>()
subnetPrefixLength = address.networkPrefixLength private val stopScript = LinkedList<String>()
}
fun p2pRule(): Routing { fun p2pRule(): Routing {
val address = hostAddress.address.address
val subnetPrefixLength = hostAddress.networkPrefixLength
// clear suffix bits // clear suffix bits
val address = hostAddress.address
var done = subnetPrefixLength.toInt() var done = subnetPrefixLength.toInt()
while (done < address.size shl 3) { while (done < address.size shl 3) {
val index = done shr 3 val index = done shr 3
address[index] = (address[index].toInt() and (0x7f00 shr (done and 7))).toByte() address[index] = (address[index].toInt() and (0x7f00 shr (done and 7))).toByte()
done = (index + 1) shl 3 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("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 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 ${InetAddress.getByAddress(address).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 route add broadcast 255.255.255.255 dev $downstream scope link table 62")
startScript.add("ip rule add iif $downstream lookup 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 default dev $upstream scope link table 62")
@@ -74,7 +68,7 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
} }
fun dnsRedirect(dns: String): Routing { fun dnsRedirect(dns: String): Routing {
val hostAddress = hostAddress.hostAddress val hostAddress = hostAddress.address.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 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") 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") stopScript.addFirst("iptables -t nat -D PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")