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

@@ -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()