From 71a854244d83c9867408cf5b98d46e29f9262079 Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 2 Oct 2020 03:47:30 +0800 Subject: [PATCH] Prevent crash when input is invalid --- .../vpnhotspot/net/monitor/InterfaceMonitor.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt index cbf2f4b4..41b17b2b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt @@ -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()