Prevent crash when input is invalid

This commit is contained in:
Mygod
2020-10-02 03:47:30 +08:00
parent 6256b76f55
commit 71a854244d

View File

@@ -9,9 +9,15 @@ import be.mygod.vpnhotspot.util.allInterfaceNames
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import timber.log.Timber
import java.util.regex.PatternSyntaxException
class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
private val iface = ifaceRegex.toRegex()
class InterfaceMonitor(private val ifaceRegex: String) : UpstreamMonitor() {
private val iface = try {
ifaceRegex.toRegex()::matches
} catch (e: PatternSyntaxException) {
Timber.d(e);
{ it == ifaceRegex }
}
private val request = networkRequestBuilder().apply {
removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
@@ -25,7 +31,7 @@ class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
val properties = Services.connectivity.getLinkProperties(network)
if (properties?.allInterfaceNames?.any(iface::matches) != true) return
if (properties?.allInterfaceNames?.any(iface) != true) return
synchronized(this@InterfaceMonitor) {
available[network] = properties
currentNetwork = network
@@ -34,7 +40,7 @@ class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
}
override fun onLinkPropertiesChanged(network: Network, properties: LinkProperties) {
val matched = properties.allInterfaceNames.any(iface::matches)
val matched = properties.allInterfaceNames.any(iface)
synchronized(this@InterfaceMonitor) {
if (!matched) {
if (currentNetwork == network) currentNetwork = null
@@ -55,7 +61,7 @@ class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
if (available.isNotEmpty()) {
val next = available.entries.first()
currentNetwork = next.key
Timber.d("Switching to ${next.value} for $iface")
Timber.d("Switching to ${next.value} for $ifaceRegex")
properties = next.value
} else currentNetwork = null
callbacks.toList()