Only return valid entries in light mode
This commit is contained in:
@@ -18,7 +18,7 @@ abstract class IpNeighbourMonitoringService : Service(), IpNeighbourMonitor.Call
|
|||||||
protected fun updateNotification() {
|
protected fun updateNotification() {
|
||||||
val sizeLookup = neighbours.groupBy { it.dev }.mapValues { (_, neighbours) ->
|
val sizeLookup = neighbours.groupBy { it.dev }.mapValues { (_, neighbours) ->
|
||||||
neighbours
|
neighbours
|
||||||
.filter { it.ip is Inet4Address && it.state != IpNeighbour.State.FAILED }
|
.filter { it.ip is Inet4Address && it.state == IpNeighbour.State.VALID }
|
||||||
.distinctBy { it.lladdr }
|
.distinctBy { it.lladdr }
|
||||||
.size
|
.size
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
|
|||||||
override fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>) {
|
override fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>) {
|
||||||
super.onIpNeighbourAvailable(neighbours)
|
super.onIpNeighbourAvailable(neighbours)
|
||||||
if (Build.VERSION.SDK_INT >= 28) timeoutMonitor?.onClientsChanged(neighbours.none {
|
if (Build.VERSION.SDK_INT >= 28) timeoutMonitor?.onClientsChanged(neighbours.none {
|
||||||
it.ip is Inet4Address && it.state != IpNeighbour.State.FAILED
|
it.ip is Inet4Address && it.state == IpNeighbour.State.VALID
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
|
|||||||
if (Services.p2p != null) ServiceForegroundConnector(this, model, RepeaterService::class)
|
if (Services.p2p != null) ServiceForegroundConnector(this, model, RepeaterService::class)
|
||||||
model.clients.observe(this) { clients ->
|
model.clients.observe(this) { clients ->
|
||||||
val count = clients.count {
|
val count = clients.count {
|
||||||
it.ip.any { (ip, state) -> ip is Inet4Address && state != IpNeighbour.State.FAILED }
|
it.ip.any { (ip, state) -> ip is Inet4Address && state == IpNeighbour.State.VALID }
|
||||||
}
|
}
|
||||||
if (count > 0) binding.navigation.getOrCreateBadge(R.id.navigation_clients).apply {
|
if (count > 0) binding.navigation.getOrCreateBadge(R.id.navigation_clients).apply {
|
||||||
backgroundColor = ContextCompat.getColor(this@MainActivity, R.color.colorSecondary)
|
backgroundColor = ContextCompat.getColor(this@MainActivity, R.color.colorSecondary)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ abstract class IpNeighbourMonitoringTileService : KillableTileService(), IpNeigh
|
|||||||
|
|
||||||
protected fun Tile.subtitleDevices(filter: (String) -> Boolean) {
|
protected fun Tile.subtitleDevices(filter: (String) -> Boolean) {
|
||||||
val size = neighbours
|
val size = neighbours
|
||||||
.filter { it.ip is Inet4Address && it.state != IpNeighbour.State.FAILED && filter(it.dev) }
|
.filter { it.ip is Inet4Address && it.state == IpNeighbour.State.VALID && filter(it.dev) }
|
||||||
.distinctBy { it.lladdr }
|
.distinctBy { it.lladdr }
|
||||||
.size
|
.size
|
||||||
if (size > 0) subtitle(resources.getQuantityString(
|
if (size > 0) subtitle(resources.getQuantityString(
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import timber.log.Timber
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileNotFoundException
|
import java.io.FileNotFoundException
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.net.Inet4Address
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.net.NetworkInterface
|
import java.net.NetworkInterface
|
||||||
import java.net.SocketException
|
import java.net.SocketException
|
||||||
@@ -35,7 +36,6 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddr
|
|||||||
* Source: https://android.googlesource.com/platform/external/iproute2/+/4b9e917/lib/ll_map.c#152
|
* Source: https://android.googlesource.com/platform/external/iproute2/+/4b9e917/lib/ll_map.c#152
|
||||||
*/
|
*/
|
||||||
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 populateList(base: IpNeighbour): List<IpNeighbour> {
|
private fun populateList(base: IpNeighbour): List<IpNeighbour> {
|
||||||
val devParser = devFallback.matchEntire(base.dev)
|
val devParser = devFallback.matchEntire(base.dev)
|
||||||
@@ -49,42 +49,43 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddr
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun parse(line: String, fullMode: Boolean): List<IpNeighbour> {
|
fun parse(line: String, fullMode: Boolean): List<IpNeighbour> {
|
||||||
if (line.isBlank()) return emptyList()
|
return if (line.isBlank()) emptyList() else 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
|
val state = if (match.groupValues[1].isNotEmpty()) State.DELETING else when (match.groupValues[7]) {
|
||||||
when (match.groupValues[7]) {
|
|
||||||
"", "INCOMPLETE" -> State.INCOMPLETE
|
"", "INCOMPLETE" -> State.INCOMPLETE
|
||||||
"REACHABLE", "DELAY", "STALE", "PROBE", "PERMANENT" -> State.VALID
|
"REACHABLE", "DELAY", "STALE", "PROBE", "PERMANENT" -> State.VALID
|
||||||
"FAILED" -> {
|
"FAILED" -> State.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
|
"NOARP" -> return emptyList() // skip
|
||||||
else -> throw IllegalArgumentException("Unknown state encountered: ${match.groupValues[7]}")
|
else -> throw IllegalArgumentException("Unknown state encountered: ${match.groupValues[7]}")
|
||||||
}
|
}
|
||||||
var lladdr = checkLladdrNotLoopback(match.groupValues[5])
|
var lladdr = MacAddressCompat.ALL_ZEROS_ADDRESS
|
||||||
// use ARP as fallback for IPv4
|
if (!fullMode && state != State.VALID) {
|
||||||
if (lladdr.isEmpty()) lladdr = checkLladdrNotLoopback(arp()
|
// skip parsing lladdr to avoid requesting root
|
||||||
|
return populateList(IpNeighbour(ip, dev, lladdr, State.DELETING))
|
||||||
|
}
|
||||||
|
if (match.groups[4] != null) try {
|
||||||
|
lladdr = MacAddressCompat.fromString(match.groupValues[5])
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
Timber.w(IOException("Failed to find MAC address for $line", e))
|
||||||
|
}
|
||||||
|
// use ARP as fallback for IPv4, except for INCOMPLETE which by definition does not have arp entry,
|
||||||
|
// or for DELETING, which we do not care about MAC not present
|
||||||
|
if (ip is Inet4Address && lladdr == MacAddressCompat.ALL_ZEROS_ADDRESS && state != State.INCOMPLETE &&
|
||||||
|
state != State.DELETING) try {
|
||||||
|
lladdr = MacAddressCompat.fromString(arp()
|
||||||
.asSequence()
|
.asSequence()
|
||||||
.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() ?: throw IllegalArgumentException("singleOrNull"))
|
||||||
val mac = try {
|
|
||||||
MacAddressCompat.fromString(lladdr)
|
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
if (match.groups[4] == null) return emptyList()
|
Timber.w(e)
|
||||||
// for DELETING, we only care about IP address and do not care if MAC is not present
|
|
||||||
if (state != State.DELETING) Timber.w(IOException("Failed to find MAC address for $line", e))
|
|
||||||
MacAddressCompat.ALL_ZEROS_ADDRESS
|
|
||||||
}
|
}
|
||||||
populateList(IpNeighbour(ip, dev, mac, state))
|
populateList(IpNeighbour(ip, dev, lladdr, state))
|
||||||
} 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<IpNeighbour>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
|
|||||||
var fullMode = false
|
var fullMode = false
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param full Whether the failed entries should also be parsed.
|
* @param full Whether the invalid entries should also be parsed.
|
||||||
* In this case it is more likely to trigger root request on API 29+.
|
* 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
|
* However, even in light mode, caller should still filter out invalid entries in
|
||||||
* [Callback.onIpNeighbourAvailable] in case the full mode was requested by other callers.
|
* [Callback.onIpNeighbourAvailable] in case the full mode was requested by other callers.
|
||||||
*/
|
*/
|
||||||
fun registerCallback(callback: Callback, full: Boolean = false) = synchronized(callbacks) {
|
fun registerCallback(callback: Callback, full: Boolean = false) = synchronized(callbacks) {
|
||||||
|
|||||||
Reference in New Issue
Block a user