Suppress links for bogon IPs

ipinfo.io does not tell a lot of information about bogon IP addresses, so let us just not show any links at all.
This commit is contained in:
Mygod
2019-01-26 21:57:58 +08:00
parent d4208affbb
commit 4918dfc856
2 changed files with 12 additions and 13 deletions

View File

@@ -55,7 +55,7 @@ open class Client(val mac: Long, val iface: String) {
SpannableStringBuilder().apply { SpannableStringBuilder().apply {
if (!record?.nickname.isNullOrEmpty()) appendln(macIface) if (!record?.nickname.isNullOrEmpty()) appendln(macIface)
ip.entries.forEach { (ip, state) -> ip.entries.forEach { (ip, state) ->
append(makeIpSpan(ip.hostAddress)) append(makeIpSpan(ip))
appendln(app.getText(when (state) { appendln(app.getText(when (state) {
IpNeighbour.State.INCOMPLETE -> R.string.connected_state_incomplete IpNeighbour.State.INCOMPLETE -> R.string.connected_state_incomplete
IpNeighbour.State.VALID -> R.string.connected_state_valid IpNeighbour.State.VALID -> R.string.connected_state_valid

View File

@@ -50,25 +50,24 @@ fun setVisibility(view: View, value: Boolean) {
view.isVisible = value view.isVisible = value
} }
fun makeIpSpan(ip: String) = SpannableString(ip).apply { fun makeIpSpan(ip: InetAddress) = ip.hostAddress.let {
if (app.hasTouch) { // exclude all bogon IP addresses supported by Android APIs
val filteredIp = ip.split('%', limit = 2).first() if (app.hasTouch && !(ip.isMulticastAddress || ip.isAnyLocalAddress || ip.isLoopbackAddress ||
setSpan(CustomTabsUrlSpan("https://ipinfo.io/$filteredIp"), 0, filteredIp.length, ip.isLinkLocalAddress || ip.isSiteLocalAddress || ip.isMCGlobal || ip.isMCNodeLocal ||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) ip.isMCLinkLocal || ip.isMCSiteLocal || ip.isMCOrgLocal)) SpannableString(it).apply {
} setSpan(CustomTabsUrlSpan("https://ipinfo.io/$it"), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
} } else it
fun makeMacSpan(mac: String) = SpannableString(mac).apply {
if (app.hasTouch) {
setSpan(CustomTabsUrlSpan("https://macvendors.co/results/$mac"), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
} }
fun makeMacSpan(mac: String) = if (app.hasTouch) SpannableString(mac).apply {
setSpan(CustomTabsUrlSpan("https://macvendors.co/results/$mac"), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
} else mac
fun NetworkInterface.formatAddresses() = SpannableStringBuilder().apply { fun NetworkInterface.formatAddresses() = SpannableStringBuilder().apply {
try { try {
hardwareAddress?.apply { appendln(makeMacSpan(asIterable().macToString())) } hardwareAddress?.apply { appendln(makeMacSpan(asIterable().macToString())) }
} catch (_: SocketException) { } } catch (_: SocketException) { }
for (address in interfaceAddresses) { for (address in interfaceAddresses) {
append(makeIpSpan(address.address.hostAddress)) append(makeIpSpan(address.address))
appendln("/${address.networkPrefixLength}") appendln("/${address.networkPrefixLength}")
} }
}.trimEnd() }.trimEnd()