mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5eecb11d10 | |||
| 083904638f |
@@ -201,6 +201,7 @@ dependencies {
|
||||
implementation(libs.material.icons.core)
|
||||
implementation(libs.material.icons.extended)
|
||||
|
||||
implementation(libs.androidx.biometric.ktx)
|
||||
implementation(libs.pin.lock.compose)
|
||||
|
||||
implementation(libs.androidx.core)
|
||||
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
-keep class com.zaneschepke.wireguardautotunnel.ui.navigation.Route { *; }
|
||||
-keep class com.zaneschepke.wireguardautotunnel.ui.navigation.Route$** { *; }
|
||||
-keepclassmembers class com.zaneschepke.wireguardautotunnel.ui.navigation.Route$** { *; }
|
||||
@@ -5,11 +5,10 @@
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
|
||||
<!--foreground service special use for non VPN service tunnels, android 14-->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
|
||||
<!--foreground service special use for VPN service tunnels, android 14-->
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
||||
<!--foreground service exempt android 14-->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
|
||||
tools:ignore="ProtectedPermissions" />
|
||||
|
||||
<!--foreground service permissions-->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
@@ -152,45 +151,21 @@
|
||||
android:name=".core.service.autotunnel.AutoTunnelService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="specialUse"
|
||||
android:foregroundServiceType="systemExempted"
|
||||
android:persistent="true"
|
||||
android:stopWithTask="false"
|
||||
tools:node="merge">
|
||||
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
|
||||
android:value="This service monitors network changes to automatically
|
||||
establish and maintain WireGuard VPN tunnels on demand, ensuring seamless connectivity.
|
||||
It requires persistent foreground execution to detect real-time events,
|
||||
which cannot be achieved with standard background APIs due to timing and reliability needs for
|
||||
network connectivity monitoring."/>
|
||||
</service>
|
||||
|
||||
<service
|
||||
android:name=".core.service.TunnelForegroundService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="specialUse"
|
||||
android:persistent="true"
|
||||
android:stopWithTask="false"
|
||||
tools:node="merge">
|
||||
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
|
||||
android:value="This service sustains non-VpnService virtual tunnels (using gVisor/netstack for
|
||||
isolated networking), keeping connections alive for continuous secure data routing.
|
||||
Persistent foreground operation is essential to handle
|
||||
low-level tunnel maintenance and avoid interruptions, beyond the capabilities of other
|
||||
service types or background work."/>
|
||||
</service>
|
||||
tools:node="merge" />
|
||||
|
||||
<service
|
||||
android:name=".core.service.VpnForegroundService"
|
||||
android:name=".core.service.TunnelForegroundService"
|
||||
android:exported="false"
|
||||
android:persistent="true"
|
||||
android:foregroundServiceType="systemExempted"
|
||||
android:foregroundServiceType="systemExempted"
|
||||
android:permission="android.permission.BIND_VPN_SERVICE">
|
||||
<intent-filter>
|
||||
<action android:name="android.net.VpnService" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<receiver
|
||||
android:name=".core.broadcast.RestartReceiver"
|
||||
android:enabled="true"
|
||||
|
||||
@@ -17,12 +17,10 @@ import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -52,7 +50,6 @@ import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.components.BottomNavbar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.components.DynamicTopAppBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.components.currentBackStackEntryAsNavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.AutoTunnelScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.AutoTunnelAdvancedScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.detection.WifiDetectionMethodScreen
|
||||
@@ -67,8 +64,6 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.settings.logs.LogsScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.monitoring.TunnelMonitoringScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.system.SystemFeaturesScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.DonateScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.crypto.AddressesScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.license.LicenseScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.TunnelsScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.autotunnel.TunnelAutoTunnelScreen
|
||||
@@ -87,7 +82,6 @@ import de.raphaelebner.roomdatabasebackup.core.RoomBackup
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -138,8 +132,6 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
val navState by
|
||||
navController.currentBackStackEntryAsNavbarState(viewModel, navController)
|
||||
val snackbar = remember { SnackbarHostState() }
|
||||
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
||||
var vpnPermissionDenied by remember { mutableStateOf(false) }
|
||||
@@ -147,8 +139,6 @@ class MainActivity : AppCompatActivity() {
|
||||
mutableStateOf<Pair<AppMode?, TunnelConf?>>(Pair(null, null))
|
||||
}
|
||||
|
||||
LaunchedEffect(navState) { Timber.d("New navbar state $navState") }
|
||||
|
||||
val vpnActivity =
|
||||
rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult(),
|
||||
@@ -247,14 +237,14 @@ class MainActivity : AppCompatActivity() {
|
||||
)
|
||||
}
|
||||
},
|
||||
topBar = { DynamicTopAppBar(navState) },
|
||||
topBar = { DynamicTopAppBar(appState.navBarState) },
|
||||
bottomBar = {
|
||||
BottomNavbar(appState.isAutoTunnelActive, navState, navController)
|
||||
BottomNavbar(
|
||||
appState.isAutoTunnelActive,
|
||||
appState.navBarState,
|
||||
navController,
|
||||
)
|
||||
},
|
||||
modifier =
|
||||
Modifier.pointerInput(Unit) {
|
||||
detectTapGestures { viewModel.clearSelectedTunnels() }
|
||||
},
|
||||
) { padding ->
|
||||
Box(
|
||||
modifier =
|
||||
@@ -390,8 +380,6 @@ class MainActivity : AppCompatActivity() {
|
||||
SupportScreen(viewModel)
|
||||
}
|
||||
composable<Route.License> { LicenseScreen() }
|
||||
composable<Route.Donate> { DonateScreen(navController) }
|
||||
composable<Route.Addresses> { AddressesScreen() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.os.StrictMode
|
||||
import android.os.StrictMode.ThreadPolicy
|
||||
import androidx.hilt.work.HiltWorkerFactory
|
||||
import androidx.work.Configuration
|
||||
import com.wireguard.android.backend.GoBackend
|
||||
import com.zaneschepke.logcatter.LogReader
|
||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationMonitor
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
@@ -24,7 +25,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import org.amnezia.awg.backend.GoBackend
|
||||
import timber.log.Timber
|
||||
|
||||
@HiltAndroidApp
|
||||
|
||||
+3
-3
@@ -31,9 +31,9 @@ class NotificationActionReceiver : BroadcastReceiver() {
|
||||
NotificationAction.AUTO_TUNNEL_OFF.name -> serviceManager.stopAutoTunnel()
|
||||
NotificationAction.TUNNEL_OFF.name -> {
|
||||
val tunnelId = intent.getIntExtra(NotificationManager.EXTRA_ID, 0)
|
||||
if (tunnelId == STOP_ALL_TUNNELS_ID)
|
||||
return@launch tunnelManager.stopActiveTunnels()
|
||||
tunnelRepository.getById(tunnelId)?.let { tunnelManager.stopTunnel(it.id) }
|
||||
if (tunnelId == STOP_ALL_TUNNELS_ID) return@launch tunnelManager.stopTunnel()
|
||||
val tunnel = tunnelRepository.getById(tunnelId)
|
||||
tunnelManager.stopTunnel(tunnel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -71,11 +71,11 @@ class RemoteControlReceiver : BroadcastReceiver() {
|
||||
Action.STOP_TUNNEL -> {
|
||||
val tunnelName =
|
||||
intent.getStringExtra(EXTRA_TUN_NAME)
|
||||
?: return@launch tunnelManager.stopActiveTunnels()
|
||||
?: return@launch tunnelManager.stopTunnel()
|
||||
val tunnel =
|
||||
appDataRepository.tunnels.findByTunnelName(tunnelName)
|
||||
?: return@launch tunnelManager.stopActiveTunnels()
|
||||
tunnelManager.stopTunnel(tunnel.id)
|
||||
?: return@launch tunnelManager.stopTunnel()
|
||||
tunnelManager.stopTunnel(tunnel)
|
||||
}
|
||||
Action.START_AUTO_TUNNEL -> serviceManager.startAutoTunnel()
|
||||
Action.STOP_AUTO_TUNNEL -> serviceManager.stopAutoTunnel()
|
||||
|
||||
+4
-4
@@ -22,12 +22,12 @@ constructor(
|
||||
}
|
||||
|
||||
private suspend fun handleTunnelErrors() =
|
||||
tunnelManager.errorEvents.collectLatest { (tunName, error) ->
|
||||
tunnelManager.errorEvents.collectLatest { (tunnelConf, error) ->
|
||||
if (!WireGuardAutoTunnel.uiActive.value) {
|
||||
val notification =
|
||||
notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = StringValue.DynamicString(tunName),
|
||||
title = StringValue.DynamicString(tunnelConf.name),
|
||||
description =
|
||||
when (error) {
|
||||
is BackendCoreException.BounceFailed -> error.toStringValue()
|
||||
@@ -46,12 +46,12 @@ constructor(
|
||||
}
|
||||
|
||||
private suspend fun handleTunnelMessages() =
|
||||
tunnelManager.messageEvents.collectLatest { (tunName, message) ->
|
||||
tunnelManager.messageEvents.collectLatest { (tunnelConf, message) ->
|
||||
if (!WireGuardAutoTunnel.uiActive.value) {
|
||||
val notification =
|
||||
notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = StringValue.DynamicString(tunName),
|
||||
title = StringValue.DynamicString(tunnelConf.name),
|
||||
description = message.toStringValue(),
|
||||
)
|
||||
notificationManager.show(
|
||||
|
||||
-153
@@ -1,153 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service
|
||||
|
||||
import android.app.Notification
|
||||
import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.ServiceCompat
|
||||
import androidx.lifecycle.LifecycleService
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
|
||||
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.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.distinctByKeys
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import io.ktor.util.collections.*
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
abstract class BaseTunnelForegroundService : LifecycleService(), TunnelService {
|
||||
|
||||
@Inject lateinit var notificationManager: NotificationManager
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
@Inject lateinit var tunnelMonitor: TunnelMonitor
|
||||
|
||||
@Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
|
||||
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
private val tunnelJobs = ConcurrentMap<Int, Job>()
|
||||
|
||||
protected abstract val fgsType: Int
|
||||
|
||||
override fun onBind(intent: Intent): IBinder {
|
||||
super.onBind(intent)
|
||||
return LocalBinder(this)
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
ServiceCompat.startForeground(
|
||||
this,
|
||||
NotificationManager.VPN_NOTIFICATION_ID,
|
||||
onCreateNotification(),
|
||||
fgsType,
|
||||
)
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
ServiceCompat.startForeground(
|
||||
this,
|
||||
NotificationManager.VPN_NOTIFICATION_ID,
|
||||
onCreateNotification(),
|
||||
fgsType,
|
||||
)
|
||||
start()
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
override fun start() {
|
||||
lifecycleScope.launch(ioDispatcher) {
|
||||
tunnelManager.activeTunnels.distinctByKeys().collect { activeTunnels ->
|
||||
val activeTunConfigs = activeTunnels.keys
|
||||
val obsoleteJobs = tunnelJobs.keys - activeTunConfigs
|
||||
obsoleteJobs.forEach { tunId -> tunnelJobs[tunId]?.cancel() }
|
||||
activeTunConfigs.forEach { tunId ->
|
||||
if (tunnelJobs.contains(tunId)) return@forEach
|
||||
tunnelJobs[tunId] = launch { tunnelMonitor.startMonitoring(tunId, true) }
|
||||
}
|
||||
val tunnels = appDataRepository.tunnels.getAll()
|
||||
val activeConfigs = tunnels.filter { activeTunConfigs.contains(it.id) }
|
||||
updateServiceNotification(activeConfigs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Would be cool to have this include kill switch
|
||||
private fun updateServiceNotification(activeConfigs: List<TunnelConf>) {
|
||||
val notification =
|
||||
when (activeConfigs.size) {
|
||||
0 -> onCreateNotification()
|
||||
1 -> createTunnelNotification(activeConfigs.first())
|
||||
else -> createTunnelsNotification()
|
||||
}
|
||||
ServiceCompat.startForeground(
|
||||
this,
|
||||
NotificationManager.VPN_NOTIFICATION_ID,
|
||||
notification,
|
||||
fgsType,
|
||||
)
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
Timber.d("Stop called")
|
||||
tunnelJobs.forEach { it.value.cancel() }
|
||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
tunnelJobs.forEach { it.value.cancel() }
|
||||
serviceManager.handleTunnelServiceDestroy()
|
||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
Timber.d("onDestroy")
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun createTunnelNotification(tunnelConf: TunnelConf): Notification {
|
||||
return notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = "${getString(R.string.tunnel_running)} - ${tunnelConf.tunName}",
|
||||
actions =
|
||||
listOf(
|
||||
notificationManager.createNotificationAction(
|
||||
NotificationAction.TUNNEL_OFF,
|
||||
tunnelConf.id,
|
||||
)
|
||||
),
|
||||
onGoing = true,
|
||||
)
|
||||
}
|
||||
|
||||
private fun createTunnelsNotification(): Notification {
|
||||
return notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = "${getString(R.string.tunnel_running)} - ${getString(R.string.multiple)}",
|
||||
actions =
|
||||
listOf(
|
||||
notificationManager.createNotificationAction(NotificationAction.TUNNEL_OFF, 0)
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun onCreateNotification(): Notification {
|
||||
return notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = getString(R.string.tunnel_starting),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service
|
||||
|
||||
import android.os.Binder
|
||||
|
||||
class LocalBinder(val service: TunnelService) : Binder()
|
||||
+52
-71
@@ -7,16 +7,15 @@ import android.content.ServiceConnection
|
||||
import android.net.VpnService
|
||||
import android.os.IBinder
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
||||
import jakarta.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
@@ -27,45 +26,29 @@ class ServiceManager
|
||||
@Inject
|
||||
constructor(
|
||||
private val context: Context,
|
||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
private val ioDispatcher: CoroutineDispatcher,
|
||||
private val applicationScope: CoroutineScope,
|
||||
private val mainDispatcher: CoroutineDispatcher,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
) {
|
||||
|
||||
private val autoTunnelMutex = Mutex()
|
||||
private val tunnelMutex = Mutex()
|
||||
|
||||
private val _tunnelService = MutableStateFlow<TunnelService?>(null)
|
||||
private val _tunnelService = MutableStateFlow<TunnelForegroundService?>(null)
|
||||
private val _autoTunnelService = MutableStateFlow<AutoTunnelService?>(null)
|
||||
val autoTunnelService = _autoTunnelService.asStateFlow()
|
||||
val tunnelService = _tunnelService.asStateFlow()
|
||||
|
||||
private val tunnelServiceConnection =
|
||||
object : ServiceConnection {
|
||||
override fun onServiceConnected(name: ComponentName, service: IBinder) {
|
||||
val binder = service as? LocalBinder
|
||||
val binder = service as? TunnelForegroundService.LocalBinder
|
||||
_tunnelService.value = binder?.service
|
||||
val serviceClass =
|
||||
when {
|
||||
name.className.contains("VpnForegroundService") -> "VpnForegroundService"
|
||||
name.className.contains("TunnelForegroundService") ->
|
||||
"TunnelForegroundService"
|
||||
else -> "Unknown"
|
||||
}
|
||||
Timber.d("$serviceClass connected")
|
||||
Timber.d("TunnelForegroundService connected")
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName) {
|
||||
_tunnelService.value = null
|
||||
val serviceClass =
|
||||
when {
|
||||
name.className.contains("VpnForegroundService") -> "VpnForegroundService"
|
||||
name.className.contains("TunnelForegroundService") ->
|
||||
"TunnelForegroundService"
|
||||
else -> "Unknown"
|
||||
}
|
||||
Timber.d("$serviceClass disconnected")
|
||||
Timber.d("TunnelForegroundService disconnected")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,74 +66,72 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
// Observe changes to the AutoTunnelService and trigger side effects
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
_autoTunnelService
|
||||
.onEach { service ->
|
||||
withContext(mainDispatcher) { updateAutoTunnelTile() }
|
||||
if (service == null) {
|
||||
// The service is disconnected, update the DB state
|
||||
val settings = appDataRepository.settings.get()
|
||||
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
|
||||
}
|
||||
}
|
||||
.launchIn(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun hasVpnPermission(): Boolean {
|
||||
return VpnService.prepare(context) == null
|
||||
}
|
||||
|
||||
suspend fun startAutoTunnel() {
|
||||
autoTunnelMutex.withLock {
|
||||
val settings = appDataRepository.settings.get()
|
||||
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true))
|
||||
if (_autoTunnelService.value != null) return
|
||||
val intent = Intent(context, AutoTunnelService::class.java)
|
||||
context.startForegroundService(intent)
|
||||
context.bindService(intent, autoTunnelServiceConnection, Context.BIND_AUTO_CREATE)
|
||||
withContext(ioDispatcher) {
|
||||
val intent = Intent(context, AutoTunnelService::class.java)
|
||||
context.startForegroundService(intent)
|
||||
context.bindService(intent, autoTunnelServiceConnection, Context.BIND_AUTO_CREATE)
|
||||
withContext(mainDispatcher) { updateAutoTunnelTile() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun stopAutoTunnel() =
|
||||
suspend fun stopAutoTunnel() {
|
||||
autoTunnelMutex.withLock {
|
||||
if (_autoTunnelService.value == null) return@withLock
|
||||
val settings = appDataRepository.settings.get()
|
||||
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
|
||||
if (_autoTunnelService.value == null) return
|
||||
_autoTunnelService.value?.let { service ->
|
||||
service.stop()
|
||||
try {
|
||||
context.unbindService(autoTunnelServiceConnection)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to unbind AutoTunnelService")
|
||||
} finally {
|
||||
_tunnelService.value = null
|
||||
}
|
||||
}
|
||||
withContext(mainDispatcher) { updateAutoTunnelTile() }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun startTunnelService(appMode: AppMode) =
|
||||
tunnelMutex.withLock {
|
||||
if (_tunnelService.value != null) return@withLock
|
||||
val serviceClass =
|
||||
when (appMode) {
|
||||
AppMode.VPN,
|
||||
AppMode.LOCK_DOWN -> VpnForegroundService::class.java
|
||||
AppMode.KERNEL,
|
||||
AppMode.PROXY -> TunnelForegroundService::class.java
|
||||
}
|
||||
val intent = Intent(context, serviceClass)
|
||||
context.startForegroundService(intent)
|
||||
context.bindService(intent, tunnelServiceConnection, Context.BIND_AUTO_CREATE)
|
||||
}
|
||||
|
||||
suspend fun stopTunnelService() =
|
||||
tunnelMutex.withLock {
|
||||
_tunnelService.value?.let { service ->
|
||||
service.stop()
|
||||
try {
|
||||
context.unbindService(tunnelServiceConnection)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to stop Tunnel Service")
|
||||
}
|
||||
suspend fun startTunnelForegroundService() {
|
||||
if (_tunnelService.value != null) return
|
||||
withContext(ioDispatcher) {
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
val intent = Intent(context, TunnelForegroundService::class.java)
|
||||
context.startForegroundService(intent)
|
||||
context.bindService(intent, tunnelServiceConnection, Context.BIND_AUTO_CREATE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stopTunnelForegroundService() {
|
||||
_tunnelService.value?.let { service ->
|
||||
service.stop()
|
||||
try {
|
||||
context.unbindService(tunnelServiceConnection)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to stop TunnelForegroundService")
|
||||
} finally {
|
||||
_tunnelService.value = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun toggleAutoTunnel() {
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
if (_autoTunnelService.value != null) stopAutoTunnel() else startAutoTunnel()
|
||||
}
|
||||
}
|
||||
|
||||
fun updateAutoTunnelTile() {
|
||||
context.requestAutoTunnelTileServiceUpdate()
|
||||
|
||||
+149
-2
@@ -1,8 +1,155 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service
|
||||
|
||||
import android.app.Notification
|
||||
import android.content.Intent
|
||||
import android.os.Binder
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.ServiceCompat
|
||||
import androidx.lifecycle.LifecycleService
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
|
||||
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.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.distinctByKeys
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import io.ktor.util.collections.*
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
class TunnelForegroundService(override val fgsType: Int = Constants.SPECIAL_USE_SERVICE_TYPE_ID) :
|
||||
BaseTunnelForegroundService()
|
||||
class TunnelForegroundService : LifecycleService() {
|
||||
|
||||
@Inject lateinit var notificationManager: NotificationManager
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
@Inject lateinit var tunnelMonitor: TunnelMonitor
|
||||
|
||||
@Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
|
||||
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
class LocalBinder(val service: TunnelForegroundService) : Binder()
|
||||
|
||||
private val tunnelJobs = ConcurrentMap<TunnelConf, Job>()
|
||||
|
||||
private val binder = LocalBinder(this)
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
ServiceCompat.startForeground(
|
||||
this@TunnelForegroundService,
|
||||
NotificationManager.VPN_NOTIFICATION_ID,
|
||||
onCreateNotification(),
|
||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder {
|
||||
super.onBind(intent)
|
||||
return binder
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
ServiceCompat.startForeground(
|
||||
this@TunnelForegroundService,
|
||||
NotificationManager.VPN_NOTIFICATION_ID,
|
||||
onCreateNotification(),
|
||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||
)
|
||||
start()
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
fun start() =
|
||||
lifecycleScope.launch(ioDispatcher) {
|
||||
tunnelManager.activeTunnels.distinctByKeys().collect { activeTunnels ->
|
||||
val activeTunConfigs = activeTunnels.keys
|
||||
val obsoleteJobs = tunnelJobs.keys - activeTunConfigs
|
||||
obsoleteJobs.forEach { tunnelConf -> tunnelJobs[tunnelConf]?.cancel() }
|
||||
activeTunConfigs.forEach { tun ->
|
||||
if (tunnelJobs.containsKey(tun)) return@forEach
|
||||
tunnelJobs[tun] = launch { tunnelMonitor.startMonitoring(tun, true) }
|
||||
}
|
||||
updateServiceNotification(activeTunnels)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Would be cool to have this include kill switch
|
||||
private fun updateServiceNotification(activeTunnels: Map<TunnelConf, TunnelState>) {
|
||||
val notification =
|
||||
when (activeTunnels.size) {
|
||||
0 -> onCreateNotification()
|
||||
1 -> createTunnelNotification(activeTunnels.keys.first())
|
||||
else -> createTunnelsNotification()
|
||||
}
|
||||
ServiceCompat.startForeground(
|
||||
this@TunnelForegroundService,
|
||||
NotificationManager.VPN_NOTIFICATION_ID,
|
||||
notification,
|
||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||
)
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
Timber.d("Stop called")
|
||||
tunnelJobs.forEach { it.value.cancel() }
|
||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
tunnelJobs.forEach { it.value.cancel() }
|
||||
serviceManager.handleTunnelServiceDestroy()
|
||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
Timber.d("onDestroy")
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun createTunnelNotification(tunnelConf: TunnelConf): Notification {
|
||||
return notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = "${getString(R.string.tunnel_running)} - ${tunnelConf.tunName}",
|
||||
actions =
|
||||
listOf(
|
||||
notificationManager.createNotificationAction(
|
||||
NotificationAction.TUNNEL_OFF,
|
||||
tunnelConf.id,
|
||||
)
|
||||
),
|
||||
onGoing = true,
|
||||
)
|
||||
}
|
||||
|
||||
private fun createTunnelsNotification(): Notification {
|
||||
return notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = "${getString(R.string.tunnel_running)} - ${getString(R.string.multiple)}",
|
||||
actions =
|
||||
listOf(
|
||||
notificationManager.createNotificationAction(NotificationAction.TUNNEL_OFF, 0)
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun onCreateNotification(): Notification {
|
||||
return notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = getString(R.string.tunnel_starting),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service
|
||||
|
||||
interface TunnelService {
|
||||
fun start()
|
||||
|
||||
fun stop()
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
@AndroidEntryPoint
|
||||
class VpnForegroundService(override val fgsType: Int = Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID) :
|
||||
BaseTunnelForegroundService()
|
||||
+70
-3
@@ -17,8 +17,10 @@ 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.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus.StopReason.Ping
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.AutoTunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.NetworkState
|
||||
@@ -29,6 +31,7 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.toMillis
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
import kotlin.math.pow
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
@@ -58,6 +61,12 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
private val autoTunnelStateFlow = MutableStateFlow(defaultState)
|
||||
|
||||
private val bounceCounts = MutableStateFlow<Map<Int, Int>>(emptyMap())
|
||||
|
||||
private var eventHandlerJob: Job? = null
|
||||
|
||||
private val lastBounceTimes = mutableMapOf<Int, Long>()
|
||||
|
||||
class LocalBinder(val service: AutoTunnelService) : Binder()
|
||||
|
||||
private val binder = LocalBinder(this)
|
||||
@@ -134,10 +143,20 @@ class AutoTunnelService : LifecycleService() {
|
||||
val tunnelsFlow =
|
||||
tunnelManager.activeTunnels.map { StateChange.ActiveTunnelsChange(it) }
|
||||
|
||||
val monitoringFlow =
|
||||
tunnelManager.activeTunnels
|
||||
.map { map -> map.mapValues { (_, state) -> state.pingStates } }
|
||||
.distinctUntilChanged()
|
||||
.map { StateChange.MonitoringChange(it) }
|
||||
|
||||
var reevaluationJob: Job? = null
|
||||
|
||||
// get everything in sync before we use merge
|
||||
combine(networkFlow, settingsFlow, tunnelsFlow) { network, settings, tunnels ->
|
||||
combine(networkFlow, settingsFlow, tunnelsFlow, monitoringFlow) {
|
||||
network,
|
||||
settings,
|
||||
tunnels,
|
||||
monitoring ->
|
||||
autoTunnelStateFlow.update {
|
||||
it.copy(
|
||||
activeTunnels = tunnels.activeTunnels,
|
||||
@@ -151,7 +170,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
// use merge to limit the noise of a combine and also increase the scalability of auto
|
||||
// tunnel handling new states
|
||||
merge(networkFlow, settingsFlow, tunnelsFlow).collect { change ->
|
||||
merge(networkFlow, settingsFlow, tunnelsFlow, monitoringFlow).collect { change ->
|
||||
if (change !is StateChange.ActiveTunnelsChange) {
|
||||
Timber.d("New state changed to ${change.javaClass.simpleName}")
|
||||
}
|
||||
@@ -182,6 +201,22 @@ class AutoTunnelService : LifecycleService() {
|
||||
autoTunnelStateFlow.update { it.copy(activeTunnels = change.activeTunnels) }
|
||||
return@collect
|
||||
}
|
||||
is StateChange.MonitoringChange -> {
|
||||
change.pingStates.forEach { (config, pingState) ->
|
||||
Timber.d("Ping state $pingState")
|
||||
if (pingState?.all { it.value.isReachable } == true) {
|
||||
Timber.d("Clearing bounce count on success")
|
||||
bounceCounts.update { current ->
|
||||
current.toMutableMap().apply { remove(config.id) }
|
||||
}
|
||||
}
|
||||
}
|
||||
return@collect handleAutoTunnelEvent(
|
||||
autoTunnelStateFlow.value.determineAutoTunnelEvent(
|
||||
StateChange.MonitoringChange(change.pingStates)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
handleAutoTunnelEvent(autoTunnelStateFlow.value.determineAutoTunnelEvent(change))
|
||||
@@ -346,8 +381,39 @@ class AutoTunnelService : LifecycleService() {
|
||||
(event.tunnelConf ?: appDataRepository.get().getPrimaryOrFirstTunnel())?.let {
|
||||
tunnelManager.startTunnel(it)
|
||||
}
|
||||
is AutoTunnelEvent.Stop -> tunnelManager.stopActiveTunnels()
|
||||
is AutoTunnelEvent.Stop -> tunnelManager.stopTunnel()
|
||||
AutoTunnelEvent.DoNothing -> Timber.i("Auto-tunneling: nothing to do")
|
||||
is AutoTunnelEvent.Bounce ->
|
||||
handleBounceWithBackoff(event.configsPeerKeyResolvedMap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleBounceWithBackoff(
|
||||
configsPeerKeyResolvedMap: List<Pair<TunnelConf, Map<String, String?>>>
|
||||
) { // Simplified param: no failureCount
|
||||
val settings = appDataRepository.get().settings.get()
|
||||
val pingIntervalMillis = settings.tunnelPingIntervalSeconds.toMillis()
|
||||
configsPeerKeyResolvedMap.forEach { (config, peerMap) ->
|
||||
val bounceCount = bounceCounts.value.getOrDefault(config.id, 0)
|
||||
val exponent = bounceCount.toDouble()
|
||||
val backoffDelay =
|
||||
(pingIntervalMillis * 2.0.pow(exponent)).toLong().coerceAtMost(MAX_BACKOFF_MS)
|
||||
val currentTime = System.currentTimeMillis()
|
||||
val lastTime = lastBounceTimes.getOrDefault(config.id, 0L)
|
||||
if (currentTime - lastTime >= backoffDelay) {
|
||||
Timber.d(
|
||||
"Bouncing tunnel ${config.name} after detecting failure, with bounce count $bounceCount and calculated backoff delay $backoffDelay ms"
|
||||
)
|
||||
tunnelManager.bounceTunnel(config, Ping(peerMap))
|
||||
lastBounceTimes[config.id] = currentTime
|
||||
bounceCounts.update { current ->
|
||||
current.toMutableMap().apply { this[config.id] = (this[config.id] ?: 0) + 1 }
|
||||
}
|
||||
} else {
|
||||
Timber.d(
|
||||
"Backoff in progress for tunnel ${config.name}, skipping bounce (required delay: $backoffDelay ms)"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -368,5 +434,6 @@ class AutoTunnelService : LifecycleService() {
|
||||
companion object {
|
||||
// try to keep this window short as it will interrupt manual overrides
|
||||
const val REEVALUATE_CHECK_DELAY = 2_000L
|
||||
const val MAX_BACKOFF_MS = 300_000L // 5 minutes
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -1,14 +1,20 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service.autotunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.NetworkState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.PingState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels
|
||||
import org.amnezia.awg.crypto.Key
|
||||
|
||||
sealed class StateChange {
|
||||
data class NetworkChange(val networkState: NetworkState) : StateChange()
|
||||
|
||||
data class SettingsChange(val settings: GeneralSettings, val tunnels: Tunnels) : StateChange()
|
||||
|
||||
data class ActiveTunnelsChange(val activeTunnels: Map<Int, TunnelState>) : StateChange()
|
||||
data class ActiveTunnelsChange(val activeTunnels: Map<TunnelConf, TunnelState>) : StateChange()
|
||||
|
||||
data class MonitoringChange(val pingStates: Map<TunnelConf, Map<Key, PingState>?>) :
|
||||
StateChange()
|
||||
}
|
||||
|
||||
+8
-9
@@ -13,7 +13,9 @@ import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -63,15 +65,12 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
|
||||
when {
|
||||
activeTunnels.isNotEmpty() -> {
|
||||
val activeIds = activeTunnels.map { it.key }
|
||||
val activeIds = activeTunnels.map { it.key.id }
|
||||
// TODO improvements would be needed to make this work well with toggling
|
||||
// multiple tunnels
|
||||
// this would be better managed elsewhere
|
||||
WireGuardAutoTunnel.setLastActiveTunnels(activeIds)
|
||||
val tunnels = appDataRepository.tunnels.getAll()
|
||||
val activeTunNames =
|
||||
tunnels.filter { activeTunnels.keys.contains(it.id) }.map { it.tunName }
|
||||
updateTileForActiveTunnels(activeTunNames)
|
||||
updateTileForActiveTunnels(activeTunnels)
|
||||
}
|
||||
else -> updateTileForLastActiveTunnels()
|
||||
}
|
||||
@@ -80,10 +79,10 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateTileForActiveTunnels(activeTunnelNames: List<String>) {
|
||||
private fun updateTileForActiveTunnels(activeTunnels: Map<TunnelConf, TunnelState>) {
|
||||
val tileName =
|
||||
when (activeTunnelNames.size) {
|
||||
1 -> activeTunnelNames[0]
|
||||
when (activeTunnels.size) {
|
||||
1 -> activeTunnels.keys.first().tunName
|
||||
else -> getString(R.string.multiple)
|
||||
}
|
||||
updateTile(tileName, true)
|
||||
@@ -112,7 +111,7 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
unlockAndRun {
|
||||
lifecycleScope.launch {
|
||||
if (tunnelManager.activeTunnels.value.isNotEmpty())
|
||||
return@launch tunnelManager.stopActiveTunnels()
|
||||
return@launch tunnelManager.stopTunnel()
|
||||
val lastActive = WireGuardAutoTunnel.getLastActiveTunnels()
|
||||
if (lastActive.isEmpty()) {
|
||||
appDataRepository.getStartTunnelConfig()?.let { tunnelManager.startTunnel(it) }
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ class ShortcutsActivity : ComponentActivity() {
|
||||
tunnelConfig?.let {
|
||||
when (intent.action) {
|
||||
Action.START.name -> tunnelManager.startTunnel(it)
|
||||
Action.STOP.name -> tunnelManager.stopActiveTunnels()
|
||||
Action.STOP.name -> tunnelManager.stopTunnel()
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
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.BackendMode
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.PingState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.ConfigProxy
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
@@ -24,60 +25,58 @@ import org.amnezia.awg.crypto.Key
|
||||
import timber.log.Timber
|
||||
|
||||
abstract class BaseTunnel(
|
||||
@ApplicationScope protected val applicationScope: CoroutineScope,
|
||||
protected val appDataRepository: AppDataRepository,
|
||||
protected val serviceManager: ServiceManager,
|
||||
private val applicationScope: CoroutineScope,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val serviceManager: ServiceManager,
|
||||
) : TunnelProvider {
|
||||
|
||||
protected val errors = MutableSharedFlow<Pair<String, BackendCoreException>>()
|
||||
override val errorEvents = errors.asSharedFlow()
|
||||
private val _errorEvents = MutableSharedFlow<Pair<TunnelConf, BackendCoreException>>()
|
||||
override val errorEvents = _errorEvents.asSharedFlow()
|
||||
|
||||
private val _messageEvents = MutableSharedFlow<Pair<String, BackendMessage>>()
|
||||
private val _messageEvents = MutableSharedFlow<Pair<TunnelConf, BackendMessage>>()
|
||||
override val messageEvents = _messageEvents.asSharedFlow()
|
||||
|
||||
protected val activeTuns = MutableStateFlow<Map<Int, TunnelState>>(emptyMap())
|
||||
private val activeTuns = MutableStateFlow<Map<TunnelConf, TunnelState>>(emptyMap())
|
||||
private val tunJobs = ConcurrentHashMap<Int, Job>()
|
||||
override val activeTunnels = activeTuns.asStateFlow()
|
||||
|
||||
private val tunJobs = ConcurrentHashMap<Int, Job>()
|
||||
private val tunMutex = Mutex()
|
||||
private val tunStatusMutex = Mutex()
|
||||
private val bounceTunnelMutex = Mutex()
|
||||
|
||||
abstract fun tunnelStateFlow(tunnelConf: TunnelConf): Flow<TunnelStatus>
|
||||
override val bouncingTunnelIds = ConcurrentHashMap<Int, TunnelStatus.StopReason>()
|
||||
|
||||
abstract override fun setBackendMode(backendMode: BackendMode)
|
||||
abstract suspend fun startBackend(tunnel: TunnelConf)
|
||||
|
||||
abstract override fun getBackendMode(): BackendMode
|
||||
|
||||
abstract override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean
|
||||
|
||||
abstract override fun getStatistics(tunnelId: Int): TunnelStatistics?
|
||||
abstract fun stopBackend(tunnel: TunnelConf)
|
||||
|
||||
override fun hasVpnPermission(): Boolean {
|
||||
return serviceManager.hasVpnPermission()
|
||||
}
|
||||
|
||||
override suspend fun updateTunnelStatus(
|
||||
tunnelId: Int,
|
||||
tunnelConf: TunnelConf,
|
||||
status: TunnelStatus?,
|
||||
stats: TunnelStatistics?,
|
||||
pingStates: Map<Key, PingState>?,
|
||||
logHealthState: LogHealthState?,
|
||||
handshakeSuccessLogs: Boolean?,
|
||||
) {
|
||||
tunStatusMutex.withLock {
|
||||
activeTuns.update { currentTuns ->
|
||||
val existingState = currentTuns[tunnelId] ?: TunnelState()
|
||||
val originalConf = currentTuns.getKeyById(tunnelConf.id) ?: tunnelConf
|
||||
val existingState = currentTuns.getValueById(tunnelConf.id) ?: TunnelState()
|
||||
val newStatus = status ?: existingState.status
|
||||
if (newStatus == TunnelStatus.Down) {
|
||||
Timber.d("Removing tunnel $tunnelId from activeTunnels as state is DOWN")
|
||||
cleanUpTunJob(tunnelId)
|
||||
currentTuns - tunnelId
|
||||
Timber.d("Removing tunnel ${tunnelConf.id} from activeTunnels as state is DOWN")
|
||||
cleanUpTunJob(tunnelConf)
|
||||
currentTuns - originalConf
|
||||
} else if (
|
||||
existingState.status == newStatus &&
|
||||
stats == null &&
|
||||
pingStates == null &&
|
||||
logHealthState == null
|
||||
handshakeSuccessLogs == null
|
||||
) {
|
||||
Timber.d("Skipping redundant state update for ${tunnelId}: $newStatus")
|
||||
Timber.d("Skipping redundant state update for ${tunnelConf.id}: $newStatus")
|
||||
currentTuns
|
||||
} else {
|
||||
val updated =
|
||||
@@ -85,15 +84,17 @@ abstract class BaseTunnel(
|
||||
status = newStatus,
|
||||
statistics = stats ?: existingState.statistics,
|
||||
pingStates = pingStates ?: existingState.pingStates,
|
||||
logHealthState = logHealthState ?: existingState.logHealthState,
|
||||
handshakeSuccessLogs =
|
||||
handshakeSuccessLogs ?: existingState.handshakeSuccessLogs,
|
||||
)
|
||||
currentTuns + (tunnelId to updated)
|
||||
currentTuns + (originalConf to updated)
|
||||
}
|
||||
}
|
||||
handleServiceStateOnChange()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun stopActiveTunnels() {
|
||||
private suspend fun stopActiveTunnels() {
|
||||
activeTunnels.value.forEach { (config, state) ->
|
||||
if (state.status.isUpOrStarting()) {
|
||||
stopTunnel(config)
|
||||
@@ -101,43 +102,191 @@ abstract class BaseTunnel(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||
tunMutex.withLock {
|
||||
if (activeTuns.value.containsKey(tunnelConf.id) || tunJobs.containsKey(tunnelConf.id)) {
|
||||
return Timber.w("Tunnel is already running: ${tunnelConf.tunName}")
|
||||
private fun configureTunnelCallbacks(tunnelConf: TunnelConf) {
|
||||
Timber.d("Configuring TunnelConf instance: ${tunnelConf.hashCode()}")
|
||||
tunnelConf.setStateChangeCallback { state ->
|
||||
applicationScope.launch {
|
||||
Timber.d(
|
||||
"State change callback triggered for tunnel ${tunnelConf.id}: ${tunnelConf.tunName} with state $state at ${System.currentTimeMillis()}"
|
||||
)
|
||||
when (state) {
|
||||
is Tunnel.State -> updateTunnelStatus(tunnelConf, state.asTunnelState())
|
||||
is org.amnezia.awg.backend.Tunnel.State ->
|
||||
updateTunnelStatus(tunnelConf, state.asTunnelState())
|
||||
}
|
||||
handleServiceStateOnChange()
|
||||
}
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
}
|
||||
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
|
||||
|
||||
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||
if (activeTuns.exists(tunnelConf.id) || tunJobs.containsKey(tunnelConf.id))
|
||||
return Timber.w("Tunnel is already running ${tunnelConf.name}")
|
||||
// For userspace, we need to make sure all previous tunnels are down
|
||||
if (this@BaseTunnel is UserspaceTunnel) stopActiveTunnels()
|
||||
tunMutex.withLock {
|
||||
val job =
|
||||
applicationScope.launch {
|
||||
try {
|
||||
tunnelStateFlow(tunnelConf).collect { status ->
|
||||
updateTunnelStatus(tunnelConf.id, status)
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
} catch (e: BackendCoreException) {
|
||||
errors.emit(tunnelConf.tunName to e)
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Down)
|
||||
} catch (_: CancellationException) {}
|
||||
Timber.d("Starting tunnel ${tunnelConf.id}...")
|
||||
startTunnelInner(tunnelConf)
|
||||
Timber.d("Started complete for tunnel ${tunnelConf.name}...")
|
||||
// catch cancellation that could occur before and during startTunnelInner
|
||||
// and trigger at that suspend point
|
||||
} catch (e: CancellationException) {
|
||||
Timber.w(
|
||||
"Tunnel start has been cancelled as ${tunnelConf.name} failed to start"
|
||||
)
|
||||
}
|
||||
}
|
||||
tunJobs[tunnelConf.id] = job
|
||||
job.invokeOnCompletion {
|
||||
tunJobs.remove(tunnelConf.id)
|
||||
activeTuns.update { it - tunnelConf.id }
|
||||
Timber.d("Start job completed for tunnel ${tunnelConf.id}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun stopTunnel(tunnelId: Int) {
|
||||
tunMutex.withLock {
|
||||
updateTunnelStatus(tunnelId, TunnelStatus.Stopping)
|
||||
tunJobs[tunnelId]?.cancel() // Triggers awaitClose to stop backend
|
||||
private suspend fun startTunnelInner(tunnelConf: TunnelConf) {
|
||||
configureTunnelCallbacks(tunnelConf)
|
||||
Timber.d("Starting backend for tunnel ${tunnelConf.id}...")
|
||||
|
||||
var currentConf = tunnelConf
|
||||
var restoreAttempted = false
|
||||
var originalError: BackendCoreException? = null
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
startBackend(currentConf)
|
||||
updateTunnelStatus(currentConf, TunnelStatus.Up)
|
||||
Timber.d("Started for tun ${currentConf.id}...")
|
||||
saveTunnelActiveState(currentConf, true)
|
||||
serviceManager.startTunnelForegroundService()
|
||||
if (restoreAttempted)
|
||||
_messageEvents.emit(tunnelConf to BackendMessage.BounceRecovery)
|
||||
if (bouncingTunnelIds[currentConf.id] is TunnelStatus.StopReason.Ping) {
|
||||
_messageEvents.emit(tunnelConf to BackendMessage.BounceSuccess)
|
||||
}
|
||||
return // Success, return
|
||||
} catch (e: BackendCoreException) {
|
||||
originalError = originalError ?: e
|
||||
val bounceReason = bouncingTunnelIds[currentConf.id]
|
||||
if (!restoreAttempted && bounceReason is TunnelStatus.StopReason.Ping) {
|
||||
Timber.i(
|
||||
"Attempting to recover bounce failure with previously resolved endpoints for ${currentConf.name}"
|
||||
)
|
||||
try {
|
||||
val previouslyResolved = bounceReason.previouslyResolvedEndpoints
|
||||
val configProxy = ConfigProxy.from(currentConf.toAmConfig())
|
||||
val updatedConfigProxy =
|
||||
configProxy.copy(
|
||||
peers =
|
||||
configProxy.peers.map {
|
||||
it.copy(
|
||||
endpoint =
|
||||
previouslyResolved[it.publicKey] ?: it.endpoint
|
||||
)
|
||||
}
|
||||
)
|
||||
val (wg, amnezia) = updatedConfigProxy.buildConfigs()
|
||||
currentConf =
|
||||
currentConf.copyWithCallback(
|
||||
amQuick = amnezia.toAwgQuickString(true, false),
|
||||
wgQuick = wg.toWgQuickString(true),
|
||||
)
|
||||
bouncingTunnelIds.remove(currentConf.id)
|
||||
restoreAttempted = true
|
||||
continue // Retry
|
||||
} catch (e: Exception) {
|
||||
Timber.e(
|
||||
e,
|
||||
"Failed to update config with resolved endpoints for ${currentConf.name}",
|
||||
)
|
||||
// Fall through to failure (will emit BounceFailed since
|
||||
// retryAttempted=true)
|
||||
}
|
||||
}
|
||||
Timber.e(e, "Failed to start backend for ${currentConf.name}")
|
||||
val emitError =
|
||||
if (restoreAttempted) BackendCoreException.BounceFailed(originalError) else e
|
||||
_errorEvents.emit(currentConf to emitError)
|
||||
updateTunnelStatus(currentConf, TunnelStatus.Down)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun cleanUpTunJob(tunnelId: Int) {
|
||||
Timber.d("Removing job for $tunnelId")
|
||||
tunJobs -= tunnelId
|
||||
private suspend fun saveTunnelActiveState(tunnelConf: TunnelConf, active: Boolean) {
|
||||
val tunnelCopy = tunnelConf.copyWithCallback(isActive = active)
|
||||
appDataRepository.tunnels.save(tunnelCopy)
|
||||
}
|
||||
|
||||
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
|
||||
if (tunnelConf == null) return stopActiveTunnels()
|
||||
tunMutex.withLock {
|
||||
if (activeTuns.isStarting(tunnelConf.id))
|
||||
return handleStuckStartingTunnelShutdown(tunnelConf)
|
||||
updateTunnelStatus(tunnelConf, TunnelStatus.Stopping(reason))
|
||||
stopTunnelInner(tunnelConf)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun stopTunnelInner(tunnelConf: TunnelConf) {
|
||||
try {
|
||||
val tunnel = activeTuns.findTunnel(tunnelConf.id) ?: return
|
||||
stopBackend(tunnel)
|
||||
saveTunnelActiveState(tunnelConf, false)
|
||||
removeActiveTunnel(tunnel)
|
||||
} catch (e: BackendCoreException) {
|
||||
Timber.e(e, "Failed to stop tunnel ${tunnelConf.id}")
|
||||
_errorEvents.emit(tunnelConf to e)
|
||||
updateTunnelStatus(tunnelConf, TunnelStatus.Down)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleServiceStateOnChange() {
|
||||
if (activeTuns.value.isEmpty()) serviceManager.stopTunnelForegroundService()
|
||||
}
|
||||
|
||||
private suspend fun handleStuckStartingTunnelShutdown(tunnel: TunnelConf) {
|
||||
Timber.d("Stuck in starting state so cancelling job for tunnel ${tunnel.name}")
|
||||
try {
|
||||
tunJobs[tunnel.id]?.cancel() ?: Timber.d("No job found for ${tunnel.name}")
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to cancel job for ${tunnel.name}")
|
||||
} finally {
|
||||
updateTunnelStatus(tunnel, TunnelStatus.Down)
|
||||
}
|
||||
}
|
||||
|
||||
private fun cleanUpTunJob(tunnel: TunnelConf) {
|
||||
Timber.d("Removing job for ${tunnel.name}")
|
||||
tunJobs -= tunnel.id
|
||||
}
|
||||
|
||||
private fun removeActiveTunnel(tunnelConf: TunnelConf) {
|
||||
activeTuns.update { current -> current.toMutableMap().apply { remove(tunnelConf) } }
|
||||
}
|
||||
|
||||
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
|
||||
bounceTunnelMutex.withLock {
|
||||
Timber.i(
|
||||
"Bounce tunnel ${tunnelConf.name} for reason: $reason, current bouncing: ${bouncingTunnelIds.size}"
|
||||
)
|
||||
bouncingTunnelIds[tunnelConf.id] = reason
|
||||
runCatching {
|
||||
stopTunnel(tunnelConf, reason)
|
||||
delay(BOUNCE_DELAY)
|
||||
startTunnel(tunnelConf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> =
|
||||
activeTuns.value.keys.map { it.tunName }.toSet()
|
||||
|
||||
companion object {
|
||||
const val BOUNCE_DELAY = 300L
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.wireguard.android.backend.Backend
|
||||
import com.wireguard.android.backend.BackendException
|
||||
import com.wireguard.android.backend.Tunnel as WgTunnel
|
||||
import com.wireguard.android.backend.Tunnel
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.di.Kernel
|
||||
@@ -13,78 +13,50 @@ import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.WireGuardStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendCoreException
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.consumeAsFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
class KernelTunnel
|
||||
@Inject
|
||||
constructor(
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||
serviceManager: ServiceManager,
|
||||
appDataRepository: AppDataRepository,
|
||||
@Kernel private val backend: Backend,
|
||||
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
|
||||
|
||||
private val runtimeTunnels = ConcurrentHashMap<Int, WgTunnel>()
|
||||
|
||||
// TODO Add DNS settings
|
||||
override fun tunnelStateFlow(tunnelConf: TunnelConf): Flow<TunnelStatus> = callbackFlow {
|
||||
if (!tunnelConf.isNameKernelCompatible) close(BackendCoreException.TunnelNameTooLong)
|
||||
|
||||
val stateChannel = Channel<WgTunnel.State>()
|
||||
|
||||
val runtimeTunnel = RuntimeWgTunnel(tunnelConf, stateChannel)
|
||||
runtimeTunnels[tunnelConf.id] = runtimeTunnel
|
||||
|
||||
val consumerJob = launch {
|
||||
stateChannel.consumeAsFlow().collect { state -> trySend(state.asTunnelState()) }
|
||||
}
|
||||
|
||||
try {
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
|
||||
backend.setState(runtimeTunnel, WgTunnel.State.UP, tunnelConf.toWgConfig())
|
||||
} catch (e: BackendException) {
|
||||
close(e.toBackendCoreException())
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Timber.e(e, "Invalid backend arguments")
|
||||
close(BackendCoreException.Config)
|
||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||
return try {
|
||||
WireGuardStatistics(backend.getStatistics(tunnelConf))
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Error while setting tunnel state")
|
||||
close(BackendCoreException.Unknown)
|
||||
}
|
||||
|
||||
awaitClose {
|
||||
try {
|
||||
backend.setState(runtimeTunnel, WgTunnel.State.DOWN, null)
|
||||
} catch (e: BackendException) {
|
||||
errors.tryEmit(tunnelConf.tunName to e.toBackendCoreException())
|
||||
} finally {
|
||||
consumerJob.cancel()
|
||||
stateChannel.close()
|
||||
runtimeTunnels.remove(tunnelConf.id)
|
||||
trySend(TunnelStatus.Down)
|
||||
close()
|
||||
}
|
||||
Timber.e(e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun getStatistics(tunnelId: Int): TunnelStatistics? {
|
||||
return try {
|
||||
val runtimeTunnel = runtimeTunnels[tunnelId] ?: return null
|
||||
WireGuardStatistics(backend.getStatistics(runtimeTunnel))
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to get stats for $tunnelId")
|
||||
null
|
||||
override suspend fun startBackend(tunnel: TunnelConf) {
|
||||
// name too long for kernel mode
|
||||
if (!tunnel.isNameKernelCompatible) throw BackendCoreException.TunnelNameTooLong
|
||||
try {
|
||||
updateTunnelStatus(tunnel, TunnelStatus.Starting)
|
||||
backend.setState(tunnel, Tunnel.State.UP, tunnel.toWgConfig())
|
||||
} catch (e: BackendException) {
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw e.toBackendCoreException()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw BackendCoreException.Config
|
||||
}
|
||||
}
|
||||
|
||||
override fun stopBackend(tunnel: TunnelConf) {
|
||||
Timber.i("Stopping tunnel ${tunnel.id} kernel")
|
||||
try {
|
||||
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toWgConfig())
|
||||
} catch (e: BackendException) {
|
||||
throw e.toBackendCoreException()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,10 +68,6 @@ constructor(
|
||||
return BackendMode.Inactive
|
||||
}
|
||||
|
||||
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
|
||||
throw NotImplementedError()
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
return backend.runningTunnelNames
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import org.amnezia.awg.backend.Tunnel
|
||||
|
||||
class RuntimeAwgTunnel(
|
||||
private val tunnelConf: TunnelConf,
|
||||
private val stateChannel: Channel<Tunnel.State>,
|
||||
) : Tunnel {
|
||||
|
||||
override fun getName() = tunnelConf.tunName
|
||||
|
||||
override fun onStateChange(newState: Tunnel.State) {
|
||||
stateChannel.trySend(newState)
|
||||
}
|
||||
|
||||
override fun isIpv4ResolutionPreferred() = tunnelConf.isIpv4Preferred
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.wireguard.android.backend.Tunnel
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
|
||||
class RuntimeWgTunnel(
|
||||
private val config: TunnelConf,
|
||||
private val stateChannel: Channel<Tunnel.State>,
|
||||
) : Tunnel {
|
||||
|
||||
override fun getName() = config.tunName
|
||||
|
||||
override fun onStateChange(newState: Tunnel.State) {
|
||||
stateChannel.trySend(newState)
|
||||
}
|
||||
|
||||
override fun isIpv4ResolutionPreferred() = config.isIpv4Preferred
|
||||
}
|
||||
+62
-230
@@ -10,21 +10,23 @@ import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.PingState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.inject.Inject
|
||||
import kotlin.concurrent.atomics.AtomicBoolean
|
||||
import kotlin.concurrent.atomics.AtomicReference
|
||||
import kotlin.concurrent.atomics.ExperimentalAtomicApi
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.plus
|
||||
import org.amnezia.awg.crypto.Key
|
||||
import timber.log.Timber
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalAtomicApi::class)
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class TunnelManager
|
||||
@Inject
|
||||
constructor(
|
||||
@@ -37,21 +39,7 @@ constructor(
|
||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||
) : TunnelProvider {
|
||||
|
||||
private data class SideEffectState(
|
||||
val activeTuns: Map<Int, TunnelState>,
|
||||
val tuns: List<TunnelConf>,
|
||||
val settings: GeneralSettings,
|
||||
val previouslyActive: Map<Int, TunnelState>,
|
||||
)
|
||||
|
||||
private data class SideEffectWithCondition(
|
||||
val effect: suspend (SideEffectState) -> Unit,
|
||||
val condition: (SideEffectState) -> Boolean,
|
||||
)
|
||||
|
||||
private val sideEffectChannelFlow =
|
||||
MutableStateFlow<Channel<SideEffectState>>(Channel(Channel.CONFLATED))
|
||||
|
||||
@OptIn(ExperimentalAtomicApi::class)
|
||||
private val tunnelProviderFlow: StateFlow<TunnelProvider> = run {
|
||||
val currentBackend = AtomicReference(userspaceTunnel)
|
||||
val currentSettings = AtomicReference(GeneralSettings())
|
||||
@@ -78,14 +66,45 @@ constructor(
|
||||
}
|
||||
.onEach { (settings, newBackend) ->
|
||||
val isInitialEmit = initialEmit.exchange(false)
|
||||
val previousBackend = currentBackend.exchange(newBackend)
|
||||
val previousSettings = currentSettings.exchange(settings)
|
||||
val oldBackend = currentBackend.exchange(newBackend)
|
||||
val oldSettings = currentSettings.exchange(settings)
|
||||
|
||||
if ((previousSettings.appMode != settings.appMode) && !isInitialEmit) {
|
||||
handleModeChangeCleanup(previousBackend, previousSettings.appMode)
|
||||
if ((oldSettings.appMode != settings.appMode) && !isInitialEmit) {
|
||||
oldBackend.stopTunnel()
|
||||
if (oldSettings.appMode == AppMode.LOCK_DOWN)
|
||||
proxyUserspaceTunnel.setBackendMode(BackendMode.Inactive)
|
||||
}
|
||||
if (settings.appMode == AppMode.LOCK_DOWN) {
|
||||
handleLockDownModeInit(settings.isLanOnKillSwitchEnabled)
|
||||
// kill switch will always catch all ipv6, just add ipv4 networks for allowsIps
|
||||
val allowedIps =
|
||||
if (settings.isLanOnKillSwitchEnabled) TunnelConf.IPV4_PUBLIC_NETWORKS
|
||||
else emptySet()
|
||||
try {
|
||||
// TODO handle situation where they don't have vpn permission, request it
|
||||
if (hasVpnPermission()) {
|
||||
proxyUserspaceTunnel.setBackendMode(BackendMode.KillSwitch(allowedIps))
|
||||
}
|
||||
} catch (e: BackendCoreException) {
|
||||
// TODO expose this error to user
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
// restore state if configured
|
||||
if (isInitialEmit && settings.isRestoreOnBootEnabled) {
|
||||
Timber.d("Restoring previous state")
|
||||
if (
|
||||
settings.isAutoTunnelEnabled &&
|
||||
serviceManager.autoTunnelService.value == null
|
||||
) {
|
||||
serviceManager.startAutoTunnel()
|
||||
} else {
|
||||
val previouslyActiveTuns = appDataRepository.tunnels.getActive()
|
||||
val tunsToStart =
|
||||
previouslyActiveTuns.filterNot { tun ->
|
||||
activeTunnels.value.any { tun.id == it.key.id }
|
||||
}
|
||||
tunsToStart.forEach { startTunnel(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
.map { (_, backend) -> backend }
|
||||
@@ -96,83 +115,17 @@ constructor(
|
||||
)
|
||||
}
|
||||
|
||||
override val activeTunnels: StateFlow<Map<Int, TunnelState>> = run {
|
||||
val activeTunsReference: AtomicReference<Map<Int, TunnelState>> =
|
||||
AtomicReference(emptyMap())
|
||||
override val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>> =
|
||||
tunnelProviderFlow
|
||||
.flatMapLatest { backend ->
|
||||
// Create a new channel for each backend to reset side-effect processing
|
||||
val newChannel = Channel<SideEffectState>(Channel.CONFLATED)
|
||||
sideEffectChannelFlow.value = newChannel
|
||||
|
||||
val sideEffects =
|
||||
listOf(
|
||||
SideEffectWithCondition(
|
||||
effect = { s ->
|
||||
handleTunnelServiceChange(s.settings.appMode, s.activeTuns)
|
||||
},
|
||||
condition = { s -> s.activeTuns.size != s.previouslyActive.size },
|
||||
),
|
||||
SideEffectWithCondition(
|
||||
effect = { s ->
|
||||
handleActiveTunnelsChange(s.previouslyActive, s.activeTuns, s.tuns)
|
||||
},
|
||||
condition = { s -> s.activeTuns.size != s.previouslyActive.size },
|
||||
),
|
||||
// TODO Not for kernel mode for now
|
||||
SideEffectWithCondition(
|
||||
effect = { s -> handleTunnelMonitoringChanges(s.activeTuns, s.tuns) },
|
||||
condition = { s ->
|
||||
s.tuns.any {
|
||||
it.restartOnPingFailure && s.activeTuns.keys.contains(it.id)
|
||||
} && s.settings.appMode != AppMode.KERNEL
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
for (state in newChannel) {
|
||||
supervisorScope {
|
||||
sideEffects
|
||||
.filter { it.condition(state) }
|
||||
.forEach { sideEffect ->
|
||||
launch {
|
||||
try {
|
||||
sideEffect.effect(state)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Side effect failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
combine(
|
||||
backend.activeTunnels,
|
||||
appDataRepository.tunnels.flow,
|
||||
appDataRepository.settings.flow.filterNotNull(),
|
||||
) { activeTuns, tuns, settings ->
|
||||
Triple(activeTuns, tuns, settings)
|
||||
}
|
||||
}
|
||||
.onStart { handleStateRestore() }
|
||||
.onEach { (activeTuns, tuns, settings) ->
|
||||
val previouslyActive = activeTunsReference.exchange(activeTuns)
|
||||
sideEffectChannelFlow.value.trySend(
|
||||
SideEffectState(activeTuns, tuns, settings, previouslyActive)
|
||||
)
|
||||
}
|
||||
.map { (activeTuns, _, _) -> activeTuns }
|
||||
.flatMapLatest { it.activeTunnels }
|
||||
.stateIn(
|
||||
scope = applicationScope,
|
||||
scope = applicationScope.plus(ioDispatcher),
|
||||
started = SharingStarted.Eagerly,
|
||||
initialValue = emptyMap(),
|
||||
)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override val errorEvents: SharedFlow<Pair<String, BackendCoreException>> =
|
||||
override val errorEvents: SharedFlow<Pair<TunnelConf, BackendCoreException>> =
|
||||
tunnelProviderFlow
|
||||
.flatMapLatest { it.errorEvents }
|
||||
.shareIn(
|
||||
@@ -182,36 +135,37 @@ constructor(
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override val messageEvents: SharedFlow<Pair<String, BackendMessage>> =
|
||||
override val messageEvents: SharedFlow<Pair<TunnelConf, BackendMessage>> =
|
||||
tunnelProviderFlow
|
||||
.flatMapLatest { it.messageEvents }
|
||||
.filterNotNull()
|
||||
.shareIn(
|
||||
scope = applicationScope.plus(ioDispatcher),
|
||||
started = SharingStarted.Eagerly,
|
||||
replay = 0,
|
||||
)
|
||||
|
||||
override val bouncingTunnelIds: ConcurrentHashMap<Int, TunnelStatus.StopReason> =
|
||||
tunnelProviderFlow.value.bouncingTunnelIds
|
||||
|
||||
override fun hasVpnPermission(): Boolean {
|
||||
return userspaceTunnel.hasVpnPermission()
|
||||
}
|
||||
|
||||
override fun getStatistics(tunnelId: Int): TunnelStatistics? {
|
||||
return tunnelProviderFlow.value.getStatistics(tunnelId)
|
||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||
return tunnelProviderFlow.value.getStatistics(tunnelConf)
|
||||
}
|
||||
|
||||
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||
// for VPN Mode, we need to stop active tunnels as we can only have one active at a time
|
||||
if (activeTunnels.value.isNotEmpty() && tunnelProviderFlow.value == userspaceTunnel)
|
||||
stopActiveTunnels()
|
||||
tunnelProviderFlow.value.startTunnel(tunnelConf)
|
||||
}
|
||||
|
||||
override suspend fun stopTunnel(tunnelId: Int) {
|
||||
tunnelProviderFlow.value.stopTunnel(tunnelId)
|
||||
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
|
||||
tunnelProviderFlow.value.stopTunnel(tunnelConf, reason)
|
||||
}
|
||||
|
||||
override suspend fun stopActiveTunnels() {
|
||||
tunnelProviderFlow.value.stopActiveTunnels()
|
||||
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
|
||||
tunnelProviderFlow.value.bounceTunnel(tunnelConf, reason)
|
||||
}
|
||||
|
||||
override fun setBackendMode(backendMode: BackendMode) {
|
||||
@@ -226,141 +180,19 @@ constructor(
|
||||
return tunnelProviderFlow.value.runningTunnelNames()
|
||||
}
|
||||
|
||||
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
|
||||
return tunnelProviderFlow.value.handleDnsReresolve(tunnelConf)
|
||||
}
|
||||
|
||||
override suspend fun updateTunnelStatus(
|
||||
tunnelId: Int,
|
||||
tunnelConf: TunnelConf,
|
||||
status: TunnelStatus?,
|
||||
stats: TunnelStatistics?,
|
||||
pingStates: Map<Key, PingState>?,
|
||||
logHealthState: LogHealthState?,
|
||||
handshakeSuccessLogs: Boolean?,
|
||||
) {
|
||||
tunnelProviderFlow.value.updateTunnelStatus(
|
||||
tunnelId,
|
||||
tunnelConf,
|
||||
status,
|
||||
stats,
|
||||
pingStates,
|
||||
logHealthState,
|
||||
handshakeSuccessLogs,
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun handleTunnelServiceChange(
|
||||
appMode: AppMode,
|
||||
activeTuns: Map<Int, TunnelState>,
|
||||
) {
|
||||
if (activeTuns.isEmpty()) serviceManager.stopTunnelService()
|
||||
if (activeTuns.isNotEmpty() && serviceManager.tunnelService.value == null)
|
||||
serviceManager.startTunnelService(appMode)
|
||||
}
|
||||
|
||||
private fun handleLockDownModeInit(withLanBypass: Boolean) {
|
||||
val allowedIps = if (withLanBypass) TunnelConf.IPV4_PUBLIC_NETWORKS else emptySet()
|
||||
try {
|
||||
// TODO handle situation where they don't have vpn permission, request it
|
||||
if (hasVpnPermission()) {
|
||||
proxyUserspaceTunnel.setBackendMode(BackendMode.KillSwitch(allowedIps))
|
||||
}
|
||||
} catch (e: BackendCoreException) {
|
||||
// TODO expose this error to user
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleModeChangeCleanup(
|
||||
previousBackend: TunnelProvider,
|
||||
previousAppMode: AppMode,
|
||||
) {
|
||||
previousBackend.stopActiveTunnels()
|
||||
// stop lockdown if we switch from that mode
|
||||
if (previousAppMode == AppMode.LOCK_DOWN)
|
||||
proxyUserspaceTunnel.setBackendMode(BackendMode.Inactive)
|
||||
}
|
||||
|
||||
private suspend fun handleStateRestore() {
|
||||
val settings = appDataRepository.settings.flow.first()
|
||||
if (settings.isRestoreOnBootEnabled) {
|
||||
// if auto tun enabled, reset active and restore auto tun, letting it start appropriate
|
||||
// tuns
|
||||
if (settings.isAutoTunnelEnabled) {
|
||||
appDataRepository.tunnels.resetActiveTunnels()
|
||||
return serviceManager.startAutoTunnel()
|
||||
}
|
||||
val tunnels = appDataRepository.tunnels.flow.first()
|
||||
when (settings.appMode) {
|
||||
// TODO eventually, lockdown/proxy can support multi
|
||||
AppMode.VPN,
|
||||
AppMode.LOCK_DOWN,
|
||||
AppMode.PROXY ->
|
||||
tunnels
|
||||
.firstOrNull { it.isActive }
|
||||
?.let {
|
||||
// clear any duplicates
|
||||
appDataRepository.tunnels.resetActiveTunnels()
|
||||
startTunnel(it)
|
||||
}
|
||||
// kernel supports multi
|
||||
AppMode.KERNEL ->
|
||||
tunnels.filter { it.isActive }.forEach { conf -> startTunnel(conf) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleTunnelMonitoringChanges(
|
||||
activeTuns: Map<Int, TunnelState>,
|
||||
configs: List<TunnelConf>,
|
||||
) {
|
||||
configs
|
||||
.filter { it.restartOnPingFailure && activeTuns.keys.contains(it.id) }
|
||||
.forEach { conf ->
|
||||
val tunState = activeTuns[conf.id] ?: return@forEach
|
||||
if (tunState.health() == TunnelState.Health.UNHEALTHY) {
|
||||
runCatching {
|
||||
val updated = handleDnsReresolve(conf)
|
||||
// TODO user messages
|
||||
if (updated) {
|
||||
Timber.i("Successfully update the peer endpoint to new address.")
|
||||
} else {
|
||||
Timber.i("Current endpoint address is already up to date.")
|
||||
}
|
||||
}
|
||||
.onFailure {
|
||||
Timber.e(it, "Failed to handle dns re-resolution for ${conf.tunName}")
|
||||
}
|
||||
// TODO backoff
|
||||
delay(30_000L)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleActiveTunnelsChange(
|
||||
previousActiveTuns: Map<Int, TunnelState>,
|
||||
activeTuns: Map<Int, TunnelState>,
|
||||
tuns: List<TunnelConf>,
|
||||
) {
|
||||
val relevantTunnels = previousActiveTuns.keys + activeTuns.keys
|
||||
|
||||
relevantTunnels.forEach { tunnelId ->
|
||||
val wasActive = previousActiveTuns.containsKey(tunnelId)
|
||||
val isActiveNow = activeTuns.containsKey(tunnelId)
|
||||
|
||||
when {
|
||||
!wasActive && isActiveNow -> {
|
||||
tuns
|
||||
.find { it.id == tunnelId }
|
||||
?.let { dbTunnelConf ->
|
||||
appDataRepository.tunnels.save(dbTunnelConf.copy(isActive = true))
|
||||
}
|
||||
}
|
||||
wasActive && !isActiveNow -> {
|
||||
tuns
|
||||
.find { it.id == tunnelId }
|
||||
?.let { dbTunnelConf ->
|
||||
appDataRepository.tunnels.save(dbTunnelConf.copy(isActive = false))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+39
-44
@@ -2,12 +2,10 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.logcatter.LogReader
|
||||
import com.zaneschepke.networkmonitor.NetworkMonitor
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.FailureReason
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.PingState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toMillis
|
||||
import com.zaneschepke.wireguardautotunnel.util.network.NetworkUtils
|
||||
@@ -31,43 +29,56 @@ constructor(
|
||||
) {
|
||||
|
||||
@OptIn(FlowPreview::class)
|
||||
suspend fun startMonitoring(tunnelId: Int, withLogs: Boolean): Job = coroutineScope {
|
||||
suspend fun startMonitoring(tunnelConf: TunnelConf, withLogs: Boolean): Job = coroutineScope {
|
||||
launch {
|
||||
val config = appDataRepository.tunnels.getById(tunnelId) ?: return@launch
|
||||
launch { startPingMonitor(config) }
|
||||
launch { startWgStatsPoll(config.id) }
|
||||
if (withLogs) launch { startLogsMonitor(config) }
|
||||
launch { startTunnelConfChangesJob(tunnelConf) }
|
||||
launch { startPingMonitor(tunnelConf) }
|
||||
launch { startWgStatsPoll(tunnelConf) }
|
||||
if (withLogs) launch { startLogsMonitor(tunnelConf) }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startTunnelConfChangesJob(tunnelConf: TunnelConf) {
|
||||
appDataRepository.tunnels.flow
|
||||
.map { storedTunnels -> storedTunnels.firstOrNull { it.id == tunnelConf.id } }
|
||||
.filterNotNull()
|
||||
.distinctUntilChanged { old, new -> old == new }
|
||||
.collect { storedTunnel ->
|
||||
if (tunnelConf != storedTunnel) {
|
||||
Timber.d("Config changed for ${storedTunnel.tunName}, bouncing")
|
||||
withContext(NonCancellable) {
|
||||
tunnelManager.bounceTunnel(
|
||||
storedTunnel,
|
||||
TunnelStatus.StopReason.ConfigChanged,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startLogsMonitor(tunnelConf: TunnelConf) {
|
||||
logReader.liveLogs
|
||||
.filter { log -> log.tag.contains(tunnelConf.tunName) }
|
||||
.mapNotNull { log ->
|
||||
val now = System.currentTimeMillis()
|
||||
|
||||
logReader.liveLogs.collect { log ->
|
||||
val healthLogs =
|
||||
when {
|
||||
successLogRegex.containsMatchIn(log.message) ->
|
||||
LogHealthState(isHealthy = true, timestamp = now)
|
||||
|
||||
failureLogRegex.containsMatchIn(log.message) ->
|
||||
LogHealthState(isHealthy = false, timestamp = now)
|
||||
log.message.contains(HANDSHAKE_RESPONSE_TEXT, true) ||
|
||||
log.message.contains(KEEPALIVE_RESPONSE_TEXT, true) -> true
|
||||
log.message.contains(HANDSHAKE_INIT_FAILED_TEXT, true) ||
|
||||
log.message.contains(HANDSHAKE_NOT_COMPLETED_TEXT) ||
|
||||
log.message.contains(DATA_PACKET_FAILED_TEXT) -> false
|
||||
|
||||
else -> null
|
||||
}
|
||||
healthLogs?.let { healthy ->
|
||||
tunnelManager.updateTunnelStatus(tunnelConf, null, null, null, healthy)
|
||||
}
|
||||
.distinctUntilChangedBy { it.isHealthy } // Only emit when health changes
|
||||
.collect { logHealthState ->
|
||||
Timber.d("Tunnel log health updated for ${tunnelConf.tunName}: $logHealthState")
|
||||
tunnelManager.updateTunnelStatus(tunnelConf.id, logHealthState = logHealthState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startPingMonitor(tunnelConf: TunnelConf) = coroutineScope {
|
||||
val pingStatsFlow = MutableStateFlow<Map<Key, PingState>>(emptyMap())
|
||||
|
||||
val tunStateFlow =
|
||||
tunnelManager.activeTunnels.mapNotNull { it[tunnelConf.id] }.stateIn(this)
|
||||
tunnelManager.activeTunnels.mapNotNull { it.getValueById(tunnelConf.id) }.stateIn(this)
|
||||
|
||||
val connectivityStateFlow = networkMonitor.connectivityStateFlow.stateIn(this)
|
||||
|
||||
@@ -98,13 +109,9 @@ constructor(
|
||||
old.tunnelPingIntervalSeconds == new.tunnelPingIntervalSeconds &&
|
||||
old.tunnelPingAttempts == new.tunnelPingAttempts &&
|
||||
old.tunnelPingTimeoutSeconds == new.tunnelPingTimeoutSeconds
|
||||
old.appMode == new.appMode
|
||||
}
|
||||
.collectLatest { settings ->
|
||||
if (!settings.isPingEnabled) return@collectLatest
|
||||
// TODO for now until we get monitoring for these modes
|
||||
if (settings.appMode == AppMode.LOCK_DOWN || settings.appMode == AppMode.PROXY)
|
||||
return@collectLatest
|
||||
|
||||
Timber.d("Starting pinger for ${tunnelConf.tunName} with settings")
|
||||
|
||||
@@ -203,7 +210,7 @@ constructor(
|
||||
|
||||
if (updates.isNotEmpty()) {
|
||||
pingStatsFlow.update { updates }
|
||||
tunnelManager.updateTunnelStatus(tunnelConf.id, null, null, updates)
|
||||
tunnelManager.updateTunnelStatus(tunnelConf, null, null, updates)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,7 +234,7 @@ constructor(
|
||||
}
|
||||
}
|
||||
tunnelManager.updateTunnelStatus(
|
||||
tunnelConf.id,
|
||||
tunnelConf,
|
||||
null,
|
||||
null,
|
||||
pingStatsFlow.value,
|
||||
@@ -238,27 +245,15 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startWgStatsPoll(tunnelId: Int) = coroutineScope {
|
||||
private suspend fun startWgStatsPoll(tunnelConf: TunnelConf) = coroutineScope {
|
||||
while (isActive) {
|
||||
val stats = tunnelManager.getStatistics(tunnelId)
|
||||
tunnelManager.updateTunnelStatus(tunnelId, null, stats, null)
|
||||
val stats = tunnelManager.getStatistics(tunnelConf)
|
||||
tunnelManager.updateTunnelStatus(tunnelConf, null, stats, null)
|
||||
delay(STATS_DELAY)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private val successLogRegex =
|
||||
Regex("Received handshake response|Receiving keepalive packet", RegexOption.IGNORE_CASE)
|
||||
|
||||
private val failureLogRegex =
|
||||
Regex(
|
||||
"Failed to send handshake initiation: write udp|" +
|
||||
"Handshake did not complete after 5 seconds, retrying|" +
|
||||
"Failed to send data packets",
|
||||
RegexOption.IGNORE_CASE,
|
||||
)
|
||||
|
||||
const val CLOUDFLARE_IPV6_IP = "2606:4700:4700::1111"
|
||||
const val CLOUDFLARE_IPV4_IP = "1.1.1.1"
|
||||
|
||||
|
||||
+27
-13
@@ -5,10 +5,10 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.PingState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import org.amnezia.awg.crypto.Key
|
||||
@@ -18,14 +18,28 @@ interface TunnelProvider {
|
||||
suspend fun startTunnel(tunnelConf: TunnelConf)
|
||||
|
||||
/**
|
||||
* Stops the specified tunnel.
|
||||
* Stops the specified tunnel, or all tunnels if none is provided.
|
||||
*
|
||||
* @param tunnelId The tunnelConf to stop.
|
||||
* @param tunnelConf The tunnel to stop, or null to stop all active tunnels.
|
||||
* @param reason The reason for stopping, defaults to USER for manual stops. Callers should
|
||||
* override with specific reasons (e.g., PING, CONFIG_CHANGED) when applicable.
|
||||
*/
|
||||
suspend fun stopTunnel(tunnelId: Int)
|
||||
suspend fun stopTunnel(
|
||||
tunnelConf: TunnelConf? = null,
|
||||
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.User,
|
||||
)
|
||||
|
||||
/** Stops all active tunnels. */
|
||||
suspend fun stopActiveTunnels()
|
||||
/**
|
||||
* Bounces (stops and restarts) the specified tunnel.
|
||||
*
|
||||
* @param tunnelConf The tunnel to bounce.
|
||||
* @param reason The reason for bouncing, defaults to User for manual actions. Callers should
|
||||
* override with specific reasons (e.g., Ping, ConfigChanged) when applicable.
|
||||
*/
|
||||
suspend fun bounceTunnel(
|
||||
tunnelConf: TunnelConf,
|
||||
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.User,
|
||||
)
|
||||
|
||||
fun setBackendMode(backendMode: BackendMode)
|
||||
|
||||
@@ -33,23 +47,23 @@ interface TunnelProvider {
|
||||
|
||||
suspend fun runningTunnelNames(): Set<String>
|
||||
|
||||
fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean
|
||||
fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics?
|
||||
|
||||
fun getStatistics(tunnelId: Int): TunnelStatistics?
|
||||
val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>>
|
||||
|
||||
val activeTunnels: StateFlow<Map<Int, TunnelState>>
|
||||
val errorEvents: SharedFlow<Pair<TunnelConf, BackendCoreException>>
|
||||
|
||||
val errorEvents: SharedFlow<Pair<String, BackendCoreException>>
|
||||
val messageEvents: SharedFlow<Pair<TunnelConf, BackendMessage>>
|
||||
|
||||
val messageEvents: SharedFlow<Pair<String, BackendMessage>>
|
||||
val bouncingTunnelIds: ConcurrentHashMap<Int, TunnelStatus.StopReason>
|
||||
|
||||
fun hasVpnPermission(): Boolean
|
||||
|
||||
suspend fun updateTunnelStatus(
|
||||
tunnelId: Int,
|
||||
tunnelConf: TunnelConf,
|
||||
status: TunnelStatus? = null,
|
||||
stats: TunnelStatistics? = null,
|
||||
pingStates: Map<Key, PingState>? = null,
|
||||
logHealthState: LogHealthState? = null,
|
||||
handshakeSuccessLogs: Boolean? = null,
|
||||
)
|
||||
}
|
||||
|
||||
+23
-55
@@ -2,7 +2,6 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
|
||||
@@ -13,23 +12,15 @@ import com.zaneschepke.wireguardautotunnel.domain.state.AmneziaStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendMode
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendMode
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendCoreException
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.consumeAsFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import org.amnezia.awg.backend.Backend
|
||||
import org.amnezia.awg.backend.BackendException
|
||||
import org.amnezia.awg.backend.ProxyGoBackend
|
||||
import org.amnezia.awg.backend.Tunnel as AwgTunnel
|
||||
import org.amnezia.awg.backend.Tunnel
|
||||
import org.amnezia.awg.config.Config
|
||||
import org.amnezia.awg.config.DnsSettings
|
||||
import org.amnezia.awg.config.proxy.HttpProxy
|
||||
@@ -40,26 +31,15 @@ import timber.log.Timber
|
||||
class UserspaceTunnel
|
||||
@Inject
|
||||
constructor(
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
serviceManager: ServiceManager,
|
||||
appDataRepository: AppDataRepository,
|
||||
applicationScope: CoroutineScope,
|
||||
val serviceManager: ServiceManager,
|
||||
val appDataRepository: AppDataRepository,
|
||||
private val backend: Backend,
|
||||
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
|
||||
|
||||
private val runtimeTunnels = ConcurrentHashMap<Int, AwgTunnel>()
|
||||
|
||||
override fun tunnelStateFlow(tunnelConf: TunnelConf): Flow<TunnelStatus> = callbackFlow {
|
||||
val stateChannel = Channel<AwgTunnel.State>()
|
||||
|
||||
val runtimeTunnel = RuntimeAwgTunnel(tunnelConf, stateChannel)
|
||||
runtimeTunnels[tunnelConf.id] = runtimeTunnel
|
||||
|
||||
val consumerJob = launch {
|
||||
stateChannel.consumeAsFlow().collect { awgState -> trySend(awgState.asTunnelState()) }
|
||||
}
|
||||
|
||||
override suspend fun startBackend(tunnel: TunnelConf) {
|
||||
try {
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
|
||||
updateTunnelStatus(tunnel, TunnelStatus.Starting)
|
||||
|
||||
val proxies: List<Proxy> =
|
||||
when (backend) {
|
||||
@@ -92,7 +72,7 @@ constructor(
|
||||
else -> emptyList()
|
||||
}
|
||||
val setting = appDataRepository.settings.get()
|
||||
val config = tunnelConf.toAmConfig()
|
||||
val config = tunnel.toAmConfig()
|
||||
val updatedConfig =
|
||||
Config.Builder()
|
||||
.apply {
|
||||
@@ -107,28 +87,23 @@ constructor(
|
||||
)
|
||||
}
|
||||
.build()
|
||||
backend.setState(runtimeTunnel, AwgTunnel.State.UP, updatedConfig)
|
||||
backend.setState(tunnel, Tunnel.State.UP, updatedConfig)
|
||||
} catch (e: BackendException) {
|
||||
close(e.toBackendCoreException())
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw e.toBackendCoreException()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
close(BackendCoreException.Config)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Error while setting tunnel state")
|
||||
close(BackendCoreException.Unknown)
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw BackendCoreException.Config
|
||||
}
|
||||
}
|
||||
|
||||
awaitClose {
|
||||
try {
|
||||
backend.setState(runtimeTunnel, AwgTunnel.State.DOWN, null)
|
||||
} catch (e: BackendException) {
|
||||
errors.tryEmit(tunnelConf.tunName to e.toBackendCoreException())
|
||||
} finally {
|
||||
consumerJob.cancel()
|
||||
stateChannel.close()
|
||||
runtimeTunnels.remove(tunnelConf.id)
|
||||
trySend(TunnelStatus.Down)
|
||||
close()
|
||||
}
|
||||
override fun stopBackend(tunnel: TunnelConf) {
|
||||
Timber.i("Stopping tunnel ${tunnel.name} userspace")
|
||||
try {
|
||||
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toAmConfig())
|
||||
} catch (e: BackendException) {
|
||||
Timber.e(e, "Failed to stop tunnel ${tunnel.id}")
|
||||
throw e.toBackendCoreException()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,22 +123,15 @@ constructor(
|
||||
return backend.backendMode.asBackendMode()
|
||||
}
|
||||
|
||||
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
|
||||
val tunnel =
|
||||
runtimeTunnels.get(tunnelConf.id) ?: throw BackendCoreException.ServiceNotRunning
|
||||
return backend.resolveDDNS(tunnelConf.toAmConfig(), tunnel.isIpv4ResolutionPreferred)
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
return backend.runningTunnelNames
|
||||
}
|
||||
|
||||
override fun getStatistics(tunnelId: Int): TunnelStatistics? {
|
||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||
return try {
|
||||
val runtimeTunnel = runtimeTunnels[tunnelId] ?: return null
|
||||
AmneziaStatistics(backend.getStatistics(runtimeTunnel))
|
||||
AmneziaStatistics(backend.getStatistics(tunnelConf))
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to get stats for $tunnelId")
|
||||
Timber.e(e, "Failed to get stats for ${tunnelConf.tunName}")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,6 @@ interface TunnelConfigDao {
|
||||
|
||||
@Query("SELECT * FROM TunnelConfig WHERE id=:id") suspend fun getById(id: Long): TunnelConfig?
|
||||
|
||||
@Query("UPDATE TunnelConfig SET is_Active = 0 WHERE is_Active = 1")
|
||||
suspend fun resetActiveTunnels()
|
||||
|
||||
@Query("SELECT * FROM TunnelConfig WHERE name=:name")
|
||||
suspend fun getByName(name: String): TunnelConfig?
|
||||
|
||||
|
||||
-4
@@ -46,10 +46,6 @@ class RoomTunnelRepository(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun resetActiveTunnels() {
|
||||
withContext(ioDispatcher) { tunnelConfigDao.resetActiveTunnels() }
|
||||
}
|
||||
|
||||
override suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) {
|
||||
withContext(ioDispatcher) {
|
||||
tunnelConfigDao.resetMobileDataTunnel()
|
||||
|
||||
@@ -2,14 +2,14 @@ package com.zaneschepke.wireguardautotunnel.di
|
||||
|
||||
import javax.inject.Qualifier
|
||||
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class DefaultDispatcher
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class DefaultDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class IoDispatcher
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class IoDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainDispatcher
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class MainDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainImmediateDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class ApplicationScope
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ApplicationScope
|
||||
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class ServiceScope
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ServiceScope
|
||||
|
||||
@@ -6,10 +6,18 @@ sealed class TunnelStatus {
|
||||
|
||||
data object Down : TunnelStatus()
|
||||
|
||||
data object Stopping : TunnelStatus()
|
||||
data class Stopping(val reason: StopReason) : TunnelStatus()
|
||||
|
||||
data object Starting : TunnelStatus()
|
||||
|
||||
sealed class StopReason {
|
||||
data object User : StopReason()
|
||||
|
||||
data class Ping(val previouslyResolvedEndpoints: Map<String, String?>) : StopReason()
|
||||
|
||||
data object ConfigChanged : StopReason()
|
||||
}
|
||||
|
||||
fun isDown(): Boolean {
|
||||
return this == Down
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
sealed class AutoTunnelEvent {
|
||||
data class Start(val tunnelConf: TunnelConf? = null) : AutoTunnelEvent()
|
||||
|
||||
data class Bounce(val configsPeerKeyResolvedMap: List<Pair<TunnelConf, Map<String, String?>>>) :
|
||||
AutoTunnelEvent()
|
||||
|
||||
data object Stop : AutoTunnelEvent()
|
||||
|
||||
data object DoNothing : AutoTunnelEvent()
|
||||
|
||||
-3
@@ -20,8 +20,6 @@ sealed class BackendCoreException : Exception() {
|
||||
|
||||
data object TunnelNameTooLong : BackendCoreException()
|
||||
|
||||
data object UapiUpdateFailed : BackendCoreException()
|
||||
|
||||
data class BounceFailed(val error: BackendCoreException) : BackendCoreException()
|
||||
|
||||
fun toStringRes() =
|
||||
@@ -35,7 +33,6 @@ sealed class BackendCoreException : Exception() {
|
||||
Unknown -> R.string.unknown_error
|
||||
TunnelNameTooLong -> R.string.error_tunnel_name
|
||||
is BounceFailed -> R.string.bounce_failed_template
|
||||
UapiUpdateFailed -> R.string.active_tunnel_update_failed
|
||||
}
|
||||
|
||||
fun toStringValue(): StringValue {
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.model
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.wireguard.android.backend.Tunnel
|
||||
import com.wireguard.config.Config
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.defaultName
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isValidIpv4orIpv6Address
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.*
|
||||
import java.io.InputStream
|
||||
import java.nio.charset.StandardCharsets
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class TunnelConf(
|
||||
val id: Int = 0,
|
||||
val tunName: String,
|
||||
@@ -21,9 +23,14 @@ data class TunnelConf(
|
||||
val isEthernetTunnel: Boolean = false,
|
||||
val isIpv4Preferred: Boolean = true,
|
||||
val position: Int = 0,
|
||||
) {
|
||||
@Transient private var stateChangeCallback: ((Any) -> Unit)? = null,
|
||||
) : Tunnel, org.amnezia.awg.backend.Tunnel, Parcelable {
|
||||
|
||||
val isNameKernelCompatible: Boolean = (tunName.length <= 15)
|
||||
val isNameKernelCompatible: Boolean = (name.length <= 15)
|
||||
|
||||
fun setStateChangeCallback(callback: (Any) -> Unit) {
|
||||
stateChangeCallback = callback
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
@@ -53,6 +60,38 @@ data class TunnelConf(
|
||||
return toAmConfig().peers.all { it.endpoint.get().host.isValidIpv4orIpv6Address() }
|
||||
}
|
||||
|
||||
fun copyWithCallback(
|
||||
id: Int = this.id,
|
||||
tunName: String = this.tunName,
|
||||
wgQuick: String = this.wgQuick,
|
||||
tunnelNetworks: Set<String> = this.tunnelNetworks,
|
||||
isMobileDataTunnel: Boolean = this.isMobileDataTunnel,
|
||||
isPrimaryTunnel: Boolean = this.isPrimaryTunnel,
|
||||
amQuick: String = this.amQuick,
|
||||
isActive: Boolean = this.isActive,
|
||||
restartOnPingFailure: Boolean = this.restartOnPingFailure,
|
||||
pingIp: String? = this.pingTarget,
|
||||
isEthernetTunnel: Boolean = this.isEthernetTunnel,
|
||||
isIpv4Preferred: Boolean = this.isIpv4Preferred,
|
||||
): TunnelConf {
|
||||
return TunnelConf(
|
||||
id,
|
||||
tunName,
|
||||
wgQuick,
|
||||
tunnelNetworks,
|
||||
isMobileDataTunnel,
|
||||
isPrimaryTunnel,
|
||||
amQuick,
|
||||
isActive,
|
||||
pingIp,
|
||||
restartOnPingFailure,
|
||||
isEthernetTunnel,
|
||||
isIpv4Preferred,
|
||||
position,
|
||||
)
|
||||
.apply { stateChangeCallback = this@TunnelConf.stateChangeCallback }
|
||||
}
|
||||
|
||||
fun toAmConfig(): org.amnezia.awg.config.Config {
|
||||
return configFromAmQuick(amQuick.ifBlank { wgQuick })
|
||||
}
|
||||
@@ -61,6 +100,36 @@ data class TunnelConf(
|
||||
return configFromWgQuick(wgQuick)
|
||||
}
|
||||
|
||||
override fun getName(): String = tunName
|
||||
|
||||
override fun onStateChange(newState: org.amnezia.awg.backend.Tunnel.State) {
|
||||
stateChangeCallback?.invoke(newState)
|
||||
}
|
||||
|
||||
override fun onStateChange(newState: Tunnel.State) {
|
||||
stateChangeCallback?.invoke(newState)
|
||||
}
|
||||
|
||||
override fun isIpv4ResolutionPreferred(): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
fun generateUniqueName(tunnelNames: List<String>): String {
|
||||
var tunnelName = this.tunName
|
||||
var num = 1
|
||||
while (tunnelNames.any { it == tunnelName }) {
|
||||
tunnelName =
|
||||
if (!tunnelName.hasNumberInParentheses()) {
|
||||
"$name($num)"
|
||||
} else {
|
||||
val pair = tunnelName.extractNameAndNumber()
|
||||
"${pair?.first}($num)"
|
||||
}
|
||||
num++
|
||||
}
|
||||
return tunnelName
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun configFromWgQuick(wgQuick: String): Config {
|
||||
val inputStream: InputStream = wgQuick.byteInputStream()
|
||||
|
||||
-2
@@ -15,8 +15,6 @@ interface TunnelRepository {
|
||||
|
||||
suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?)
|
||||
|
||||
suspend fun resetActiveTunnels()
|
||||
|
||||
suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?)
|
||||
|
||||
suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?)
|
||||
|
||||
+39
-5
@@ -2,21 +2,20 @@ package com.zaneschepke.wireguardautotunnel.domain.state
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.StateChange
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.DoNothing
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.Start
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.*
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
||||
|
||||
data class AutoTunnelState(
|
||||
val activeTunnels: Map<Int, TunnelState> = emptyMap(),
|
||||
val activeTunnels: Map<TunnelConf, TunnelState> = emptyMap(),
|
||||
val networkState: NetworkState = NetworkState(),
|
||||
val settings: GeneralSettings = GeneralSettings(),
|
||||
val tunnels: List<TunnelConf> = emptyList(),
|
||||
) {
|
||||
|
||||
fun determineAutoTunnelEvent(stateChange: StateChange): AutoTunnelEvent {
|
||||
when (stateChange) {
|
||||
when (val change = stateChange) {
|
||||
is StateChange.NetworkChange,
|
||||
is StateChange.SettingsChange -> {
|
||||
// Compute desired tunnel based on network conditions
|
||||
@@ -41,7 +40,7 @@ data class AutoTunnelState(
|
||||
|
||||
// Handle tunnel start/stop/change
|
||||
if (desiredTunnel != null) {
|
||||
if (currentTunnel != desiredTunnel.id) {
|
||||
if (currentTunnel != desiredTunnel) {
|
||||
// Start or switch to the desired tunnel (overrides any kill switch)
|
||||
return Start(desiredTunnel)
|
||||
}
|
||||
@@ -55,6 +54,12 @@ data class AutoTunnelState(
|
||||
}
|
||||
}
|
||||
}
|
||||
is StateChange.MonitoringChange -> {
|
||||
val bounceTunnels = bounceOnPingFailed()
|
||||
if (bounceTunnels.isNotEmpty()) {
|
||||
return Bounce(bounceTunnels)
|
||||
}
|
||||
}
|
||||
|
||||
is StateChange.ActiveTunnelsChange -> Unit
|
||||
}
|
||||
@@ -91,12 +96,41 @@ data class AutoTunnelState(
|
||||
return !networkState.isEthernetConnected && networkState.isWifiConnected
|
||||
}
|
||||
|
||||
private fun stopKillSwitchOnTrusted(): Boolean {
|
||||
return networkState.isWifiConnected &&
|
||||
settings.isVpnKillSwitchEnabled &&
|
||||
settings.isDisableKillSwitchOnTrustedEnabled &&
|
||||
isCurrentSSIDTrusted()
|
||||
}
|
||||
|
||||
private fun startKillSwitch(): Boolean {
|
||||
return settings.isVpnKillSwitchEnabled &&
|
||||
(!settings.isDisableKillSwitchOnTrustedEnabled || !isCurrentSSIDTrusted())
|
||||
}
|
||||
|
||||
private fun isNoConnectivity(): Boolean {
|
||||
return !networkState.isEthernetConnected &&
|
||||
!networkState.isWifiConnected &&
|
||||
!networkState.isMobileDataConnected
|
||||
}
|
||||
|
||||
private fun bounceOnPingFailed(): List<Pair<TunnelConf, Map<String, String?>>> {
|
||||
return activeTunnels.entries
|
||||
.filter { (tunnel, state) ->
|
||||
tunnel.restartOnPingFailure &&
|
||||
(state.pingStates?.any { (key, pingState) ->
|
||||
pingState.failureReason == FailureReason.PingFailed
|
||||
} ?: false)
|
||||
}
|
||||
.map { (tunnel, state) ->
|
||||
val peerMap =
|
||||
(state.statistics?.getPeers()?.associate { peerKey ->
|
||||
peerKey.toBase64() to state.statistics.peerStats(peerKey)?.resolvedEndpoint
|
||||
} ?: emptyMap())
|
||||
Pair(tunnel, peerMap)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isCurrentSSIDTrusted(): Boolean {
|
||||
return networkState.wifiName?.let { hasTrustedWifiName(it) } == true
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.state
|
||||
|
||||
data class LogHealthState(val isHealthy: Boolean, val timestamp: Long = System.currentTimeMillis())
|
||||
@@ -9,51 +9,5 @@ data class TunnelState(
|
||||
val backendState: BackendMode = BackendMode.Inactive,
|
||||
val statistics: TunnelStatistics? = null,
|
||||
val pingStates: Map<Key, PingState>? = null,
|
||||
val logHealthState: LogHealthState? = null,
|
||||
) {
|
||||
|
||||
fun health(): Health {
|
||||
val now = System.currentTimeMillis()
|
||||
|
||||
if (pingStates == null && logHealthState == null && statistics == null)
|
||||
return Health.UNKNOWN
|
||||
|
||||
if (logHealthState?.isHealthy == false) return Health.UNHEALTHY
|
||||
|
||||
val healthLogs =
|
||||
logHealthState?.isHealthy == true &&
|
||||
(now - logHealthState.timestamp) <= LOG_HEALTH_SUCCESS_TIMEOUT_MS
|
||||
|
||||
if (pingStates?.any { !it.value.isReachable } == true) return Health.UNHEALTHY
|
||||
|
||||
if (statistics != null) {
|
||||
if (statistics.isTunnelStale()) {
|
||||
return Health.STALE
|
||||
}
|
||||
if ((logHealthState == null || !healthLogs) && pingStates == null) {
|
||||
return Health.HEALTHY
|
||||
}
|
||||
} else {
|
||||
if (!healthLogs) {
|
||||
return Health.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
if (healthLogs) {
|
||||
return Health.HEALTHY
|
||||
}
|
||||
|
||||
return Health.UNKNOWN
|
||||
}
|
||||
|
||||
enum class Health {
|
||||
UNKNOWN,
|
||||
UNHEALTHY,
|
||||
HEALTHY,
|
||||
STALE,
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val LOG_HEALTH_SUCCESS_TIMEOUT_MS = 2 * 60 * 1000L // 2 minutes
|
||||
}
|
||||
}
|
||||
val handshakeSuccessLogs: Boolean? = null,
|
||||
)
|
||||
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
||||
|
||||
import androidx.compose.foundation.focusable
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.outlined.Launch
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||
|
||||
@Composable
|
||||
fun LaunchButton(modifier: Modifier = Modifier.focusable(), onClick: () -> Unit) {
|
||||
IconButton(modifier = modifier, onClick = onClick) {
|
||||
val icon = Icons.AutoMirrored.Outlined.Launch
|
||||
Icon(icon, icon.name, Modifier.size(iconSize))
|
||||
}
|
||||
}
|
||||
+1
-6
@@ -3,7 +3,6 @@ package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
data class SelectionItem(
|
||||
@@ -12,9 +11,5 @@ data class SelectionItem(
|
||||
val title: (@Composable () -> Unit),
|
||||
val description: (@Composable () -> Unit)? = null,
|
||||
val onClick: (() -> Unit)? = null,
|
||||
val isEnabled: Boolean = true,
|
||||
val disabledReason: String? = null,
|
||||
val modifier: Modifier = Modifier.height(48.dp),
|
||||
val padding: Dp = if (description == null && disabledReason == null) 16.dp else 6.dp,
|
||||
val onLongPress: (() -> Unit)? = null,
|
||||
val modifier: Modifier = Modifier.height(64.dp),
|
||||
)
|
||||
|
||||
+35
-83
@@ -1,113 +1,65 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
|
||||
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.semantics.stateDescription
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
@Composable
|
||||
fun SurfaceSelectionGroupButton(items: List<SelectionItem>, modifier: Modifier = Modifier) {
|
||||
if (items.isEmpty()) return
|
||||
val context = LocalContext.current
|
||||
|
||||
Card(
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
) {
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(0.dp),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
items.forEach { item ->
|
||||
Box(
|
||||
contentAlignment = Alignment.Center,
|
||||
modifier =
|
||||
Modifier.fillMaxWidth()
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.alpha(if (item.isEnabled) 1f else 0.6f)
|
||||
.semantics {
|
||||
if (!item.isEnabled) {
|
||||
stateDescription =
|
||||
item.disabledReason ?: context.getString(R.string.disabled)
|
||||
}
|
||||
}
|
||||
.combinedClickable(
|
||||
onClick = { item.onClick?.invoke() },
|
||||
onLongClick = { item.onLongPress?.invoke() },
|
||||
enabled = true,
|
||||
),
|
||||
items.map { item ->
|
||||
Box(
|
||||
contentAlignment = Alignment.Center,
|
||||
modifier =
|
||||
modifier
|
||||
.fillMaxWidth()
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.then(item.onClick?.let { modifier.clickable { it() } } ?: modifier),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),
|
||||
modifier = Modifier.weight(4f, false).fillMaxWidth(),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
item.leading?.invoke()
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement =
|
||||
Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
||||
modifier =
|
||||
Modifier.weight(4f, false)
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = item.padding),
|
||||
Modifier.fillMaxWidth()
|
||||
.padding(start = if (item.leading != null) 16.dp else 0.dp)
|
||||
.weight(1f)
|
||||
.padding(
|
||||
vertical = if (item.description == null) 16.dp else 6.dp
|
||||
),
|
||||
) {
|
||||
item.leading?.let {
|
||||
Box(modifier = Modifier.alpha(if (item.isEnabled) 1f else 0.6f)) {
|
||||
it()
|
||||
}
|
||||
}
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement =
|
||||
Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
||||
modifier =
|
||||
Modifier.fillMaxWidth()
|
||||
.padding(start = if (item.leading != null) 16.dp else 0.dp)
|
||||
.weight(1f),
|
||||
) {
|
||||
Box(modifier = Modifier.alpha(if (item.isEnabled) 1f else 0.6f)) {
|
||||
item.title()
|
||||
}
|
||||
item.description?.invoke()
|
||||
if (!item.isEnabled && item.disabledReason != null) {
|
||||
Text(
|
||||
text = item.disabledReason,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color =
|
||||
MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
|
||||
)
|
||||
}
|
||||
}
|
||||
item.title()
|
||||
item.description?.let { it() }
|
||||
}
|
||||
item.trailing?.let {
|
||||
Box(
|
||||
contentAlignment = Alignment.CenterEnd,
|
||||
modifier =
|
||||
Modifier.padding(start = 16.dp)
|
||||
.alpha(if (item.isEnabled) 1f else 0.6f)
|
||||
.run {
|
||||
if (!item.isEnabled) {
|
||||
semantics {
|
||||
stateDescription =
|
||||
context.getString(R.string.disabled)
|
||||
}
|
||||
} else {
|
||||
this
|
||||
}
|
||||
},
|
||||
) {
|
||||
it()
|
||||
}
|
||||
}
|
||||
item.trailing?.let {
|
||||
Box(
|
||||
contentAlignment = Alignment.CenterEnd,
|
||||
modifier = Modifier.padding(start = 16.dp),
|
||||
) {
|
||||
it()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.prompt
|
||||
|
||||
import androidx.biometric.BiometricManager
|
||||
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
|
||||
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
|
||||
import androidx.biometric.BiometricPrompt
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
@Composable
|
||||
fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (String) -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val biometricManager = BiometricManager.from(context)
|
||||
val bio = biometricManager.canAuthenticate(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
|
||||
val isBiometricAvailable = remember {
|
||||
when (bio) {
|
||||
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
|
||||
onError(context.getString(R.string.bio_not_created))
|
||||
false
|
||||
}
|
||||
|
||||
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
|
||||
onError(context.getString(R.string.bio_update_required))
|
||||
false
|
||||
}
|
||||
|
||||
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED,
|
||||
BiometricManager.BIOMETRIC_STATUS_UNKNOWN,
|
||||
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE,
|
||||
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
|
||||
onError(context.getString(R.string.bio_not_supported))
|
||||
false
|
||||
}
|
||||
|
||||
BiometricManager.BIOMETRIC_SUCCESS -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
if (isBiometricAvailable) {
|
||||
val executor = remember { ContextCompat.getMainExecutor(context) }
|
||||
|
||||
val promptInfo =
|
||||
BiometricPrompt.PromptInfo.Builder()
|
||||
.setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
|
||||
.setTitle(context.getString(R.string.bio_auth_title))
|
||||
.setSubtitle(context.getString(R.string.bio_subtitle))
|
||||
.build()
|
||||
|
||||
val biometricPrompt =
|
||||
BiometricPrompt(
|
||||
context as FragmentActivity,
|
||||
executor,
|
||||
object : BiometricPrompt.AuthenticationCallback() {
|
||||
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
|
||||
super.onAuthenticationError(errorCode, errString)
|
||||
onFailure()
|
||||
}
|
||||
|
||||
override fun onAuthenticationSucceeded(
|
||||
result: BiometricPrompt.AuthenticationResult
|
||||
) {
|
||||
super.onAuthenticationSucceeded(result)
|
||||
onSuccess()
|
||||
}
|
||||
|
||||
override fun onAuthenticationFailed() {
|
||||
super.onAuthenticationFailed()
|
||||
onFailure()
|
||||
}
|
||||
},
|
||||
)
|
||||
biometricPrompt.authenticate(promptInfo)
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,58 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.navigation
|
||||
|
||||
import androidx.annotation.Keep
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Keep
|
||||
@Serializable
|
||||
sealed class Route {
|
||||
@Serializable data object TunnelsGraph : Route()
|
||||
|
||||
@Keep @Serializable data object TunnelsGraph : Route()
|
||||
@Serializable data object AutoTunnelGraph : Route()
|
||||
|
||||
@Keep @Serializable data object AutoTunnelGraph : Route()
|
||||
@Serializable data object SettingsGraph : Route()
|
||||
|
||||
@Keep @Serializable data object SettingsGraph : Route()
|
||||
@Serializable data object SupportGraph : Route()
|
||||
|
||||
@Keep @Serializable data object SupportGraph : Route()
|
||||
@Serializable data object Support : Route()
|
||||
|
||||
@Keep @Serializable data object Support : Route()
|
||||
@Serializable data object Lock : Route()
|
||||
|
||||
@Keep @Serializable data object Lock : Route()
|
||||
@Serializable data object License : Route()
|
||||
|
||||
@Keep @Serializable data object License : Route()
|
||||
@Serializable data object Logs : Route()
|
||||
|
||||
@Keep @Serializable data object Logs : Route()
|
||||
@Serializable data object Appearance : Route()
|
||||
|
||||
@Keep @Serializable data object Appearance : Route()
|
||||
@Serializable data object Language : Route()
|
||||
|
||||
@Keep @Serializable data object Language : Route()
|
||||
@Serializable data object Display : Route()
|
||||
|
||||
@Keep @Serializable data object Display : Route()
|
||||
@Serializable data object Tunnels : Route()
|
||||
|
||||
@Keep @Serializable data object Tunnels : Route()
|
||||
@Serializable data class TunnelOptions(val id: Int) : Route()
|
||||
|
||||
@Keep @Serializable data class TunnelOptions(val id: Int) : Route()
|
||||
@Serializable data class Config(val id: Int?) : Route()
|
||||
|
||||
@Keep @Serializable data class Config(val id: Int?) : Route()
|
||||
@Serializable data class SplitTunnel(val id: Int) : Route()
|
||||
|
||||
@Keep @Serializable data class SplitTunnel(val id: Int) : Route()
|
||||
@Serializable data class TunnelAutoTunnel(val id: Int) : Route()
|
||||
|
||||
@Keep @Serializable data class TunnelAutoTunnel(val id: Int) : Route()
|
||||
@Serializable data object Sort : Route()
|
||||
|
||||
@Keep @Serializable data object Sort : Route()
|
||||
@Serializable data object Settings : Route()
|
||||
|
||||
@Keep @Serializable data object Settings : Route()
|
||||
@Serializable data object TunnelMonitoring : Route()
|
||||
|
||||
@Keep @Serializable data object TunnelMonitoring : Route()
|
||||
@Serializable data object SystemFeatures : Route()
|
||||
|
||||
@Keep @Serializable data object SystemFeatures : Route()
|
||||
@Serializable data object Dns : Route()
|
||||
|
||||
@Keep @Serializable data object Dns : Route()
|
||||
@Serializable data object ProxySettings : Route()
|
||||
|
||||
@Keep @Serializable data object ProxySettings : Route()
|
||||
@Serializable data object AutoTunnel : Route()
|
||||
|
||||
@Keep @Serializable data object AutoTunnel : Route()
|
||||
@Serializable data object AdvancedAutoTunnel : Route()
|
||||
|
||||
@Keep @Serializable data object AdvancedAutoTunnel : Route()
|
||||
@Serializable data object WifiDetectionMethod : Route()
|
||||
|
||||
@Keep @Serializable data object WifiDetectionMethod : Route()
|
||||
|
||||
@Keep @Serializable data object LocationDisclosure : Route()
|
||||
|
||||
@Keep @Serializable data object Donate : Route()
|
||||
|
||||
@Keep @Serializable data object Addresses : Route()
|
||||
@Serializable data object LocationDisclosure : Route()
|
||||
}
|
||||
|
||||
+18
-4
@@ -17,6 +17,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
@@ -24,6 +25,7 @@ import com.zaneschepke.wireguardautotunnel.ui.navigation.BottomNavItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.debounce
|
||||
|
||||
@Composable
|
||||
fun NavHostController.getCurrentGraph(): State<Route?> {
|
||||
@@ -52,32 +54,44 @@ fun BottomNavbar(
|
||||
) {
|
||||
|
||||
val currentGraph by navController.getCurrentGraph()
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
val navigateToDebounced =
|
||||
remember<(Route) -> Unit> {
|
||||
debounce(scope = coroutineScope, 150L) { route ->
|
||||
navController.navigate(route) {
|
||||
popUpTo(navController.graph.findStartDestination().id) { saveState = true }
|
||||
launchSingleTop = true
|
||||
restoreState = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val items =
|
||||
listOf(
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.tunnels),
|
||||
icon = Icons.Rounded.Home,
|
||||
onClick = { navController.navigate(Route.TunnelsGraph) },
|
||||
onClick = { navigateToDebounced(Route.TunnelsGraph) },
|
||||
route = Route.TunnelsGraph,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.auto_tunnel),
|
||||
icon = Icons.Rounded.Bolt,
|
||||
onClick = { navController.navigate(Route.AutoTunnelGraph) },
|
||||
onClick = { navigateToDebounced(Route.AutoTunnelGraph) },
|
||||
route = Route.AutoTunnelGraph,
|
||||
active = isAutoTunnelActive,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.settings),
|
||||
icon = Icons.Rounded.Settings,
|
||||
onClick = { navController.navigate(Route.SettingsGraph) },
|
||||
onClick = { navigateToDebounced(Route.SettingsGraph) },
|
||||
route = Route.SettingsGraph,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.support),
|
||||
icon = Icons.Rounded.QuestionMark,
|
||||
onClick = { navController.navigate(Route.SupportGraph) },
|
||||
onClick = { navigateToDebounced(Route.SupportGraph) },
|
||||
route = Route.SupportGraph,
|
||||
),
|
||||
)
|
||||
|
||||
-334
@@ -1,334 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.navigation.components
|
||||
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.rounded.Sort
|
||||
import androidx.compose.material.icons.rounded.*
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.toRoute
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route.Config
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SharedAppViewModel
|
||||
|
||||
@Composable
|
||||
fun NavHostController.currentBackStackEntryAsNavbarState(
|
||||
sharedViewModel: SharedAppViewModel,
|
||||
navController: NavHostController,
|
||||
): State<NavbarState> {
|
||||
val sharedState by sharedViewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
val backStackEntry by currentBackStackEntryAsState()
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
val route =
|
||||
remember(backStackEntry) {
|
||||
backStackEntry?.destination?.route?.let {
|
||||
when (it.substringBefore("?").substringBefore("/").substringAfterLast(".")) {
|
||||
Route.Support::class.simpleName -> backStackEntry?.toRoute<Route.Support>()
|
||||
Route.Lock::class.simpleName -> backStackEntry?.toRoute<Route.Lock>()
|
||||
Route.License::class.simpleName -> backStackEntry?.toRoute<Route.License>()
|
||||
Route.Logs::class.simpleName -> backStackEntry?.toRoute<Route.Logs>()
|
||||
Route.Appearance::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.Appearance>()
|
||||
Route.Language::class.simpleName -> backStackEntry?.toRoute<Route.Language>()
|
||||
Route.Display::class.simpleName -> backStackEntry?.toRoute<Route.Display>()
|
||||
Route.Tunnels::class.simpleName -> backStackEntry?.toRoute<Route.Tunnels>()
|
||||
Route.TunnelOptions::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.TunnelOptions>()
|
||||
Route.Config::class.simpleName -> backStackEntry?.toRoute<Route.Config>()
|
||||
Route.SplitTunnel::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.SplitTunnel>()
|
||||
Route.TunnelAutoTunnel::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.TunnelAutoTunnel>()
|
||||
Route.Sort::class.simpleName -> backStackEntry?.toRoute<Route.Sort>()
|
||||
Route.Settings::class.simpleName -> backStackEntry?.toRoute<Route.Settings>()
|
||||
Route.TunnelMonitoring::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.TunnelMonitoring>()
|
||||
Route.SystemFeatures::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.SystemFeatures>()
|
||||
Route.Dns::class.simpleName -> backStackEntry?.toRoute<Route.Dns>()
|
||||
Route.ProxySettings::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.ProxySettings>()
|
||||
Route.AutoTunnel::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.AutoTunnel>()
|
||||
Route.AdvancedAutoTunnel::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.AdvancedAutoTunnel>()
|
||||
Route.WifiDetectionMethod::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.WifiDetectionMethod>()
|
||||
Route.LocationDisclosure::class.simpleName ->
|
||||
backStackEntry?.toRoute<Route.LocationDisclosure>()
|
||||
Route.Donate::class.simpleName -> backStackEntry?.toRoute<Route.Donate>()
|
||||
Route.Addresses::class.simpleName -> backStackEntry?.toRoute<Route.Addresses>()
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val disableDelete by
|
||||
rememberSaveable(sharedState.selectedTunnels, sharedState.tunnels) {
|
||||
mutableStateOf(
|
||||
sharedState.tunnels.any { tunnel ->
|
||||
tunnel.isActive &&
|
||||
sharedState.tunnels.any { selected -> selected.id == tunnel.id }
|
||||
}
|
||||
)
|
||||
}
|
||||
val selectedCount by
|
||||
rememberSaveable(sharedState.selectedTunnels) {
|
||||
mutableStateOf(sharedState.selectedTunnels.size)
|
||||
}
|
||||
|
||||
return produceState(initialValue = NavbarState(), route, selectedCount, disableDelete) {
|
||||
value =
|
||||
when (route) {
|
||||
Route.AdvancedAutoTunnel ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.advanced_settings)) },
|
||||
)
|
||||
Route.Appearance ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.appearance)) },
|
||||
)
|
||||
Route.AutoTunnel ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.auto_tunnel)) },
|
||||
)
|
||||
is Route.Config -> {
|
||||
val tunnel = sharedState.tunnels.find { it.id == route.id }
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = {
|
||||
val title = tunnel?.tunName ?: stringResource(R.string.new_tunnel)
|
||||
Text(title)
|
||||
},
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
keyboardController?.hide()
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
Route.Display ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.display_theme)) },
|
||||
)
|
||||
Route.Dns ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.dns_settings)) },
|
||||
)
|
||||
Route.Language ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.language)) },
|
||||
)
|
||||
Route.License ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.licenses)) },
|
||||
)
|
||||
Route.LocationDisclosure -> NavbarState(showBottomItems = true)
|
||||
Route.Lock -> NavbarState(showBottomItems = false)
|
||||
Route.Logs ->
|
||||
NavbarState(
|
||||
showBottomItems = false,
|
||||
removeBottom = true,
|
||||
topTitle = { Text(stringResource(R.string.logs)) },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Menu, R.string.quick_actions) {
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.Sheet.LoggerActions)
|
||||
}
|
||||
},
|
||||
)
|
||||
Route.ProxySettings ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.proxy_settings)) },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
keyboardController?.hide()
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
|
||||
}
|
||||
},
|
||||
)
|
||||
Route.Settings ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.settings)) },
|
||||
topTrailing = {
|
||||
ActionIconButton(
|
||||
Icons.Rounded.SettingsBackupRestore,
|
||||
R.string.quick_actions,
|
||||
) {
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.Sheet.BackupApp)
|
||||
}
|
||||
},
|
||||
)
|
||||
Route.Sort ->
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.sort)) },
|
||||
topTrailing = {
|
||||
Row {
|
||||
ActionIconButton(Icons.Rounded.SortByAlpha, R.string.sort) {
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.Sort)
|
||||
}
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
is Route.SplitTunnel -> {
|
||||
val tunnel = sharedState.tunnels.find { it.id == route.id }
|
||||
NavbarState(
|
||||
topTitle = { Text(tunnel?.tunName ?: "") },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
|
||||
}
|
||||
},
|
||||
showBottomItems = true,
|
||||
)
|
||||
}
|
||||
Route.Support ->
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.support)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
Route.SystemFeatures ->
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.android_integrations)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
is Route.TunnelAutoTunnel -> {
|
||||
val tunnel = sharedState.tunnels.find { it.id == route.id }
|
||||
NavbarState(showBottomItems = true, topTitle = { Text(tunnel?.tunName ?: "") })
|
||||
}
|
||||
Route.TunnelMonitoring ->
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.tunnel_monitoring)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
is Route.TunnelOptions -> {
|
||||
val tunnel = sharedState.tunnels.find { it.id == route.id }
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(tunnel?.tunName ?: "") },
|
||||
topTrailing = {
|
||||
Row {
|
||||
ActionIconButton(Icons.Rounded.QrCode2, R.string.show_qr) {
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.Modal.QR)
|
||||
}
|
||||
ActionIconButton(Icons.Rounded.Edit, R.string.edit_tunnel) {
|
||||
navigate(Config(route.id))
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
Route.Tunnels -> {
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.tunnels)) },
|
||||
topTrailing = {
|
||||
when (selectedCount) {
|
||||
0 -> DefaultTunnelsActions(navController, sharedViewModel)
|
||||
else ->
|
||||
Row {
|
||||
ActionIconButton(
|
||||
Icons.Rounded.SelectAll,
|
||||
R.string.select_all,
|
||||
) {
|
||||
sharedViewModel.toggleSelectAllTunnels()
|
||||
}
|
||||
// due to permissions, and SAF issues on TV, not support
|
||||
// less than Android
|
||||
// 10
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
ActionIconButton(
|
||||
Icons.Rounded.Download,
|
||||
R.string.download,
|
||||
) {
|
||||
sharedViewModel.postSideEffect(
|
||||
LocalSideEffect.Sheet.ExportTunnels
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedCount == 1) {
|
||||
ActionIconButton(Icons.Rounded.CopyAll, R.string.copy) {
|
||||
sharedViewModel.copySelectedTunnel()
|
||||
}
|
||||
}
|
||||
|
||||
if (!disableDelete) {
|
||||
ActionIconButton(
|
||||
Icons.Rounded.Delete,
|
||||
R.string.delete_tunnel,
|
||||
) {
|
||||
sharedViewModel.postSideEffect(
|
||||
LocalSideEffect.Modal.DeleteTunnels
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
showBottomItems = true,
|
||||
)
|
||||
}
|
||||
Route.WifiDetectionMethod ->
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.wifi_detection_method)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
Route.Donate -> {
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.donate_title)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
}
|
||||
Route.Addresses -> {
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.addresses)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
}
|
||||
Route.TunnelsGraph,
|
||||
Route.SettingsGraph,
|
||||
Route.AutoTunnelGraph,
|
||||
Route.SupportGraph,
|
||||
null -> NavbarState()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun DefaultTunnelsActions(
|
||||
navController: NavHostController,
|
||||
sharedViewModel: SharedAppViewModel,
|
||||
) {
|
||||
Row {
|
||||
ActionIconButton(Icons.AutoMirrored.Rounded.Sort, R.string.sort) {
|
||||
navController.navigate(Route.Sort)
|
||||
}
|
||||
ActionIconButton(Icons.Rounded.Add, R.string.add_tunnel) {
|
||||
sharedViewModel.postSideEffect(LocalSideEffect.Sheet.ImportTunnels)
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -22,6 +22,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.banner.WarningBanner
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
@@ -31,6 +32,7 @@ import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.AdvancedSettingsItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.networkTunnelingItems
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.wifiTunnelingItems
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchAppSettings
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchLocationServicesSettings
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
|
||||
@@ -39,10 +41,21 @@ import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
|
||||
@Composable
|
||||
fun AutoTunnelScreen(viewModel: AutoTunnelViewModel = hiltViewModel()) {
|
||||
val context = LocalContext.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val navController = LocalNavController.current
|
||||
val autoTunnelState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
if (!autoTunnelState.stateInitialized) return
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.auto_tunnel)) },
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
var showLocationDialog by remember { mutableStateOf(false) }
|
||||
|
||||
val showLocationServicesWarning by
|
||||
|
||||
+14
@@ -12,6 +12,7 @@ import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -19,12 +20,25 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.dropdown.LabelledDropdown
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
|
||||
|
||||
@Composable
|
||||
fun AutoTunnelAdvancedScreen(viewModel: AutoTunnelViewModel) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val autoTunnelState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.advanced_settings)) },
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
|
||||
|
||||
+2
@@ -36,6 +36,7 @@ fun networkTunnelingItems(
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
enabled = !autoTunnelState.generalSettings.isAlwaysOnVpnEnabled,
|
||||
checked = autoTunnelState.generalSettings.isTunnelOnMobileDataEnabled,
|
||||
onClick = { viewModel.setTunnelOnCellular(it) },
|
||||
)
|
||||
@@ -76,6 +77,7 @@ fun networkTunnelingItems(
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
enabled = !autoTunnelState.generalSettings.isAlwaysOnVpnEnabled,
|
||||
checked = autoTunnelState.generalSettings.isTunnelOnEthernetEnabled,
|
||||
onClick = { viewModel.setTunnelOnEthernet(it) },
|
||||
)
|
||||
|
||||
+1
@@ -60,6 +60,7 @@ fun wifiTunnelingItems(
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
enabled = !autoTunnelState.generalSettings.isAlwaysOnVpnEnabled,
|
||||
checked = autoTunnelState.generalSettings.isTunnelOnWifiEnabled,
|
||||
onClick = { viewModel.setAutoTunnelOnWifiEnabled(it) },
|
||||
)
|
||||
|
||||
+14
@@ -4,16 +4,21 @@ import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
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
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.WifiDetectionMethod
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.IconSurfaceButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asDescriptionString
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asTitleString
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
|
||||
@@ -25,6 +30,15 @@ fun WifiDetectionMethodScreen(viewModel: AutoTunnelViewModel) {
|
||||
|
||||
val autoTunnelState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.wifi_detection_method)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||
|
||||
+5
@@ -10,15 +10,20 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.LocationDisclosureHeader
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.appSettingsItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.skipItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
|
||||
|
||||
@Composable
|
||||
fun LocationDisclosureScreen(viewModel: AutoTunnelViewModel) {
|
||||
val navController = LocalNavController.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
|
||||
LaunchedEffect(Unit) { sharedViewModel.updateNavbarState(NavbarState(showBottomItems = true)) }
|
||||
|
||||
LaunchedEffect(Unit) { viewModel.setLocationDisclosureShown() }
|
||||
|
||||
|
||||
+4
-4
@@ -3,16 +3,14 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.pin
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import xyz.teamgravity.pin_lock_compose.PinLock
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
@@ -24,6 +22,8 @@ fun PinLockScreen() {
|
||||
val pinAlreadyExists by rememberSaveable { mutableStateOf(PinManager.pinExists()) }
|
||||
var pinCreated by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(Unit) { sharedViewModel.updateNavbarState(NavbarState(showBottomItems = false)) }
|
||||
|
||||
PinLock(
|
||||
title = {
|
||||
Text(
|
||||
|
||||
+33
-51
@@ -8,12 +8,16 @@ 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.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.SettingsBackupRestore
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
@@ -23,15 +27,13 @@ import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.*
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.proxy.compoents.AppModeBottomSheet
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asString
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SettingsViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
|
||||
@Composable
|
||||
@@ -45,22 +47,10 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel()) {
|
||||
|
||||
val settingsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
var showBackupSheet by rememberSaveable { mutableStateOf(false) }
|
||||
var showAppModeSheet by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
val appMode by
|
||||
rememberSaveable(settingsState.settings.appMode) {
|
||||
mutableStateOf(settingsState.settings.appMode)
|
||||
}
|
||||
|
||||
if (!settingsState.stateInitialized) return
|
||||
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
when (sideEffect) {
|
||||
LocalSideEffect.Sheet.BackupApp -> showBackupSheet = true
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
var showBackupSheet by rememberSaveable { mutableStateOf(false) }
|
||||
var showAppModeSheet by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
val showProxySettings by
|
||||
remember(settingsState.settings.appMode) {
|
||||
@@ -72,6 +62,20 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel()) {
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.settings)) },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.SettingsBackupRestore, R.string.quick_actions) {
|
||||
showBackupSheet = true
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
if (showBackupSheet) BackupBottomSheet() { showBackupSheet = false }
|
||||
if (showAppModeSheet)
|
||||
AppModeBottomSheet(sharedViewModel::setAppMode, settingsState.settings.appMode) {
|
||||
@@ -99,62 +103,40 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel()) {
|
||||
),
|
||||
) {
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(appModeItem(settingsState.settings.appMode) { showAppModeSheet = true })
|
||||
buildList {
|
||||
add(backendModeItem(settingsState.settings.appMode) { showAppModeSheet = true })
|
||||
}
|
||||
)
|
||||
SectionDivider()
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
buildList {
|
||||
if (appMode == AppMode.LOCK_DOWN) {
|
||||
if (settingsState.settings.appMode == AppMode.LOCK_DOWN) {
|
||||
add(
|
||||
lanTrafficItem(settingsState.settings.isLanOnKillSwitchEnabled) {
|
||||
viewModel.setLanKillSwitchEnabled(it)
|
||||
}
|
||||
)
|
||||
}
|
||||
add(
|
||||
tunnelMonitoringItem(
|
||||
appMode,
|
||||
onClick = { navController.navigate(Route.TunnelMonitoring) },
|
||||
) {
|
||||
sharedViewModel.showSnackMessage(
|
||||
StringValue.StringResource(
|
||||
R.string.mode_disabled_template,
|
||||
appMode.asString(context),
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
add(
|
||||
dnsSettingsItem(appMode, onClick = { navController.navigate(Route.Dns) }) {
|
||||
sharedViewModel.showSnackMessage(
|
||||
StringValue.StringResource(
|
||||
R.string.mode_disabled_template,
|
||||
appMode.asString(context),
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
if (showProxySettings)
|
||||
add(proxYSettingsItem() { navController.navigate(Route.ProxySettings) })
|
||||
add(tunnelMonitoringItem(navController))
|
||||
add(dnsSettingsItem(navController))
|
||||
// TODO changing these settings won't work in certain app states
|
||||
if (showProxySettings) add(proxYSettingsItem(navController))
|
||||
}
|
||||
)
|
||||
SectionDivider()
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(systemFeaturesItem() { navController.navigate(Route.SystemFeatures) })
|
||||
)
|
||||
SurfaceSelectionGroupButton(listOf(systemFeaturesItem(navController)))
|
||||
SectionDivider()
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
buildList {
|
||||
add(appearanceItem() { navController.navigate(Route.Appearance) })
|
||||
add(appearanceItem(navController))
|
||||
add(
|
||||
localLoggingItem(settingsState.isLocalLoggingEnabled) {
|
||||
viewModel.setLocalLogging(it)
|
||||
}
|
||||
)
|
||||
if (settingsState.isLocalLoggingEnabled)
|
||||
add(readLogsItem() { navController.navigate(Route.Logs) })
|
||||
if (settingsState.isLocalLoggingEnabled) add(readLogsItem(navController))
|
||||
add(
|
||||
pinLockItem(settingsState.isPinLockEnabled) { enabled ->
|
||||
if (enabled) {
|
||||
|
||||
+16
@@ -4,20 +4,36 @@ import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.components.DisplayThemeItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.components.LanguageItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.components.NotificationsItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
|
||||
@Composable
|
||||
fun AppearanceScreen() {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val navController = LocalNavController.current
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.appearance)) },
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
|
||||
|
||||
+2
-2
@@ -9,7 +9,7 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.LaunchButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchNotificationSettings
|
||||
|
||||
@@ -25,7 +25,7 @@ fun NotificationsItem(): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
trailing = { LaunchButton { context.launchNotificationSettings() } },
|
||||
trailing = { ForwardButton { context.launchNotificationSettings() } },
|
||||
onClick = { context.launchNotificationSettings() },
|
||||
)
|
||||
}
|
||||
|
||||
+12
@@ -4,7 +4,9 @@ import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -14,6 +16,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.IconSurfaceButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
|
||||
|
||||
@Composable
|
||||
@@ -23,6 +26,15 @@ fun DisplayScreen() {
|
||||
|
||||
val appState by sharedViewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.display_theme)) },
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||
|
||||
+11
@@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -17,6 +18,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.IconSurfaceButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||
import java.text.Collator
|
||||
import java.util.*
|
||||
@@ -27,6 +29,15 @@ fun LanguageScreen() {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val appState by sharedViewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.language)) },
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val collator = Collator.getInstance(Locale.getDefault())
|
||||
val locales =
|
||||
LocaleUtil.supportedLocales.map {
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.components
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
|
||||
@Composable
|
||||
fun AuthorizationPromptWrapper(onSuccess: () -> Unit, onDismiss: () -> Unit) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
AuthorizationPrompt(
|
||||
onSuccess = { onSuccess() },
|
||||
onError = { _ ->
|
||||
onDismiss()
|
||||
sharedViewModel.showSnackMessage(
|
||||
StringValue.StringResource(R.string.error_authentication_failed)
|
||||
)
|
||||
},
|
||||
onFailure = {
|
||||
onDismiss()
|
||||
sharedViewModel.showSnackMessage(
|
||||
StringValue.StringResource(R.string.error_authorization_failed)
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
+5
-3
@@ -7,12 +7,14 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun appearanceItem(onClick: () -> Unit): SelectionItem {
|
||||
fun appearanceItem(navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.AutoMirrored.Outlined.ViewQuilt, contentDescription = null) },
|
||||
title = {
|
||||
@@ -22,7 +24,7 @@ fun appearanceItem(onClick: () -> Unit): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
onClick = onClick,
|
||||
trailing = { ForwardButton { navController.navigate(Route.Appearance) } },
|
||||
onClick = { navController.navigate(Route.Appearance) },
|
||||
)
|
||||
}
|
||||
|
||||
+2
-12
@@ -1,12 +1,9 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.components
|
||||
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.ExpandMore
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@@ -16,22 +13,15 @@ import com.zaneschepke.wireguardautotunnel.data.model.AppMode
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asTitleString
|
||||
|
||||
@Composable
|
||||
fun appModeItem(appMode: AppMode, onClick: () -> Unit): SelectionItem {
|
||||
fun backendModeItem(appMode: AppMode, onClick: () -> Unit): SelectionItem {
|
||||
val context = LocalContext.current
|
||||
return SelectionItem(
|
||||
leading = { Icon(ImageVector.vectorResource(R.drawable.sdk), contentDescription = null) },
|
||||
trailing = {
|
||||
IconButton(onClick = onClick) {
|
||||
Icon(
|
||||
Icons.Outlined.ExpandMore,
|
||||
contentDescription = stringResource(R.string.select),
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
}
|
||||
Icon(Icons.Outlined.ExpandMore, contentDescription = stringResource(R.string.select))
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.backend_mode), SelectionLabelType.TITLE)
|
||||
+5
-18
@@ -6,29 +6,18 @@ import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asString
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun dnsSettingsItem(
|
||||
appMode: AppMode,
|
||||
onClick: () -> Unit,
|
||||
onDisabledClick: () -> Unit,
|
||||
): SelectionItem {
|
||||
val context = LocalContext.current
|
||||
val enabled by rememberSaveable(appMode) { mutableStateOf(appMode != AppMode.KERNEL) }
|
||||
val click = if (enabled) onClick else onDisabledClick
|
||||
fun dnsSettingsItem(navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Dns, null) },
|
||||
trailing = { ForwardButton { click() } },
|
||||
trailing = { ForwardButton { navController.navigate(Route.Dns) } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.dns_settings),
|
||||
@@ -36,8 +25,6 @@ fun dnsSettingsItem(
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = click,
|
||||
disabledReason =
|
||||
context.getString(R.string.mode_disabled_template, appMode.asString(context)),
|
||||
onClick = { navController.navigate(Route.Dns) },
|
||||
)
|
||||
}
|
||||
|
||||
+5
-3
@@ -7,15 +7,17 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.res.vectorResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun proxYSettingsItem(onClick: () -> Unit): SelectionItem {
|
||||
fun proxYSettingsItem(navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(ImageVector.vectorResource(R.drawable.proxy), null) },
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
trailing = { ForwardButton { navController.navigate(Route.ProxySettings) } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.proxy_settings),
|
||||
@@ -23,6 +25,6 @@ fun proxYSettingsItem(onClick: () -> Unit): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = onClick,
|
||||
onClick = { navController.navigate(Route.ProxySettings) },
|
||||
)
|
||||
}
|
||||
|
||||
+5
-3
@@ -5,20 +5,22 @@ import androidx.compose.material.icons.filled.ViewTimeline
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun readLogsItem(onClick: () -> Unit): SelectionItem {
|
||||
fun readLogsItem(navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Filled.ViewTimeline, contentDescription = null) },
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.read_logs), SelectionLabelType.TITLE)
|
||||
},
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
onClick = onClick,
|
||||
trailing = { ForwardButton { navController.navigate(Route.Logs) } },
|
||||
onClick = { navController.navigate(Route.Logs) },
|
||||
)
|
||||
}
|
||||
|
||||
+5
-3
@@ -7,15 +7,17 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun systemFeaturesItem(onClick: () -> Unit): SelectionItem {
|
||||
fun systemFeaturesItem(navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Android, null) },
|
||||
trailing = { ForwardButton { onClick.invoke() } },
|
||||
trailing = { ForwardButton { navController.navigate(Route.SystemFeatures) } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.system_features),
|
||||
@@ -23,6 +25,6 @@ fun systemFeaturesItem(onClick: () -> Unit): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = onClick,
|
||||
onClick = { navController.navigate(Route.SystemFeatures) },
|
||||
)
|
||||
}
|
||||
|
||||
+5
-22
@@ -6,32 +6,18 @@ import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asString
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun tunnelMonitoringItem(
|
||||
appMode: AppMode,
|
||||
onClick: () -> Unit,
|
||||
onDisabledClick: () -> Unit,
|
||||
): SelectionItem {
|
||||
val context = LocalContext.current
|
||||
val enabled by
|
||||
rememberSaveable(appMode) {
|
||||
mutableStateOf(appMode == AppMode.VPN || appMode == AppMode.KERNEL)
|
||||
}
|
||||
val click = if (enabled) onClick else onDisabledClick
|
||||
fun tunnelMonitoringItem(navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.MonitorHeart, null) },
|
||||
trailing = { ForwardButton { click() } },
|
||||
trailing = { ForwardButton { navController.navigate(Route.TunnelMonitoring) } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.tunnel_monitoring),
|
||||
@@ -39,9 +25,6 @@ fun tunnelMonitoringItem(
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
isEnabled = enabled,
|
||||
onClick = click,
|
||||
disabledReason =
|
||||
context.getString(R.string.mode_disabled_template, appMode.asString(context)),
|
||||
onClick = { navController.navigate(Route.TunnelMonitoring) },
|
||||
)
|
||||
}
|
||||
|
||||
+13
@@ -14,6 +14,7 @@ import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -24,14 +25,26 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.DnsProvider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.dropdown.LabelledDropdown
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SettingsViewModel
|
||||
|
||||
@Composable
|
||||
fun DnsSettingsScreen(viewModel: SettingsViewModel) {
|
||||
val context = LocalContext.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val settingsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.dns_settings)) },
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
|
||||
|
||||
+18
-5
@@ -4,6 +4,8 @@ import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Menu
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
@@ -17,15 +19,15 @@ import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.logs.components.LogList
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.logs.components.LogsBottomSheet
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.LoggerViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
|
||||
@Composable
|
||||
fun LogsScreen(viewModel: LoggerViewModel = hiltViewModel()) {
|
||||
val sharedAppViewModel = LocalSharedVm.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val loggerState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
val lazyColumnListState = rememberLazyListState()
|
||||
@@ -33,8 +35,19 @@ fun LogsScreen(viewModel: LoggerViewModel = hiltViewModel()) {
|
||||
var lastScrollPosition by rememberSaveable() { mutableIntStateOf(0) }
|
||||
var showLogsSheet by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
sharedAppViewModel.collectSideEffect { sideEffect ->
|
||||
if (sideEffect is LocalSideEffect.Sheet.LoggerActions) showLogsSheet = true
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = false,
|
||||
removeBottom = true,
|
||||
topTitle = { Text(stringResource(R.string.logs)) },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Menu, R.string.quick_actions) {
|
||||
showLogsSheet = true
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(isAutoScrolling) {
|
||||
|
||||
+13
@@ -14,6 +14,7 @@ import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -21,16 +22,28 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.dropdown.LabelledDropdown
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.monitoring.components.detailedPingStatsItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.monitoring.components.enablePingMonitoringItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SettingsViewModel
|
||||
|
||||
@Composable
|
||||
fun TunnelMonitoringScreen(viewModel: SettingsViewModel) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val settingsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.tunnel_monitoring)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
|
||||
|
||||
+57
-80
@@ -1,15 +1,14 @@
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Forward5
|
||||
import androidx.compose.material.icons.outlined.Http
|
||||
import androidx.compose.material.icons.outlined.RemoveRedEye
|
||||
import androidx.compose.material.icons.rounded.Save
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -25,6 +24,7 @@ import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.AppProxySettings
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SecureScreenFromRecording
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
@@ -32,68 +32,53 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLab
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.ConfigurationTextBox
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.ProxySettingsViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
|
||||
@Composable
|
||||
fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
|
||||
val proxySettingsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
if (!proxySettingsState.stateInitialized) return
|
||||
val proxySettings by remember { derivedStateOf { proxySettingsState.proxySettings } }
|
||||
|
||||
val proxySettings by
|
||||
remember(proxySettingsState) { mutableStateOf(proxySettingsState.proxySettings) }
|
||||
|
||||
var socks5Enabled by
|
||||
remember(proxySettings) {
|
||||
mutableStateOf(proxySettingsState.proxySettings.socks5ProxyEnabled)
|
||||
}
|
||||
var httpEnabled by
|
||||
remember(proxySettings) {
|
||||
mutableStateOf(proxySettingsState.proxySettings.httpProxyEnabled)
|
||||
}
|
||||
var socksBindAddress by
|
||||
remember(proxySettings) {
|
||||
mutableStateOf(proxySettingsState.proxySettings.socks5ProxyBindAddress ?: "")
|
||||
}
|
||||
var httpBindAddress by
|
||||
remember(proxySettings) {
|
||||
mutableStateOf(proxySettingsState.proxySettings.httpProxyBindAddress ?: "")
|
||||
}
|
||||
var proxyUsername by
|
||||
remember(proxySettings) {
|
||||
mutableStateOf(proxySettingsState.proxySettings.proxyUsername ?: "")
|
||||
}
|
||||
var proxyPassword by
|
||||
remember(proxySettings) {
|
||||
mutableStateOf(proxySettingsState.proxySettings.proxyPassword ?: "")
|
||||
}
|
||||
var passwordVisible by
|
||||
remember(proxySettings) { mutableStateOf(proxySettingsState.passwordVisible) }
|
||||
var socksBindAddress by remember { mutableStateOf(proxySettings.socks5ProxyBindAddress ?: "") }
|
||||
var httpBindAddress by remember { mutableStateOf(proxySettings.httpProxyBindAddress ?: "") }
|
||||
var proxyUsername by remember { mutableStateOf(proxySettings.proxyUsername ?: "") }
|
||||
var proxyPassword by remember { mutableStateOf(proxySettings.proxyPassword ?: "") }
|
||||
var passwordVisible by remember { mutableStateOf(proxySettingsState.passwordVisible) }
|
||||
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
|
||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
when (sideEffect) {
|
||||
LocalSideEffect.SaveChanges -> {
|
||||
viewModel.save(
|
||||
AppProxySettings(
|
||||
socks5ProxyEnabled = socks5Enabled,
|
||||
socks5ProxyBindAddress = socksBindAddress,
|
||||
httpProxyEnabled = httpEnabled,
|
||||
httpProxyBindAddress = httpBindAddress,
|
||||
proxyUsername = proxyUsername,
|
||||
proxyPassword = proxyPassword,
|
||||
)
|
||||
)
|
||||
}
|
||||
else -> Unit
|
||||
}
|
||||
if (!proxySettingsState.stateInitialized) return
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.proxy_settings)) },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
keyboardController?.hide()
|
||||
viewModel.save(
|
||||
AppProxySettings(
|
||||
socks5ProxyEnabled = proxySettings.socks5ProxyEnabled,
|
||||
socks5ProxyBindAddress = socksBindAddress,
|
||||
httpProxyEnabled = proxySettings.httpProxyEnabled,
|
||||
httpProxyBindAddress = httpBindAddress,
|
||||
proxyUsername = proxyUsername,
|
||||
proxyPassword = proxyPassword,
|
||||
)
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
SecureScreenFromRecording()
|
||||
@@ -114,15 +99,17 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(checked = socks5Enabled, onClick = { socks5Enabled = it })
|
||||
ScaledSwitch(
|
||||
checked = proxySettings.socks5ProxyEnabled,
|
||||
onClick = { viewModel.setEnableSocks5(it) },
|
||||
)
|
||||
},
|
||||
onClick = { socks5Enabled = !socks5Enabled },
|
||||
onClick = { viewModel.setEnableSocks5(!proxySettings.socks5ProxyEnabled) },
|
||||
)
|
||||
)
|
||||
)
|
||||
if (socks5Enabled) {
|
||||
if (proxySettings.socks5ProxyEnabled) {
|
||||
ConfigurationTextBox(
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.defaults_to_template,
|
||||
@@ -131,11 +118,7 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
label = stringResource(R.string.socks_5_bind_address),
|
||||
value = socksBindAddress,
|
||||
isError = proxySettingsState.isSocks5BindAddressError,
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isSocks5BindAddressError)
|
||||
viewModel.clearSocks5BindError()
|
||||
socksBindAddress = it
|
||||
},
|
||||
onValueChange = { socksBindAddress = it },
|
||||
)
|
||||
}
|
||||
SurfaceSelectionGroupButton(
|
||||
@@ -149,13 +132,16 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(checked = httpEnabled, onClick = { httpEnabled = it })
|
||||
ScaledSwitch(
|
||||
checked = proxySettings.httpProxyEnabled,
|
||||
onClick = { viewModel.setEnableHttp(it) },
|
||||
)
|
||||
},
|
||||
onClick = { httpEnabled = !httpEnabled },
|
||||
onClick = { viewModel.setEnableHttp(!proxySettings.httpProxyEnabled) },
|
||||
)
|
||||
)
|
||||
)
|
||||
if (httpEnabled) {
|
||||
if (proxySettings.httpProxyEnabled) {
|
||||
ConfigurationTextBox(
|
||||
hint =
|
||||
stringResource(
|
||||
@@ -165,17 +151,14 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
label = stringResource(R.string.http_bind_address),
|
||||
value = httpBindAddress,
|
||||
isError = proxySettingsState.isHttpBindAddressError,
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isSocks5BindAddressError) viewModel.clearHttpBindError()
|
||||
httpBindAddress = it
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
onValueChange = { httpBindAddress = it },
|
||||
)
|
||||
}
|
||||
if (socks5Enabled || httpEnabled) {
|
||||
if (proxySettings.httpProxyEnabled || proxySettings.socks5ProxyEnabled) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
) {
|
||||
GroupLabel(
|
||||
stringResource(
|
||||
@@ -185,29 +168,23 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = proxyUsername,
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isUserNameError) viewModel.clearUsernameError()
|
||||
proxyUsername = it
|
||||
},
|
||||
onValueChange = { proxyUsername = it },
|
||||
label = stringResource(R.string.username),
|
||||
isError = proxySettingsState.isUserNameError,
|
||||
hint = "",
|
||||
keyboardActions = keyboardActions,
|
||||
keyboardOptions = keyboardOptions,
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = proxyPassword,
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isUserNameError) viewModel.clearPasswordError()
|
||||
proxyPassword = it
|
||||
},
|
||||
onValueChange = { proxyPassword = it },
|
||||
label = stringResource(R.string.password),
|
||||
isError = proxySettingsState.isPasswordError,
|
||||
hint = "",
|
||||
keyboardActions = keyboardActions,
|
||||
keyboardOptions = keyboardOptions,
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
trailing = {
|
||||
IconButton(onClick = { passwordVisible = !passwordVisible }) {
|
||||
Icon(
|
||||
|
||||
+16
@@ -6,25 +6,41 @@ 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.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SecureScreenFromRecording
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.system.components.*
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SettingsViewModel
|
||||
|
||||
@Composable
|
||||
fun SystemFeaturesScreen(viewModel: SettingsViewModel) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val settingsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
val isTv = LocalIsAndroidTV.current
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.android_integrations)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
SecureScreenFromRecording()
|
||||
|
||||
Column(
|
||||
|
||||
+2
-2
@@ -9,7 +9,7 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.LaunchButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchVpnSettings
|
||||
|
||||
@@ -25,7 +25,7 @@ fun nativeKillSwitchItem(): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
trailing = { LaunchButton { context.launchVpnSettings() } },
|
||||
trailing = { ForwardButton { context.launchVpnSettings() } },
|
||||
onClick = { context.launchVpnSettings() },
|
||||
)
|
||||
}
|
||||
|
||||
+14
-9
@@ -6,11 +6,8 @@ import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.LinearProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.StrokeCap
|
||||
@@ -23,14 +20,14 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.components.ContactSupportOptions
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.components.DonateSection
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.components.GeneralSupportOptions
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.components.UpdateSection
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.canInstallPackages
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
@@ -40,11 +37,21 @@ import com.zaneschepke.wireguardautotunnel.viewmodel.SupportViewModel
|
||||
@Composable
|
||||
fun SupportScreen(viewModel: SupportViewModel) {
|
||||
val context = LocalContext.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val navController = LocalNavController.current
|
||||
val supportState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
var showPermissionDialog by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.support)) },
|
||||
showBottomItems = true,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
if (supportState.appUpdate != null) {
|
||||
InfoDialog(
|
||||
onDismiss = { viewModel.dismissUpdate() },
|
||||
@@ -136,9 +143,7 @@ fun SupportScreen(viewModel: SupportViewModel) {
|
||||
stringResource(R.string.thank_you),
|
||||
modifier = Modifier.padding(horizontal = 12.dp).padding(bottom = 12.dp),
|
||||
)
|
||||
UpdateSection { viewModel.checkForUpdate() }
|
||||
SectionDivider()
|
||||
DonateSection { navController.navigate(Route.Donate) }
|
||||
UpdateSection(onUpdateCheck = { viewModel.checkForUpdate() })
|
||||
SectionDivider()
|
||||
GeneralSupportOptions(navController)
|
||||
SectionDivider()
|
||||
|
||||
+104
-72
@@ -2,6 +2,7 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.support.components
|
||||
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Favorite
|
||||
import androidx.compose.material.icons.outlined.Mail
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
@@ -9,13 +10,15 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.res.vectorResource
|
||||
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.LaunchButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchSupportEmail
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
|
||||
@@ -23,77 +26,106 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
fun ContactSupportOptions(context: android.content.Context) {
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
listOf(
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.matrix),
|
||||
contentDescription = null,
|
||||
Modifier.size(iconSize),
|
||||
buildList {
|
||||
addAll(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.matrix),
|
||||
contentDescription = null,
|
||||
Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.join_matrix),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton {
|
||||
context.openWebUrl(context.getString(R.string.matrix_url))
|
||||
}
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.matrix_url)) },
|
||||
),
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.telegram),
|
||||
contentDescription = null,
|
||||
Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.join_telegram),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton {
|
||||
context.openWebUrl(context.getString(R.string.telegram_url))
|
||||
}
|
||||
},
|
||||
onClick = {
|
||||
context.openWebUrl(context.getString(R.string.telegram_url))
|
||||
},
|
||||
),
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.github),
|
||||
contentDescription = null,
|
||||
Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.open_issue),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton {
|
||||
context.openWebUrl(context.getString(R.string.github_url))
|
||||
}
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.github_url)) },
|
||||
),
|
||||
SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Mail, contentDescription = null) },
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.email_description),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = { ForwardButton { context.launchSupportEmail() } },
|
||||
onClick = { context.launchSupportEmail() },
|
||||
),
|
||||
)
|
||||
)
|
||||
if (BuildConfig.FLAVOR != Constants.GOOGLE_PLAY_FLAVOR) {
|
||||
add(
|
||||
SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Favorite, contentDescription = null) },
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.donate),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton {
|
||||
context.openWebUrl(context.getString(R.string.donate_url))
|
||||
}
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.donate_url)) },
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.join_matrix),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton { context.openWebUrl(context.getString(R.string.matrix_url)) }
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.matrix_url)) },
|
||||
),
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.telegram),
|
||||
contentDescription = null,
|
||||
Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.join_telegram),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton {
|
||||
context.openWebUrl(context.getString(R.string.telegram_url))
|
||||
}
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.telegram_url)) },
|
||||
),
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.github),
|
||||
contentDescription = null,
|
||||
Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.open_issue),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton { context.openWebUrl(context.getString(R.string.github_url)) }
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.github_url)) },
|
||||
),
|
||||
SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Mail, contentDescription = null) },
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.email_description),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = { LaunchButton { context.launchSupportEmail() } },
|
||||
onClick = { context.launchSupportEmail() },
|
||||
),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.components
|
||||
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Favorite
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
|
||||
@Composable
|
||||
fun DonateSection(onClick: () -> Unit) {
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Favorite, contentDescription = null) },
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.donate), SelectionLabelType.TITLE)
|
||||
},
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
onClick = onClick,
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
+2
-3
@@ -11,7 +11,6 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.LaunchButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
@@ -35,7 +34,7 @@ fun GeneralSupportOptions(navController: NavController) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton {
|
||||
ForwardButton {
|
||||
context.openWebUrl(context.getString(R.string.docs_url))
|
||||
}
|
||||
},
|
||||
@@ -52,7 +51,7 @@ fun GeneralSupportOptions(navController: NavController) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton {
|
||||
ForwardButton {
|
||||
context.openWebUrl(context.getString(R.string.privacy_policy_url))
|
||||
}
|
||||
},
|
||||
|
||||
+6
-10
@@ -5,7 +5,6 @@ import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.CloudDownload
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
@@ -13,15 +12,9 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionIte
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberClipboardHelper
|
||||
|
||||
@Composable
|
||||
fun UpdateSection(onUpdateCheck: () -> Unit) {
|
||||
val clipboardManager = rememberClipboardHelper()
|
||||
val version = remember {
|
||||
"v${BuildConfig.VERSION_NAME +
|
||||
if(BuildConfig.DEBUG) "-debug" else "" }"
|
||||
}
|
||||
fun UpdateSection(onUpdateCheck: () -> Unit = {}) {
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
@@ -35,7 +28,11 @@ fun UpdateSection(onUpdateCheck: () -> Unit) {
|
||||
description = {
|
||||
Column {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.version_template, version),
|
||||
stringResource(
|
||||
R.string.version_template,
|
||||
"v${BuildConfig.VERSION_NAME +
|
||||
if(BuildConfig.DEBUG) "-debug" else "" }",
|
||||
),
|
||||
SelectionLabelType.DESCRIPTION,
|
||||
)
|
||||
SelectionItemLabel(
|
||||
@@ -45,7 +42,6 @@ fun UpdateSection(onUpdateCheck: () -> Unit) {
|
||||
}
|
||||
},
|
||||
onClick = onUpdateCheck,
|
||||
onLongPress = { clipboardManager.copy(version) },
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.donate
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
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.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.components.DonationHeroSection
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.components.DonationOptions
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.components.GoogleDonationMessage
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
|
||||
@Composable
|
||||
fun DonateScreen(navController: NavController) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
|
||||
modifier =
|
||||
Modifier.fillMaxSize()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(vertical = 24.dp)
|
||||
.padding(horizontal = 12.dp),
|
||||
) {
|
||||
DonationHeroSection()
|
||||
SectionDivider()
|
||||
if (BuildConfig.FLAVOR != Constants.GOOGLE_PLAY_FLAVOR) {
|
||||
DonationOptions { navController.navigate(Route.Addresses) }
|
||||
} else {
|
||||
GoogleDonationMessage()
|
||||
}
|
||||
}
|
||||
}
|
||||
-80
@@ -1,80 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.components
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
@Composable
|
||||
fun DonationHeroSection() {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.donation_thanks_intro),
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
textAlign = TextAlign.Start,
|
||||
)
|
||||
|
||||
Text(
|
||||
text = stringResource(R.string.donation_dev_message),
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
textAlign = TextAlign.Start,
|
||||
)
|
||||
|
||||
Text(
|
||||
text = stringResource(R.string.donation_closing),
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
textAlign = TextAlign.Start,
|
||||
)
|
||||
|
||||
Text(
|
||||
text = stringResource(R.string.donation_signoff),
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
fontWeight = FontWeight.Bold,
|
||||
textAlign = TextAlign.Start,
|
||||
)
|
||||
Text(
|
||||
text = stringResource(R.string.dev_name),
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
fontWeight = FontWeight.Bold,
|
||||
textAlign = TextAlign.Start,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
-97
@@ -1,97 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.components
|
||||
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.CurrencyBitcoin
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.res.vectorResource
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.LaunchButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
|
||||
@Composable
|
||||
fun DonationOptions(onAddressesClick: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
Icons.Outlined.CurrencyBitcoin,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.crypto), SelectionLabelType.TITLE)
|
||||
},
|
||||
trailing = { ForwardButton { onAddressesClick() } },
|
||||
onClick = onAddressesClick,
|
||||
),
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.github),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(
|
||||
stringResource(R.string.github_sponsors),
|
||||
SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton {
|
||||
context.openWebUrl(context.getString(R.string.github_sponsors_url))
|
||||
}
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.github_sponsors_url)) },
|
||||
),
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.liberapay),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.liberapay), SelectionLabelType.TITLE)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton { context.openWebUrl(context.getString(R.string.liberapay_url)) }
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.liberapay_url)) },
|
||||
),
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.kofi),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.kofi), SelectionLabelType.TITLE)
|
||||
},
|
||||
trailing = {
|
||||
LaunchButton { context.openWebUrl(context.getString(R.string.kofi_url)) }
|
||||
},
|
||||
onClick = { context.openWebUrl(context.getString(R.string.kofi_url)) },
|
||||
),
|
||||
)
|
||||
)
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.components
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
@Composable
|
||||
fun GoogleDonationMessage() {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.google_donation_message),
|
||||
style =
|
||||
MaterialTheme.typography.bodyMedium.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
textAlign = TextAlign.Start,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
-107
@@ -1,107 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.crypto
|
||||
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.StringRes
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
data class Address(
|
||||
@StringRes val name: Int,
|
||||
@StringRes val address: Int,
|
||||
@DrawableRes val icon: Int,
|
||||
) {
|
||||
companion object {
|
||||
val allAddresses =
|
||||
listOf(
|
||||
Address(
|
||||
name = R.string.bitcoin,
|
||||
address = R.string.bitcoin_address,
|
||||
icon = R.drawable.btc,
|
||||
),
|
||||
Address(
|
||||
name = R.string.monero,
|
||||
address = R.string.monero_address,
|
||||
icon = R.drawable.xmr,
|
||||
),
|
||||
Address(
|
||||
name = R.string.ethereum,
|
||||
address = R.string.ethereum_address,
|
||||
icon = R.drawable.eth,
|
||||
),
|
||||
Address(
|
||||
name = R.string.zcash,
|
||||
address = R.string.zcash_address,
|
||||
icon = R.drawable.zcash,
|
||||
),
|
||||
Address(
|
||||
name = R.string.litecoin_mweb,
|
||||
address = R.string.litecoin_mweb_address,
|
||||
icon = R.drawable.ltc,
|
||||
),
|
||||
Address(
|
||||
name = R.string.litecoin,
|
||||
address = R.string.litecoin_address,
|
||||
icon = R.drawable.ltc,
|
||||
),
|
||||
Address(
|
||||
name = R.string.polygon,
|
||||
address = R.string.polygon_address,
|
||||
icon = R.drawable.polygon,
|
||||
),
|
||||
Address(
|
||||
name = R.string.avalanche,
|
||||
address = R.string.avalanche_address,
|
||||
icon = R.drawable.avalanche,
|
||||
),
|
||||
Address(
|
||||
name = R.string.solana,
|
||||
address = R.string.solana_address,
|
||||
icon = R.drawable.solana,
|
||||
),
|
||||
Address(
|
||||
name = R.string.stellar,
|
||||
address = R.string.stellar_address,
|
||||
icon = R.drawable.stellar,
|
||||
),
|
||||
Address(
|
||||
name = R.string.tron,
|
||||
address = R.string.tron_address,
|
||||
icon = R.drawable.tron,
|
||||
),
|
||||
Address(
|
||||
name = R.string.bitcoin_cash,
|
||||
address = R.string.bitcoin_cash_address,
|
||||
icon = R.drawable.bitcoin_cash,
|
||||
),
|
||||
Address(
|
||||
name = R.string.ecash,
|
||||
address = R.string.ecash_address,
|
||||
icon = R.drawable.ecash,
|
||||
),
|
||||
Address(
|
||||
name = R.string.nano,
|
||||
address = R.string.nano_address,
|
||||
icon = R.drawable.nano,
|
||||
),
|
||||
Address(
|
||||
name = R.string.zano,
|
||||
address = R.string.zano_address,
|
||||
icon = R.drawable.zano,
|
||||
),
|
||||
Address(
|
||||
name = R.string.decred,
|
||||
address = R.string.decred_address,
|
||||
icon = R.drawable.decred,
|
||||
),
|
||||
Address(
|
||||
name = R.string.wownero,
|
||||
address = R.string.wownero_address,
|
||||
icon = R.drawable.wownero,
|
||||
),
|
||||
Address(
|
||||
name = R.string.doge,
|
||||
address = R.string.doge_address,
|
||||
icon = R.drawable.doge,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.crypto
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
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.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberClipboardHelper
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.crypto.components.addressItem
|
||||
|
||||
@Composable
|
||||
fun AddressesScreen() {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
|
||||
modifier =
|
||||
Modifier.fillMaxSize()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(vertical = 24.dp)
|
||||
.padding(horizontal = 12.dp),
|
||||
) {
|
||||
val clipboard = rememberClipboardHelper()
|
||||
SurfaceSelectionGroupButton(
|
||||
Address.allAddresses.map { addressItem(it) { address -> clipboard.copy(address) } }
|
||||
)
|
||||
}
|
||||
}
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.crypto.components
|
||||
|
||||
import androidx.compose.animation.animateContentSize
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.CopyAll
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.crypto.Address
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||
|
||||
@Composable
|
||||
fun addressItem(address: Address, onClick: (address: String) -> Unit): SelectionItem {
|
||||
val context = LocalContext.current
|
||||
val walletAddress = context.getString(address.address)
|
||||
var expand by rememberSaveable { mutableStateOf(false) }
|
||||
return SelectionItem(
|
||||
leading = {
|
||||
Image(
|
||||
painter = painterResource(id = address.icon),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
IconButton(onClick = { onClick(walletAddress) }) {
|
||||
Icon(Icons.Outlined.CopyAll, contentDescription = null)
|
||||
}
|
||||
},
|
||||
title = { SelectionItemLabel(stringResource(address.name), SelectionLabelType.TITLE) },
|
||||
description = {
|
||||
Text(
|
||||
text = walletAddress,
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
color = MaterialTheme.colorScheme.outline
|
||||
),
|
||||
maxLines = if (expand) Int.MAX_VALUE else 1,
|
||||
overflow = if (expand) TextOverflow.Clip else TextOverflow.Ellipsis,
|
||||
modifier = Modifier.animateContentSize(),
|
||||
)
|
||||
},
|
||||
onClick = { expand = !expand },
|
||||
)
|
||||
}
|
||||
+15
@@ -5,11 +5,16 @@ import android.content.Context
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.license.components.LicenseList
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
@@ -17,8 +22,18 @@ import kotlinx.serialization.json.Json
|
||||
@Composable
|
||||
fun LicenseScreen() {
|
||||
val context = LocalContext.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
var licenses by remember { mutableStateOf<List<LicenseFileEntry>>(emptyList()) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.licenses)) },
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) { licenses = loadLicenseeJson(context) }
|
||||
|
||||
if (licenses.isEmpty()) {
|
||||
|
||||
+73
-18
@@ -1,15 +1,17 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunnels
|
||||
|
||||
import android.os.Build
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.rounded.Sort
|
||||
import androidx.compose.material.icons.rounded.*
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -19,6 +21,7 @@ import com.journeyapps.barcodescanner.ScanOptions
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberClipboardHelper
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
||||
@@ -27,11 +30,10 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.components.ExportT
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.components.TunnelImportSheet
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.components.TunnelList
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.components.UrlImportDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.util.FileUtils
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelsViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
|
||||
@Composable
|
||||
fun TunnelsScreen(viewModel: TunnelsViewModel) {
|
||||
@@ -39,7 +41,6 @@ fun TunnelsScreen(viewModel: TunnelsViewModel) {
|
||||
val navController = LocalNavController.current
|
||||
val clipboard = rememberClipboardHelper()
|
||||
|
||||
val sharedState by sharedViewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
val tunnelsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
var showExportSheet by rememberSaveable { mutableStateOf(false) }
|
||||
@@ -47,16 +48,70 @@ fun TunnelsScreen(viewModel: TunnelsViewModel) {
|
||||
var showDeleteModal by rememberSaveable { mutableStateOf(false) }
|
||||
var showUrlDialog by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
when (sideEffect) {
|
||||
LocalSideEffect.Sheet.ImportTunnels -> showImportSheet = true
|
||||
LocalSideEffect.Modal.DeleteTunnels -> showDeleteModal = true
|
||||
LocalSideEffect.Sheet.ExportTunnels -> showExportSheet = true
|
||||
else -> Unit
|
||||
if (!tunnelsState.stateInitialized) return
|
||||
|
||||
@Composable
|
||||
fun TunnelActionBar() {
|
||||
val selectedCount by
|
||||
remember(tunnelsState.selectedTunnels) {
|
||||
derivedStateOf { tunnelsState.selectedTunnels.size }
|
||||
}
|
||||
val disableDelete by
|
||||
remember(tunnelsState.activeTunnels, tunnelsState.selectedTunnels) {
|
||||
derivedStateOf {
|
||||
tunnelsState.activeTunnels.any { active ->
|
||||
tunnelsState.selectedTunnels.any { it.id == active.key.id }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Row {
|
||||
if (selectedCount == 0) {
|
||||
val showSort by
|
||||
remember(tunnelsState.tunnels) {
|
||||
derivedStateOf { tunnelsState.tunnels.size > 1 }
|
||||
}
|
||||
if (showSort)
|
||||
ActionIconButton(Icons.AutoMirrored.Rounded.Sort, R.string.sort) {
|
||||
navController.navigate(Route.Sort)
|
||||
}
|
||||
ActionIconButton(Icons.Rounded.Add, R.string.add_tunnel) { showImportSheet = true }
|
||||
return@Row
|
||||
}
|
||||
ActionIconButton(Icons.Rounded.SelectAll, R.string.select_all) {
|
||||
viewModel.toggleSelectAllTunnels()
|
||||
}
|
||||
// due to permissions, and SAF issues on TV, not support less than Android 10 on
|
||||
// Android TV for file exports
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
ActionIconButton(Icons.Rounded.Download, R.string.download) {
|
||||
showExportSheet = true
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedCount == 1) {
|
||||
ActionIconButton(Icons.Rounded.CopyAll, R.string.copy) {
|
||||
viewModel.copySelectedTunnel()
|
||||
}
|
||||
}
|
||||
|
||||
if (!disableDelete) {
|
||||
ActionIconButton(Icons.Rounded.Delete, R.string.delete_tunnel) {
|
||||
showDeleteModal = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!tunnelsState.stateInitialized) return
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
topTitle = { Text(stringResource(R.string.tunnels)) },
|
||||
topTrailing = { TunnelActionBar() },
|
||||
showBottomItems = true,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val tunnelFileImportResultLauncher =
|
||||
rememberFileImportLauncherForResult(
|
||||
@@ -95,7 +150,7 @@ fun TunnelsScreen(viewModel: TunnelsViewModel) {
|
||||
InfoDialog(
|
||||
onDismiss = { showDeleteModal = false },
|
||||
onAttest = {
|
||||
sharedViewModel.deleteSelectedTunnels()
|
||||
viewModel.deleteSelectedTunnels()
|
||||
showDeleteModal = false
|
||||
},
|
||||
title = { Text(text = stringResource(R.string.delete_tunnel)) },
|
||||
@@ -106,10 +161,10 @@ fun TunnelsScreen(viewModel: TunnelsViewModel) {
|
||||
|
||||
if (showExportSheet) {
|
||||
ExportTunnelsBottomSheet({ type, uri ->
|
||||
sharedViewModel.exportSelectedTunnels(type, uri)
|
||||
viewModel.exportSelectedTunnels(type, uri, tunnelsState.selectedTunnels)
|
||||
}) {
|
||||
showExportSheet = false
|
||||
sharedViewModel.clearSelectedTunnels()
|
||||
viewModel.clearSelectedTunnels()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,8 +197,8 @@ fun TunnelsScreen(viewModel: TunnelsViewModel) {
|
||||
|
||||
TunnelList(
|
||||
tunnelsState,
|
||||
sharedState,
|
||||
modifier = Modifier.fillMaxSize().padding(vertical = 24.dp).padding(horizontal = 12.dp),
|
||||
viewModel,
|
||||
sharedViewModel,
|
||||
navController,
|
||||
)
|
||||
|
||||
+11
-4
@@ -6,23 +6,24 @@ 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.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.autotunnel.components.MobileDataTunnelItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.autotunnel.components.PingRestartItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.autotunnel.components.WifiTunnelItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.autotunnel.components.ethernetTunnelItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelsViewModel
|
||||
|
||||
@Composable
|
||||
fun TunnelAutoTunnelScreen(tunnelId: Int, viewModel: TunnelsViewModel) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val tunnelsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
val tunnelConf by
|
||||
@@ -30,6 +31,12 @@ fun TunnelAutoTunnelScreen(tunnelId: Int, viewModel: TunnelsViewModel) {
|
||||
derivedStateOf { tunnelsState.tunnels.find { it.id == tunnelId }!! }
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(showBottomItems = true, topTitle = { Text(tunnelConf.name) })
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
|
||||
|
||||
+30
-2
@@ -3,26 +3,35 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.components
|
||||
import android.net.Uri
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.FolderZip
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileExportLauncherForResult
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.sheet.CustomBottomSheet
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.sheet.SheetOption
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.AuthorizationPromptWrapper
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.FileUtils
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.hasSAFSupport
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ExportTunnelsBottomSheet(
|
||||
onExport: (configType: ConfigType, uri: Uri?) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val isTv = LocalIsAndroidTV.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
|
||||
var exportConfigType by remember { mutableStateOf(ConfigType.WG) }
|
||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||
var isAuthorized by remember { mutableStateOf(false) }
|
||||
var shouldExport by remember { mutableStateOf(false) }
|
||||
|
||||
val selectedTunnelsExportLauncher =
|
||||
@@ -50,6 +59,17 @@ fun ExportTunnelsBottomSheet(
|
||||
}
|
||||
}
|
||||
|
||||
if (showAuthPrompt) {
|
||||
AuthorizationPromptWrapper(
|
||||
onDismiss = { showAuthPrompt = false },
|
||||
onSuccess = {
|
||||
showAuthPrompt = false
|
||||
isAuthorized = true
|
||||
shouldExport = true
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
CustomBottomSheet(
|
||||
listOf(
|
||||
SheetOption(
|
||||
@@ -57,7 +77,11 @@ fun ExportTunnelsBottomSheet(
|
||||
stringResource(R.string.export_tunnels_amnezia),
|
||||
onClick = {
|
||||
exportConfigType = ConfigType.AM
|
||||
shouldExport = true
|
||||
if (!isAuthorized && !isTv) {
|
||||
showAuthPrompt = true
|
||||
} else {
|
||||
shouldExport = true
|
||||
}
|
||||
},
|
||||
),
|
||||
SheetOption(
|
||||
@@ -65,7 +89,11 @@ fun ExportTunnelsBottomSheet(
|
||||
stringResource(R.string.export_tunnels_wireguard),
|
||||
onClick = {
|
||||
exportConfigType = ConfigType.WG
|
||||
shouldExport = true
|
||||
if (!isAuthorized && !isTv) {
|
||||
showAuthPrompt = true
|
||||
} else {
|
||||
shouldExport = true
|
||||
}
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
+12
-13
@@ -18,20 +18,21 @@ import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.getValueById
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.SharedAppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.TunnelsUiState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SharedAppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelsViewModel
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun TunnelList(
|
||||
tunnelsState: TunnelsUiState,
|
||||
sharedState: SharedAppUiState,
|
||||
modifier: Modifier = Modifier,
|
||||
viewModel: TunnelsViewModel,
|
||||
sharedViewModel: SharedAppViewModel,
|
||||
navController: NavController,
|
||||
) {
|
||||
@@ -47,7 +48,7 @@ fun TunnelList(
|
||||
modifier
|
||||
.pointerInput(Unit) {
|
||||
if (tunnelsState.tunnels.isEmpty()) return@pointerInput
|
||||
sharedViewModel.clearSelectedTunnels()
|
||||
viewModel.clearSelectedTunnels()
|
||||
}
|
||||
.overscroll(rememberOverscrollEffect()),
|
||||
state = lazyListState,
|
||||
@@ -61,11 +62,11 @@ fun TunnelList(
|
||||
items(tunnelsState.tunnels, key = { it.id }) { tunnel ->
|
||||
val tunnelState =
|
||||
remember(tunnelsState.activeTunnels) {
|
||||
tunnelsState.activeTunnels[tunnel.id] ?: TunnelState()
|
||||
tunnelsState.activeTunnels.getValueById(tunnel.id) ?: TunnelState()
|
||||
}
|
||||
val selected =
|
||||
remember(sharedState.selectedTunnels) {
|
||||
sharedState.selectedTunnels.any { it.id == tunnel.id }
|
||||
remember(tunnelsState.selectedTunnels) {
|
||||
tunnelsState.selectedTunnels.any { it.id == tunnel.id }
|
||||
}
|
||||
TunnelRowItem(
|
||||
state = tunnelState,
|
||||
@@ -73,9 +74,7 @@ fun TunnelList(
|
||||
tunnel = tunnel,
|
||||
tunnelState = tunnelState,
|
||||
onTvClick = { navController.navigate(Route.TunnelOptions(tunnel.id)) },
|
||||
onToggleSelectedTunnel = { tunnel ->
|
||||
sharedViewModel.toggleSelectedTunnel(tunnel.id)
|
||||
},
|
||||
onToggleSelectedTunnel = { tunnel -> viewModel.toggleSelectedTunnel(tunnel.id) },
|
||||
onSwitchClick = { checked ->
|
||||
if (checked) sharedViewModel.startTunnel(tunnel)
|
||||
else sharedViewModel.stopTunnel(tunnel)
|
||||
@@ -87,14 +86,14 @@ fun TunnelList(
|
||||
if (!isTv)
|
||||
Modifier.combinedClickable(
|
||||
onClick = {
|
||||
if (sharedState.selectedTunnels.isNotEmpty()) {
|
||||
sharedViewModel.toggleSelectedTunnel(tunnel.id)
|
||||
if (tunnelsState.selectedTunnels.isNotEmpty()) {
|
||||
viewModel.toggleSelectedTunnel(tunnel.id)
|
||||
} else {
|
||||
navController.navigate(Route.TunnelOptions(tunnel.id))
|
||||
sharedViewModel.clearSelectedTunnels()
|
||||
viewModel.clearSelectedTunnels()
|
||||
}
|
||||
},
|
||||
onLongClick = { sharedViewModel.toggleSelectedTunnel(tunnel.id) },
|
||||
onLongClick = { viewModel.toggleSelectedTunnel(tunnel.id) },
|
||||
interactionSource = remember { MutableInteractionSource() },
|
||||
indication = null,
|
||||
)
|
||||
|
||||
+8
-3
@@ -9,9 +9,11 @@ import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.minimumInteractiveComponentSize
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
@@ -39,7 +41,10 @@ fun TunnelRowItem(
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
var leadingIconColor by remember(state) { mutableStateOf(state.health().asColor()) }
|
||||
val leadingIconColor =
|
||||
remember(state) {
|
||||
if (state.status.isUp()) tunnelState.statistics.asColor() else Color.Gray
|
||||
}
|
||||
|
||||
val (leadingIcon, size, typeDescription) =
|
||||
remember(tunnel) {
|
||||
@@ -50,7 +55,7 @@ fun TunnelRowItem(
|
||||
Triple(
|
||||
Icons.Rounded.Smartphone,
|
||||
16.dp,
|
||||
context.getString(R.string.mobile_tunnel),
|
||||
context.getString(R.string.mobile_data_tunnel),
|
||||
)
|
||||
tunnel.isEthernetTunnel ->
|
||||
Triple(
|
||||
|
||||
+46
-8
@@ -6,26 +6,37 @@ 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.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Save
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SecureScreenFromRecording
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.AuthorizationPromptWrapper
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.config.components.AddPeerButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.config.components.InterfaceSection
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.config.components.PeersSection
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.ConfigProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelsViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
|
||||
@Composable
|
||||
fun ConfigScreen(tunnelId: Int? = null, viewModel: TunnelsViewModel = hiltViewModel()) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val isTv = LocalIsAndroidTV.current
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
|
||||
val tunnelsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
@@ -38,22 +49,45 @@ fun ConfigScreen(tunnelId: Int? = null, viewModel: TunnelsViewModel = hiltViewMo
|
||||
mutableStateOf(tunnelConf?.let { ConfigProxy.from(it.toAmConfig()) } ?: ConfigProxy())
|
||||
}
|
||||
|
||||
var tunnelName by remember { mutableStateOf(tunnelConf?.tunName ?: "") }
|
||||
var tunnelName by remember { mutableStateOf(tunnelConf?.name ?: "") }
|
||||
|
||||
LaunchedEffect(key1 = Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(tunnelConf?.name ?: stringResource(R.string.new_tunnel)) },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
keyboardController?.hide()
|
||||
viewModel.saveConfigProxy(tunnelId, configProxy, tunnelName)
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val isTunnelNameTaken by
|
||||
remember(tunnelName, tunnelsState.tunnels) {
|
||||
derivedStateOf {
|
||||
tunnelsState.tunnels.any { it.tunName == tunnelName && it.id != tunnelConf?.id }
|
||||
tunnelsState.tunnels.any { it.name == tunnelName && it.id != tunnelConf?.id }
|
||||
}
|
||||
}
|
||||
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
if (sideEffect is LocalSideEffect.SaveChanges)
|
||||
viewModel.saveConfigProxy(tunnelId, configProxy, tunnelName)
|
||||
}
|
||||
var showAuthPrompt by rememberSaveable { mutableStateOf(false) }
|
||||
var isAuthorized by rememberSaveable { mutableStateOf(isTv) }
|
||||
|
||||
SecureScreenFromRecording()
|
||||
|
||||
if (showAuthPrompt) {
|
||||
AuthorizationPromptWrapper(
|
||||
onDismiss = { showAuthPrompt = false },
|
||||
onSuccess = {
|
||||
showAuthPrompt = false
|
||||
isAuthorized = true
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||
@@ -67,6 +101,8 @@ fun ConfigScreen(tunnelId: Int? = null, viewModel: TunnelsViewModel = hiltViewMo
|
||||
configProxy = configProxy,
|
||||
tunnelName,
|
||||
isTunnelNameTaken,
|
||||
isAuthorized,
|
||||
toggleAuthPrompt = { showAuthPrompt = !showAuthPrompt },
|
||||
onInterfaceChange = { configProxy = configProxy.copy(`interface` = it) },
|
||||
onTunnelNameChange = { tunnelName = it },
|
||||
onMimicQuic = {
|
||||
@@ -81,6 +117,7 @@ fun ConfigScreen(tunnelId: Int? = null, viewModel: TunnelsViewModel = hiltViewMo
|
||||
)
|
||||
PeersSection(
|
||||
configProxy,
|
||||
isAuthorized,
|
||||
onRemove = {
|
||||
configProxy =
|
||||
configProxy.copy(
|
||||
@@ -106,6 +143,7 @@ fun ConfigScreen(tunnelId: Int? = null, viewModel: TunnelsViewModel = hiltViewMo
|
||||
peers = configProxy.peers.toMutableList().apply { set(index, peer) }
|
||||
)
|
||||
},
|
||||
showAuth = { showAuthPrompt = true },
|
||||
)
|
||||
AddPeerButton() { configProxy = configProxy.copy(peers = configProxy.peers + PeerProxy()) }
|
||||
}
|
||||
|
||||
+33
-36
@@ -1,15 +1,19 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.config.components
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.RemoveRedEye
|
||||
import androidx.compose.material.icons.rounded.ContentCopy
|
||||
import androidx.compose.material.icons.rounded.Refresh
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@@ -25,10 +29,11 @@ import com.zaneschepke.wireguardautotunnel.ui.common.textbox.ConfigurationTextBo
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
||||
import java.util.*
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun InterfaceFields(
|
||||
interfaceState: InterfaceProxy,
|
||||
showAuthPrompt: () -> Unit,
|
||||
isAuthorized: Boolean,
|
||||
showScripts: Boolean,
|
||||
showAmneziaValues: Boolean,
|
||||
onInterfaceChange: (InterfaceProxy) -> Unit,
|
||||
@@ -38,7 +43,6 @@ fun InterfaceFields(
|
||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||
val locale = Locale.getDefault()
|
||||
var showPrivateKey by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
ConfigurationTextBox(
|
||||
@@ -48,40 +52,33 @@ fun InterfaceFields(
|
||||
.lowercase(Locale.getDefault()),
|
||||
onValueChange = { onInterfaceChange(interfaceState.copy(privateKey = it)) },
|
||||
label = stringResource(R.string.private_key),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
modifier = Modifier.fillMaxWidth().clickable { if (!isAuthorized) showAuthPrompt() },
|
||||
visualTransformation =
|
||||
if (showPrivateKey) VisualTransformation.None else PasswordVisualTransformation(),
|
||||
if (isAuthorized) VisualTransformation.None else PasswordVisualTransformation(),
|
||||
trailing = {
|
||||
CompositionLocalProvider(LocalMinimumInteractiveComponentSize provides 4.dp) {
|
||||
Row(modifier = Modifier.padding(end = 4.dp)) {
|
||||
IconButton(onClick = { showPrivateKey = !showPrivateKey }) {
|
||||
Icon(
|
||||
Icons.Outlined.RemoveRedEye,
|
||||
stringResource(R.string.show_password),
|
||||
IconButton(
|
||||
enabled = true,
|
||||
onClick = {
|
||||
if (!isAuthorized) return@IconButton showAuthPrompt()
|
||||
val keypair = KeyPair()
|
||||
onInterfaceChange(
|
||||
interfaceState.copy(
|
||||
privateKey = keypair.privateKey.toBase64(),
|
||||
publicKey = keypair.publicKey.toBase64(),
|
||||
)
|
||||
}
|
||||
IconButton(
|
||||
enabled = true,
|
||||
onClick = {
|
||||
val keypair = KeyPair()
|
||||
onInterfaceChange(
|
||||
interfaceState.copy(
|
||||
privateKey = keypair.privateKey.toBase64(),
|
||||
publicKey = keypair.publicKey.toBase64(),
|
||||
)
|
||||
)
|
||||
},
|
||||
) {
|
||||
Icon(
|
||||
Icons.Rounded.Refresh,
|
||||
stringResource(R.string.rotate_keys),
|
||||
tint = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
},
|
||||
) {
|
||||
Icon(
|
||||
Icons.Rounded.Refresh,
|
||||
stringResource(R.string.rotate_keys),
|
||||
tint =
|
||||
if (isAuthorized) MaterialTheme.colorScheme.onSurface
|
||||
else MaterialTheme.colorScheme.outline,
|
||||
)
|
||||
}
|
||||
},
|
||||
enabled = true,
|
||||
enabled = isAuthorized,
|
||||
singleLine = true,
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
|
||||
+4
@@ -23,6 +23,8 @@ fun InterfaceSection(
|
||||
configProxy: ConfigProxy,
|
||||
tunnelName: String,
|
||||
isTunnelNameTaken: Boolean,
|
||||
isAuthorized: Boolean,
|
||||
toggleAuthPrompt: () -> Unit,
|
||||
onInterfaceChange: (InterfaceProxy) -> Unit,
|
||||
onTunnelNameChange: (String) -> Unit,
|
||||
onMimicQuic: () -> Unit,
|
||||
@@ -94,9 +96,11 @@ fun InterfaceSection(
|
||||
)
|
||||
InterfaceFields(
|
||||
interfaceState = configProxy.`interface`,
|
||||
showAuthPrompt = toggleAuthPrompt,
|
||||
showScripts = showScripts,
|
||||
showAmneziaValues = showAmneziaValues,
|
||||
onInterfaceChange = onInterfaceChange,
|
||||
isAuthorized = isAuthorized,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+10
-18
@@ -1,20 +1,13 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.config.components
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.RemoveRedEye
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@@ -28,11 +21,15 @@ import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
||||
import java.util.*
|
||||
|
||||
@Composable
|
||||
fun PeerFields(peer: PeerProxy, onPeerChange: (PeerProxy) -> Unit) {
|
||||
fun PeerFields(
|
||||
peer: PeerProxy,
|
||||
onPeerChange: (PeerProxy) -> Unit,
|
||||
showAuthPrompt: () -> Unit,
|
||||
isAuthenticated: Boolean,
|
||||
) {
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||
var showPresharedKey by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
ConfigurationTextBox(
|
||||
value = peer.publicKey,
|
||||
@@ -45,21 +42,16 @@ fun PeerFields(peer: PeerProxy, onPeerChange: (PeerProxy) -> Unit) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
visualTransformation =
|
||||
if (showPresharedKey) VisualTransformation.None else PasswordVisualTransformation(),
|
||||
if (isAuthenticated) VisualTransformation.None else PasswordVisualTransformation(),
|
||||
value = peer.preSharedKey,
|
||||
enabled = true,
|
||||
enabled = isAuthenticated,
|
||||
hint = stringResource(R.string.optional),
|
||||
onValueChange = { onPeerChange(peer.copy(preSharedKey = it)) },
|
||||
label = stringResource(R.string.preshared_key),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
modifier = Modifier.fillMaxWidth().clickable { if (!isAuthenticated) showAuthPrompt() },
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
singleLine = true,
|
||||
trailing = {
|
||||
IconButton(onClick = { showPresharedKey = !showPresharedKey }) {
|
||||
Icon(Icons.Outlined.RemoveRedEye, stringResource(R.string.show_password))
|
||||
}
|
||||
},
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = peer.persistentKeepalive,
|
||||
|
||||
+8
-1
@@ -23,9 +23,11 @@ import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||
@Composable
|
||||
fun PeersSection(
|
||||
configProxy: ConfigProxy,
|
||||
isAuthorized: Boolean,
|
||||
onRemove: (index: Int) -> Unit,
|
||||
onToggleLan: (index: Int) -> Unit,
|
||||
onUpdatePeer: (PeerProxy, index: Int) -> Unit,
|
||||
showAuth: () -> Unit,
|
||||
) {
|
||||
configProxy.peers.forEachIndexed { index, peer ->
|
||||
var isDropDownExpanded by remember { mutableStateOf(false) }
|
||||
@@ -88,7 +90,12 @@ fun PeersSection(
|
||||
}
|
||||
}
|
||||
}
|
||||
PeerFields(peer = peer, onPeerChange = { onUpdatePeer(it, index) })
|
||||
PeerFields(
|
||||
peer = peer,
|
||||
onPeerChange = { onUpdatePeer(it, index) },
|
||||
showAuthPrompt = showAuth,
|
||||
isAuthenticated = isAuthorized,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+34
-27
@@ -11,13 +11,13 @@ import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.ArrowDownward
|
||||
import androidx.compose.material.icons.filled.ArrowUpward
|
||||
import androidx.compose.material.icons.filled.DragHandle
|
||||
import androidx.compose.material.icons.rounded.Save
|
||||
import androidx.compose.material.icons.rounded.SortByAlpha
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||
@@ -30,10 +30,10 @@ import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isSortedBy
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelsViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
import sh.calvin.reorderable.DragGestureDetector
|
||||
import sh.calvin.reorderable.ReorderableItem
|
||||
import sh.calvin.reorderable.rememberReorderableLazyListState
|
||||
@@ -48,27 +48,34 @@ fun SortScreen(viewModel: TunnelsViewModel) {
|
||||
var sortAscending by rememberSaveable { mutableStateOf<Boolean?>(null) }
|
||||
var editableTunnels by rememberSaveable { mutableStateOf(tunnelsState.tunnels) }
|
||||
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
when (sideEffect) {
|
||||
LocalSideEffect.SaveChanges -> {
|
||||
viewModel.saveSortChanges(editableTunnels)
|
||||
}
|
||||
LocalSideEffect.Sort -> {
|
||||
sortAscending =
|
||||
when (sortAscending) {
|
||||
null -> !editableTunnels.isSortedBy { it.tunName }
|
||||
true -> false
|
||||
false -> null
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(stringResource(R.string.sort)) },
|
||||
topTrailing = {
|
||||
Row {
|
||||
ActionIconButton(Icons.Rounded.SortByAlpha, R.string.sort) {
|
||||
sortAscending =
|
||||
when (sortAscending) {
|
||||
null -> !editableTunnels.isSortedBy { it.name }
|
||||
true -> false
|
||||
false -> null
|
||||
}
|
||||
editableTunnels =
|
||||
when (sortAscending) {
|
||||
true -> editableTunnels.sortedBy { it.name }
|
||||
false -> editableTunnels.sortedByDescending { it.name }
|
||||
null -> tunnelsState.tunnels
|
||||
}
|
||||
}
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
viewModel.saveSortChanges(editableTunnels)
|
||||
}
|
||||
}
|
||||
editableTunnels =
|
||||
when (sortAscending) {
|
||||
true -> editableTunnels.sortedBy { it.tunName }
|
||||
false -> editableTunnels.sortedByDescending { it.tunName }
|
||||
null -> tunnelsState.tunnels
|
||||
}
|
||||
}
|
||||
else -> Unit
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val lazyListState = rememberLazyListState()
|
||||
@@ -99,7 +106,7 @@ fun SortScreen(viewModel: TunnelsViewModel) {
|
||||
ReorderableItem(reorderableLazyListState, tunnel.id) { isDragging ->
|
||||
ExpandingRowListItem(
|
||||
leading = {},
|
||||
text = tunnel.tunName,
|
||||
text = tunnel.name,
|
||||
trailing = {
|
||||
if (!isTv)
|
||||
Icon(
|
||||
|
||||
+19
-6
@@ -3,24 +3,28 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.splittunnel
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Save
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.splittunnel.components.SplitTunnelContent
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.splittunnel.state.SplitOption
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SplitTunnelViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
|
||||
@Composable
|
||||
fun SplitTunnelScreen(tunnelId: Int, viewModel: SplitTunnelViewModel = hiltViewModel()) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val splitTunnelState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
|
||||
if (!splitTunnelState.stateInitialized) {
|
||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
@@ -47,9 +51,18 @@ fun SplitTunnelScreen(tunnelId: Int, viewModel: SplitTunnelViewModel = hiltViewM
|
||||
)
|
||||
}
|
||||
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
if (sideEffect is LocalSideEffect.SaveChanges)
|
||||
viewModel.saveSplitTunnelSelection(tunnelId, splitConfig)
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
topTitle = { Text(tunnelConf.name) },
|
||||
topTrailing = {
|
||||
ActionIconButton(Icons.Rounded.Save, R.string.save) {
|
||||
viewModel.saveSplitTunnelSelection(tunnelId, splitConfig)
|
||||
}
|
||||
},
|
||||
showBottomItems = true,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
SplitTunnelContent(
|
||||
|
||||
+57
-35
@@ -1,29 +1,34 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.tunneloptions
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Edit
|
||||
import androidx.compose.material.icons.rounded.QrCode2
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route.Config
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.AuthorizationPromptWrapper
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.tunneloptions.components.*
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelsViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
|
||||
@Composable
|
||||
fun TunnelOptionsScreen(tunnelId: Int, viewModel: TunnelsViewModel) {
|
||||
val isTv = LocalIsAndroidTV.current
|
||||
val navController = LocalNavController.current
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
|
||||
@@ -34,17 +39,50 @@ fun TunnelOptionsScreen(tunnelId: Int, viewModel: TunnelsViewModel) {
|
||||
derivedStateOf { tunnelsState.tunnels.find { it.id == tunnelId }!! }
|
||||
}
|
||||
|
||||
val ipv6Preferred by
|
||||
remember(tunnelConf.isIpv4Preferred) { mutableStateOf(!tunnelConf.isIpv4Preferred) }
|
||||
|
||||
var showAuthPrompt by rememberSaveable { mutableStateOf(!isTv) }
|
||||
var isAuthorized by rememberSaveable { mutableStateOf(isTv) }
|
||||
var showQrModal by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
if (sideEffect is LocalSideEffect.Modal.QR) showQrModal = true
|
||||
LaunchedEffect(Unit) {
|
||||
sharedViewModel.updateNavbarState(
|
||||
NavbarState(
|
||||
showBottomItems = true,
|
||||
topTitle = { Text(tunnelConf.name) },
|
||||
topTrailing = {
|
||||
Row {
|
||||
ActionIconButton(
|
||||
Icons.Rounded.QrCode2,
|
||||
com.zaneschepke.wireguardautotunnel.R.string.show_qr,
|
||||
) {
|
||||
showQrModal = true
|
||||
}
|
||||
ActionIconButton(Icons.Rounded.Edit, R.string.edit_tunnel) {
|
||||
navController.navigate(Config(tunnelId))
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
if (showQrModal) {
|
||||
QrCodeDialog(tunnelConf = tunnelConf, onDismiss = { showQrModal = false })
|
||||
|
||||
// Show authorization prompt if needed
|
||||
if (showAuthPrompt) {
|
||||
AuthorizationPromptWrapper(
|
||||
onDismiss = {
|
||||
showAuthPrompt = false
|
||||
showQrModal = false
|
||||
},
|
||||
onSuccess = {
|
||||
showAuthPrompt = false
|
||||
isAuthorized = true
|
||||
},
|
||||
)
|
||||
}
|
||||
if (isAuthorized) {
|
||||
QrCodeDialog(tunnelConf = tunnelConf, onDismiss = { showQrModal = false })
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
@@ -58,28 +96,12 @@ fun TunnelOptionsScreen(tunnelId: Int, viewModel: TunnelsViewModel) {
|
||||
) {
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
buildList {
|
||||
add(primaryTunnelItem(tunnelConf) { viewModel.togglePrimaryTunnel(tunnelId) })
|
||||
add(autoTunnelingItem(tunnelConf, navController))
|
||||
add(splitTunnelingItem(tunnelConf, navController))
|
||||
}
|
||||
)
|
||||
SectionDivider()
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
buildList {
|
||||
add(
|
||||
dynamicDnsItem(tunnelConf.restartOnPingFailure) {
|
||||
viewModel.setRestartOnPing(tunnelId, it)
|
||||
}
|
||||
)
|
||||
if (tunnelsState.appMode != AppMode.KERNEL)
|
||||
add(
|
||||
preferIpv6Item(ipv6Preferred) {
|
||||
viewModel.toggleIpv4Preferred(tunnelId)
|
||||
}
|
||||
)
|
||||
}
|
||||
listOf(
|
||||
PrimaryTunnelItem(tunnelConf) { viewModel.togglePrimaryTunnel(tunnelId) },
|
||||
AutoTunnelingItem(tunnelConf, navController),
|
||||
serverIpv4Item(tunnelConf) { viewModel.toggleIpv4Preferred(tunnelId) },
|
||||
SplitTunnelingItem(tunnelConf, navController),
|
||||
)
|
||||
)
|
||||
if (tunnelsState.isPingEnabled) {
|
||||
SectionDivider()
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionIte
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun autoTunnelingItem(tunnelConf: TunnelConf, navController: NavController): SelectionItem {
|
||||
fun AutoTunnelingItem(tunnelConf: TunnelConf, navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Bolt, contentDescription = null) },
|
||||
title = {
|
||||
+1
-1
@@ -13,7 +13,7 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||
|
||||
@Composable
|
||||
fun primaryTunnelItem(tunnelConf: TunnelConf, onClick: () -> Unit): SelectionItem {
|
||||
fun PrimaryTunnelItem(tunnelConf: TunnelConf, onClick: () -> Unit): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Star, contentDescription = null) },
|
||||
title = {
|
||||
+1
-1
@@ -51,7 +51,7 @@ private fun QrCodeAlertDialog(tunnelConf: TunnelConf, onDismiss: () -> Unit) {
|
||||
},
|
||||
title = {
|
||||
Text(
|
||||
text = tunnelConf.tunName,
|
||||
text = tunnelConf.name,
|
||||
color = Color.Black,
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
)
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionIte
|
||||
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
|
||||
|
||||
@Composable
|
||||
fun splitTunnelingItem(tunnelConf: TunnelConf, navController: NavController): SelectionItem {
|
||||
fun SplitTunnelingItem(tunnelConf: TunnelConf, navController: NavController): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.AutoMirrored.Outlined.CallSplit, contentDescription = null) },
|
||||
title = {
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user