mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4098a1afb0 | |||
| a39feeeea6 | |||
| c1619ff012 | |||
| 2534b86005 | |||
| 15c550737c | |||
| e1e7e27bb5 | |||
| 8021c133a5 | |||
| 6009445a15 | |||
| f80af9dd5e | |||
| 3f912ed532 |
+38
-1
@@ -2,4 +2,41 @@
|
||||
|
||||
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
|
||||
<fields>;
|
||||
}
|
||||
}
|
||||
|
||||
# Keep all classes in the org.xbill.DNS package and subpackages
|
||||
-keep class org.xbill.DNS.** { *; }
|
||||
-dontwarn org.xbill.DNS.**
|
||||
|
||||
# Preserve JNA classes if used (e.g., for IPHlpAPI on Windows)
|
||||
-keep class com.sun.jna.** { *; }
|
||||
-dontwarn com.sun.jna.**
|
||||
|
||||
# Keep DNS resolver configuration classes that might be loaded dynamically
|
||||
-keep class org.xbill.DNS.config.** { *; }
|
||||
-dontwarn org.xbill.DNS.config.**
|
||||
|
||||
-keep class org.xbill.DNS.** { *; }
|
||||
|
||||
# Prevent optimization issues with native or reflection-based calls
|
||||
-dontoptimize
|
||||
-dontshrink
|
||||
# Uncomment the above if errors persist, but use sparingly as they’re broad
|
||||
|
||||
# Suppress warnings about missing classes if not all features are used
|
||||
-dontwarn java.lang.management.**
|
||||
-dontwarn sun.nio.ch.**
|
||||
|
||||
-dontwarn com.google.api.client.http.GenericUrl
|
||||
-dontwarn com.google.api.client.http.HttpHeaders
|
||||
-dontwarn com.google.api.client.http.HttpRequest
|
||||
-dontwarn com.google.api.client.http.HttpRequestFactory
|
||||
-dontwarn com.google.api.client.http.HttpResponse
|
||||
-dontwarn com.google.api.client.http.HttpTransport
|
||||
-dontwarn com.google.api.client.http.javanet.NetHttpTransport$Builder
|
||||
-dontwarn com.google.api.client.http.javanet.NetHttpTransport
|
||||
-dontwarn javax.lang.model.element.Modifier
|
||||
-dontwarn org.joda.time.Instant
|
||||
-dontwarn org.slf4j.impl.StaticLoggerBinder
|
||||
-dontwarn org.slf4j.impl.StaticMDCBinder
|
||||
-dontwarn org.slf4j.impl.StaticMarkerBinder
|
||||
|
||||
Vendored
+38
-1
@@ -21,4 +21,41 @@
|
||||
#-renamesourcefileattribute SourceFile
|
||||
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
|
||||
<fields>;
|
||||
}
|
||||
}
|
||||
|
||||
# Keep all classes in the org.xbill.DNS package and subpackages
|
||||
-keep class org.xbill.DNS.** { *; }
|
||||
-dontwarn org.xbill.DNS.**
|
||||
|
||||
# Preserve JNA classes if used (e.g., for IPHlpAPI on Windows)
|
||||
-keep class com.sun.jna.** { *; }
|
||||
-dontwarn com.sun.jna.**
|
||||
|
||||
# Keep DNS resolver configuration classes that might be loaded dynamically
|
||||
-keep class org.xbill.DNS.config.** { *; }
|
||||
-dontwarn org.xbill.DNS.config.**
|
||||
|
||||
-keep class org.xbill.DNS.** { *; }
|
||||
|
||||
# Prevent optimization issues with native or reflection-based calls
|
||||
-dontoptimize
|
||||
-dontshrink
|
||||
# Uncomment the above if errors persist, but use sparingly as they’re broad
|
||||
|
||||
# Suppress warnings about missing classes if not all features are used
|
||||
-dontwarn java.lang.management.**
|
||||
-dontwarn sun.nio.ch.**
|
||||
|
||||
-dontwarn com.google.api.client.http.GenericUrl
|
||||
-dontwarn com.google.api.client.http.HttpHeaders
|
||||
-dontwarn com.google.api.client.http.HttpRequest
|
||||
-dontwarn com.google.api.client.http.HttpRequestFactory
|
||||
-dontwarn com.google.api.client.http.HttpResponse
|
||||
-dontwarn com.google.api.client.http.HttpTransport
|
||||
-dontwarn com.google.api.client.http.javanet.NetHttpTransport$Builder
|
||||
-dontwarn com.google.api.client.http.javanet.NetHttpTransport
|
||||
-dontwarn javax.lang.model.element.Modifier
|
||||
-dontwarn org.joda.time.Instant
|
||||
-dontwarn org.slf4j.impl.StaticLoggerBinder
|
||||
-dontwarn org.slf4j.impl.StaticMDCBinder
|
||||
-dontwarn org.slf4j.impl.StaticMarkerBinder
|
||||
|
||||
@@ -67,7 +67,6 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.support.LogsScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
@@ -121,20 +120,14 @@ class MainActivity : AppCompatActivity() {
|
||||
viewModel.getEmitSplitTunnelApps(this@MainActivity)
|
||||
}
|
||||
|
||||
LaunchedEffect(appUiState.autoTunnelActive) {
|
||||
requestAutoTunnelTileServiceUpdate()
|
||||
}
|
||||
|
||||
with(appUiState.appSettings) {
|
||||
LaunchedEffect(isAutoTunnelEnabled) {
|
||||
this@MainActivity.requestAutoTunnelTileServiceUpdate()
|
||||
}
|
||||
LaunchedEffect(isShortcutsEnabled) {
|
||||
if (!isShortcutsEnabled) return@LaunchedEffect shortcutManager.removeShortcuts()
|
||||
shortcutManager.addShortcuts()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO could improve this to cancel when no tuns or autotun on
|
||||
ServiceWorker.start(this)
|
||||
|
||||
CompositionLocalProvider(LocalNavController provides navController) {
|
||||
@@ -221,14 +214,14 @@ class MainActivity : AppCompatActivity() {
|
||||
composable<Route.Logs> {
|
||||
LogsScreen()
|
||||
}
|
||||
composable<Route.Config> {
|
||||
val args = it.toRoute<Route.Config>()
|
||||
composable<Route.Config> { backStack ->
|
||||
val args = backStack.toRoute<Route.Config>()
|
||||
val config =
|
||||
appUiState.tunnels.firstOrNull { it.id == args.id }
|
||||
ConfigScreen(config, viewModel)
|
||||
}
|
||||
composable<Route.TunnelOptions> {
|
||||
val args = it.toRoute<Route.TunnelOptions>()
|
||||
composable<Route.TunnelOptions> { backStack ->
|
||||
val args = backStack.toRoute<Route.TunnelOptions>()
|
||||
val config = appUiState.tunnels.first { it.id == args.id }
|
||||
OptionsScreen(config)
|
||||
}
|
||||
@@ -241,13 +234,13 @@ class MainActivity : AppCompatActivity() {
|
||||
composable<Route.KillSwitch> {
|
||||
KillSwitchScreen(appUiState, viewModel)
|
||||
}
|
||||
composable<Route.SplitTunnel> {
|
||||
val args = it.toRoute<Route.SplitTunnel>()
|
||||
composable<Route.SplitTunnel> { backStack ->
|
||||
val args = backStack.toRoute<Route.SplitTunnel>()
|
||||
val config = appUiState.tunnels.first { it.id == args.id }
|
||||
SplitTunnelScreen(config, viewModel)
|
||||
}
|
||||
composable<Route.TunnelAutoTunnel> {
|
||||
val args = it.toRoute<Route.TunnelOptions>()
|
||||
composable<Route.TunnelAutoTunnel> { backStack ->
|
||||
val args = backStack.toRoute<Route.TunnelOptions>()
|
||||
val config = appUiState.tunnels.first { it.id == args.id }
|
||||
TunnelAutoTunnelScreen(config, appUiState.appSettings)
|
||||
}
|
||||
|
||||
+2
-2
@@ -32,8 +32,8 @@ class KernelReceiver : BroadcastReceiver() {
|
||||
val action = intent.action ?: return
|
||||
applicationScope.launch {
|
||||
if (action == REFRESH_TUNNELS_ACTION) {
|
||||
tunnelManager.runningTunnelNames().forEach {
|
||||
val tunnel = tunnelRepository.findByTunnelName(it)
|
||||
tunnelManager.runningTunnelNames().forEach { name ->
|
||||
val tunnel = tunnelRepository.findByTunnelName(name)
|
||||
tunnel?.let {
|
||||
tunnelRepository.save(it.copy(isActive = true))
|
||||
}
|
||||
|
||||
+1
-1
@@ -187,7 +187,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
buildNetworkState(it)
|
||||
}.distinctUntilChanged(),
|
||||
) { double, networkState ->
|
||||
AutoTunnelState(tunnelManager.activeTunnels().value, networkState, double.first, double.second)
|
||||
AutoTunnelState(tunnelManager.activeTunnels.value, networkState, double.first, double.second)
|
||||
}.collect { state ->
|
||||
autoTunnelStateFlow.update {
|
||||
it.copy(activeTunnels = state.activeTunnels, networkState = state.networkState, settings = state.settings, tunnels = state.tunnels)
|
||||
|
||||
+9
-4
@@ -52,9 +52,14 @@ class TunnelControlTile : TileService() {
|
||||
}
|
||||
|
||||
fun updateTileState() = applicationScope.launch {
|
||||
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
||||
with(tunnelManager.activeTunnels().value) {
|
||||
if (isNotEmpty()) return@launch updateTile(if (size == 1) first().tunName else getString(R.string.multiple), true)
|
||||
val tunnels = appDataRepository.tunnels.getAll()
|
||||
if (tunnels.isEmpty()) return@launch setUnavailable()
|
||||
with(tunnelManager.activeTunnels.value) {
|
||||
if (isNotEmpty()) if (size == 1) {
|
||||
tunnels.firstOrNull { it.id == keys.first() }?.let { return@launch updateTile(it.tunName, true) }
|
||||
} else {
|
||||
return@launch updateTile(getString(R.string.multiple), true)
|
||||
}
|
||||
}
|
||||
appDataRepository.getStartTunnelConfig()?.let {
|
||||
updateTile(it.tunName, false)
|
||||
@@ -65,7 +70,7 @@ class TunnelControlTile : TileService() {
|
||||
super.onClick()
|
||||
unlockAndRun {
|
||||
applicationScope.launch {
|
||||
if (tunnelManager.activeTunnels().value.isNotEmpty()) return@launch tunnelManager.stopTunnel()
|
||||
if (tunnelManager.activeTunnels.value.isNotEmpty()) return@launch tunnelManager.stopTunnel()
|
||||
appDataRepository.getStartTunnelConfig()?.let {
|
||||
tunnelManager.startTunnel(it)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
@@ -45,6 +46,8 @@ open class BaseTunnel(
|
||||
|
||||
internal val tunnels = MutableStateFlow<List<TunnelConf>>(emptyList())
|
||||
|
||||
private val _activeTunnels = MutableStateFlow<Map<Int, TunnelState>>(emptyMap())
|
||||
|
||||
private val tunnelJobs = mutableMapOf<TunnelConf, Job>()
|
||||
|
||||
private val isNetworkAvailable = AtomicBoolean(false)
|
||||
@@ -67,8 +70,9 @@ open class BaseTunnel(
|
||||
removedItems.forEach { tun ->
|
||||
tunnelJobs[tun]?.cancelWithMessage("Canceling tunnel jobs for tunnel: ${tun.name}")
|
||||
tunnelJobs.remove(tun)
|
||||
_activeTunnels.update { it - tun.id }
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,6 +87,9 @@ open class BaseTunnel(
|
||||
launch {
|
||||
startTunnelConfigChangeJob(tunnel)
|
||||
}
|
||||
launch {
|
||||
startStateJob(tunnel)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun bounceTunnel(tunnelConf: TunnelConf) {
|
||||
@@ -99,14 +106,13 @@ open class BaseTunnel(
|
||||
return emptySet()
|
||||
}
|
||||
|
||||
override suspend fun activeTunnels(): StateFlow<List<TunnelConf>> {
|
||||
return tunnels.asStateFlow()
|
||||
}
|
||||
override val activeTunnels: StateFlow<Map<Int, TunnelState>>
|
||||
get() = _activeTunnels.asStateFlow()
|
||||
|
||||
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||
if (tunnels.value.any { it.id == tunnelConf.id }) return Timber.w("Tunnel already running")
|
||||
serviceManager.startBackgroundService(tunnelConf)
|
||||
appDataRepository.tunnels.save(tunnelConf.copy(isActive = true))
|
||||
addToActiveTunnels(tunnelConf)
|
||||
}
|
||||
|
||||
override suspend fun stopTunnel(tunnelConf: TunnelConf?) {
|
||||
@@ -131,7 +137,7 @@ open class BaseTunnel(
|
||||
}
|
||||
}
|
||||
|
||||
internal fun addToActiveTunnels(conf: TunnelConf) {
|
||||
private fun addToActiveTunnels(conf: TunnelConf) {
|
||||
tunnels.update {
|
||||
it.toMutableList().apply {
|
||||
add(conf)
|
||||
@@ -153,13 +159,22 @@ open class BaseTunnel(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startStateJob(tunnel: TunnelConf) {
|
||||
tunnel.state.collect { state ->
|
||||
_activeTunnels.update {
|
||||
it + (tunnel.id to state)
|
||||
}
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startPingJob(tunnel: TunnelConf) = coroutineScope {
|
||||
while (isActive) {
|
||||
if (isNetworkAvailable.get() && tunnel.isActive) {
|
||||
val pingResult = tunnel.pingTunnel(ioDispatcher)
|
||||
handlePingResult(tunnel, pingResult)
|
||||
}
|
||||
delay(CHECK_INTERVAL)
|
||||
delay(tunnel.pingInterval ?: Constants.PING_INTERVAL)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +215,7 @@ open class BaseTunnel(
|
||||
private suspend fun startTunnelConfigChangeJob(tunnel: TunnelConf) = coroutineScope {
|
||||
appDataRepository.tunnels.flow.collect { storageTuns ->
|
||||
storageTuns.firstOrNull { it.id == tunnel.id }?.let { storageTun ->
|
||||
if (tunnel.isQuickConfigChanged(storageTun) || tunnel.isPingConfigMatching(storageTun)) {
|
||||
if (!tunnel.isQuickConfigMatching(storageTun) || !tunnel.isPingConfigMatching(storageTun)) {
|
||||
bounceTunnel(tunnel)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,10 +33,10 @@ class KernelTunnel @Inject constructor(
|
||||
|
||||
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||
withContext(ioDispatcher) {
|
||||
super.startTunnel(tunnelConf)
|
||||
if (tunnels.value.any { it.id == tunnelConf.id }) return@withContext Timber.w("Tunnel already running")
|
||||
runCatching {
|
||||
backend.setState(tunnelConf, Tunnel.State.UP, tunnelConf.toWgConfig())
|
||||
addToActiveTunnels(tunnelConf)
|
||||
super.startTunnel(tunnelConf)
|
||||
}.onFailure {
|
||||
onTunnelStop(tunnelConf)
|
||||
if (it is BackendException) {
|
||||
@@ -66,8 +66,8 @@ class KernelTunnel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun toggleTunnel(tunnelConf: TunnelConf, status: TunnelStatus) {
|
||||
when (status) {
|
||||
override suspend fun toggleTunnel(tunnelConf: TunnelConf, state: TunnelStatus) {
|
||||
when (state) {
|
||||
TunnelStatus.UP -> backend.setState(tunnelConf, Tunnel.State.UP, tunnelConf.toWgConfig())
|
||||
TunnelStatus.DOWN -> backend.setState(tunnelConf, Tunnel.State.DOWN, tunnelConf.toWgConfig())
|
||||
}
|
||||
|
||||
@@ -11,10 +11,12 @@ import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.withData
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.plus
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -34,14 +36,21 @@ class TunnelManager @Inject constructor(
|
||||
initialValue = null,
|
||||
)
|
||||
|
||||
override suspend fun activeTunnels(): StateFlow<List<TunnelConf>> {
|
||||
return withContext(ioDispatcher) {
|
||||
appSettings.filterNotNull().first().let {
|
||||
if (it.isKernelEnabled) return@withContext kernelTunnel.activeTunnels()
|
||||
userspaceTunnel.activeTunnels()
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override val activeTunnels = appSettings
|
||||
.filterNotNull()
|
||||
.flatMapLatest { settings ->
|
||||
if (settings.isKernelEnabled) {
|
||||
kernelTunnel.activeTunnels
|
||||
} else {
|
||||
userspaceTunnel.activeTunnels
|
||||
}
|
||||
}
|
||||
}
|
||||
.stateIn(
|
||||
scope = applicationScope,
|
||||
started = SharingStarted.Eagerly,
|
||||
initialValue = emptyMap(),
|
||||
)
|
||||
|
||||
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||
appSettings.withData {
|
||||
@@ -84,14 +93,14 @@ class TunnelManager @Inject constructor(
|
||||
if (isRestoreOnBootEnabled) {
|
||||
val previouslyActiveTuns = appDataRepository.tunnels.getActive()
|
||||
// handle kernel mode
|
||||
val tunsToStart = previouslyActiveTuns.filterNot { tun -> activeTunnels().value.any { tun.id == it.id } }
|
||||
val tunsToStart = previouslyActiveTuns.filterNot { tun -> activeTunnels.value.any { tun.id == it.key } }
|
||||
if (isKernelEnabled) {
|
||||
return@withContext tunsToStart.forEach {
|
||||
startTunnel(it)
|
||||
}
|
||||
}
|
||||
// handle userspace
|
||||
if (activeTunnels().value.isEmpty()) tunsToStart.firstOrNull()?.let { startTunnel(it) }
|
||||
if (activeTunnels.value.isEmpty()) tunsToStart.firstOrNull()?.let { startTunnel(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
interface TunnelProvider {
|
||||
|
||||
suspend fun activeTunnels(): StateFlow<List<TunnelConf>>
|
||||
val activeTunnels: StateFlow<Map<Int, TunnelState>>
|
||||
|
||||
suspend fun startTunnel(tunnelConf: TunnelConf)
|
||||
|
||||
|
||||
+5
-2
@@ -34,10 +34,13 @@ class UserspaceTunnel @Inject constructor(
|
||||
|
||||
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||
withContext(ioDispatcher) {
|
||||
super.startTunnel(tunnelConf)
|
||||
if (tunnels.value.any { it.id == tunnelConf.id }) return@withContext Timber.w("Tunnel already running")
|
||||
if (tunnels.value.isNotEmpty()) {
|
||||
stopAllTunnels()
|
||||
}
|
||||
runCatching {
|
||||
backend.setState(tunnelConf, Tunnel.State.UP, tunnelConf.toAmConfig())
|
||||
addToActiveTunnels(tunnelConf)
|
||||
super.startTunnel(tunnelConf)
|
||||
}.onFailure {
|
||||
onTunnelStop(tunnelConf)
|
||||
if (it is BackendException) {
|
||||
|
||||
@@ -53,7 +53,7 @@ class ServiceWorker @AssistedInject constructor(
|
||||
Timber.i("Service worker started")
|
||||
with(appDataRepository.settings.get()) {
|
||||
if (isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) return@with serviceManager.startAutoTunnel(true)
|
||||
if (tunnelManager.activeTunnels().value.isEmpty()) tunnelManager.restorePreviousState()
|
||||
if (tunnelManager.activeTunnels.value.isEmpty()) tunnelManager.restorePreviousState()
|
||||
}
|
||||
Result.success()
|
||||
}
|
||||
|
||||
@@ -62,9 +62,9 @@ data class TunnelConf(
|
||||
}
|
||||
}
|
||||
|
||||
fun isQuickConfigChanged(updatedConf: TunnelConf): Boolean {
|
||||
return updatedConf.wgQuick != wgQuick ||
|
||||
updatedConf.amQuick != amQuick
|
||||
fun isQuickConfigMatching(updatedConf: TunnelConf): Boolean {
|
||||
return updatedConf.wgQuick == wgQuick ||
|
||||
updatedConf.amQuick == amQuick
|
||||
}
|
||||
|
||||
fun isPingConfigMatching(updatedConf: TunnelConf): Boolean {
|
||||
|
||||
+5
-5
@@ -7,7 +7,7 @@ import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
||||
|
||||
data class AutoTunnelState(
|
||||
val activeTunnels: List<TunnelConf> = emptyList(),
|
||||
val activeTunnels: Map<Int, TunnelState> = emptyMap(),
|
||||
val networkState: NetworkState = NetworkState(),
|
||||
val settings: AppSettings = AppSettings(),
|
||||
val tunnels: List<TunnelConf> = emptyList(),
|
||||
@@ -20,12 +20,12 @@ data class AutoTunnelState(
|
||||
private fun isMobileTunnelDataChangeNeeded(): Boolean {
|
||||
val preferredTunnel = preferredMobileDataTunnel()
|
||||
return preferredTunnel != null &&
|
||||
activeTunnels.isNotEmpty() && !activeTunnels.any { it.id == preferredTunnel.id }
|
||||
activeTunnels.isNotEmpty() && !activeTunnels.any { it.key == preferredTunnel.id }
|
||||
}
|
||||
|
||||
private fun isEthernetTunnelChangeNeeded(): Boolean {
|
||||
val preferredTunnel = preferredEthernetTunnel()
|
||||
return preferredTunnel != null && activeTunnels.isNotEmpty() && !activeTunnels.any { it.id == preferredTunnel.id }
|
||||
return preferredTunnel != null && activeTunnels.isNotEmpty() && !activeTunnels.any { it.key == preferredTunnel.id }
|
||||
}
|
||||
|
||||
private fun preferredMobileDataTunnel(): TunnelConf? {
|
||||
@@ -62,7 +62,7 @@ data class AutoTunnelState(
|
||||
return settings.isVpnKillSwitchEnabled && (!settings.isDisableKillSwitchOnTrustedEnabled || !isCurrentSSIDTrusted())
|
||||
}
|
||||
|
||||
fun isNoConnectivity(): Boolean {
|
||||
private fun isNoConnectivity(): Boolean {
|
||||
return !networkState.isEthernetConnected && !networkState.isWifiConnected && !networkState.isMobileDataConnected
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ data class AutoTunnelState(
|
||||
|
||||
private fun isWifiTunnelPreferred(): Boolean {
|
||||
val preferred = preferredWifiTunnel()
|
||||
return activeTunnels.any { it.id == preferred?.id }
|
||||
return activeTunnels.any { it.key == preferred?.id }
|
||||
}
|
||||
|
||||
fun asAutoTunnelEvent(): AutoTunnelEvent {
|
||||
|
||||
+7
-13
@@ -77,7 +77,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
||||
var selectedTunnel by remember { mutableStateOf<TunnelConf?>(null) }
|
||||
val isRunningOnTv = remember { context.isRunningOnTv() }
|
||||
|
||||
val activeTunnels by viewModel.activeTunnels.collectAsStateWithLifecycle(emptyList())
|
||||
val activeTunnels by viewModel.tunnelManager.activeTunnels.collectAsStateWithLifecycle(emptyMap())
|
||||
|
||||
val collator = Collator.getInstance(Locale.getDefault())
|
||||
|
||||
@@ -89,6 +89,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
||||
val startTunnel = withVpnPermission<TunnelConf> {
|
||||
viewModel.onTunnelStart(it)
|
||||
}
|
||||
|
||||
val autoTunnelToggleBattery = withIgnoreBatteryOpt(uiState.generalState.isBatteryOptimizationDisableShown) {
|
||||
if (!uiState.generalState.isBatteryOptimizationDisableShown) viewModel.setBatteryOptimizeDisableShown()
|
||||
if (uiState.appSettings.isKernelEnabled) {
|
||||
@@ -132,15 +133,8 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
||||
}
|
||||
|
||||
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConf) {
|
||||
if (!checked) {
|
||||
viewModel.onTunnelStop(tunnel)
|
||||
return
|
||||
}
|
||||
if (uiState.appSettings.isKernelEnabled) {
|
||||
viewModel.onTunnelStart(tunnel)
|
||||
} else {
|
||||
startTunnel.invoke(tunnel)
|
||||
}
|
||||
if (!checked) return viewModel.onTunnelStop(tunnel).let { }
|
||||
if (uiState.appSettings.isKernelEnabled) viewModel.onTunnelStart(tunnel) else startTunnel(tunnel)
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
@@ -233,13 +227,13 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
||||
key = { tunnel -> tunnel.id },
|
||||
) { tunnel ->
|
||||
val expanded = uiState.generalState.isTunnelStatsExpanded
|
||||
val tunnelState = activeTunnels.firstOrNull { it.id == tunnel.id }?.state?.collectAsStateWithLifecycle()
|
||||
val tunnelState = activeTunnels.getOrDefault(tunnel.id, TunnelState())
|
||||
TunnelRowItem(
|
||||
tunnel.isActive,
|
||||
tunnelState.state.isUp(),
|
||||
expanded,
|
||||
selectedTunnel?.id == tunnel.id,
|
||||
tunnel,
|
||||
tunnelState = tunnelState?.value ?: TunnelState(),
|
||||
tunnelState = tunnelState,
|
||||
{ selectedTunnel = tunnel },
|
||||
{ viewModel.onExpandedChanged(!expanded) },
|
||||
onDelete = { showDeleteTunnelAlertDialog = true },
|
||||
|
||||
+2
-2
@@ -46,9 +46,9 @@ fun TunnelStatistics.PeerStats.handshakeStatus(): HandshakeStatus {
|
||||
fun Peer.isReachable(preferIpv4: Boolean): Boolean {
|
||||
val host =
|
||||
if (this.endpoint.isPresent &&
|
||||
this.endpoint.get().getResolved(preferIpv4).isPresent
|
||||
this.endpoint.get().resolved.isPresent
|
||||
) {
|
||||
this.endpoint.get().getResolved(preferIpv4).get().host
|
||||
this.endpoint.get().resolved.get().host
|
||||
} else {
|
||||
Constants.DEFAULT_PING_IP
|
||||
}
|
||||
|
||||
@@ -109,8 +109,8 @@ constructor(
|
||||
}
|
||||
|
||||
private suspend fun initTunnels() {
|
||||
tunnels.withData {
|
||||
it.filter { it.isActive }.forEach {
|
||||
tunnels.withData { tunnels ->
|
||||
tunnels.filter { it.isActive }.forEach {
|
||||
tunnelManager.startTunnel(it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.withData
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.amnezia.awg.config.Config
|
||||
@@ -44,15 +42,6 @@ constructor(
|
||||
appDataRepository: AppDataRepository,
|
||||
) : BaseViewModel(appDataRepository) {
|
||||
|
||||
private val _activeTunnels = MutableStateFlow<List<TunnelConf>>(emptyList())
|
||||
val activeTunnels = _activeTunnels.asStateFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
tunnelManager.activeTunnels().collect(_activeTunnels::emit)
|
||||
}
|
||||
}
|
||||
|
||||
fun onDelete(tunnel: TunnelConf) = viewModelScope.launch {
|
||||
appSettings.withData { settings ->
|
||||
tunnels.withData {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
[versions]
|
||||
accompanist = "0.37.0"
|
||||
activityCompose = "1.10.0"
|
||||
amneziawgAndroid = "1.2.6"
|
||||
amneziawgAndroid = "1.2.9"
|
||||
androidx-junit = "1.2.1"
|
||||
appcompat = "1.7.0"
|
||||
biometricKtx = "1.2.0-alpha05"
|
||||
coreKtx = "1.15.0"
|
||||
datastorePreferences = "1.1.2"
|
||||
datastorePreferences = "1.1.3"
|
||||
desugar_jdk_libs = "2.1.4"
|
||||
espressoCore = "3.6.1"
|
||||
hiltAndroid = "2.55"
|
||||
@@ -19,7 +19,7 @@ navigationCompose = "2.8.7"
|
||||
pinLockCompose = "1.0.4"
|
||||
roomVersion = "2.6.1"
|
||||
timber = "5.0.1"
|
||||
tunnel = "1.2.2"
|
||||
tunnel = "1.2.5"
|
||||
androidGradlePlugin = "8.8.0-alpha05"
|
||||
kotlin = "2.1.10"
|
||||
ksp = "2.1.10-1.0.30"
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
1
|
||||
3
|
||||
Reference in New Issue
Block a user