Add light ip neigh monitoring mode to reduce root requests
This commit is contained in:
@@ -59,7 +59,7 @@ class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callb
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
app.registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
|
app.registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
|
||||||
IpNeighbourMonitor.registerCallback(this)
|
IpNeighbourMonitor.registerCallback(this, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
|
|||||||
@@ -37,12 +37,35 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddr
|
|||||||
private val devFallback = "^if(\\d+)\$".toRegex()
|
private val devFallback = "^if(\\d+)\$".toRegex()
|
||||||
private fun checkLladdrNotLoopback(lladdr: String) = if (lladdr == "00:00:00:00:00:00") "" else lladdr
|
private fun checkLladdrNotLoopback(lladdr: String) = if (lladdr == "00:00:00:00:00:00") "" else lladdr
|
||||||
|
|
||||||
fun parse(line: String): List<IpNeighbour> {
|
private fun populateList(base: IpNeighbour): List<IpNeighbour> {
|
||||||
|
val devParser = devFallback.matchEntire(base.dev)
|
||||||
|
if (devParser != null) try {
|
||||||
|
val index = devParser.groupValues[1].toInt()
|
||||||
|
val iface = NetworkInterface.getByIndex(index)
|
||||||
|
if (iface == null) Timber.w("Failed to find network interface #$index")
|
||||||
|
else return listOf(base.copy(dev = iface.name), base)
|
||||||
|
} catch (_: SocketException) { }
|
||||||
|
return listOf(base)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun parse(line: String, fullMode: Boolean): List<IpNeighbour> {
|
||||||
if (line.isBlank()) return emptyList()
|
if (line.isBlank()) return emptyList()
|
||||||
return try {
|
return try {
|
||||||
val match = parser.matchEntire(line)!!
|
val match = parser.matchEntire(line)!!
|
||||||
val ip = parseNumericAddress(match.groupValues[2]) // by regex, ip is non-empty
|
val ip = parseNumericAddress(match.groupValues[2]) // by regex, ip is non-empty
|
||||||
val dev = match.groupValues[3] // by regex, dev is non-empty as well
|
val dev = match.groupValues[3] // by regex, dev is non-empty as well
|
||||||
|
val state = if (match.groupValues[1].isNotEmpty()) State.DELETING else
|
||||||
|
when (match.groupValues[7]) {
|
||||||
|
"", "INCOMPLETE" -> State.INCOMPLETE
|
||||||
|
"REACHABLE", "DELAY", "STALE", "PROBE", "PERMANENT" -> State.VALID
|
||||||
|
"FAILED" -> {
|
||||||
|
if (!fullMode) return populateList(IpNeighbour(ip, dev, MacAddressCompat.ALL_ZEROS_ADDRESS,
|
||||||
|
State.DELETING)) // skip parsing lladdr to avoid requesting root
|
||||||
|
State.FAILED
|
||||||
|
}
|
||||||
|
"NOARP" -> return emptyList() // skip
|
||||||
|
else -> throw IllegalArgumentException("Unknown state encountered: ${match.groupValues[7]}")
|
||||||
|
}
|
||||||
var lladdr = checkLladdrNotLoopback(match.groupValues[5])
|
var lladdr = checkLladdrNotLoopback(match.groupValues[5])
|
||||||
// use ARP as fallback for IPv4
|
// use ARP as fallback for IPv4
|
||||||
if (lladdr.isEmpty()) lladdr = checkLladdrNotLoopback(arp()
|
if (lladdr.isEmpty()) lladdr = checkLladdrNotLoopback(arp()
|
||||||
@@ -50,14 +73,6 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddr
|
|||||||
.filter { parseNumericAddress(it[ARP_IP_ADDRESS]) == ip && it[ARP_DEVICE] == dev }
|
.filter { parseNumericAddress(it[ARP_IP_ADDRESS]) == ip && it[ARP_DEVICE] == dev }
|
||||||
.map { it[ARP_HW_ADDRESS] }
|
.map { it[ARP_HW_ADDRESS] }
|
||||||
.singleOrNull() ?: "")
|
.singleOrNull() ?: "")
|
||||||
val state = if (match.groupValues[1].isNotEmpty()) State.DELETING else
|
|
||||||
when (match.groupValues[7]) {
|
|
||||||
"", "INCOMPLETE" -> State.INCOMPLETE
|
|
||||||
"REACHABLE", "DELAY", "STALE", "PROBE", "PERMANENT" -> State.VALID
|
|
||||||
"FAILED" -> State.FAILED
|
|
||||||
"NOARP" -> return emptyList() // skip
|
|
||||||
else -> throw IllegalArgumentException("Unknown state encountered: ${match.groupValues[7]}")
|
|
||||||
}
|
|
||||||
val mac = try {
|
val mac = try {
|
||||||
MacAddressCompat.fromString(lladdr)
|
MacAddressCompat.fromString(lladdr)
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
@@ -66,15 +81,7 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddr
|
|||||||
if (state != State.DELETING) Timber.w(IOException("Failed to find MAC address for $line", e))
|
if (state != State.DELETING) Timber.w(IOException("Failed to find MAC address for $line", e))
|
||||||
MacAddressCompat.ALL_ZEROS_ADDRESS
|
MacAddressCompat.ALL_ZEROS_ADDRESS
|
||||||
}
|
}
|
||||||
val result = IpNeighbour(ip, dev, mac, state)
|
populateList(IpNeighbour(ip, dev, mac, state))
|
||||||
val devParser = devFallback.matchEntire(dev)
|
|
||||||
if (devParser != null) try {
|
|
||||||
val index = devParser.groupValues[1].toInt()
|
|
||||||
val iface = NetworkInterface.getByIndex(index)
|
|
||||||
if (iface == null) Timber.w("Failed to find network interface #$index")
|
|
||||||
else return listOf(IpNeighbour(ip, iface.name, mac, state), result)
|
|
||||||
} catch (_: SocketException) { }
|
|
||||||
listOf(result)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.w(IllegalArgumentException("Unable to parse line: $line", e))
|
Timber.w(IllegalArgumentException("Unable to parse line: $line", e))
|
||||||
emptyList()
|
emptyList()
|
||||||
@@ -114,3 +121,8 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class IpDev(val ip: InetAddress, val dev: String) {
|
||||||
|
override fun toString() = "$ip%$dev"
|
||||||
|
}
|
||||||
|
fun IpDev(neighbour: IpNeighbour) = IpDev(neighbour.ip, neighbour.dev)
|
||||||
|
|||||||
@@ -348,7 +348,7 @@ class Routing(private val caller: Any, private val downstream: String,
|
|||||||
ipRule("unreachable", RULE_PRIORITY_UPSTREAM_DISABLE_SYSTEM)
|
ipRule("unreachable", RULE_PRIORITY_UPSTREAM_DISABLE_SYSTEM)
|
||||||
}
|
}
|
||||||
UpstreamMonitor.registerCallback(upstream)
|
UpstreamMonitor.registerCallback(upstream)
|
||||||
IpNeighbourMonitor.registerCallback(this)
|
IpNeighbourMonitor.registerCallback(this, true)
|
||||||
}
|
}
|
||||||
fun revert() {
|
fun revert() {
|
||||||
stop()
|
stop()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package be.mygod.vpnhotspot.net.monitor
|
package be.mygod.vpnhotspot.net.monitor
|
||||||
|
|
||||||
|
import be.mygod.vpnhotspot.net.IpDev
|
||||||
import be.mygod.vpnhotspot.net.IpNeighbour
|
import be.mygod.vpnhotspot.net.IpNeighbour
|
||||||
import kotlinx.collections.immutable.PersistentMap
|
import kotlinx.collections.immutable.PersistentMap
|
||||||
import kotlinx.collections.immutable.persistentMapOf
|
import kotlinx.collections.immutable.persistentMapOf
|
||||||
@@ -7,15 +8,22 @@ import kotlinx.coroutines.GlobalScope
|
|||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.channels.actor
|
import kotlinx.coroutines.channels.actor
|
||||||
import kotlinx.coroutines.channels.sendBlocking
|
import kotlinx.coroutines.channels.sendBlocking
|
||||||
import java.net.InetAddress
|
|
||||||
|
|
||||||
class IpNeighbourMonitor private constructor() : IpMonitor() {
|
class IpNeighbourMonitor private constructor() : IpMonitor() {
|
||||||
companion object {
|
companion object {
|
||||||
private val callbacks = mutableSetOf<Callback>()
|
private val callbacks = mutableMapOf<Callback, Boolean>()
|
||||||
var instance: IpNeighbourMonitor? = null
|
var instance: IpNeighbourMonitor? = null
|
||||||
|
var fullMode = false
|
||||||
|
|
||||||
fun registerCallback(callback: Callback) = synchronized(callbacks) {
|
/**
|
||||||
if (!callbacks.add(callback)) return@synchronized null
|
* @param full Whether the failed entries should also be parsed.
|
||||||
|
* In this case it is more likely to trigger root request on API 29+.
|
||||||
|
* However, even in light mode, caller should still filter out failed entries in
|
||||||
|
* [Callback.onIpNeighbourAvailable] in case the full mode was requested by other callers.
|
||||||
|
*/
|
||||||
|
fun registerCallback(callback: Callback, full: Boolean = false) = synchronized(callbacks) {
|
||||||
|
if (callbacks.put(callback, full) == full) return@synchronized null
|
||||||
|
fullMode = full || callbacks.any { it.value }
|
||||||
var monitor = instance
|
var monitor = instance
|
||||||
if (monitor == null) {
|
if (monitor == null) {
|
||||||
monitor = IpNeighbourMonitor()
|
monitor = IpNeighbourMonitor()
|
||||||
@@ -25,7 +33,7 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
|
|||||||
} else monitor.neighbours.values
|
} else monitor.neighbours.values
|
||||||
}?.let { callback.onIpNeighbourAvailable(it) }
|
}?.let { callback.onIpNeighbourAvailable(it) }
|
||||||
fun unregisterCallback(callback: Callback) = synchronized(callbacks) {
|
fun unregisterCallback(callback: Callback) = synchronized(callbacks) {
|
||||||
if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return@synchronized
|
if (callbacks.remove(callback) == null || callbacks.isNotEmpty()) return@synchronized
|
||||||
instance?.destroy()
|
instance?.destroy()
|
||||||
instance = null
|
instance = null
|
||||||
}
|
}
|
||||||
@@ -35,30 +43,30 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
|
|||||||
fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>)
|
fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val aggregator = GlobalScope.actor<PersistentMap<InetAddress, IpNeighbour>>(capacity = Channel.CONFLATED) {
|
private val aggregator = GlobalScope.actor<PersistentMap<IpDev, IpNeighbour>>(capacity = Channel.CONFLATED) {
|
||||||
for (value in channel) {
|
for (value in channel) {
|
||||||
val neighbours = value.values
|
val neighbours = value.values
|
||||||
synchronized(callbacks) { for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours) }
|
synchronized(callbacks) { for ((callback, _) in callbacks) callback.onIpNeighbourAvailable(neighbours) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private var neighbours = persistentMapOf<InetAddress, IpNeighbour>()
|
private var neighbours = persistentMapOf<IpDev, IpNeighbour>()
|
||||||
|
|
||||||
override val monitoredObject: String get() = "neigh"
|
override val monitoredObject: String get() = "neigh"
|
||||||
|
|
||||||
override fun processLine(line: String) {
|
override fun processLine(line: String) {
|
||||||
val old = neighbours
|
val old = neighbours
|
||||||
for (neighbour in IpNeighbour.parse(line)) neighbours = when (neighbour.state) {
|
for (neighbour in IpNeighbour.parse(line, fullMode)) neighbours = when (neighbour.state) {
|
||||||
IpNeighbour.State.DELETING -> neighbours.remove(neighbour.ip)
|
IpNeighbour.State.DELETING -> neighbours.remove(IpDev(neighbour))
|
||||||
else -> neighbours.put(neighbour.ip, neighbour)
|
else -> neighbours.put(IpDev(neighbour), neighbour)
|
||||||
}
|
}
|
||||||
if (neighbours != old) aggregator.sendBlocking(neighbours)
|
if (neighbours != old) aggregator.sendBlocking(neighbours)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun processLines(lines: Sequence<String>) {
|
override fun processLines(lines: Sequence<String>) {
|
||||||
neighbours = lines
|
neighbours = lines
|
||||||
.flatMap { IpNeighbour.parse(it).asSequence() }
|
.flatMap { IpNeighbour.parse(it, fullMode).asSequence() }
|
||||||
.filter { it.state != IpNeighbour.State.DELETING } // skip entries without lladdr
|
.filter { it.state != IpNeighbour.State.DELETING } // skip entries without lladdr
|
||||||
.associateByTo(persistentMapOf<InetAddress, IpNeighbour>().builder()) { it.ip }
|
.associateByTo(persistentMapOf<IpDev, IpNeighbour>().builder()) { IpDev(it) }
|
||||||
.build()
|
.build()
|
||||||
aggregator.sendBlocking(neighbours)
|
aggregator.sendBlocking(neighbours)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package be.mygod.vpnhotspot.net.monitor
|
|||||||
|
|
||||||
import androidx.collection.LongSparseArray
|
import androidx.collection.LongSparseArray
|
||||||
import androidx.collection.set
|
import androidx.collection.set
|
||||||
|
import be.mygod.vpnhotspot.net.IpDev
|
||||||
import be.mygod.vpnhotspot.net.MacAddressCompat
|
import be.mygod.vpnhotspot.net.MacAddressCompat
|
||||||
import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
|
import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
|
||||||
import be.mygod.vpnhotspot.room.AppDatabase
|
import be.mygod.vpnhotspot.room.AppDatabase
|
||||||
@@ -19,22 +20,24 @@ object TrafficRecorder {
|
|||||||
private const val ANYWHERE = "0.0.0.0/0"
|
private const val ANYWHERE = "0.0.0.0/0"
|
||||||
|
|
||||||
private var lastUpdate = 0L
|
private var lastUpdate = 0L
|
||||||
private val records = mutableMapOf<Pair<InetAddress, String>, TrafficRecord>()
|
private val records = mutableMapOf<IpDev, TrafficRecord>()
|
||||||
val foregroundListeners = Event2<Collection<TrafficRecord>, LongSparseArray<TrafficRecord>>()
|
val foregroundListeners = Event2<Collection<TrafficRecord>, LongSparseArray<TrafficRecord>>()
|
||||||
|
|
||||||
fun register(ip: InetAddress, downstream: String, mac: MacAddressCompat) {
|
fun register(ip: InetAddress, downstream: String, mac: MacAddressCompat) {
|
||||||
val record = TrafficRecord(mac = mac.addr, ip = ip, downstream = downstream)
|
val record = TrafficRecord(mac = mac.addr, ip = ip, downstream = downstream)
|
||||||
AppDatabase.instance.trafficRecordDao.insert(record)
|
AppDatabase.instance.trafficRecordDao.insert(record)
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
Timber.d("Registering $ip%$downstream")
|
val key = IpDev(ip, downstream)
|
||||||
check(records.putIfAbsent(Pair(ip, downstream), record) == null)
|
Timber.d("Registering $key")
|
||||||
|
check(records.putIfAbsent(key, record) == null)
|
||||||
scheduleUpdateLocked()
|
scheduleUpdateLocked()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun unregister(ip: InetAddress, downstream: String) = synchronized(this) {
|
fun unregister(ip: InetAddress, downstream: String) = synchronized(this) {
|
||||||
update() // flush stats before removing
|
update() // flush stats before removing
|
||||||
Timber.d("Unregistering $ip%$downstream")
|
val key = IpDev(ip, downstream)
|
||||||
if (records.remove(Pair(ip, downstream)) == null) Timber.w("Failed to find traffic record for $ip%$downstream.")
|
Timber.d("Unregistering $key")
|
||||||
|
if (records.remove(key) == null) Timber.w("Failed to find traffic record for $key.")
|
||||||
}
|
}
|
||||||
|
|
||||||
private var updateJob: Job? = null
|
private var updateJob: Job? = null
|
||||||
@@ -81,7 +84,7 @@ object TrafficRecorder {
|
|||||||
check(isReceive != isSend) { "Failed to set up blocking rules, please clean routing rules" }
|
check(isReceive != isSend) { "Failed to set up blocking rules, please clean routing rules" }
|
||||||
val ip = parseNumericAddress(columns[if (isReceive) 8 else 7])
|
val ip = parseNumericAddress(columns[if (isReceive) 8 else 7])
|
||||||
val downstream = columns[if (isReceive) 6 else 5]
|
val downstream = columns[if (isReceive) 6 else 5]
|
||||||
val key = Pair(ip, downstream)
|
val key = IpDev(ip, downstream)
|
||||||
val oldRecord = records[key] ?: continue@loop // assuming they're legacy old rules
|
val oldRecord = records[key] ?: continue@loop // assuming they're legacy old rules
|
||||||
val record = if (oldRecord.id == null) oldRecord else TrafficRecord(
|
val record = if (oldRecord.id == null) oldRecord else TrafficRecord(
|
||||||
timestamp = timestamp,
|
timestamp = timestamp,
|
||||||
|
|||||||
Reference in New Issue
Block a user