Prevent crash when input is invalid
This commit is contained in:
@@ -9,9 +9,15 @@ import be.mygod.vpnhotspot.util.allInterfaceNames
|
|||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.util.regex.PatternSyntaxException
|
||||||
|
|
||||||
class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
|
class InterfaceMonitor(private val ifaceRegex: String) : UpstreamMonitor() {
|
||||||
private val iface = ifaceRegex.toRegex()
|
private val iface = try {
|
||||||
|
ifaceRegex.toRegex()::matches
|
||||||
|
} catch (e: PatternSyntaxException) {
|
||||||
|
Timber.d(e);
|
||||||
|
{ it == ifaceRegex }
|
||||||
|
}
|
||||||
private val request = networkRequestBuilder().apply {
|
private val request = networkRequestBuilder().apply {
|
||||||
removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
|
removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
|
||||||
removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
|
removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
|
||||||
@@ -25,7 +31,7 @@ class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
|
|||||||
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
|
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
|
||||||
override fun onAvailable(network: Network) {
|
override fun onAvailable(network: Network) {
|
||||||
val properties = Services.connectivity.getLinkProperties(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) {
|
synchronized(this@InterfaceMonitor) {
|
||||||
available[network] = properties
|
available[network] = properties
|
||||||
currentNetwork = network
|
currentNetwork = network
|
||||||
@@ -34,7 +40,7 @@ class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLinkPropertiesChanged(network: Network, properties: LinkProperties) {
|
override fun onLinkPropertiesChanged(network: Network, properties: LinkProperties) {
|
||||||
val matched = properties.allInterfaceNames.any(iface::matches)
|
val matched = properties.allInterfaceNames.any(iface)
|
||||||
synchronized(this@InterfaceMonitor) {
|
synchronized(this@InterfaceMonitor) {
|
||||||
if (!matched) {
|
if (!matched) {
|
||||||
if (currentNetwork == network) currentNetwork = null
|
if (currentNetwork == network) currentNetwork = null
|
||||||
@@ -55,7 +61,7 @@ class InterfaceMonitor(ifaceRegex: String) : UpstreamMonitor() {
|
|||||||
if (available.isNotEmpty()) {
|
if (available.isNotEmpty()) {
|
||||||
val next = available.entries.first()
|
val next = available.entries.first()
|
||||||
currentNetwork = next.key
|
currentNetwork = next.key
|
||||||
Timber.d("Switching to ${next.value} for $iface")
|
Timber.d("Switching to ${next.value} for $ifaceRegex")
|
||||||
properties = next.value
|
properties = next.value
|
||||||
} else currentNetwork = null
|
} else currentNetwork = null
|
||||||
callbacks.toList()
|
callbacks.toList()
|
||||||
|
|||||||
Reference in New Issue
Block a user