parent
d9f0de2dd4
commit
105c753c66
|
@ -19,6 +19,8 @@ import com.zaneschepke.wireguardautotunnel.util.FileReadException
|
||||||
import com.zaneschepke.wireguardautotunnel.util.InvalidFileExtensionException
|
import com.zaneschepke.wireguardautotunnel.util.InvalidFileExtensionException
|
||||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.extractNameAndNumber
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.hasNumberInParentheses
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
@ -106,7 +108,12 @@ constructor(
|
||||||
var tunnelName = name
|
var tunnelName = name
|
||||||
var num = 1
|
var num = 1
|
||||||
while (tunnels.any { it.name == tunnelName }) {
|
while (tunnels.any { it.name == tunnelName }) {
|
||||||
tunnelName = "$name($num)"
|
tunnelName = if (!tunnelName.hasNumberInParentheses()) {
|
||||||
|
"$name($num)"
|
||||||
|
} else {
|
||||||
|
val pair = tunnelName.extractNameAndNumber()
|
||||||
|
"${pair?.first}($num)"
|
||||||
|
}
|
||||||
num++
|
num++
|
||||||
}
|
}
|
||||||
tunnelName
|
tunnelName
|
||||||
|
@ -233,14 +240,15 @@ constructor(
|
||||||
|
|
||||||
private fun saveSettings(settings: Settings) = viewModelScope.launch { appDataRepository.settings.save(settings) }
|
private fun saveSettings(settings: Settings) = viewModelScope.launch { appDataRepository.settings.save(settings) }
|
||||||
|
|
||||||
fun onCopyTunnel(tunnel: TunnelConfig?) = viewModelScope.launch {
|
fun onCopyTunnel(tunnel: TunnelConfig) = viewModelScope.launch {
|
||||||
tunnel?.let {
|
|
||||||
saveTunnel(
|
saveTunnel(
|
||||||
TunnelConfig(
|
tunnel.copy(
|
||||||
name = it.name.plus(NumberUtils.randomThree()),
|
id = 0,
|
||||||
wgQuick = it.wgQuick,
|
isPrimaryTunnel = false,
|
||||||
|
isMobileDataTunnel = false,
|
||||||
|
isActive = false,
|
||||||
|
name = makeTunnelNameUnique(tunnel.name),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package com.zaneschepke.wireguardautotunnel.util.extensions
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
val hasNumberInParentheses = """^(.+?)\((\d+)\)$""".toRegex()
|
||||||
|
|
||||||
fun String.isValidIpv4orIpv6Address(): Boolean {
|
fun String.isValidIpv4orIpv6Address(): Boolean {
|
||||||
val ipv4Pattern = Pattern.compile(
|
val ipv4Pattern = Pattern.compile(
|
||||||
"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\$",
|
"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\$",
|
||||||
|
@ -13,6 +15,18 @@ fun String.isValidIpv4orIpv6Address(): Boolean {
|
||||||
return ipv4Pattern.matcher(this).matches() || ipv6Pattern.matcher(this).matches()
|
return ipv4Pattern.matcher(this).matches() || ipv6Pattern.matcher(this).matches()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun String.hasNumberInParentheses(): Boolean {
|
||||||
|
return hasNumberInParentheses.matches(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to extract name and number
|
||||||
|
fun String.extractNameAndNumber(): Pair<String, Int>? {
|
||||||
|
val matchResult = hasNumberInParentheses.matchEntire(this)
|
||||||
|
return matchResult?.let {
|
||||||
|
Pair(it.groupValues[1], it.groupValues[2].toInt())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun List<String>.isMatchingToWildcardList(value: String): Boolean {
|
fun List<String>.isMatchingToWildcardList(value: String): Boolean {
|
||||||
val excludeValues = this.filter { it.startsWith("!") }.map { it.removePrefix("!").toRegexWithWildcards() }
|
val excludeValues = this.filter { it.startsWith("!") }.map { it.removePrefix("!").toRegexWithWildcards() }
|
||||||
Timber.d("Excluded values: $excludeValues")
|
Timber.d("Excluded values: $excludeValues")
|
||||||
|
|
Loading…
Reference in New Issue