From 4918dfc85671b8be6c886fa35324e0f0b2ede71f Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 26 Jan 2019 21:57:58 +0800 Subject: [PATCH] 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. --- .../java/be/mygod/vpnhotspot/client/Client.kt | 2 +- .../java/be/mygod/vpnhotspot/util/Utils.kt | 23 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt index 01dd121a..f1fa1301 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt @@ -55,7 +55,7 @@ open class Client(val mac: Long, val iface: String) { SpannableStringBuilder().apply { if (!record?.nickname.isNullOrEmpty()) appendln(macIface) ip.entries.forEach { (ip, state) -> - append(makeIpSpan(ip.hostAddress)) + append(makeIpSpan(ip)) appendln(app.getText(when (state) { IpNeighbour.State.INCOMPLETE -> R.string.connected_state_incomplete IpNeighbour.State.VALID -> R.string.connected_state_valid diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index 43f27df0..bf084bac 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -50,25 +50,24 @@ fun setVisibility(view: View, value: Boolean) { view.isVisible = value } -fun makeIpSpan(ip: String) = SpannableString(ip).apply { - if (app.hasTouch) { - val filteredIp = ip.split('%', limit = 2).first() - setSpan(CustomTabsUrlSpan("https://ipinfo.io/$filteredIp"), 0, filteredIp.length, - Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) - } -} -fun makeMacSpan(mac: String) = SpannableString(mac).apply { - if (app.hasTouch) { - setSpan(CustomTabsUrlSpan("https://macvendors.co/results/$mac"), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) - } +fun makeIpSpan(ip: InetAddress) = ip.hostAddress.let { + // exclude all bogon IP addresses supported by Android APIs + if (app.hasTouch && !(ip.isMulticastAddress || ip.isAnyLocalAddress || ip.isLoopbackAddress || + ip.isLinkLocalAddress || ip.isSiteLocalAddress || ip.isMCGlobal || ip.isMCNodeLocal || + 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) = 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 { try { hardwareAddress?.apply { appendln(makeMacSpan(asIterable().macToString())) } } catch (_: SocketException) { } for (address in interfaceAddresses) { - append(makeIpSpan(address.address.hostAddress)) + append(makeIpSpan(address.address)) appendln("/${address.networkPrefixLength}") } }.trimEnd()