Refrain from using reflection to parse numerical address

This commit is contained in:
Mygod
2019-01-26 01:35:34 +08:00
parent 074252ad1c
commit 72851d4417
4 changed files with 7 additions and 10 deletions

View File

@@ -33,8 +33,8 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: String,
fun parse(line: String): List<IpNeighbour> {
return try {
val match = parser.matchEntire(line)!!
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 ip = parseNumericAddress(match.groupValues[2])!! // by regex, ip is non-empty
val dev = match.groupValues[3] // by regex, dev is non-empty as well
var lladdr = checkLladdrNotLoopback(match.groupValues[5])
// use ARP as fallback
if (lladdr.isEmpty()) lladdr = checkLladdrNotLoopback(arp()

View File

@@ -80,7 +80,7 @@ object TrafficRecorder {
val isReceive = columns[7] == ANYWHERE
val isSend = columns[8] == ANYWHERE
check(isReceive != isSend) // this check might fail when the user performed an upgrade from 1.x
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 key = Pair(ip, downstream)
val oldRecord = records[key] ?: continue@loop // assuming they're legacy old rules

View File

@@ -2,6 +2,8 @@ package be.mygod.vpnhotspot.util
import android.content.*
import android.os.Build
import android.system.Os
import android.system.OsConstants
import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
@@ -52,12 +54,8 @@ fun NetworkInterface.formatAddresses() =
}))
.joinToString("\n")
private val parseNumericAddress by lazy {
InetAddress::class.java.getDeclaredMethod("parseNumericAddress", String::class.java).apply {
isAccessible = true
}
}
fun parseNumericAddress(address: String) = parseNumericAddress.invoke(null, address) as InetAddress
fun parseNumericAddress(address: String?): InetAddress? =
Os.inet_pton(OsConstants.AF_INET, address) ?: Os.inet_pton(OsConstants.AF_INET6, address)
fun Context.stopAndUnbind(connection: ServiceConnection) {
connection.onServiceDisconnected(null)