mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c8634c71a3 | |||
| 3894efa066 | |||
| 3c60913917 |
@@ -118,11 +118,6 @@ jobs:
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: android_artifacts_${{ inputs.flavor }}
|
||||
path: >-
|
||||
app/build/outputs/apk/${{ inputs.flavor }}/${{ inputs.build_type }}/${{
|
||||
inputs.flavor == 'fdroid' && inputs.build_type == 'release'
|
||||
&& 'wgtunnel-fdroid-release-*.apk'
|
||||
|| format('wgtunnel-{0}-v*.apk', inputs.flavor)
|
||||
}}
|
||||
path: app/build/outputs/apk/${{ inputs.flavor }}/${{ inputs.build_type }}/wgtunnel-${{ inputs.flavor }}${{ inputs.flavor == 'fdroid' && '-release' || '' }}-*.apk
|
||||
retention-days: 1
|
||||
if-no-files-found: warn
|
||||
@@ -69,7 +69,7 @@ jobs:
|
||||
run: mkdir ${{ github.workspace }}/temp
|
||||
|
||||
- name: Download artifacts
|
||||
uses: actions/download-artifact@v5
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
pattern: android_artifacts_*
|
||||
path: ${{ github.workspace }}/temp
|
||||
|
||||
@@ -108,7 +108,7 @@ jobs:
|
||||
run: mkdir ${{ github.workspace }}/temp
|
||||
|
||||
- name: Download artifacts
|
||||
uses: actions/download-artifact@v5
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
pattern: android_artifacts_*
|
||||
path: ${{ github.workspace }}/temp
|
||||
|
||||
@@ -47,10 +47,6 @@
|
||||
<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, appUiState, viewModel)
|
||||
ConfigScreen(config, 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.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
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.setBackendStatus(BackendStatus.Inactive)
|
||||
tunnelManager.setBackendState(BackendState.INACTIVE, emptyList())
|
||||
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.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
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.getBackendStatus() !is BackendStatus.KillSwitch
|
||||
tunnelManager.getBackendState() != BackendState.KILL_SWITCH_ACTIVE
|
||||
) {
|
||||
eventHandlerJob?.cancel()
|
||||
val allowedIps =
|
||||
if (settings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS
|
||||
else emptyList()
|
||||
tunnelManager.setBackendStatus(BackendStatus.KillSwitch(allowedIps))
|
||||
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -402,11 +402,11 @@ class AutoTunnelService : LifecycleService() {
|
||||
handleBounceWithBackoff(event.configsPeerKeyResolvedMap)
|
||||
is AutoTunnelEvent.StartKillSwitch -> {
|
||||
Timber.d("Starting kill switch")
|
||||
tunnelManager.setBackendStatus(BackendStatus.KillSwitch(event.allowedIps))
|
||||
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, event.allowedIps)
|
||||
}
|
||||
AutoTunnelEvent.StopKillSwitch -> {
|
||||
Timber.d("Stopping kill switch")
|
||||
tunnelManager.setBackendStatus(BackendStatus.Active)
|
||||
tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptySet())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
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 setBackendStatus(backendStatus: BackendStatus) {
|
||||
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||
Timber.w("Not yet implemented for kernel")
|
||||
}
|
||||
|
||||
override fun getBackendStatus(): BackendStatus {
|
||||
return BackendStatus.Inactive
|
||||
override fun getBackendState(): BackendState {
|
||||
return BackendState.INACTIVE
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
|
||||
@@ -34,8 +34,7 @@ constructor(
|
||||
appDataRepository.settings.flow
|
||||
.filterNotNull()
|
||||
.flatMapLatest { settings ->
|
||||
val backend = if (settings.isKernelEnabled) kernelTunnel else userspaceTunnel
|
||||
MutableStateFlow(backend)
|
||||
MutableStateFlow(if (settings.isKernelEnabled) kernelTunnel else userspaceTunnel)
|
||||
}
|
||||
.stateIn(
|
||||
scope = applicationScope.plus(ioDispatcher),
|
||||
@@ -90,12 +89,12 @@ constructor(
|
||||
tunnelProviderFlow.value.bounceTunnel(tunnelConf, reason)
|
||||
}
|
||||
|
||||
override fun setBackendStatus(backendStatus: BackendStatus) {
|
||||
tunnelProviderFlow.value.setBackendStatus(backendStatus)
|
||||
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||
tunnelProviderFlow.value.setBackendState(backendState, allowedIps)
|
||||
}
|
||||
|
||||
override fun getBackendStatus(): BackendStatus {
|
||||
return tunnelProviderFlow.value.getBackendStatus()
|
||||
override fun getBackendState(): BackendState {
|
||||
return tunnelProviderFlow.value.getBackendState()
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
|
||||
+11
-14
@@ -138,22 +138,19 @@ constructor(
|
||||
}
|
||||
|
||||
val host =
|
||||
tunnelConf.pingTarget
|
||||
?: {
|
||||
val parts = allowedIpStr.split("/")
|
||||
val internalIp =
|
||||
if (parts.size == 2) parts[0] else allowedIpStr
|
||||
{
|
||||
val parts = allowedIpStr.split("/")
|
||||
val internalIp = if (parts.size == 2) parts[0] else allowedIpStr
|
||||
|
||||
val prefix =
|
||||
if (parts.size == 2) parts[1].toIntOrNull() ?: 32
|
||||
else 32
|
||||
if (prefix <= 1) {
|
||||
CLOUDFLARE_IPV4_IP
|
||||
} else {
|
||||
internalIp.removeSurrounding("[", "]")
|
||||
}
|
||||
val prefix =
|
||||
if (parts.size == 2) parts[1].toIntOrNull() ?: 32 else 32
|
||||
if (prefix <= 1) {
|
||||
tunnelConf.pingTarget ?: CLOUDFLARE_IPV4_IP
|
||||
} else {
|
||||
internalIp.removeSurrounding("[", "]")
|
||||
}
|
||||
.invoke()
|
||||
}
|
||||
.invoke()
|
||||
|
||||
val attemptTime = System.currentTimeMillis()
|
||||
runCatching {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
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 setBackendStatus(backendStatus: BackendStatus)
|
||||
fun setBackendState(backendState: BackendState, allowedIps: Collection<String>)
|
||||
|
||||
fun getBackendStatus(): BackendStatus
|
||||
fun getBackendState(): BackendState
|
||||
|
||||
suspend fun runningTunnelNames(): Set<String>
|
||||
|
||||
|
||||
+39
-18
@@ -1,15 +1,15 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
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.asAmBackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
|
||||
import javax.inject.Inject
|
||||
import kotlin.jvm.optionals.getOrNull
|
||||
@@ -17,6 +17,7 @@ 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
|
||||
@@ -28,21 +29,14 @@ 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()
|
||||
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)
|
||||
}
|
||||
handleVpnKillSwitchWithDomainEndpoints(amConfig)
|
||||
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()
|
||||
@@ -59,20 +53,47 @@ constructor(
|
||||
} catch (e: BackendException) {
|
||||
Timber.e(e, "Failed to stop tunnel ${tunnel.id}")
|
||||
throw e.toBackendError()
|
||||
} finally {
|
||||
handlePreviouslyEnabledVpnKillSwitch()
|
||||
}
|
||||
}
|
||||
|
||||
override fun setBackendStatus(backendStatus: BackendStatus) {
|
||||
Timber.d("Setting backend state: $backendStatus")
|
||||
// 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")
|
||||
try {
|
||||
backend.backendStatus = backendStatus.asAmBackendStatus()
|
||||
backend.setBackendState(backendState.asAmBackendState(), allowedIps)
|
||||
} catch (e: BackendException) {
|
||||
throw e.toBackendError()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBackendStatus(): BackendStatus {
|
||||
return backend.backendStatus.asBackendStatus()
|
||||
override fun getBackendState(): BackendState {
|
||||
return backend.backendState.asBackendState()
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.enums
|
||||
|
||||
sealed class BackendStatus {
|
||||
data object Inactive : BackendStatus()
|
||||
|
||||
data object Active : BackendStatus()
|
||||
|
||||
data class KillSwitch(val allowedIps: List<String>) : BackendStatus()
|
||||
enum class BackendState {
|
||||
KILL_SWITCH_ACTIVE,
|
||||
SERVICE_ACTIVE,
|
||||
INACTIVE,
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.state
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import org.amnezia.awg.crypto.Key
|
||||
|
||||
data class TunnelState(
|
||||
val status: TunnelStatus = TunnelStatus.Down,
|
||||
val backendState: BackendStatus = BackendStatus.Inactive,
|
||||
val backendState: BackendState = BackendState.INACTIVE,
|
||||
val statistics: TunnelStatistics? = null,
|
||||
val pingStates: Map<Key, PingState>? = null,
|
||||
val handshakeSuccessLogs: Boolean? = null,
|
||||
|
||||
+19
-37
@@ -7,7 +7,10 @@ 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.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -22,7 +25,6 @@ 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
|
||||
@@ -30,7 +32,6 @@ import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||
@Composable
|
||||
fun ConfigScreen(
|
||||
tunnelConf: TunnelConf?,
|
||||
appUiState: AppUiState,
|
||||
appViewModel: AppViewModel,
|
||||
viewModel: ConfigViewModel = hiltViewModel(),
|
||||
) {
|
||||
@@ -41,17 +42,6 @@ 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
|
||||
@@ -68,34 +58,26 @@ fun ConfigScreen(
|
||||
appViewModel.handleEvent(
|
||||
AppEvent.SetScreenAction {
|
||||
keyboardController?.hide()
|
||||
if (!isTunnelNameTaken) {
|
||||
save = true
|
||||
}
|
||||
viewModel.save(tunnelConf)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(tunnelConf) { viewModel.initFromTunnel(tunnelConf) }
|
||||
|
||||
// TODO improve error messages
|
||||
LaunchedEffect(save) {
|
||||
if (save) {
|
||||
try {
|
||||
appViewModel.handleEvent(
|
||||
AppEvent.SaveTunnel(
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +111,7 @@ fun ConfigScreen(
|
||||
modifier =
|
||||
Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 24.dp),
|
||||
) {
|
||||
InterfaceSection(isTunnelNameTaken, uiState, viewModel)
|
||||
InterfaceSection(uiState, viewModel)
|
||||
PeersSection(uiState, viewModel)
|
||||
AddPeerButton(viewModel)
|
||||
}
|
||||
|
||||
+48
-1
@@ -1,20 +1,32 @@
|
||||
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() : ViewModel() {
|
||||
class ConfigViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val tunnelRepository: TunnelRepository,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
|
||||
private val _uiState = MutableStateFlow(ConfigUiState())
|
||||
val uiState: StateFlow<ConfigUiState> = _uiState.asStateFlow()
|
||||
@@ -109,6 +121,41 @@ class ConfigViewModel @Inject constructor() : ViewModel() {
|
||||
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) }
|
||||
}
|
||||
|
||||
+1
-6
@@ -17,11 +17,7 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigViewMode
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
|
||||
|
||||
@Composable
|
||||
fun InterfaceSection(
|
||||
isTunnelNameTaken: Boolean,
|
||||
uiState: ConfigUiState,
|
||||
viewModel: ConfigViewModel,
|
||||
) {
|
||||
fun InterfaceSection(uiState: ConfigUiState, viewModel: ConfigViewModel) {
|
||||
var isDropDownExpanded by remember { mutableStateOf(false) }
|
||||
val isAmneziaCompatibilitySet =
|
||||
remember(uiState.configProxy.`interface`) {
|
||||
@@ -54,7 +50,6 @@ fun InterfaceSection(
|
||||
value = uiState.tunnelName,
|
||||
onValueChange = viewModel::updateTunnelName,
|
||||
label = stringResource(R.string.name),
|
||||
isError = isTunnelNameTaken,
|
||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
|
||||
+3
-1
@@ -3,6 +3,7 @@ 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 = "",
|
||||
@@ -12,5 +13,6 @@ data class ConfigUiState(
|
||||
val showScripts: Boolean = false,
|
||||
val isAuthenticated: Boolean = true,
|
||||
val showAuthPrompt: Boolean = false,
|
||||
val saveChanges: Boolean = false,
|
||||
val message: StringValue? = null,
|
||||
val success: Boolean? = null,
|
||||
)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
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) {
|
||||
@@ -29,20 +28,6 @@ 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
|
||||
|
||||
+5
-14
@@ -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.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.HandshakeStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
@@ -75,21 +75,12 @@ fun Config.defaultName(): String {
|
||||
}
|
||||
}
|
||||
|
||||
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 Backend.BackendState.asBackendState(): BackendState {
|
||||
return 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 BackendState.asAmBackendState(): Backend.BackendState {
|
||||
return Backend.BackendState.valueOf(this.name)
|
||||
}
|
||||
|
||||
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.BackendStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings
|
||||
@@ -35,12 +35,6 @@ 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.*
|
||||
@@ -51,6 +45,12 @@ 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,7 +148,8 @@ constructor(
|
||||
is AppEvent.ImportTunnelFromClipboard ->
|
||||
handleClipboardImport(event.text, state.tunnels)
|
||||
|
||||
is AppEvent.ImportTunnelFromFile -> handleImportTunnelFromFile(event.data)
|
||||
is AppEvent.ImportTunnelFromFile ->
|
||||
handleImportTunnelFromFile(event.data, state.tunnels)
|
||||
|
||||
is AppEvent.ImportTunnelFromUrl ->
|
||||
handleImportTunnelFromUrl(event.url, state.tunnels)
|
||||
@@ -263,45 +264,11 @@ constructor(
|
||||
saveSettings(
|
||||
state.appSettings.copy(tunnelPingTimeoutSeconds = event.timeout)
|
||||
)
|
||||
is AppEvent.SaveTunnel -> saveTunnel(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) }
|
||||
|
||||
@@ -452,7 +419,7 @@ constructor(
|
||||
_appViewState.update { it.copy(bottomSheet = bottomSheet) }
|
||||
|
||||
private suspend fun handleTunnelPingTargetChange(tunnelConf: TunnelConf, target: String) =
|
||||
saveTunnel(tunnelConf.copy(pingTarget = target.ifBlank { null }))
|
||||
saveTunnel(tunnelConf.copy(pingTarget = target))
|
||||
|
||||
private suspend fun handleTogglePingTunnel(tunnel: TunnelConf) =
|
||||
saveTunnel(tunnel.copy(restartOnPingFailure = !tunnel.restartOnPingFailure))
|
||||
@@ -548,10 +515,17 @@ constructor(
|
||||
_appViewState.update { it.copy(popBackStack = true) }
|
||||
}
|
||||
|
||||
private suspend fun handleImportTunnelFromFile(uri: Uri) {
|
||||
private suspend fun handleImportTunnelFromFile(uri: Uri, tunnels: List<TunnelConf>) {
|
||||
runCatching {
|
||||
val tunnelConfigs = fileUtils.buildTunnelsFromUri(uri)
|
||||
saveTunnelsUniquely(tunnelConfigs)
|
||||
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)
|
||||
}
|
||||
.onFailure {
|
||||
when (it) {
|
||||
@@ -570,7 +544,11 @@ constructor(
|
||||
runCatching {
|
||||
val amConfig = TunnelConf.configFromAmQuick(config)
|
||||
val tunnelConf = TunnelConf.tunnelConfigFromAmConfig(amConfig)
|
||||
saveTunnelsUniquely(listOf(tunnelConf))
|
||||
saveTunnel(
|
||||
tunnelConf.copy(
|
||||
tunName = tunnelConf.generateUniqueName(tunnels.map { it.tunName })
|
||||
)
|
||||
)
|
||||
}
|
||||
.onFailure {
|
||||
Timber.e(it)
|
||||
@@ -588,7 +566,11 @@ constructor(
|
||||
url.openStream().use { stream ->
|
||||
val amConfig = Config.parse(stream)
|
||||
val tunnelConf = TunnelConf.tunnelConfigFromAmConfig(amConfig)
|
||||
saveTunnelsUniquely(listOf(tunnelConf))
|
||||
saveTunnel(
|
||||
tunnelConf.copy(
|
||||
tunName = tunnelConf.generateUniqueName(tunnels.map { it.tunName })
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
.onFailure {
|
||||
@@ -666,19 +648,19 @@ constructor(
|
||||
val updatedSettings =
|
||||
appSettings.copy(isLanOnKillSwitchEnabled = !appSettings.isLanOnKillSwitchEnabled)
|
||||
saveSettings(updatedSettings)
|
||||
handleKillSwitchChange(updatedSettings)
|
||||
handleKillSwitchChange(appSettings)
|
||||
}
|
||||
|
||||
private fun handleKillSwitchChange(appSettings: AppSettings) {
|
||||
// let auto tunnel handle kill switch changes if running
|
||||
if (uiState.value.isAutoTunnelActive) return
|
||||
if (!appSettings.isVpnKillSwitchEnabled)
|
||||
return tunnelManager.setBackendStatus(BackendStatus.Active)
|
||||
return tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||
Timber.d("Starting kill switch")
|
||||
val allowedIps =
|
||||
if (appSettings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS
|
||||
else emptyList()
|
||||
tunnelManager.setBackendStatus(BackendStatus.KillSwitch(allowedIps))
|
||||
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
|
||||
}
|
||||
|
||||
private suspend fun handleToggleAppShortcuts(appSettings: AppSettings) {
|
||||
@@ -713,7 +695,7 @@ constructor(
|
||||
}
|
||||
if (enabled && !requestRoot()) return
|
||||
// disable kill switch feature in kernel mode
|
||||
tunnelManager.setBackendStatus(BackendStatus.Inactive)
|
||||
tunnelManager.setBackendState(BackendState.INACTIVE, emptyList())
|
||||
saveSettings(
|
||||
appSettings.copy(
|
||||
isKernelEnabled = enabled,
|
||||
|
||||
@@ -77,8 +77,6 @@ sealed class AppEvent {
|
||||
|
||||
data class SetTheme(val theme: Theme) : AppEvent()
|
||||
|
||||
data class SaveTunnel(val tunnel: TunnelConf) : AppEvent()
|
||||
|
||||
data class SaveMonitoringSettings(
|
||||
val pingInterval: Int,
|
||||
val tunnelPingAttempts: Int,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
object Constants {
|
||||
const val VERSION_NAME = "3.9.5"
|
||||
const val VERSION_CODE = 39500
|
||||
const val VERSION_NAME = "3.9.4"
|
||||
const val VERSION_CODE = 39400
|
||||
const val TARGET_SDK = 35
|
||||
const val MIN_SDK = 26
|
||||
const val APP_ID = "com.zaneschepke.wireguardautotunnel"
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
What's new:
|
||||
- Fix for tunnel sort bug
|
||||
- Improved location permissions flow
|
||||
- Location permission detection and notifications
|
||||
- Fix for AndroidTV apps detection for split tunneling
|
||||
- Improved tunnel monitoring and reboot recovery
|
||||
- Fix tunnel slow reconnect from sleep
|
||||
@@ -1,7 +1,7 @@
|
||||
[versions]
|
||||
accompanist = "0.37.3"
|
||||
activityCompose = "1.10.1"
|
||||
amneziawgAndroid = "1.6.2"
|
||||
amneziawgAndroid = "1.4.0"
|
||||
androidx-junit = "1.3.0"
|
||||
icmp4a = "1.0.0"
|
||||
roomdatabasebackup = "1.1.0"
|
||||
|
||||
Reference in New Issue
Block a user