mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8fd13f7dd6 | |||
| 5d8fb38906 | |||
| 6d100a2f46 | |||
| 5feb2827fd | |||
| 0336c2ac9f | |||
| 96d8114d37 | |||
| e78469c730 | |||
| 6f365a4490 | |||
| 2885d1a539 |
@@ -72,7 +72,7 @@ jobs:
|
||||
outputs:
|
||||
UPLOAD_DIR_ANDROID: ${{ env.UPLOAD_DIR_ANDROID }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Set up JDK 17
|
||||
|
||||
@@ -16,7 +16,7 @@ jobs:
|
||||
has_new_commits: ${{ steps.check.outputs.new_commits }}
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
- name: Check for new commits
|
||||
id: check
|
||||
env:
|
||||
@@ -41,7 +41,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
|
||||
- name: Install system dependencies
|
||||
run: |
|
||||
@@ -69,7 +69,7 @@ jobs:
|
||||
run: mkdir ${{ github.workspace }}/temp
|
||||
|
||||
- name: Download artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@v5
|
||||
with:
|
||||
pattern: android_artifacts_*
|
||||
path: ${{ github.workspace }}/temp
|
||||
|
||||
@@ -10,7 +10,7 @@ jobs:
|
||||
format_check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
|
||||
@@ -72,7 +72,7 @@ jobs:
|
||||
name: publish-github
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
with:
|
||||
ref: ${{ github.event_name == 'push' && github.ref || 'main' }}
|
||||
- name: Install system dependencies
|
||||
@@ -108,7 +108,7 @@ jobs:
|
||||
run: mkdir ${{ github.workspace }}/temp
|
||||
|
||||
- name: Download artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@v5
|
||||
with:
|
||||
pattern: android_artifacts_*
|
||||
path: ${{ github.workspace }}/temp
|
||||
@@ -189,7 +189,7 @@ jobs:
|
||||
KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
|
||||
@@ -47,6 +47,10 @@
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent>
|
||||
<intent>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent>
|
||||
</queries>
|
||||
<application
|
||||
android:name=".WireGuardAutoTunnel"
|
||||
|
||||
@@ -301,7 +301,7 @@ class MainActivity : AppCompatActivity() {
|
||||
val args = backStack.toRoute<Route.Config>()
|
||||
val config =
|
||||
appUiState.tunnels.firstOrNull { it.id == args.id }
|
||||
ConfigScreen(config, viewModel)
|
||||
ConfigScreen(config, appUiState, viewModel)
|
||||
}
|
||||
composable<Route.TunnelOptions> { backStack ->
|
||||
val args = backStack.toRoute<Route.TunnelOptions>()
|
||||
|
||||
@@ -13,7 +13,7 @@ import com.zaneschepke.wireguardautotunnel.core.worker.ServiceWorker
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.di.MainDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
|
||||
@@ -91,7 +91,7 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
||||
|
||||
override fun onTerminate() {
|
||||
applicationScope.cancel()
|
||||
tunnelManager.setBackendState(BackendState.INACTIVE, emptyList())
|
||||
tunnelManager.setBackendStatus(BackendStatus.Inactive)
|
||||
super.onTerminate()
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -16,7 +16,7 @@ import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelMonitor
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus.StopReason.Ping
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
||||
@@ -109,13 +109,13 @@ class AutoTunnelService : LifecycleService() {
|
||||
with(autoTunnelStateFlow.value) {
|
||||
if (
|
||||
settings.isVpnKillSwitchEnabled &&
|
||||
tunnelManager.getBackendState() != BackendState.KILL_SWITCH_ACTIVE
|
||||
tunnelManager.getBackendStatus() !is BackendStatus.KillSwitch
|
||||
) {
|
||||
eventHandlerJob?.cancel()
|
||||
val allowedIps =
|
||||
if (settings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS
|
||||
else emptyList()
|
||||
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
|
||||
tunnelManager.setBackendStatus(BackendStatus.KillSwitch(allowedIps))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -402,11 +402,11 @@ class AutoTunnelService : LifecycleService() {
|
||||
handleBounceWithBackoff(event.configsPeerKeyResolvedMap)
|
||||
is AutoTunnelEvent.StartKillSwitch -> {
|
||||
Timber.d("Starting kill switch")
|
||||
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, event.allowedIps)
|
||||
tunnelManager.setBackendStatus(BackendStatus.KillSwitch(event.allowedIps))
|
||||
}
|
||||
AutoTunnelEvent.StopKillSwitch -> {
|
||||
Timber.d("Stopping kill switch")
|
||||
tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptySet())
|
||||
tunnelManager.setBackendStatus(BackendStatus.Active)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.wireguard.android.backend.BackendException
|
||||
import com.wireguard.android.backend.Tunnel
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
@@ -59,12 +59,12 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||
override fun setBackendStatus(backendStatus: BackendStatus) {
|
||||
Timber.w("Not yet implemented for kernel")
|
||||
}
|
||||
|
||||
override fun getBackendState(): BackendState {
|
||||
return BackendState.INACTIVE
|
||||
override fun getBackendStatus(): BackendStatus {
|
||||
return BackendStatus.Inactive
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
|
||||
@@ -34,7 +34,8 @@ constructor(
|
||||
appDataRepository.settings.flow
|
||||
.filterNotNull()
|
||||
.flatMapLatest { settings ->
|
||||
MutableStateFlow(if (settings.isKernelEnabled) kernelTunnel else userspaceTunnel)
|
||||
val backend = if (settings.isKernelEnabled) kernelTunnel else userspaceTunnel
|
||||
MutableStateFlow(backend)
|
||||
}
|
||||
.stateIn(
|
||||
scope = applicationScope.plus(ioDispatcher),
|
||||
@@ -89,12 +90,12 @@ constructor(
|
||||
tunnelProviderFlow.value.bounceTunnel(tunnelConf, reason)
|
||||
}
|
||||
|
||||
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||
tunnelProviderFlow.value.setBackendState(backendState, allowedIps)
|
||||
override fun setBackendStatus(backendStatus: BackendStatus) {
|
||||
tunnelProviderFlow.value.setBackendStatus(backendStatus)
|
||||
}
|
||||
|
||||
override fun getBackendState(): BackendState {
|
||||
return tunnelProviderFlow.value.getBackendState()
|
||||
override fun getBackendStatus(): BackendStatus {
|
||||
return tunnelProviderFlow.value.getBackendStatus()
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
|
||||
@@ -41,9 +41,9 @@ interface TunnelProvider {
|
||||
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.User,
|
||||
)
|
||||
|
||||
fun setBackendState(backendState: BackendState, allowedIps: Collection<String>)
|
||||
fun setBackendStatus(backendStatus: BackendStatus)
|
||||
|
||||
fun getBackendState(): BackendState
|
||||
fun getBackendStatus(): BackendStatus
|
||||
|
||||
suspend fun runningTunnelNames(): Set<String>
|
||||
|
||||
|
||||
+18
-39
@@ -1,15 +1,15 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.AmneziaStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
|
||||
import javax.inject.Inject
|
||||
import kotlin.jvm.optionals.getOrNull
|
||||
@@ -17,7 +17,6 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import org.amnezia.awg.backend.Backend
|
||||
import org.amnezia.awg.backend.BackendException
|
||||
import org.amnezia.awg.backend.Tunnel
|
||||
import org.amnezia.awg.config.Config
|
||||
import timber.log.Timber
|
||||
|
||||
class UserspaceTunnel
|
||||
@@ -29,14 +28,21 @@ constructor(
|
||||
private val backend: Backend,
|
||||
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
|
||||
|
||||
private var previousBackendState: Pair<BackendState, Boolean>? = null
|
||||
|
||||
override suspend fun startBackend(tunnel: TunnelConf) {
|
||||
try {
|
||||
updateTunnelStatus(tunnel, TunnelStatus.Starting)
|
||||
val amConfig = tunnel.toAmConfig()
|
||||
handleVpnKillSwitchWithDomainEndpoints(amConfig)
|
||||
var previousKillSwitch: Backend.BackendStatus? = null
|
||||
// prevent dns failures from bringing tuns up when vpn kill switch active
|
||||
if (
|
||||
amConfig.peers.any { it.endpoint.getOrNull()?.toString()?.isUrl() == true } &&
|
||||
backend.backendStatus is Backend.BackendStatus.KillSwitchActive
|
||||
) {
|
||||
previousKillSwitch = backend.backendStatus
|
||||
setBackendStatus(BackendStatus.Active)
|
||||
}
|
||||
backend.setState(tunnel, Tunnel.State.UP, amConfig)
|
||||
previousKillSwitch?.let { backend.backendStatus = it }
|
||||
} catch (e: BackendException) {
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw e.toBackendError()
|
||||
@@ -53,47 +59,20 @@ constructor(
|
||||
} catch (e: BackendException) {
|
||||
Timber.e(e, "Failed to stop tunnel ${tunnel.id}")
|
||||
throw e.toBackendError()
|
||||
} finally {
|
||||
handlePreviouslyEnabledVpnKillSwitch()
|
||||
}
|
||||
}
|
||||
|
||||
// stop vpn kill switch if we need to resolve DNS for peer endpoints
|
||||
private suspend fun handleVpnKillSwitchWithDomainEndpoints(config: Config) {
|
||||
if (
|
||||
config.peers.any { it.endpoint.getOrNull()?.toString()?.isUrl() == true } &&
|
||||
backend.backendState.asBackendState() == BackendState.KILL_SWITCH_ACTIVE
|
||||
) {
|
||||
val bypassLan = appDataRepository.settings.get().isLanOnKillSwitchEnabled
|
||||
previousBackendState = Pair(BackendState.KILL_SWITCH_ACTIVE, bypassLan)
|
||||
setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||
}
|
||||
}
|
||||
|
||||
// restore vpn kill switch if needed
|
||||
private fun handlePreviouslyEnabledVpnKillSwitch() {
|
||||
// let auto tunnel handle this if it is active
|
||||
if (serviceManager.autoTunnelService.value == null) {
|
||||
previousBackendState?.let { (state, lanEnabled) ->
|
||||
Timber.d("Restoring kill switch configuration")
|
||||
val lan = if (lanEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS else emptyList()
|
||||
backend.setBackendState(state.asAmBackendState(), lan)
|
||||
}
|
||||
}
|
||||
previousBackendState = null
|
||||
}
|
||||
|
||||
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||
Timber.d("Setting backend state: $backendState with allowedIps: $allowedIps")
|
||||
override fun setBackendStatus(backendStatus: BackendStatus) {
|
||||
Timber.d("Setting backend state: $backendStatus")
|
||||
try {
|
||||
backend.setBackendState(backendState.asAmBackendState(), allowedIps)
|
||||
backend.backendStatus = backendStatus.asAmBackendStatus()
|
||||
} catch (e: BackendException) {
|
||||
throw e.toBackendError()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBackendState(): BackendState {
|
||||
return backend.backendState.asBackendState()
|
||||
override fun getBackendStatus(): BackendStatus {
|
||||
return backend.backendStatus.asBackendStatus()
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.enums
|
||||
|
||||
enum class BackendState {
|
||||
KILL_SWITCH_ACTIVE,
|
||||
SERVICE_ACTIVE,
|
||||
INACTIVE,
|
||||
sealed class BackendStatus {
|
||||
data object Inactive : BackendStatus()
|
||||
|
||||
data object Active : BackendStatus()
|
||||
|
||||
data class KillSwitch(val allowedIps: List<String>) : BackendStatus()
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.state
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import org.amnezia.awg.crypto.Key
|
||||
|
||||
data class TunnelState(
|
||||
val status: TunnelStatus = TunnelStatus.Down,
|
||||
val backendState: BackendState = BackendState.INACTIVE,
|
||||
val backendState: BackendStatus = BackendStatus.Inactive,
|
||||
val statistics: TunnelStatistics? = null,
|
||||
val pingStates: Map<Key, PingState>? = null,
|
||||
val handshakeSuccessLogs: Boolean? = null,
|
||||
|
||||
+37
-19
@@ -7,10 +7,7 @@ import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -25,6 +22,7 @@ import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.AddPeerButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.InterfaceSection
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.PeersSection
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||
@@ -32,6 +30,7 @@ import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||
@Composable
|
||||
fun ConfigScreen(
|
||||
tunnelConf: TunnelConf?,
|
||||
appUiState: AppUiState,
|
||||
appViewModel: AppViewModel,
|
||||
viewModel: ConfigViewModel = hiltViewModel(),
|
||||
) {
|
||||
@@ -42,6 +41,17 @@ fun ConfigScreen(
|
||||
|
||||
val activity = context as? MainActivity
|
||||
|
||||
var save by remember { mutableStateOf(false) }
|
||||
|
||||
val isTunnelNameTaken by
|
||||
remember(uiState.tunnelName, appUiState.tunnels) {
|
||||
derivedStateOf {
|
||||
appUiState.tunnels
|
||||
.filter { it.id != tunnelConf?.id }
|
||||
.any { it.name == uiState.tunnelName }
|
||||
}
|
||||
}
|
||||
|
||||
// Secure screen due to sensitive information
|
||||
DisposableEffect(Unit) {
|
||||
activity
|
||||
@@ -58,26 +68,34 @@ fun ConfigScreen(
|
||||
appViewModel.handleEvent(
|
||||
AppEvent.SetScreenAction {
|
||||
keyboardController?.hide()
|
||||
viewModel.save(tunnelConf)
|
||||
if (!isTunnelNameTaken) {
|
||||
save = true
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(tunnelConf) { viewModel.initFromTunnel(tunnelConf) }
|
||||
|
||||
LaunchedEffect(uiState.success) {
|
||||
if (uiState.success == true) {
|
||||
appViewModel.handleEvent(
|
||||
AppEvent.ShowMessage(StringValue.StringResource(R.string.config_changes_saved))
|
||||
)
|
||||
appViewModel.handleEvent(AppEvent.PopBackStack(true))
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(uiState.message) {
|
||||
uiState.message?.let { message ->
|
||||
appViewModel.handleEvent(AppEvent.ShowMessage(message))
|
||||
viewModel.setMessage(null)
|
||||
// TODO improve error messages
|
||||
LaunchedEffect(save) {
|
||||
if (save) {
|
||||
try {
|
||||
appViewModel.handleEvent(
|
||||
AppEvent.SaveTunnelUniquely(
|
||||
uiState.configProxy.buildTunnelConfFromState(uiState.tunnelName, tunnelConf)
|
||||
)
|
||||
)
|
||||
appViewModel.handleEvent(
|
||||
AppEvent.ShowMessage(StringValue.StringResource(R.string.config_changes_saved))
|
||||
)
|
||||
appViewModel.handleEvent(AppEvent.PopBackStack(true))
|
||||
} catch (e: Exception) {
|
||||
val message = e.message ?: context.resources.getString(R.string.unknown_error)
|
||||
appViewModel.handleEvent(AppEvent.ShowMessage(StringValue.DynamicString(message)))
|
||||
} finally {
|
||||
save = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +129,7 @@ fun ConfigScreen(
|
||||
modifier =
|
||||
Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 24.dp),
|
||||
) {
|
||||
InterfaceSection(uiState, viewModel)
|
||||
InterfaceSection(isTunnelNameTaken, uiState, viewModel)
|
||||
PeersSection(uiState, viewModel)
|
||||
AddPeerButton(viewModel)
|
||||
}
|
||||
|
||||
+1
-48
@@ -1,32 +1,20 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.ConfigProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@HiltViewModel
|
||||
class ConfigViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val tunnelRepository: TunnelRepository,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
class ConfigViewModel @Inject constructor() : ViewModel() {
|
||||
|
||||
private val _uiState = MutableStateFlow(ConfigUiState())
|
||||
val uiState: StateFlow<ConfigUiState> = _uiState.asStateFlow()
|
||||
@@ -121,41 +109,6 @@ constructor(
|
||||
updatePeer(index, updated)
|
||||
}
|
||||
|
||||
fun setMessage(message: StringValue?) {
|
||||
_uiState.update { it.copy(message = message) }
|
||||
}
|
||||
|
||||
// TODO improve error messaging
|
||||
fun save(tunnelConf: TunnelConf?) =
|
||||
viewModelScope.launch(ioDispatcher) {
|
||||
val message =
|
||||
try {
|
||||
val saveConfig = buildTunnelConfFromState(tunnelConf)
|
||||
tunnelRepository.save(saveConfig)
|
||||
_uiState.update { it.copy(success = true) }
|
||||
} catch (e: Exception) {
|
||||
setMessage(
|
||||
e.message?.let { message -> (StringValue.DynamicString(message)) }
|
||||
?: StringValue.StringResource(R.string.unknown_error)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildTunnelConfFromState(tunnelConf: TunnelConf?): TunnelConf {
|
||||
val (wg, am) = _uiState.value.configProxy.buildConfigs()
|
||||
val name = _uiState.value.tunnelName
|
||||
return tunnelConf?.copyWithCallback(
|
||||
tunName = name,
|
||||
amQuick = am.toAwgQuickString(true),
|
||||
wgQuick = wg.toWgQuickString(true),
|
||||
)
|
||||
?: TunnelConf(
|
||||
tunName = name,
|
||||
amQuick = am.toAwgQuickString(true),
|
||||
wgQuick = wg.toWgQuickString(true),
|
||||
)
|
||||
}
|
||||
|
||||
fun onAuthenticated() {
|
||||
_uiState.update { it.copy(isAuthenticated = true) }
|
||||
}
|
||||
|
||||
+6
-1
@@ -17,7 +17,11 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigViewMode
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
|
||||
|
||||
@Composable
|
||||
fun InterfaceSection(uiState: ConfigUiState, viewModel: ConfigViewModel) {
|
||||
fun InterfaceSection(
|
||||
isTunnelNameTaken: Boolean,
|
||||
uiState: ConfigUiState,
|
||||
viewModel: ConfigViewModel,
|
||||
) {
|
||||
var isDropDownExpanded by remember { mutableStateOf(false) }
|
||||
val isAmneziaCompatibilitySet =
|
||||
remember(uiState.configProxy.`interface`) {
|
||||
@@ -50,6 +54,7 @@ fun InterfaceSection(uiState: ConfigUiState, viewModel: ConfigViewModel) {
|
||||
value = uiState.tunnelName,
|
||||
onValueChange = viewModel::updateTunnelName,
|
||||
label = stringResource(R.string.name),
|
||||
isError = isTunnelNameTaken,
|
||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
|
||||
+1
-3
@@ -3,7 +3,6 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.ConfigProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
|
||||
data class ConfigUiState(
|
||||
val tunnelName: String = "",
|
||||
@@ -13,6 +12,5 @@ data class ConfigUiState(
|
||||
val showScripts: Boolean = false,
|
||||
val isAuthenticated: Boolean = true,
|
||||
val showAuthPrompt: Boolean = false,
|
||||
val message: StringValue? = null,
|
||||
val success: Boolean? = null,
|
||||
val saveChanges: Boolean = false,
|
||||
)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.state
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import org.amnezia.awg.config.Config
|
||||
|
||||
data class ConfigProxy(val peers: List<PeerProxy>, val `interface`: InterfaceProxy) {
|
||||
@@ -28,6 +29,20 @@ data class ConfigProxy(val peers: List<PeerProxy>, val `interface`: InterfacePro
|
||||
)
|
||||
}
|
||||
|
||||
fun buildTunnelConfFromState(name: String, tunnelConf: TunnelConf?): TunnelConf {
|
||||
val (wg, am) = buildConfigs()
|
||||
return tunnelConf?.copyWithCallback(
|
||||
tunName = name,
|
||||
amQuick = am.toAwgQuickString(true),
|
||||
wgQuick = wg.toWgQuickString(true),
|
||||
)
|
||||
?: TunnelConf(
|
||||
tunName = name,
|
||||
amQuick = am.toAwgQuickString(true),
|
||||
wgQuick = wg.toWgQuickString(true),
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(amConfig: Config): ConfigProxy {
|
||||
return ConfigProxy(
|
||||
|
||||
+1
-1
@@ -21,10 +21,10 @@ import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.tile.AutoTunnelControlTile
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.tile.TunnelControlTile
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import kotlin.system.exitProcess
|
||||
import timber.log.Timber
|
||||
|
||||
fun Context.openWebUrl(url: String): Result<Unit> {
|
||||
return kotlin
|
||||
|
||||
+14
-5
@@ -2,7 +2,7 @@ package com.zaneschepke.wireguardautotunnel.util.extensions
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import com.wireguard.android.backend.BackendException
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.HandshakeStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
@@ -75,12 +75,21 @@ fun Config.defaultName(): String {
|
||||
}
|
||||
}
|
||||
|
||||
fun Backend.BackendState.asBackendState(): BackendState {
|
||||
return BackendState.valueOf(this.name)
|
||||
fun Backend.BackendStatus.asBackendStatus(): BackendStatus {
|
||||
return when (val status = this) {
|
||||
is Backend.BackendStatus.KillSwitchActive ->
|
||||
BackendStatus.KillSwitch(status.allowedIps.toList())
|
||||
is Backend.BackendStatus.ServiceActive -> BackendStatus.Active
|
||||
else -> BackendStatus.Inactive
|
||||
}
|
||||
}
|
||||
|
||||
fun BackendState.asAmBackendState(): Backend.BackendState {
|
||||
return Backend.BackendState.valueOf(this.name)
|
||||
fun BackendStatus.asAmBackendStatus(): Backend.BackendStatus {
|
||||
return when (val status = this) {
|
||||
is BackendStatus.Active -> Backend.BackendStatus.ServiceActive.INSTANCE
|
||||
is BackendStatus.Inactive -> Backend.BackendStatus.Inactive.INSTANCE
|
||||
is BackendStatus.KillSwitch -> Backend.BackendStatus.KillSwitchActive(status.allowedIps)
|
||||
}
|
||||
}
|
||||
|
||||
fun Tunnel.State.asTunnelState(): TunnelStatus {
|
||||
|
||||
@@ -19,7 +19,7 @@ import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.AppShell
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.di.MainDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings
|
||||
@@ -35,6 +35,12 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.withFirstState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.event.UiEvent
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import java.io.IOException
|
||||
import java.net.URL
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -45,12 +51,6 @@ import org.amnezia.awg.config.Config
|
||||
import rikka.shizuku.Shizuku
|
||||
import timber.log.Timber
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
import java.io.IOException
|
||||
import java.net.URL
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
|
||||
@HiltViewModel
|
||||
class AppViewModel
|
||||
@@ -148,8 +148,7 @@ constructor(
|
||||
is AppEvent.ImportTunnelFromClipboard ->
|
||||
handleClipboardImport(event.text, state.tunnels)
|
||||
|
||||
is AppEvent.ImportTunnelFromFile ->
|
||||
handleImportTunnelFromFile(event.data, state.tunnels)
|
||||
is AppEvent.ImportTunnelFromFile -> handleImportTunnelFromFile(event.data)
|
||||
|
||||
is AppEvent.ImportTunnelFromUrl ->
|
||||
handleImportTunnelFromUrl(event.url, state.tunnels)
|
||||
@@ -264,11 +263,45 @@ constructor(
|
||||
saveSettings(
|
||||
state.appSettings.copy(tunnelPingTimeoutSeconds = event.timeout)
|
||||
)
|
||||
is AppEvent.SaveTunnelUniquely -> saveTunnelsUniquely(listOf(event.tunnel))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveTunnelsUniquely(tunnels: List<TunnelConf>) {
|
||||
withContext(ioDispatcher) {
|
||||
tunnelMutex.withLock {
|
||||
val existingTunnels = appDataRepository.tunnels.getAll()
|
||||
val uniqueTuns = generateUniquelyNamedConfigs(tunnels, existingTunnels)
|
||||
appDataRepository.tunnels.saveAll(uniqueTuns)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateUniquelyNamedConfigs(
|
||||
incoming: List<TunnelConf>,
|
||||
existing: List<TunnelConf>,
|
||||
): List<TunnelConf> {
|
||||
val usedNames = existing.map { it.tunName }.toMutableSet()
|
||||
val result = mutableListOf<TunnelConf>()
|
||||
|
||||
for (tun in incoming) {
|
||||
var uniqueName = tun.tunName
|
||||
var counter = 1
|
||||
|
||||
while (uniqueName in usedNames) {
|
||||
uniqueName = "${tun.tunName} ($counter)"
|
||||
counter++
|
||||
}
|
||||
|
||||
usedNames.add(uniqueName)
|
||||
result.add(tun.copy(tunName = uniqueName))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun handleUiEvent(event: UiEvent): Job =
|
||||
viewModelScope.launch(mainDispatcher) { _uiEvent.emit(event) }
|
||||
|
||||
@@ -419,7 +452,7 @@ constructor(
|
||||
_appViewState.update { it.copy(bottomSheet = bottomSheet) }
|
||||
|
||||
private suspend fun handleTunnelPingTargetChange(tunnelConf: TunnelConf, target: String) =
|
||||
saveTunnel(tunnelConf.copy(pingTarget = target))
|
||||
saveTunnel(tunnelConf.copy(pingTarget = target.ifBlank { null }))
|
||||
|
||||
private suspend fun handleTogglePingTunnel(tunnel: TunnelConf) =
|
||||
saveTunnel(tunnel.copy(restartOnPingFailure = !tunnel.restartOnPingFailure))
|
||||
@@ -515,17 +548,10 @@ constructor(
|
||||
_appViewState.update { it.copy(popBackStack = true) }
|
||||
}
|
||||
|
||||
private suspend fun handleImportTunnelFromFile(uri: Uri, tunnels: List<TunnelConf>) {
|
||||
private suspend fun handleImportTunnelFromFile(uri: Uri) {
|
||||
runCatching {
|
||||
val tunnelConfigs = fileUtils.buildTunnelsFromUri(uri)
|
||||
val existingNames = tunnels.map { it.tunName }.toMutableList()
|
||||
val uniqueTunnelConfigs =
|
||||
tunnelConfigs.map { config ->
|
||||
val uniqueName = config.generateUniqueName(existingNames)
|
||||
existingNames.add(uniqueName)
|
||||
config.copy(tunName = uniqueName)
|
||||
}
|
||||
appDataRepository.tunnels.saveAll(uniqueTunnelConfigs)
|
||||
saveTunnelsUniquely(tunnelConfigs)
|
||||
}
|
||||
.onFailure {
|
||||
when (it) {
|
||||
@@ -544,11 +570,7 @@ constructor(
|
||||
runCatching {
|
||||
val amConfig = TunnelConf.configFromAmQuick(config)
|
||||
val tunnelConf = TunnelConf.tunnelConfigFromAmConfig(amConfig)
|
||||
saveTunnel(
|
||||
tunnelConf.copy(
|
||||
tunName = tunnelConf.generateUniqueName(tunnels.map { it.tunName })
|
||||
)
|
||||
)
|
||||
saveTunnelsUniquely(listOf(tunnelConf))
|
||||
}
|
||||
.onFailure {
|
||||
Timber.e(it)
|
||||
@@ -566,11 +588,7 @@ constructor(
|
||||
url.openStream().use { stream ->
|
||||
val amConfig = Config.parse(stream)
|
||||
val tunnelConf = TunnelConf.tunnelConfigFromAmConfig(amConfig)
|
||||
saveTunnel(
|
||||
tunnelConf.copy(
|
||||
tunName = tunnelConf.generateUniqueName(tunnels.map { it.tunName })
|
||||
)
|
||||
)
|
||||
saveTunnelsUniquely(listOf(tunnelConf))
|
||||
}
|
||||
}
|
||||
.onFailure {
|
||||
@@ -648,19 +666,19 @@ constructor(
|
||||
val updatedSettings =
|
||||
appSettings.copy(isLanOnKillSwitchEnabled = !appSettings.isLanOnKillSwitchEnabled)
|
||||
saveSettings(updatedSettings)
|
||||
handleKillSwitchChange(appSettings)
|
||||
handleKillSwitchChange(updatedSettings)
|
||||
}
|
||||
|
||||
private fun handleKillSwitchChange(appSettings: AppSettings) {
|
||||
// let auto tunnel handle kill switch changes if running
|
||||
if (uiState.value.isAutoTunnelActive) return
|
||||
if (!appSettings.isVpnKillSwitchEnabled)
|
||||
return tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||
return tunnelManager.setBackendStatus(BackendStatus.Active)
|
||||
Timber.d("Starting kill switch")
|
||||
val allowedIps =
|
||||
if (appSettings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS
|
||||
else emptyList()
|
||||
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
|
||||
tunnelManager.setBackendStatus(BackendStatus.KillSwitch(allowedIps))
|
||||
}
|
||||
|
||||
private suspend fun handleToggleAppShortcuts(appSettings: AppSettings) {
|
||||
@@ -695,7 +713,7 @@ constructor(
|
||||
}
|
||||
if (enabled && !requestRoot()) return
|
||||
// disable kill switch feature in kernel mode
|
||||
tunnelManager.setBackendState(BackendState.INACTIVE, emptyList())
|
||||
tunnelManager.setBackendStatus(BackendStatus.Inactive)
|
||||
saveSettings(
|
||||
appSettings.copy(
|
||||
isKernelEnabled = enabled,
|
||||
|
||||
@@ -77,6 +77,8 @@ sealed class AppEvent {
|
||||
|
||||
data class SetTheme(val theme: Theme) : AppEvent()
|
||||
|
||||
data class SaveTunnelUniquely(val tunnel: TunnelConf) : AppEvent()
|
||||
|
||||
data class SaveMonitoringSettings(
|
||||
val pingInterval: Int,
|
||||
val tunnelPingAttempts: Int,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[versions]
|
||||
accompanist = "0.37.3"
|
||||
activityCompose = "1.10.1"
|
||||
amneziawgAndroid = "1.4.0"
|
||||
amneziawgAndroid = "1.6.1"
|
||||
androidx-junit = "1.3.0"
|
||||
icmp4a = "1.0.0"
|
||||
roomdatabasebackup = "1.1.0"
|
||||
|
||||
Reference in New Issue
Block a user