mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c220b57a8 | |||
| 439dbf48a0 | |||
| 6e6f405535 | |||
| 3d8254f738 | |||
| 255877db3b | |||
| 2b8131da41 | |||
| c8699f5610 | |||
| fe21c0eda3 | |||
| 144421987a | |||
| 4b4a8cc273 | |||
| fe9315b64a | |||
| 280c187c5b | |||
| 41cfa8fcec | |||
| 274e6aec0f | |||
| 1127db1c56 | |||
| 5762a023a9 | |||
| 5b0cda2859 | |||
| 706b2e8d90 | |||
| 62b662950a | |||
| f90765ff38 | |||
| 39fe7691e8 | |||
| 2c6946cc76 | |||
| 512d765c55 | |||
| e5888a628d | |||
| 7a3fb037ee | |||
| 5356246eea | |||
| ee808e0b4d | |||
| 5749f06229 | |||
| 207c1a4d1a | |||
| 050efe2fb3 | |||
| 5bd497c8bb | |||
| 82b39695de | |||
| 67eacc576c | |||
| c9c4bbf3bf | |||
| 297e8c1f93 | |||
| 5f8bc7b4f6 | |||
| 37845f2e77 | |||
| 4cb65c5d30 | |||
| ee1fcc6b24 | |||
| 875eae29e7 | |||
| 0ea84b3e31 | |||
| 7a0af2462b | |||
| df7154d0c1 | |||
| fa4cc84c0e |
@@ -72,11 +72,11 @@ jobs:
|
||||
outputs:
|
||||
UPLOAD_DIR_ANDROID: ${{ env.UPLOAD_DIR_ANDROID }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: '17'
|
||||
|
||||
@@ -16,7 +16,7 @@ jobs:
|
||||
has_new_commits: ${{ steps.check.outputs.new_commits }}
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
- name: Check for new commits
|
||||
id: check
|
||||
env:
|
||||
@@ -41,7 +41,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
|
||||
- name: Install system dependencies
|
||||
run: |
|
||||
|
||||
@@ -10,9 +10,9 @@ jobs:
|
||||
format_check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: '17'
|
||||
|
||||
@@ -72,7 +72,7 @@ jobs:
|
||||
name: publish-github
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
with:
|
||||
ref: ${{ github.event_name == 'push' && github.ref || 'main' }}
|
||||
- name: Install system dependencies
|
||||
@@ -189,9 +189,9 @@ jobs:
|
||||
KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: '17'
|
||||
|
||||
@@ -4,7 +4,7 @@ WG Tunnel
|
||||
|
||||
<div align="center">
|
||||
|
||||
An alternative Android client app for [WireGuard](https://www.wireguard.com/)
|
||||
An alternative FOSS Android client for [WireGuard](https://www.wireguard.com/)
|
||||
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
||||
<br />
|
||||
<br />
|
||||
@@ -37,11 +37,11 @@ and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
||||
<summary>Table of Contents</summary>
|
||||
|
||||
- [About](#about)
|
||||
- [Acknowledgements](#acknowledgements)
|
||||
- [Screenshots](#screenshots)
|
||||
- [Features](#features)
|
||||
- [Building](#building)
|
||||
- [Translation](#translation)
|
||||
- [Acknowledgements](#acknowledgements)
|
||||
- [Contributing](#contributing)
|
||||
|
||||
</details>
|
||||
@@ -49,22 +49,13 @@ and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
||||
<div style="text-align: left;">
|
||||
|
||||
## About
|
||||
Inspired by the official [wireguard-android](https://github.com/WireGuard/wireguard-android) app, WG Tunnel was created to address features and support missing from the official app. This app combines support for both [WireGuard](https://www.wireguard.com/)
|
||||
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/), with its primary feature of auto-tunneling (on-demand tunneling).
|
||||
|
||||
WG Tunnel is an alternative Android client for WireGuard and AmneziaWG, inspired by the official WireGuard Android app. It fills gaps in the official client by adding advanced features like auto-tunneling (on-demand VPN activation), while seamlessly supporting both protocols across app modes—including Kernel (for direct WireGuard kernel integration; AmneziaWG not supported), VPN (standard system-level tunneling), Lockdown (a custom kill switch for leak prevention), and Proxy (built-in HTTP/SOCKS5 forwarding)—for enhanced privacy, censorship resistance, and flexibility.
|
||||
|
||||
</div>
|
||||
|
||||
<div style="text-align: left;">
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
Thank you to the following:
|
||||
|
||||
- All of the users that have helped contribute to the project with ideas, translations, feedback, bug reports, testing, and donations.
|
||||
- [WireGuard](https://www.wireguard.com/) - Jason A. Donenfeld (https://github.com/WireGuard/wireguard-android)
|
||||
|
||||
- [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) - Amnezia Team (https://github.com/amnezia-vpn/amneziawg-android)
|
||||
|
||||
## Screenshots
|
||||
|
||||
</div>
|
||||
@@ -79,26 +70,26 @@ Thank you to the following:
|
||||
|
||||
## Features
|
||||
|
||||
* Add tunnels via .conf file, zip, manual entry, clipboard, or QR code
|
||||
* Auto-tunnel based on Wi-Fi SSID, ethernet, or mobile data
|
||||
* Split tunneling by application with search
|
||||
* Support for kernel and userspace modes
|
||||
* Amnezia support for userspace mode for DPI/censorship protection
|
||||
* Pre/Post Up/Down scripts support for all modes on a rooted device
|
||||
* Always-On VPN support
|
||||
* Export tunnels to zip
|
||||
* Quick tile support for tunnel toggling, auto-tunneling
|
||||
* Shortcuts support for tunnel toggling, auto-tunneling
|
||||
* Intent automation support for all tunnels
|
||||
* In app VPN kill switch with LAN bypass
|
||||
* Automatic auto-tunneling service and/or tunnel restart after reboot or app update
|
||||
* Battery preservation measures
|
||||
* Restart tunnel on ping failure
|
||||
- **Tunnel Import Methods**: Easily add tunnels using .conf files, ZIP archives, manual entry, or QR code scanning.
|
||||
- **Auto-Tunneling**: Automatically activate tunnels based on Wi-Fi SSID, Ethernet connections, or mobile data networks.
|
||||
- **Split Tunneling**: Flexible support for routing specific apps or traffic through the VPN.
|
||||
- **WireGuard Modes**: Full compatibility with WireGuard in both kernel and userspace implementations.
|
||||
- **AmneziaWG Integration**: Userspace mode for AmneziaWG, providing robust censorship evasion.
|
||||
- **Always-On VPN**: Ensures continuous protection with Android's Always-On VPN feature.
|
||||
- **Quick Controls**: Quick Settings tile and home screen shortcuts for easy VPN toggling.
|
||||
- **Automation Support**: Intent-based automation for controlling tunnels.
|
||||
- **Auto-Restore**: Seamlessly restores auto-tunneling and active tunnels after device restarts or app updates.
|
||||
- **Proxying Options**: Built-in HTTP and SOCKS5 proxy support within tunnels.
|
||||
- **Lockdown Mode**: Custom kill switch for maximum leak prevention and security.
|
||||
- **Dynamic DNS Handling**: Detects and updates DNS changes without tunnel restarts.
|
||||
- **Monitoring Tools**: Advanced tunnel monitoring features for tunnel performance monitoring.
|
||||
- **Android TV Support**: Android TV support for secure streaming and browsing.
|
||||
- **Advanced DNS**: DNS over HTTPS support for tunnel endpoint resolutions.
|
||||
|
||||
## Building
|
||||
|
||||
```sh
|
||||
git clone https://github.com/zaneschepke/wgtunnel
|
||||
git clone https://github.com/wgtunnel/wgtunnel
|
||||
cd wgtunnel
|
||||
```
|
||||
|
||||
@@ -114,6 +105,15 @@ Help translate WG Tunnel into your language
|
||||
at [Hosted Weblate](https://hosted.weblate.org/engage/wg-tunnel/).\
|
||||
[](https://hosted.weblate.org/engage/wg-tunnel/)
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
Thank you to the following:
|
||||
|
||||
- All of the users that have helped contribute to the project with ideas, translations, feedback, bug reports, testing, and donations.
|
||||
- [WireGuard](https://www.wireguard.com/) - Jason A. Donenfeld (https://github.com/WireGuard/wireguard-android)
|
||||
- [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) - Amnezia Team (https://github.com/amnezia-vpn/amneziawg-android)
|
||||
- [JetBrains](https://jetbrains.com) - For supporting open-source developers with free software licenses.
|
||||
|
||||
## Contributing
|
||||
|
||||
Any contributions in the form of feedback, issues, code, or translations are welcome and much
|
||||
|
||||
@@ -9,7 +9,6 @@ plugins {
|
||||
alias(libs.plugins.compose.compiler)
|
||||
alias(libs.plugins.grgit)
|
||||
alias(libs.plugins.licensee)
|
||||
id("kotlin-parcelize")
|
||||
}
|
||||
|
||||
android {
|
||||
@@ -25,6 +24,9 @@ android {
|
||||
|
||||
ksp { arg("room.schemaLocation", "$projectDir/schemas") }
|
||||
|
||||
// fix okhttp proguard issue
|
||||
packaging { resources { pickFirsts.add("okhttp3/internal/publicsuffix/publicsuffixes.gz") } }
|
||||
|
||||
defaultConfig {
|
||||
applicationId = Constants.APP_ID
|
||||
minSdk = Constants.MIN_SDK
|
||||
@@ -201,7 +203,6 @@ 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
@@ -1,3 +0,0 @@
|
||||
-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,10 +5,11 @@
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
|
||||
<!--foreground service exempt android 14-->
|
||||
<!--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" />
|
||||
<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" />
|
||||
@@ -151,21 +152,45 @@
|
||||
android:name=".core.service.autotunnel.AutoTunnelService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="systemExempted"
|
||||
android:foregroundServiceType="specialUse"
|
||||
android:persistent="true"
|
||||
android:stopWithTask="false"
|
||||
tools:node="merge" />
|
||||
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>
|
||||
|
||||
<service
|
||||
android:name=".core.service.TunnelForegroundService"
|
||||
android:name=".core.service.VpnForegroundService"
|
||||
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,10 +17,12 @@ 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
|
||||
@@ -50,6 +52,7 @@ 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
|
||||
@@ -64,6 +67,8 @@ 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
|
||||
@@ -82,6 +87,7 @@ 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
|
||||
@@ -97,8 +103,8 @@ class MainActivity : AppCompatActivity() {
|
||||
@SuppressLint("BatteryLife")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
enableEdgeToEdge(
|
||||
statusBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT),
|
||||
navigationBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT),
|
||||
statusBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT),
|
||||
navigationBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT),
|
||||
)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
window.isNavigationBarContrastEnforced = false
|
||||
@@ -132,6 +138,8 @@ 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) }
|
||||
@@ -139,6 +147,8 @@ class MainActivity : AppCompatActivity() {
|
||||
mutableStateOf<Pair<AppMode?, TunnelConf?>>(Pair(null, null))
|
||||
}
|
||||
|
||||
LaunchedEffect(navState) { Timber.d("New navbar state $navState") }
|
||||
|
||||
val vpnActivity =
|
||||
rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult(),
|
||||
@@ -237,14 +247,14 @@ class MainActivity : AppCompatActivity() {
|
||||
)
|
||||
}
|
||||
},
|
||||
topBar = { DynamicTopAppBar(appState.navBarState) },
|
||||
topBar = { DynamicTopAppBar(navState) },
|
||||
bottomBar = {
|
||||
BottomNavbar(
|
||||
appState.isAutoTunnelActive,
|
||||
appState.navBarState,
|
||||
navController,
|
||||
)
|
||||
BottomNavbar(appState.isAutoTunnelActive, navState, navController)
|
||||
},
|
||||
modifier =
|
||||
Modifier.pointerInput(Unit) {
|
||||
detectTapGestures { viewModel.clearSelectedTunnels() }
|
||||
},
|
||||
) { padding ->
|
||||
Box(
|
||||
modifier =
|
||||
@@ -307,9 +317,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
navigation<Route.AutoTunnelGraph>(
|
||||
startDestination =
|
||||
if (appState.isLocationDisclosureShown) Route.AutoTunnel
|
||||
else Route.LocationDisclosure
|
||||
startDestination = Route.AutoTunnel
|
||||
) {
|
||||
composable<Route.LocationDisclosure> {
|
||||
val viewModel =
|
||||
@@ -380,6 +388,8 @@ class MainActivity : AppCompatActivity() {
|
||||
SupportScreen(viewModel)
|
||||
}
|
||||
composable<Route.License> { LicenseScreen() }
|
||||
composable<Route.Donate> { DonateScreen(navController) }
|
||||
composable<Route.Addresses> { AddressesScreen() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,16 @@ 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
|
||||
import com.zaneschepke.wireguardautotunnel.core.worker.ServiceWorker
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.di.MainDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
import javax.inject.Inject
|
||||
@@ -25,6 +25,7 @@ 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
|
||||
@@ -39,11 +40,11 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
||||
|
||||
@Inject lateinit var logReader: LogReader
|
||||
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
|
||||
|
||||
@Inject @MainDispatcher lateinit var mainDispatcher: CoroutineDispatcher
|
||||
@Inject lateinit var settingsRepository: GeneralSettingRepository
|
||||
@Inject lateinit var tunnelsRepository: TunnelRepository
|
||||
@Inject lateinit var appStateRepository: AppStateRepository
|
||||
|
||||
@Inject lateinit var notificationMonitor: NotificationMonitor
|
||||
|
||||
@@ -67,15 +68,15 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
||||
}
|
||||
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
launch { if (appDataRepository.appState.isLocalLogsEnabled()) logReader.start() }
|
||||
launch { if (appStateRepository.isLocalLogsEnabled()) logReader.start() }
|
||||
launch { notificationMonitor.handleApplicationNotifications() }
|
||||
}
|
||||
|
||||
GoBackend.setAlwaysOnCallback {
|
||||
applicationScope.launch {
|
||||
val settings = appDataRepository.settings.get()
|
||||
val settings = settingsRepository.get()
|
||||
if (settings.isAlwaysOnVpnEnabled) {
|
||||
val tunnel = appDataRepository.getPrimaryOrFirstTunnel()
|
||||
val tunnel = tunnelsRepository.getDefaultTunnel()
|
||||
tunnel?.let { tunnelManager.startTunnel(it) }
|
||||
} else {
|
||||
Timber.w("Always-on VPN is not enabled in app settings")
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.zaneschepke.wireguardautotunnel.core.broadcast
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
@@ -19,8 +18,6 @@ class KernelReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject lateinit var tunnelRepository: TunnelRepository
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
@@ -31,7 +28,6 @@ class KernelReceiver : BroadcastReceiver() {
|
||||
val tunnel = tunnelRepository.findByTunnelName(name)
|
||||
tunnel?.let { tunnelRepository.save(it.copy(isActive = true)) }
|
||||
}
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-7
@@ -4,10 +4,10 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
@@ -17,23 +17,24 @@ import kotlinx.coroutines.launch
|
||||
@AndroidEntryPoint
|
||||
class NotificationActionReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
@Inject lateinit var tunnelRepository: TunnelRepository
|
||||
|
||||
@Inject lateinit var settingsRepository: GeneralSettingRepository
|
||||
|
||||
@Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
applicationScope.launch {
|
||||
when (intent.action) {
|
||||
NotificationAction.AUTO_TUNNEL_OFF.name -> serviceManager.stopAutoTunnel()
|
||||
NotificationAction.AUTO_TUNNEL_OFF.name ->
|
||||
settingsRepository.updateAutoTunnelEnabled(false)
|
||||
NotificationAction.TUNNEL_OFF.name -> {
|
||||
val tunnelId = intent.getIntExtra(NotificationManager.EXTRA_ID, 0)
|
||||
if (tunnelId == STOP_ALL_TUNNELS_ID) return@launch tunnelManager.stopTunnel()
|
||||
val tunnel = tunnelRepository.getById(tunnelId)
|
||||
tunnelManager.stopTunnel(tunnel)
|
||||
if (tunnelId == STOP_ALL_TUNNELS_ID)
|
||||
return@launch tunnelManager.stopActiveTunnels()
|
||||
tunnelRepository.getById(tunnelId)?.let { tunnelManager.stopTunnel(it.id) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-17
@@ -3,10 +3,11 @@ package com.zaneschepke.wireguardautotunnel.core.broadcast
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
@@ -19,9 +20,9 @@ class RemoteControlReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
|
||||
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
@Inject lateinit var appStateRepository: AppStateRepository
|
||||
@Inject lateinit var settingsRepository: GeneralSettingRepository
|
||||
@Inject lateinit var tunnelsRepository: TunnelRepository
|
||||
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
@@ -52,10 +53,10 @@ class RemoteControlReceiver : BroadcastReceiver() {
|
||||
val action = intent.action ?: return
|
||||
val appAction = Action.fromAction(action) ?: return Timber.w("Unknown action $action")
|
||||
applicationScope.launch {
|
||||
if (!appDataRepository.appState.isRemoteControlEnabled())
|
||||
if (!appStateRepository.isRemoteControlEnabled())
|
||||
return@launch Timber.w("Remote control disabled")
|
||||
val key =
|
||||
appDataRepository.appState.getRemoteKey()
|
||||
appStateRepository.getRemoteKey()
|
||||
?: return@launch Timber.w("Remote control key missing")
|
||||
if (key != intent.getStringExtra(EXTRA_KEY)?.trim())
|
||||
return@launch Timber.w("Invalid remote control key")
|
||||
@@ -64,29 +65,27 @@ class RemoteControlReceiver : BroadcastReceiver() {
|
||||
val tunnelName =
|
||||
intent.getStringExtra(EXTRA_TUN_NAME) ?: return@launch startDefaultTunnel()
|
||||
val tunnel =
|
||||
appDataRepository.tunnels.findByTunnelName(tunnelName)
|
||||
tunnelsRepository.findByTunnelName(tunnelName)
|
||||
?: return@launch startDefaultTunnel()
|
||||
tunnelManager.startTunnel(tunnel)
|
||||
}
|
||||
Action.STOP_TUNNEL -> {
|
||||
val tunnelName =
|
||||
intent.getStringExtra(EXTRA_TUN_NAME)
|
||||
?: return@launch tunnelManager.stopTunnel()
|
||||
?: return@launch tunnelManager.stopActiveTunnels()
|
||||
val tunnel =
|
||||
appDataRepository.tunnels.findByTunnelName(tunnelName)
|
||||
?: return@launch tunnelManager.stopTunnel()
|
||||
tunnelManager.stopTunnel(tunnel)
|
||||
tunnelsRepository.findByTunnelName(tunnelName)
|
||||
?: return@launch tunnelManager.stopActiveTunnels()
|
||||
tunnelManager.stopTunnel(tunnel.id)
|
||||
}
|
||||
Action.START_AUTO_TUNNEL -> serviceManager.startAutoTunnel()
|
||||
Action.STOP_AUTO_TUNNEL -> serviceManager.stopAutoTunnel()
|
||||
Action.START_AUTO_TUNNEL -> settingsRepository.updateAutoTunnelEnabled(true)
|
||||
Action.STOP_AUTO_TUNNEL -> settingsRepository.updateAutoTunnelEnabled(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startDefaultTunnel() {
|
||||
appDataRepository.getPrimaryOrFirstTunnel()?.let { tunnel ->
|
||||
tunnelManager.startTunnel(tunnel)
|
||||
}
|
||||
tunnelsRepository.getDefaultTunnel()?.let { tunnel -> tunnelManager.startTunnel(tunnel) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
-8
@@ -4,11 +4,9 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.zaneschepke.logcatter.LogReader
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
@@ -18,13 +16,9 @@ import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
class RestartReceiver : BroadcastReceiver() {
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
|
||||
// injecting this should let tunnelManger handle clean startup
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
@Inject lateinit var logReader: LogReader
|
||||
@@ -33,8 +27,6 @@ class RestartReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
Timber.d("RestartReceiver triggered with action: ${intent.action}")
|
||||
serviceManager.updateTunnelTile()
|
||||
serviceManager.updateAutoTunnelTile()
|
||||
if (intent.action == Intent.ACTION_MY_PACKAGE_REPLACED)
|
||||
applicationScope.launch(ioDispatcher) { logReader.deleteAndClearLogs() }
|
||||
}
|
||||
|
||||
+4
-4
@@ -22,12 +22,12 @@ constructor(
|
||||
}
|
||||
|
||||
private suspend fun handleTunnelErrors() =
|
||||
tunnelManager.errorEvents.collectLatest { (tunnelConf, error) ->
|
||||
tunnelManager.errorEvents.collectLatest { (tunName, error) ->
|
||||
if (!WireGuardAutoTunnel.uiActive.value) {
|
||||
val notification =
|
||||
notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = StringValue.DynamicString(tunnelConf.name),
|
||||
title = StringValue.DynamicString(tunName),
|
||||
description =
|
||||
when (error) {
|
||||
is BackendCoreException.BounceFailed -> error.toStringValue()
|
||||
@@ -46,12 +46,12 @@ constructor(
|
||||
}
|
||||
|
||||
private suspend fun handleTunnelMessages() =
|
||||
tunnelManager.messageEvents.collectLatest { (tunnelConf, message) ->
|
||||
tunnelManager.messageEvents.collectLatest { (tunName, message) ->
|
||||
if (!WireGuardAutoTunnel.uiActive.value) {
|
||||
val notification =
|
||||
notificationManager.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = StringValue.DynamicString(tunnelConf.name),
|
||||
title = StringValue.DynamicString(tunName),
|
||||
description = message.toStringValue(),
|
||||
)
|
||||
notificationManager.show(
|
||||
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
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.TunnelRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.distinctByKeys
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
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 tunnelsRepository: TunnelRepository
|
||||
|
||||
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 tunnels = tunnelsRepository.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")
|
||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
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),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service
|
||||
|
||||
import android.os.Binder
|
||||
|
||||
class LocalBinder(val service: TunnelService) : Binder()
|
||||
+88
-65
@@ -7,15 +7,16 @@ import android.content.ServiceConnection
|
||||
import android.net.VpnService
|
||||
import android.os.IBinder
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
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.GeneralSettingRepository
|
||||
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.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
@@ -26,29 +27,71 @@ class ServiceManager
|
||||
@Inject
|
||||
constructor(
|
||||
private val context: Context,
|
||||
private val ioDispatcher: CoroutineDispatcher,
|
||||
private val applicationScope: CoroutineScope,
|
||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
private val mainDispatcher: CoroutineDispatcher,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val settingsRepository: GeneralSettingRepository,
|
||||
) {
|
||||
|
||||
private val autoTunnelMutex = Mutex()
|
||||
private val tunnelMutex = Mutex()
|
||||
|
||||
private val _tunnelService = MutableStateFlow<TunnelForegroundService?>(null)
|
||||
private val _tunnelService = MutableStateFlow<TunnelService?>(null)
|
||||
private val _autoTunnelService = MutableStateFlow<AutoTunnelService?>(null)
|
||||
val autoTunnelService = _autoTunnelService.asStateFlow()
|
||||
val tunnelService = _tunnelService.asStateFlow()
|
||||
|
||||
init {
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
_autoTunnelService
|
||||
.onEach { _ -> withContext(mainDispatcher) { updateAutoTunnelTile() } }
|
||||
.launchIn(this)
|
||||
}
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
combine(
|
||||
settingsRepository.flow.map { it.isAutoTunnelEnabled }.distinctUntilChanged(),
|
||||
_autoTunnelService,
|
||||
) { enabled, service ->
|
||||
enabled to (service != null)
|
||||
}
|
||||
.collect { (enabled, isRunning) ->
|
||||
when {
|
||||
enabled && !isRunning -> {
|
||||
autoTunnelMutex.withLock { startServiceInternal() }
|
||||
}
|
||||
!enabled && isRunning -> {
|
||||
autoTunnelMutex.withLock { stopServiceInternal() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val tunnelServiceConnection =
|
||||
object : ServiceConnection {
|
||||
override fun onServiceConnected(name: ComponentName, service: IBinder) {
|
||||
val binder = service as? TunnelForegroundService.LocalBinder
|
||||
val binder = service as? LocalBinder
|
||||
_tunnelService.value = binder?.service
|
||||
Timber.d("TunnelForegroundService connected")
|
||||
val serviceClass =
|
||||
when {
|
||||
name.className.contains("VpnForegroundService") -> "VpnForegroundService"
|
||||
name.className.contains("TunnelForegroundService") ->
|
||||
"TunnelForegroundService"
|
||||
else -> "Unknown"
|
||||
}
|
||||
Timber.d("$serviceClass connected")
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName) {
|
||||
_tunnelService.value = null
|
||||
Timber.d("TunnelForegroundService disconnected")
|
||||
val serviceClass =
|
||||
when {
|
||||
name.className.contains("VpnForegroundService") -> "VpnForegroundService"
|
||||
name.className.contains("TunnelForegroundService") ->
|
||||
"TunnelForegroundService"
|
||||
else -> "Unknown"
|
||||
}
|
||||
Timber.d("$serviceClass disconnected")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,68 +113,48 @@ constructor(
|
||||
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
|
||||
withContext(ioDispatcher) {
|
||||
val intent = Intent(context, AutoTunnelService::class.java)
|
||||
context.startForegroundService(intent)
|
||||
context.bindService(intent, autoTunnelServiceConnection, Context.BIND_AUTO_CREATE)
|
||||
withContext(mainDispatcher) { updateAutoTunnelTile() }
|
||||
}
|
||||
}
|
||||
private fun startServiceInternal() {
|
||||
val intent = Intent(context, AutoTunnelService::class.java)
|
||||
context.startForegroundService(intent)
|
||||
context.bindService(intent, autoTunnelServiceConnection, Context.BIND_AUTO_CREATE)
|
||||
}
|
||||
|
||||
suspend fun stopAutoTunnel() {
|
||||
autoTunnelMutex.withLock {
|
||||
val settings = appDataRepository.settings.get()
|
||||
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
|
||||
if (_autoTunnelService.value == null) return
|
||||
_autoTunnelService.value?.let { service ->
|
||||
private fun stopServiceInternal() {
|
||||
_autoTunnelService.value?.stop()
|
||||
try {
|
||||
context.unbindService(autoTunnelServiceConnection)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to unbind AutoTunnelService")
|
||||
}
|
||||
_autoTunnelService.update { null }
|
||||
}
|
||||
|
||||
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(autoTunnelServiceConnection)
|
||||
context.unbindService(tunnelServiceConnection)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to unbind AutoTunnelService")
|
||||
} finally {
|
||||
_tunnelService.value = null
|
||||
Timber.e(e, "Failed to stop Tunnel Service")
|
||||
}
|
||||
}
|
||||
withContext(mainDispatcher) { updateAutoTunnelTile() }
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
+2
-149
@@ -1,155 +1,8 @@
|
||||
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 : 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),
|
||||
)
|
||||
}
|
||||
}
|
||||
class TunnelForegroundService(override val fgsType: Int = Constants.SPECIAL_USE_SERVICE_TYPE_ID) :
|
||||
BaseTunnelForegroundService()
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.zaneschepke.wireguardautotunnel.core.service
|
||||
|
||||
interface TunnelService {
|
||||
fun start()
|
||||
|
||||
fun stop()
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
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()
|
||||
+14
-86
@@ -14,14 +14,12 @@ import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelMonitor
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.enums.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.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.AutoTunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.NetworkState
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
@@ -31,7 +29,6 @@ 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
|
||||
@@ -43,8 +40,6 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
@Inject lateinit var networkMonitor: NetworkMonitor
|
||||
|
||||
@Inject lateinit var appDataRepository: Provider<AppDataRepository>
|
||||
|
||||
@Inject lateinit var notificationManager: NotificationManager
|
||||
|
||||
@Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
|
||||
@@ -53,7 +48,8 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
@Inject lateinit var tunnelMonitor: TunnelMonitor
|
||||
@Inject lateinit var settingsRepository: Provider<GeneralSettingRepository>
|
||||
@Inject lateinit var tunnelsRepository: TunnelRepository
|
||||
|
||||
private val defaultState = AutoTunnelState()
|
||||
|
||||
@@ -61,12 +57,6 @@ 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)
|
||||
@@ -124,7 +114,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
this,
|
||||
NotificationManager.AUTO_TUNNEL_NOTIFICATION_ID,
|
||||
notification,
|
||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||
Constants.SPECIAL_USE_SERVICE_TYPE_ID,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -143,20 +133,10 @@ 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, monitoringFlow) {
|
||||
network,
|
||||
settings,
|
||||
tunnels,
|
||||
monitoring ->
|
||||
combine(networkFlow, settingsFlow, tunnelsFlow) { network, settings, tunnels ->
|
||||
autoTunnelStateFlow.update {
|
||||
it.copy(
|
||||
activeTunnels = tunnels.activeTunnels,
|
||||
@@ -170,7 +150,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, monitoringFlow).collect { change ->
|
||||
merge(networkFlow, settingsFlow, tunnelsFlow).collect { change ->
|
||||
if (change !is StateChange.ActiveTunnelsChange) {
|
||||
Timber.d("New state changed to ${change.javaClass.simpleName}")
|
||||
}
|
||||
@@ -201,22 +181,6 @@ 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))
|
||||
@@ -253,17 +217,14 @@ class AutoTunnelService : LifecycleService() {
|
||||
old.isVpnKillSwitchEnabled == new.isVpnKillSwitchEnabled &&
|
||||
old.isLanOnKillSwitchEnabled == new.isLanOnKillSwitchEnabled &&
|
||||
old.isDisableKillSwitchOnTrustedEnabled == new.isDisableKillSwitchOnTrustedEnabled &&
|
||||
old.isStopOnNoInternetEnabled == new.isStopOnNoInternetEnabled)
|
||||
old.isStopOnNoInternetEnabled == new.isStopOnNoInternetEnabled &&
|
||||
old.appMode == new.appMode)
|
||||
}
|
||||
|
||||
private fun combineSettings(): Flow<Pair<GeneralSettings, Tunnels>> {
|
||||
return combine(
|
||||
appDataRepository
|
||||
.get()
|
||||
.settings
|
||||
.flow
|
||||
.distinctUntilChanged(::areAutoTunnelSettingsTheSame),
|
||||
appDataRepository.get().tunnels.flow.map { tunnels ->
|
||||
settingsRepository.get().flow.distinctUntilChanged(::areAutoTunnelSettingsTheSame),
|
||||
tunnelsRepository.flow.map { tunnels ->
|
||||
// isActive is ignored for equality checks so user can manually toggle off
|
||||
// tunnel with auto-tunnel
|
||||
tunnels.map { it.copy(isActive = false) }
|
||||
@@ -378,51 +339,19 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
) {
|
||||
is AutoTunnelEvent.Start ->
|
||||
(event.tunnelConf ?: appDataRepository.get().getPrimaryOrFirstTunnel())?.let {
|
||||
(event.tunnelConf ?: tunnelsRepository.getDefaultTunnel())?.let {
|
||||
tunnelManager.startTunnel(it)
|
||||
}
|
||||
is AutoTunnelEvent.Stop -> tunnelManager.stopTunnel()
|
||||
is AutoTunnelEvent.Stop -> tunnelManager.stopActiveTunnels()
|
||||
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)"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
|
||||
private val debouncedConnectivityStateFlow: Flow<ConnectivityState> by lazy {
|
||||
appDataRepository
|
||||
settingsRepository
|
||||
.get()
|
||||
.settings
|
||||
.flow
|
||||
.map { it.debounceDelaySeconds.toMillis() }
|
||||
.distinctUntilChanged()
|
||||
@@ -434,6 +363,5 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
+1
-7
@@ -1,20 +1,14 @@
|
||||
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<TunnelConf, TunnelState>) : StateChange()
|
||||
|
||||
data class MonitoringChange(val pingStates: Map<TunnelConf, Map<Key, PingState>?>) :
|
||||
StateChange()
|
||||
data class ActiveTunnelsChange(val activeTunnels: Map<Int, TunnelState>) : StateChange()
|
||||
}
|
||||
|
||||
+8
-5
@@ -9,7 +9,8 @@ import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.LifecycleRegistry
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -17,7 +18,9 @@ import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject lateinit var settingsRepository: GeneralSettingRepository
|
||||
@Inject lateinit var tunnelsRepository: TunnelRepository
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
|
||||
@@ -44,7 +47,7 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
||||
}
|
||||
}
|
||||
lifecycleScope.launch {
|
||||
appDataRepository.tunnels.flow.collect {
|
||||
tunnelsRepository.flow.collect {
|
||||
if (it.isEmpty()) {
|
||||
setUnavailable()
|
||||
}
|
||||
@@ -57,10 +60,10 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
||||
unlockAndRun {
|
||||
lifecycleScope.launch {
|
||||
if (serviceManager.autoTunnelService.value != null) {
|
||||
serviceManager.stopAutoTunnel()
|
||||
settingsRepository.updateAutoTunnelEnabled(false)
|
||||
setInactive()
|
||||
} else {
|
||||
serviceManager.startAutoTunnel()
|
||||
settingsRepository.updateAutoTunnelEnabled(true)
|
||||
setActive()
|
||||
}
|
||||
}
|
||||
|
||||
+16
-15
@@ -13,9 +13,7 @@ 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 com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -23,7 +21,8 @@ import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject lateinit var tunnelsRepository: TunnelRepository
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
|
||||
@@ -54,7 +53,7 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
|
||||
private suspend fun updateTileState() {
|
||||
try {
|
||||
val tunnels = appDataRepository.tunnels.getAll()
|
||||
val tunnels = tunnelsRepository.getAll()
|
||||
if (tunnels.isEmpty()) {
|
||||
setUnavailable()
|
||||
return
|
||||
@@ -65,12 +64,14 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
|
||||
when {
|
||||
activeTunnels.isNotEmpty() -> {
|
||||
val activeIds = activeTunnels.map { it.key.id }
|
||||
val activeIds = activeTunnels.map { it.key }
|
||||
// TODO improvements would be needed to make this work well with toggling
|
||||
// multiple tunnels
|
||||
// this would be better managed elsewhere
|
||||
WireGuardAutoTunnel.setLastActiveTunnels(activeIds)
|
||||
updateTileForActiveTunnels(activeTunnels)
|
||||
val activeTunNames =
|
||||
tunnels.filter { activeTunnels.keys.contains(it.id) }.map { it.tunName }
|
||||
updateTileForActiveTunnels(activeTunNames)
|
||||
}
|
||||
else -> updateTileForLastActiveTunnels()
|
||||
}
|
||||
@@ -79,10 +80,10 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateTileForActiveTunnels(activeTunnels: Map<TunnelConf, TunnelState>) {
|
||||
private fun updateTileForActiveTunnels(activeTunnelNames: List<String>) {
|
||||
val tileName =
|
||||
when (activeTunnels.size) {
|
||||
1 -> activeTunnels.keys.first().tunName
|
||||
when (activeTunnelNames.size) {
|
||||
1 -> activeTunnelNames[0]
|
||||
else -> getString(R.string.multiple)
|
||||
}
|
||||
updateTile(tileName, true)
|
||||
@@ -92,14 +93,14 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
val lastActiveIds = WireGuardAutoTunnel.getLastActiveTunnels()
|
||||
when {
|
||||
lastActiveIds.isEmpty() -> {
|
||||
appDataRepository.getStartTunnelConfig()?.let { config ->
|
||||
tunnelsRepository.getStartTunnel()?.let { config ->
|
||||
updateTile(config.tunName, false)
|
||||
} ?: setUnavailable()
|
||||
}
|
||||
lastActiveIds.size > 1 -> updateTile(getString(R.string.multiple), false)
|
||||
else -> {
|
||||
val tunnelId = lastActiveIds.first()
|
||||
appDataRepository.tunnels.getById(tunnelId)?.let { tunnel ->
|
||||
tunnelsRepository.getById(tunnelId)?.let { tunnel ->
|
||||
updateTile(tunnel.tunName, false)
|
||||
} ?: setUnavailable()
|
||||
}
|
||||
@@ -111,13 +112,13 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
||||
unlockAndRun {
|
||||
lifecycleScope.launch {
|
||||
if (tunnelManager.activeTunnels.value.isNotEmpty())
|
||||
return@launch tunnelManager.stopTunnel()
|
||||
return@launch tunnelManager.stopActiveTunnels()
|
||||
val lastActive = WireGuardAutoTunnel.getLastActiveTunnels()
|
||||
if (lastActive.isEmpty()) {
|
||||
appDataRepository.getStartTunnelConfig()?.let { tunnelManager.startTunnel(it) }
|
||||
tunnelsRepository.getStartTunnel()?.let { tunnelManager.startTunnel(it) }
|
||||
} else {
|
||||
lastActive.forEach { id ->
|
||||
appDataRepository.tunnels.getById(id)?.let { tunnelManager.startTunnel(it) }
|
||||
tunnelsRepository.getById(id)?.let { tunnelManager.startTunnel(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-13
@@ -2,12 +2,12 @@ package com.zaneschepke.wireguardautotunnel.core.shortcut
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelProvider
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -16,9 +16,9 @@ import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
class ShortcutsActivity : ComponentActivity() {
|
||||
@Inject lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject lateinit var serviceManager: ServiceManager
|
||||
@Inject lateinit var settingsRepository: GeneralSettingRepository
|
||||
@Inject lateinit var tunnelsRepository: TunnelRepository
|
||||
|
||||
@Inject lateinit var tunnelManager: TunnelManager
|
||||
|
||||
@@ -27,7 +27,7 @@ class ShortcutsActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
applicationScope.launch {
|
||||
val settings = appDataRepository.settings.get()
|
||||
val settings = settingsRepository.get()
|
||||
if (settings.isShortcutsEnabled) {
|
||||
when (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)) {
|
||||
LEGACY_TUNNEL_SERVICE_NAME,
|
||||
@@ -35,16 +35,13 @@ class ShortcutsActivity : ComponentActivity() {
|
||||
val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY)
|
||||
Timber.d("Tunnel name extra: $tunnelName")
|
||||
val tunnelConfig =
|
||||
tunnelName?.let {
|
||||
appDataRepository.tunnels.getAll().firstOrNull {
|
||||
it.tunName == tunnelName
|
||||
}
|
||||
} ?: appDataRepository.getStartTunnelConfig()
|
||||
tunnelName?.let { tunnelsRepository.findByTunnelName(it) }
|
||||
?: tunnelsRepository.getDefaultTunnel()
|
||||
Timber.d("Shortcut action on name: ${tunnelConfig?.tunName}")
|
||||
tunnelConfig?.let {
|
||||
when (intent.action) {
|
||||
Action.START.name -> tunnelManager.startTunnel(it)
|
||||
Action.STOP.name -> tunnelManager.stopTunnel()
|
||||
Action.STOP.name -> tunnelManager.stopActiveTunnels()
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
@@ -52,8 +49,8 @@ class ShortcutsActivity : ComponentActivity() {
|
||||
AutoTunnelService::class.java.simpleName,
|
||||
LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
|
||||
when (intent.action) {
|
||||
Action.START.name -> serviceManager.startAutoTunnel()
|
||||
Action.STOP.name -> serviceManager.stopAutoTunnel()
|
||||
Action.START.name -> settingsRepository.updateAutoTunnelEnabled(true)
|
||||
Action.STOP.name -> settingsRepository.updateAutoTunnelEnabled(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
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,59 +21,54 @@ import kotlinx.coroutines.sync.withLock
|
||||
import org.amnezia.awg.crypto.Key
|
||||
import timber.log.Timber
|
||||
|
||||
abstract class BaseTunnel(
|
||||
private val applicationScope: CoroutineScope,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val serviceManager: ServiceManager,
|
||||
) : TunnelProvider {
|
||||
abstract class BaseTunnel(@ApplicationScope protected val applicationScope: CoroutineScope) :
|
||||
TunnelProvider {
|
||||
|
||||
private val _errorEvents = MutableSharedFlow<Pair<TunnelConf, BackendCoreException>>()
|
||||
override val errorEvents = _errorEvents.asSharedFlow()
|
||||
protected val errors = MutableSharedFlow<Pair<String, BackendCoreException>>()
|
||||
override val errorEvents = errors.asSharedFlow()
|
||||
|
||||
private val _messageEvents = MutableSharedFlow<Pair<TunnelConf, BackendMessage>>()
|
||||
private val _messageEvents = MutableSharedFlow<Pair<String, BackendMessage>>()
|
||||
override val messageEvents = _messageEvents.asSharedFlow()
|
||||
|
||||
private val activeTuns = MutableStateFlow<Map<TunnelConf, TunnelState>>(emptyMap())
|
||||
private val tunJobs = ConcurrentHashMap<Int, Job>()
|
||||
protected val activeTuns = MutableStateFlow<Map<Int, TunnelState>>(emptyMap())
|
||||
override val activeTunnels = activeTuns.asStateFlow()
|
||||
|
||||
private val tunJobs = ConcurrentHashMap<Int, Job>()
|
||||
private val tunMutex = Mutex()
|
||||
private val tunStatusMutex = Mutex()
|
||||
private val bounceTunnelMutex = Mutex()
|
||||
|
||||
override val bouncingTunnelIds = ConcurrentHashMap<Int, TunnelStatus.StopReason>()
|
||||
abstract fun tunnelStateFlow(tunnelConf: TunnelConf): Flow<TunnelStatus>
|
||||
|
||||
abstract suspend fun startBackend(tunnel: TunnelConf)
|
||||
abstract override fun setBackendMode(backendMode: BackendMode)
|
||||
|
||||
abstract fun stopBackend(tunnel: TunnelConf)
|
||||
abstract override fun getBackendMode(): BackendMode
|
||||
|
||||
override fun hasVpnPermission(): Boolean {
|
||||
return serviceManager.hasVpnPermission()
|
||||
}
|
||||
abstract override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean
|
||||
|
||||
abstract override fun getStatistics(tunnelId: Int): TunnelStatistics?
|
||||
|
||||
override suspend fun updateTunnelStatus(
|
||||
tunnelConf: TunnelConf,
|
||||
tunnelId: Int,
|
||||
status: TunnelStatus?,
|
||||
stats: TunnelStatistics?,
|
||||
pingStates: Map<Key, PingState>?,
|
||||
handshakeSuccessLogs: Boolean?,
|
||||
logHealthState: LogHealthState?,
|
||||
) {
|
||||
tunStatusMutex.withLock {
|
||||
activeTuns.update { currentTuns ->
|
||||
val originalConf = currentTuns.getKeyById(tunnelConf.id) ?: tunnelConf
|
||||
val existingState = currentTuns.getValueById(tunnelConf.id) ?: TunnelState()
|
||||
val existingState = currentTuns[tunnelId] ?: TunnelState()
|
||||
val newStatus = status ?: existingState.status
|
||||
if (newStatus == TunnelStatus.Down) {
|
||||
Timber.d("Removing tunnel ${tunnelConf.id} from activeTunnels as state is DOWN")
|
||||
cleanUpTunJob(tunnelConf)
|
||||
currentTuns - originalConf
|
||||
Timber.d("Removing tunnel $tunnelId from activeTunnels as state is DOWN")
|
||||
cleanUpTunJob(tunnelId)
|
||||
currentTuns - tunnelId
|
||||
} else if (
|
||||
existingState.status == newStatus &&
|
||||
stats == null &&
|
||||
pingStates == null &&
|
||||
handshakeSuccessLogs == null
|
||||
logHealthState == null
|
||||
) {
|
||||
Timber.d("Skipping redundant state update for ${tunnelConf.id}: $newStatus")
|
||||
Timber.d("Skipping redundant state update for ${tunnelId}: $newStatus")
|
||||
currentTuns
|
||||
} else {
|
||||
val updated =
|
||||
@@ -84,17 +76,15 @@ abstract class BaseTunnel(
|
||||
status = newStatus,
|
||||
statistics = stats ?: existingState.statistics,
|
||||
pingStates = pingStates ?: existingState.pingStates,
|
||||
handshakeSuccessLogs =
|
||||
handshakeSuccessLogs ?: existingState.handshakeSuccessLogs,
|
||||
logHealthState = logHealthState ?: existingState.logHealthState,
|
||||
)
|
||||
currentTuns + (originalConf to updated)
|
||||
currentTuns + (tunnelId to updated)
|
||||
}
|
||||
}
|
||||
handleServiceStateOnChange()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun stopActiveTunnels() {
|
||||
override suspend fun stopActiveTunnels() {
|
||||
activeTunnels.value.forEach { (config, state) ->
|
||||
if (state.status.isUpOrStarting()) {
|
||||
stopTunnel(config)
|
||||
@@ -102,191 +92,42 @@ abstract class BaseTunnel(
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if (activeTuns.value.containsKey(tunnelConf.id) || tunJobs.containsKey(tunnelConf.id)) {
|
||||
return Timber.w("Tunnel is already running: ${tunnelConf.tunName}")
|
||||
}
|
||||
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
|
||||
|
||||
val job =
|
||||
applicationScope.launch {
|
||||
try {
|
||||
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"
|
||||
)
|
||||
}
|
||||
tunnelStateFlow(tunnelConf).collect { status ->
|
||||
updateTunnelStatus(tunnelConf.id, status)
|
||||
}
|
||||
} catch (e: BackendCoreException) {
|
||||
errors.emit(tunnelConf.tunName to e)
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Down)
|
||||
} catch (_: CancellationException) {}
|
||||
}
|
||||
tunJobs[tunnelConf.id] = job
|
||||
job.invokeOnCompletion {
|
||||
tunJobs.remove(tunnelConf.id)
|
||||
Timber.d("Start job completed for tunnel ${tunnelConf.id}")
|
||||
activeTuns.update { it - tunnelConf.id }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 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()
|
||||
override suspend fun stopTunnel(tunnelId: Int) {
|
||||
tunMutex.withLock {
|
||||
if (activeTuns.isStarting(tunnelConf.id))
|
||||
return handleStuckStartingTunnelShutdown(tunnelConf)
|
||||
updateTunnelStatus(tunnelConf, TunnelStatus.Stopping(reason))
|
||||
stopTunnelInner(tunnelConf)
|
||||
updateTunnelStatus(tunnelId, TunnelStatus.Stopping)
|
||||
tunJobs[tunnelId]?.cancel() // Triggers awaitClose to stop backend
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
private fun cleanUpTunJob(tunnelId: Int) {
|
||||
Timber.d("Removing job for $tunnelId")
|
||||
tunJobs -= tunnelId
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,61 +2,85 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||
|
||||
import com.wireguard.android.backend.Backend
|
||||
import com.wireguard.android.backend.BackendException
|
||||
import com.wireguard.android.backend.Tunnel
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.wireguard.android.backend.Tunnel as WgTunnel
|
||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.di.Kernel
|
||||
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.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 private val applicationScope: CoroutineScope,
|
||||
serviceManager: ServiceManager,
|
||||
appDataRepository: AppDataRepository,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
@Kernel private val backend: Backend,
|
||||
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
|
||||
) : BaseTunnel(applicationScope) {
|
||||
|
||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||
return try {
|
||||
WireGuardStatistics(backend.getStatistics(tunnelConf))
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
null
|
||||
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()) }
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
|
||||
backend.setState(runtimeTunnel, WgTunnel.State.UP, tunnelConf.toWgConfig())
|
||||
} catch (e: BackendException) {
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw e.toBackendCoreException()
|
||||
close(e.toBackendCoreException())
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw BackendCoreException.Config
|
||||
Timber.e(e, "Invalid backend arguments")
|
||||
close(BackendCoreException.Config)
|
||||
} 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +92,10 @@ constructor(
|
||||
return BackendMode.Inactive
|
||||
}
|
||||
|
||||
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
|
||||
throw NotImplementedError()
|
||||
}
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
return backend.runningTunnelNames
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
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
|
||||
}
|
||||
+275
-70
@@ -9,7 +9,9 @@ import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
|
||||
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.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
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
|
||||
@@ -18,15 +20,13 @@ import javax.inject.Inject
|
||||
import kotlin.concurrent.atomics.AtomicBoolean
|
||||
import kotlin.concurrent.atomics.AtomicReference
|
||||
import kotlin.concurrent.atomics.ExperimentalAtomicApi
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.plus
|
||||
import org.amnezia.awg.crypto.Key
|
||||
import timber.log.Timber
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalAtomicApi::class)
|
||||
class TunnelManager
|
||||
@Inject
|
||||
constructor(
|
||||
@@ -34,18 +34,36 @@ constructor(
|
||||
@Userspace private val userspaceTunnel: TunnelProvider,
|
||||
@ProxyUserspace private val proxyUserspaceTunnel: TunnelProvider,
|
||||
private val serviceManager: ServiceManager,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||
private val settingsRepository: GeneralSettingRepository,
|
||||
private val tunnelsRepository: TunnelRepository,
|
||||
private val tunnelMonitor: TunnelMonitor,
|
||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : TunnelProvider {
|
||||
|
||||
@OptIn(ExperimentalAtomicApi::class)
|
||||
private val monitoringJobs = ConcurrentHashMap<Int, Job>()
|
||||
|
||||
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))
|
||||
|
||||
private val tunnelProviderFlow: StateFlow<TunnelProvider> = run {
|
||||
val currentBackend = AtomicReference(userspaceTunnel)
|
||||
val currentSettings = AtomicReference(GeneralSettings())
|
||||
val initialEmit = AtomicBoolean(true)
|
||||
|
||||
appDataRepository.settings.flow
|
||||
settingsRepository.flow
|
||||
.filterNotNull()
|
||||
// ignore default state
|
||||
.filterNot { it == GeneralSettings() }
|
||||
@@ -66,45 +84,14 @@ constructor(
|
||||
}
|
||||
.onEach { (settings, newBackend) ->
|
||||
val isInitialEmit = initialEmit.exchange(false)
|
||||
val oldBackend = currentBackend.exchange(newBackend)
|
||||
val oldSettings = currentSettings.exchange(settings)
|
||||
val previousBackend = currentBackend.exchange(newBackend)
|
||||
val previousSettings = currentSettings.exchange(settings)
|
||||
|
||||
if ((oldSettings.appMode != settings.appMode) && !isInitialEmit) {
|
||||
oldBackend.stopTunnel()
|
||||
if (oldSettings.appMode == AppMode.LOCK_DOWN)
|
||||
proxyUserspaceTunnel.setBackendMode(BackendMode.Inactive)
|
||||
if ((previousSettings.appMode != settings.appMode) && !isInitialEmit) {
|
||||
handleModeChangeCleanup(previousBackend, previousSettings.appMode)
|
||||
}
|
||||
if (settings.appMode == AppMode.LOCK_DOWN) {
|
||||
// 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) }
|
||||
}
|
||||
handleLockDownModeInit(settings.isLanOnKillSwitchEnabled)
|
||||
}
|
||||
}
|
||||
.map { (_, backend) -> backend }
|
||||
@@ -115,17 +102,89 @@ constructor(
|
||||
)
|
||||
}
|
||||
|
||||
override val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>> =
|
||||
override val activeTunnels: StateFlow<Map<Int, TunnelState>> = run {
|
||||
val activeTunsReference: AtomicReference<Map<Int, TunnelState>> =
|
||||
AtomicReference(emptyMap())
|
||||
tunnelProviderFlow
|
||||
.flatMapLatest { it.activeTunnels }
|
||||
.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 ->
|
||||
handleTunnelsActiveChange(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
|
||||
},
|
||||
),
|
||||
SideEffectWithCondition(
|
||||
effect = { s ->
|
||||
handleFullTunnelMonitoring(s.activeTuns, s.tuns, s.settings)
|
||||
},
|
||||
condition = { s -> s.activeTuns.keys != s.previouslyActive.keys },
|
||||
),
|
||||
)
|
||||
|
||||
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,
|
||||
tunnelsRepository.flow,
|
||||
settingsRepository.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 }
|
||||
.stateIn(
|
||||
scope = applicationScope.plus(ioDispatcher),
|
||||
scope = applicationScope,
|
||||
started = SharingStarted.Eagerly,
|
||||
initialValue = emptyMap(),
|
||||
)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override val errorEvents: SharedFlow<Pair<TunnelConf, BackendCoreException>> =
|
||||
override val errorEvents: SharedFlow<Pair<String, BackendCoreException>> =
|
||||
tunnelProviderFlow
|
||||
.flatMapLatest { it.errorEvents }
|
||||
.shareIn(
|
||||
@@ -135,37 +194,32 @@ constructor(
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override val messageEvents: SharedFlow<Pair<TunnelConf, BackendMessage>> =
|
||||
override val messageEvents: SharedFlow<Pair<String, 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(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||
return tunnelProviderFlow.value.getStatistics(tunnelConf)
|
||||
override fun getStatistics(tunnelId: Int): TunnelStatistics? {
|
||||
return tunnelProviderFlow.value.getStatistics(tunnelId)
|
||||
}
|
||||
|
||||
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(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
|
||||
tunnelProviderFlow.value.stopTunnel(tunnelConf, reason)
|
||||
override suspend fun stopTunnel(tunnelId: Int) {
|
||||
tunnelProviderFlow.value.stopTunnel(tunnelId)
|
||||
}
|
||||
|
||||
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
|
||||
tunnelProviderFlow.value.bounceTunnel(tunnelConf, reason)
|
||||
override suspend fun stopActiveTunnels() {
|
||||
tunnelProviderFlow.value.stopActiveTunnels()
|
||||
}
|
||||
|
||||
override fun setBackendMode(backendMode: BackendMode) {
|
||||
@@ -180,19 +234,170 @@ constructor(
|
||||
return tunnelProviderFlow.value.runningTunnelNames()
|
||||
}
|
||||
|
||||
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
|
||||
return tunnelProviderFlow.value.handleDnsReresolve(tunnelConf)
|
||||
}
|
||||
|
||||
override suspend fun updateTunnelStatus(
|
||||
tunnelConf: TunnelConf,
|
||||
tunnelId: Int,
|
||||
status: TunnelStatus?,
|
||||
stats: TunnelStatistics?,
|
||||
pingStates: Map<Key, PingState>?,
|
||||
handshakeSuccessLogs: Boolean?,
|
||||
logHealthState: LogHealthState?,
|
||||
) {
|
||||
tunnelProviderFlow.value.updateTunnelStatus(
|
||||
tunnelConf,
|
||||
tunnelId,
|
||||
status,
|
||||
stats,
|
||||
pingStates,
|
||||
handshakeSuccessLogs,
|
||||
logHealthState,
|
||||
)
|
||||
}
|
||||
|
||||
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)
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
|
||||
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 (serviceManager.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 = settingsRepository.flow.first()
|
||||
if (settings.isRestoreOnBootEnabled) {
|
||||
if (settings.isAutoTunnelEnabled) {
|
||||
tunnelsRepository.resetActiveTunnels()
|
||||
return settingsRepository.updateAutoTunnelEnabled(true)
|
||||
}
|
||||
val tunnels = tunnelsRepository.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
|
||||
tunnelsRepository.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 handleTunnelsActiveChange(
|
||||
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 ->
|
||||
tunnelsRepository.save(dbTunnelConf.copy(isActive = true))
|
||||
}
|
||||
}
|
||||
wasActive && !isActiveNow -> {
|
||||
tuns
|
||||
.find { it.id == tunnelId }
|
||||
?.let { dbTunnelConf ->
|
||||
tunnelsRepository.save(dbTunnelConf.copy(isActive = false))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleFullTunnelMonitoring(
|
||||
activeTuns: Map<Int, TunnelState>,
|
||||
configs: List<TunnelConf>,
|
||||
settings: GeneralSettings,
|
||||
) {
|
||||
val activeIds = activeTuns.keys
|
||||
val obsoleteIds = monitoringJobs.keys - activeIds
|
||||
obsoleteIds.forEach { id ->
|
||||
monitoringJobs[id]?.cancel()
|
||||
monitoringJobs.remove(id)
|
||||
}
|
||||
activeIds.forEach { id ->
|
||||
if (monitoringJobs.contains(id)) return@forEach
|
||||
configs.find { it.id == id } ?: return@forEach
|
||||
val tunStateFlow = activeTunnels.map { it[id] }.stateIn(applicationScope + ioDispatcher)
|
||||
monitoringJobs[id] =
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
tunnelMonitor.startMonitoring(
|
||||
id,
|
||||
withLogs = settings.appMode != AppMode.KERNEL,
|
||||
tunStateFlow = tunStateFlow,
|
||||
getStatistics = { tunnelId -> getStatistics(tunnelId) },
|
||||
updateTunnelStatus = { tid, status, stats, pings, logHealth ->
|
||||
updateTunnelStatus(tid, null, stats, pings, logHealth)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+82
-60
@@ -2,11 +2,16 @@ 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.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
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.domain.state.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toMillis
|
||||
import com.zaneschepke.wireguardautotunnel.util.network.NetworkUtils
|
||||
import dagger.hilt.android.scopes.ServiceScoped
|
||||
@@ -21,65 +26,71 @@ import timber.log.Timber
|
||||
class TunnelMonitor
|
||||
@Inject
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val tunnelManager: TunnelManager,
|
||||
private val settingsRepository: GeneralSettingRepository,
|
||||
private val tunnelsRepository: TunnelRepository,
|
||||
private val networkMonitor: NetworkMonitor,
|
||||
private val networkUtils: NetworkUtils,
|
||||
private val logReader: LogReader,
|
||||
) {
|
||||
|
||||
@OptIn(FlowPreview::class)
|
||||
suspend fun startMonitoring(tunnelConf: TunnelConf, withLogs: Boolean): Job = coroutineScope {
|
||||
suspend fun startMonitoring(
|
||||
tunnelId: Int,
|
||||
withLogs: Boolean,
|
||||
tunStateFlow: StateFlow<TunnelState?>,
|
||||
getStatistics: suspend (Int) -> TunnelStatistics?,
|
||||
updateTunnelStatus:
|
||||
suspend (
|
||||
Int, TunnelStatus?, TunnelStatistics?, Map<Key, PingState>?, LogHealthState?,
|
||||
) -> Unit,
|
||||
): Job = coroutineScope {
|
||||
launch {
|
||||
launch { startTunnelConfChangesJob(tunnelConf) }
|
||||
launch { startPingMonitor(tunnelConf) }
|
||||
launch { startWgStatsPoll(tunnelConf) }
|
||||
if (withLogs) launch { startLogsMonitor(tunnelConf) }
|
||||
val config = tunnelsRepository.getById(tunnelId) ?: return@launch
|
||||
launch { startPingMonitor(config, tunStateFlow, updateTunnelStatus) }
|
||||
launch { startWgStatsPoll(tunnelId, getStatistics, updateTunnelStatus) }
|
||||
if (withLogs) launch { startLogsMonitor(config, updateTunnelStatus) }
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
updateTunnelStatus:
|
||||
suspend (
|
||||
Int, TunnelStatus?, TunnelStatistics?, Map<Key, PingState>?, LogHealthState?,
|
||||
) -> Unit,
|
||||
) {
|
||||
logReader.liveLogs
|
||||
.filter { log -> log.tag.contains(tunnelConf.tunName) }
|
||||
.mapNotNull { log ->
|
||||
val now = System.currentTimeMillis()
|
||||
|
||||
private suspend fun startLogsMonitor(tunnelConf: TunnelConf) {
|
||||
logReader.liveLogs.collect { log ->
|
||||
val healthLogs =
|
||||
when {
|
||||
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
|
||||
successLogRegex.containsMatchIn(log.message) ->
|
||||
LogHealthState(isHealthy = true, timestamp = now)
|
||||
|
||||
failureLogRegex.containsMatchIn(log.message) ->
|
||||
LogHealthState(isHealthy = false, timestamp = now)
|
||||
|
||||
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")
|
||||
updateTunnelStatus(tunnelConf.id, null, null, null, logHealthState)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startPingMonitor(tunnelConf: TunnelConf) = coroutineScope {
|
||||
private suspend fun startPingMonitor(
|
||||
tunnelConf: TunnelConf,
|
||||
tunStateFlow: StateFlow<TunnelState?>,
|
||||
updateTunnelStatus:
|
||||
suspend (
|
||||
Int, TunnelStatus?, TunnelStatistics?, Map<Key, PingState>?, LogHealthState?,
|
||||
) -> Unit,
|
||||
) = coroutineScope {
|
||||
val pingStatsFlow = MutableStateFlow<Map<Key, PingState>>(emptyMap())
|
||||
|
||||
val tunStateFlow =
|
||||
tunnelManager.activeTunnels.mapNotNull { it.getValueById(tunnelConf.id) }.stateIn(this)
|
||||
|
||||
val connectivityStateFlow = networkMonitor.connectivityStateFlow.stateIn(this)
|
||||
|
||||
val isNetworkConnected = connectivityStateFlow.map { it.hasConnectivity() }.stateIn(this)
|
||||
@@ -103,15 +114,19 @@ constructor(
|
||||
.distinctUntilChanged()
|
||||
.stateIn(this)
|
||||
|
||||
appDataRepository.settings.flow
|
||||
settingsRepository.flow
|
||||
.distinctUntilChanged { old, new ->
|
||||
old.isPingEnabled == new.isPingEnabled &&
|
||||
old.tunnelPingIntervalSeconds == new.tunnelPingIntervalSeconds &&
|
||||
old.tunnelPingAttempts == new.tunnelPingAttempts &&
|
||||
old.tunnelPingTimeoutSeconds == new.tunnelPingTimeoutSeconds
|
||||
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")
|
||||
|
||||
@@ -210,12 +225,12 @@ constructor(
|
||||
|
||||
if (updates.isNotEmpty()) {
|
||||
pingStatsFlow.update { updates }
|
||||
tunnelManager.updateTunnelStatus(tunnelConf, null, null, updates)
|
||||
updateTunnelStatus(tunnelConf.id, null, null, updates, null)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the tunnel to be fully active
|
||||
tunStateFlow.filter { state -> state.status == TunnelStatus.Up }.first()
|
||||
tunStateFlow.filter { state -> state?.status == TunnelStatus.Up }.first()
|
||||
|
||||
// small delay to make sure tunnel is fully up before we actively monitor
|
||||
delay(3_000L)
|
||||
@@ -233,37 +248,44 @@ constructor(
|
||||
)
|
||||
}
|
||||
}
|
||||
tunnelManager.updateTunnelStatus(
|
||||
tunnelConf,
|
||||
null,
|
||||
null,
|
||||
pingStatsFlow.value,
|
||||
)
|
||||
updateTunnelStatus(tunnelConf.id, null, null, pingStatsFlow.value, null)
|
||||
}
|
||||
delay(settings.tunnelPingIntervalSeconds.toMillis())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startWgStatsPoll(tunnelConf: TunnelConf) = coroutineScope {
|
||||
private suspend fun startWgStatsPoll(
|
||||
tunnelId: Int,
|
||||
getStatistics: suspend (Int) -> TunnelStatistics?,
|
||||
updateTunnelStatus:
|
||||
suspend (
|
||||
Int, TunnelStatus?, TunnelStatistics?, Map<Key, PingState>?, LogHealthState?,
|
||||
) -> Unit,
|
||||
) = coroutineScope {
|
||||
while (isActive) {
|
||||
val stats = tunnelManager.getStatistics(tunnelConf)
|
||||
tunnelManager.updateTunnelStatus(tunnelConf, null, stats, null)
|
||||
val stats = getStatistics(tunnelId)
|
||||
updateTunnelStatus(tunnelId, null, stats, null, 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"
|
||||
|
||||
const val STATS_DELAY = 1_000L
|
||||
|
||||
const val KEEPALIVE_RESPONSE_TEXT = "Receiving keepalive packet"
|
||||
const val HANDSHAKE_RESPONSE_TEXT = "Received handshake response"
|
||||
const val HANDSHAKE_INIT_FAILED_TEXT = "Failed to send handshake initiation: write udp"
|
||||
const val DATA_PACKET_FAILED_TEXT = "Failed to send data packets"
|
||||
const val HANDSHAKE_NOT_COMPLETED_TEXT =
|
||||
"Handshake did not complete after 5 seconds, retrying"
|
||||
}
|
||||
}
|
||||
|
||||
+13
-29
@@ -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,28 +18,14 @@ interface TunnelProvider {
|
||||
suspend fun startTunnel(tunnelConf: TunnelConf)
|
||||
|
||||
/**
|
||||
* Stops the specified tunnel, or all tunnels if none is provided.
|
||||
* Stops the specified tunnel.
|
||||
*
|
||||
* @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.
|
||||
* @param tunnelId The tunnelConf to stop.
|
||||
*/
|
||||
suspend fun stopTunnel(
|
||||
tunnelConf: TunnelConf? = null,
|
||||
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.User,
|
||||
)
|
||||
suspend fun stopTunnel(tunnelId: Int)
|
||||
|
||||
/**
|
||||
* 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,
|
||||
)
|
||||
/** Stops all active tunnels. */
|
||||
suspend fun stopActiveTunnels()
|
||||
|
||||
fun setBackendMode(backendMode: BackendMode)
|
||||
|
||||
@@ -47,23 +33,21 @@ interface TunnelProvider {
|
||||
|
||||
suspend fun runningTunnelNames(): Set<String>
|
||||
|
||||
fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics?
|
||||
fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean
|
||||
|
||||
val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>>
|
||||
fun getStatistics(tunnelId: Int): TunnelStatistics?
|
||||
|
||||
val errorEvents: SharedFlow<Pair<TunnelConf, BackendCoreException>>
|
||||
val activeTunnels: StateFlow<Map<Int, TunnelState>>
|
||||
|
||||
val messageEvents: SharedFlow<Pair<TunnelConf, BackendMessage>>
|
||||
val errorEvents: SharedFlow<Pair<String, BackendCoreException>>
|
||||
|
||||
val bouncingTunnelIds: ConcurrentHashMap<Int, TunnelStatus.StopReason>
|
||||
|
||||
fun hasVpnPermission(): Boolean
|
||||
val messageEvents: SharedFlow<Pair<String, BackendMessage>>
|
||||
|
||||
suspend fun updateTunnelStatus(
|
||||
tunnelConf: TunnelConf,
|
||||
tunnelId: Int,
|
||||
status: TunnelStatus? = null,
|
||||
stats: TunnelStatistics? = null,
|
||||
pingStates: Map<Key, PingState>? = null,
|
||||
handshakeSuccessLogs: Boolean? = null,
|
||||
logHealthState: LogHealthState? = null,
|
||||
)
|
||||
}
|
||||
|
||||
+60
-28
@@ -1,26 +1,35 @@
|
||||
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
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.AppProxySettings
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.ProxySettingsRepository
|
||||
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
|
||||
import org.amnezia.awg.backend.Tunnel as AwgTunnel
|
||||
import org.amnezia.awg.config.Config
|
||||
import org.amnezia.awg.config.DnsSettings
|
||||
import org.amnezia.awg.config.proxy.HttpProxy
|
||||
@@ -31,20 +40,31 @@ import timber.log.Timber
|
||||
class UserspaceTunnel
|
||||
@Inject
|
||||
constructor(
|
||||
applicationScope: CoroutineScope,
|
||||
val serviceManager: ServiceManager,
|
||||
val appDataRepository: AppDataRepository,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
private val proxySettingsRepository: ProxySettingsRepository,
|
||||
private val settingsRepository: GeneralSettingRepository,
|
||||
private val backend: Backend,
|
||||
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
|
||||
) : BaseTunnel(applicationScope) {
|
||||
|
||||
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(tunnel, TunnelStatus.Starting)
|
||||
updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
|
||||
|
||||
val proxies: List<Proxy> =
|
||||
when (backend) {
|
||||
is ProxyGoBackend -> {
|
||||
val proxySettings = appDataRepository.proxySettings.get()
|
||||
val proxySettings = proxySettingsRepository.get()
|
||||
Timber.d("Adding proxy configs")
|
||||
buildList {
|
||||
if (proxySettings.socks5ProxyEnabled) {
|
||||
@@ -71,8 +91,8 @@ constructor(
|
||||
}
|
||||
else -> emptyList()
|
||||
}
|
||||
val setting = appDataRepository.settings.get()
|
||||
val config = tunnel.toAmConfig()
|
||||
val setting = settingsRepository.get()
|
||||
val config = tunnelConf.toAmConfig()
|
||||
val updatedConfig =
|
||||
Config.Builder()
|
||||
.apply {
|
||||
@@ -87,23 +107,28 @@ constructor(
|
||||
)
|
||||
}
|
||||
.build()
|
||||
backend.setState(tunnel, Tunnel.State.UP, updatedConfig)
|
||||
backend.setState(runtimeTunnel, AwgTunnel.State.UP, updatedConfig)
|
||||
} catch (e: BackendException) {
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw e.toBackendCoreException()
|
||||
close(e.toBackendCoreException())
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||
throw BackendCoreException.Config
|
||||
close(BackendCoreException.Config)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Error while setting tunnel state")
|
||||
close(BackendCoreException.Unknown)
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,15 +148,22 @@ 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(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||
override fun getStatistics(tunnelId: Int): TunnelStatistics? {
|
||||
return try {
|
||||
AmneziaStatistics(backend.getStatistics(tunnelConf))
|
||||
val runtimeTunnel = runtimeTunnels[tunnelId] ?: return null
|
||||
AmneziaStatistics(backend.getStatistics(runtimeTunnel))
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to get stats for ${tunnelConf.tunName}")
|
||||
Timber.e(e, "Failed to get stats for $tunnelId")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.hilt.work.HiltWorker
|
||||
import androidx.work.*
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
import java.util.concurrent.TimeUnit
|
||||
@@ -20,7 +20,7 @@ constructor(
|
||||
@Assisted private val context: Context,
|
||||
@Assisted private val params: WorkerParameters,
|
||||
private val serviceManager: ServiceManager,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val settingsRepository: GeneralSettingRepository,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : CoroutineWorker(context, params) {
|
||||
|
||||
@@ -50,11 +50,11 @@ constructor(
|
||||
override suspend fun doWork(): Result =
|
||||
withContext(ioDispatcher) {
|
||||
Timber.i("Service worker started")
|
||||
with(appDataRepository.settings.get()) {
|
||||
with(settingsRepository.get()) {
|
||||
Timber.i("Checking to see if auto-tunnel has been killed by system")
|
||||
if (isAutoTunnelEnabled && serviceManager.autoTunnelService.value == null) {
|
||||
Timber.i("Service has been killed by system, restoring.")
|
||||
serviceManager.startAutoTunnel()
|
||||
settingsRepository.updateAutoTunnelEnabled(true)
|
||||
}
|
||||
}
|
||||
Result.success()
|
||||
|
||||
@@ -42,7 +42,7 @@ class DataStoreManager(
|
||||
try {
|
||||
context.dataStore.data.first()
|
||||
} catch (e: IOException) {
|
||||
Timber.Forest.e(e)
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,9 +52,9 @@ class DataStoreManager(
|
||||
try {
|
||||
context.dataStore.edit { it[key] = value }
|
||||
} catch (e: IOException) {
|
||||
Timber.Forest.e(e)
|
||||
Timber.e(e)
|
||||
} catch (e: Exception) {
|
||||
Timber.Forest.e(e)
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,9 +64,9 @@ class DataStoreManager(
|
||||
try {
|
||||
context.dataStore.edit { it.remove(key) }
|
||||
} catch (e: IOException) {
|
||||
Timber.Forest.e(e)
|
||||
Timber.e(e)
|
||||
} catch (e: Exception) {
|
||||
Timber.Forest.e(e)
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ class DataStoreManager(
|
||||
try {
|
||||
context.dataStore.data.map { it[key] }.first()
|
||||
} catch (e: IOException) {
|
||||
Timber.Forest.e(e)
|
||||
Timber.e(e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,7 @@ interface SettingsDao {
|
||||
@Delete suspend fun delete(t: Settings)
|
||||
|
||||
@Query("SELECT COUNT('id') FROM settings") suspend fun count(): Long
|
||||
|
||||
@Query("UPDATE settings SET is_tunnel_enabled = :enabled")
|
||||
suspend fun updateAutoTunnelEnabled(enabled: Boolean)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@ 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?
|
||||
|
||||
@@ -44,6 +47,28 @@ interface TunnelConfigDao {
|
||||
@Query("SELECT * FROM TUNNELCONFIG WHERE is_mobile_data_tunnel=1")
|
||||
suspend fun findByMobileDataTunnel(): TunnelConfigs
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT * FROM TunnelConfig
|
||||
ORDER BY
|
||||
CASE WHEN is_primary_tunnel = 1 THEN 0 ELSE 1 END,
|
||||
position ASC
|
||||
LIMIT 1"""
|
||||
)
|
||||
suspend fun getDefaultTunnel(): TunnelConfig?
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT * FROM TunnelConfig
|
||||
ORDER BY
|
||||
CASE WHEN is_Active = 1 THEN 0
|
||||
WHEN is_primary_tunnel = 1 THEN 1
|
||||
ELSE 2 END,
|
||||
position ASC
|
||||
LIMIT 1"""
|
||||
)
|
||||
suspend fun getStartTunnel(): TunnelConfig?
|
||||
|
||||
@Query("SELECT * FROM tunnelconfig ORDER BY position")
|
||||
fun getAllFlow(): Flow<List<TunnelConfig>>
|
||||
}
|
||||
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class AppDataRoomRepository
|
||||
@Inject
|
||||
constructor(
|
||||
override val settings: GeneralSettingRepository,
|
||||
override val tunnels: TunnelRepository,
|
||||
override val appState: AppStateRepository,
|
||||
override val proxySettings: ProxySettingsRepository,
|
||||
) : AppDataRepository {
|
||||
|
||||
override suspend fun getPrimaryOrFirstTunnel(): TunnelConf? {
|
||||
return tunnels.findPrimary().firstOrNull() ?: tunnels.getAll().firstOrNull()
|
||||
}
|
||||
|
||||
override suspend fun getStartTunnelConfig(): TunnelConf? {
|
||||
tunnels.getActive().let {
|
||||
if (it.isNotEmpty()) return it.first()
|
||||
return getPrimaryOrFirstTunnel()
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
@@ -29,4 +29,8 @@ class RoomSettingsRepository(
|
||||
(settingsDoa.getAll().firstOrNull() ?: Settings()).toAppSettings()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateAutoTunnelEnabled(enabled: Boolean) {
|
||||
withContext(ioDispatcher) { settingsDoa.updateAutoTunnelEnabled(enabled) }
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -46,6 +46,10 @@ class RoomTunnelRepository(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun resetActiveTunnels() {
|
||||
withContext(ioDispatcher) { tunnelConfigDao.resetActiveTunnels() }
|
||||
}
|
||||
|
||||
override suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) {
|
||||
withContext(ioDispatcher) {
|
||||
tunnelConfigDao.resetMobileDataTunnel()
|
||||
@@ -78,6 +82,18 @@ class RoomTunnelRepository(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getDefaultTunnel(): TunnelConf? {
|
||||
return withContext(ioDispatcher) {
|
||||
tunnelConfigDao.getDefaultTunnel()?.let(TunnelConfigMapper::toTunnelConf)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getStartTunnel(): TunnelConf? {
|
||||
return withContext(ioDispatcher) {
|
||||
tunnelConfigDao.getStartTunnel()?.let(TunnelConfigMapper::toTunnelConf)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun count(): Int {
|
||||
return withContext(ioDispatcher) { tunnelConfigDao.count().toInt() }
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package com.zaneschepke.wireguardautotunnel.di
|
||||
|
||||
import javax.inject.Qualifier
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class DefaultDispatcher
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class DefaultDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class IoDispatcher
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class IoDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class MainDispatcher
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainImmediateDispatcher
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ApplicationScope
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class ApplicationScope
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ServiceScope
|
||||
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class ServiceScope
|
||||
|
||||
@@ -124,22 +124,6 @@ class RepositoryModule {
|
||||
return DataStoreAppStateRepository(dataStoreManager, applicationScope, ioDispatcher)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideAppDataRepository(
|
||||
settingsRepository: GeneralSettingRepository,
|
||||
tunnelRepository: TunnelRepository,
|
||||
appStateRepository: AppStateRepository,
|
||||
proxySettingsRepository: ProxySettingsRepository,
|
||||
): AppDataRepository {
|
||||
return AppDataRoomRepository(
|
||||
settingsRepository,
|
||||
tunnelRepository,
|
||||
appStateRepository,
|
||||
proxySettingsRepository,
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideHttpClient(): HttpClient {
|
||||
|
||||
@@ -9,8 +9,9 @@ import com.zaneschepke.networkmonitor.AndroidNetworkMonitor
|
||||
import com.zaneschepke.networkmonitor.NetworkMonitor
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.*
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.ProxySettingsRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.to
|
||||
import com.zaneschepke.wireguardautotunnel.util.network.NetworkUtils
|
||||
import dagger.Module
|
||||
@@ -84,11 +85,9 @@ class TunnelModule {
|
||||
@Kernel
|
||||
fun provideKernelProvider(
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
serviceManager: ServiceManager,
|
||||
appDataRepository: AppDataRepository,
|
||||
backend: com.wireguard.android.backend.Backend,
|
||||
): TunnelProvider {
|
||||
return KernelTunnel(applicationScope, serviceManager, appDataRepository, backend)
|
||||
return KernelTunnel(applicationScope, backend)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -96,11 +95,16 @@ class TunnelModule {
|
||||
@Userspace
|
||||
fun provideUserspaceProvider(
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
serviceManager: ServiceManager,
|
||||
appDataRepository: AppDataRepository,
|
||||
proxySettingsRepository: ProxySettingsRepository,
|
||||
settingsRepository: GeneralSettingRepository,
|
||||
@Userspace backend: Backend,
|
||||
): TunnelProvider {
|
||||
return UserspaceTunnel(applicationScope, serviceManager, appDataRepository, backend)
|
||||
return UserspaceTunnel(
|
||||
applicationScope,
|
||||
proxySettingsRepository,
|
||||
settingsRepository,
|
||||
backend,
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -108,11 +112,16 @@ class TunnelModule {
|
||||
@ProxyUserspace
|
||||
fun provideProxyUserspaceProvider(
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
serviceManager: ServiceManager,
|
||||
appDataRepository: AppDataRepository,
|
||||
settingsRepository: GeneralSettingRepository,
|
||||
proxySettingsRepository: ProxySettingsRepository,
|
||||
@ProxyUserspace backend: Backend,
|
||||
): TunnelProvider {
|
||||
return UserspaceTunnel(applicationScope, serviceManager, appDataRepository, backend)
|
||||
return UserspaceTunnel(
|
||||
applicationScope,
|
||||
proxySettingsRepository,
|
||||
settingsRepository,
|
||||
backend,
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -122,7 +131,9 @@ class TunnelModule {
|
||||
@Userspace userspaceTunnel: TunnelProvider,
|
||||
@ProxyUserspace proxyTunnel: TunnelProvider,
|
||||
serviceManager: ServiceManager,
|
||||
appDataRepository: AppDataRepository,
|
||||
tunnelRepository: TunnelRepository,
|
||||
settingsRepository: GeneralSettingRepository,
|
||||
tunnelMonitor: TunnelMonitor,
|
||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
): TunnelManager {
|
||||
@@ -131,7 +142,9 @@ class TunnelModule {
|
||||
userspaceTunnel,
|
||||
proxyTunnel,
|
||||
serviceManager,
|
||||
appDataRepository,
|
||||
settingsRepository,
|
||||
tunnelRepository,
|
||||
tunnelMonitor,
|
||||
applicationScope,
|
||||
ioDispatcher,
|
||||
)
|
||||
@@ -168,30 +181,29 @@ class TunnelModule {
|
||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||
@MainDispatcher mainCoroutineDispatcher: CoroutineDispatcher,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
appDataRepository: AppDataRepository,
|
||||
settingsRepository: GeneralSettingRepository,
|
||||
): ServiceManager {
|
||||
return ServiceManager(
|
||||
context,
|
||||
ioDispatcher,
|
||||
applicationScope,
|
||||
mainCoroutineDispatcher,
|
||||
appDataRepository,
|
||||
settingsRepository,
|
||||
)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideTunnelMonitor(
|
||||
@ApplicationContext context: Context,
|
||||
tunnelManager: TunnelManager,
|
||||
networkMonitor: NetworkMonitor,
|
||||
networkUtils: NetworkUtils,
|
||||
logReader: LogReader,
|
||||
appDataRepository: AppDataRepository,
|
||||
tunnelsRepository: TunnelRepository,
|
||||
settingsRepository: GeneralSettingRepository,
|
||||
): TunnelMonitor {
|
||||
return TunnelMonitor(
|
||||
appDataRepository,
|
||||
tunnelManager,
|
||||
settingsRepository,
|
||||
tunnelsRepository,
|
||||
networkMonitor,
|
||||
networkUtils,
|
||||
logReader,
|
||||
|
||||
@@ -6,18 +6,10 @@ sealed class TunnelStatus {
|
||||
|
||||
data object Down : TunnelStatus()
|
||||
|
||||
data class Stopping(val reason: StopReason) : TunnelStatus()
|
||||
data object Stopping : 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,9 +5,6 @@ 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,6 +20,8 @@ sealed class BackendCoreException : Exception() {
|
||||
|
||||
data object TunnelNameTooLong : BackendCoreException()
|
||||
|
||||
data object UapiUpdateFailed : BackendCoreException()
|
||||
|
||||
data class BounceFailed(val error: BackendCoreException) : BackendCoreException()
|
||||
|
||||
fun toStringRes() =
|
||||
@@ -33,6 +35,7 @@ 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,14 +1,11 @@
|
||||
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.*
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.defaultName
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isValidIpv4orIpv6Address
|
||||
import java.io.InputStream
|
||||
import java.nio.charset.StandardCharsets
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class TunnelConf(
|
||||
val id: Int = 0,
|
||||
val tunName: String,
|
||||
@@ -23,14 +20,9 @@ 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 = (name.length <= 15)
|
||||
|
||||
fun setStateChangeCallback(callback: (Any) -> Unit) {
|
||||
stateChangeCallback = callback
|
||||
}
|
||||
val isNameKernelCompatible: Boolean = (tunName.length <= 15)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
@@ -60,38 +52,6 @@ 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 })
|
||||
}
|
||||
@@ -100,36 +60,6 @@ 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()
|
||||
@@ -145,7 +75,7 @@ data class TunnelConf(
|
||||
|
||||
fun tunnelConfFromQuick(amQuick: String, name: String? = null): TunnelConf {
|
||||
val config = configFromAmQuick(amQuick)
|
||||
val wgQuick = config.toWgQuickString()
|
||||
val wgQuick = config.toWgQuickString(true)
|
||||
return TunnelConf(
|
||||
tunName = name ?: config.defaultName(),
|
||||
wgQuick = wgQuick,
|
||||
@@ -158,7 +88,7 @@ data class TunnelConf(
|
||||
name: String? = null,
|
||||
): TunnelConf {
|
||||
val amQuick = config.toAwgQuickString(true, false)
|
||||
val wgQuick = config.toWgQuickString()
|
||||
val wgQuick = config.toWgQuickString(true)
|
||||
return TunnelConf(
|
||||
tunName = name ?: config.defaultName(),
|
||||
wgQuick = wgQuick,
|
||||
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.repository
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
|
||||
|
||||
interface AppDataRepository {
|
||||
suspend fun getPrimaryOrFirstTunnel(): TunnelConf?
|
||||
|
||||
suspend fun getStartTunnelConfig(): TunnelConf?
|
||||
|
||||
val settings: GeneralSettingRepository
|
||||
val tunnels: TunnelRepository
|
||||
val appState: AppStateRepository
|
||||
|
||||
val proxySettings: ProxySettingsRepository
|
||||
}
|
||||
+2
@@ -9,4 +9,6 @@ interface GeneralSettingRepository {
|
||||
val flow: Flow<GeneralSettings>
|
||||
|
||||
suspend fun get(): GeneralSettings
|
||||
|
||||
suspend fun updateAutoTunnelEnabled(enabled: Boolean)
|
||||
}
|
||||
|
||||
+6
@@ -15,6 +15,8 @@ interface TunnelRepository {
|
||||
|
||||
suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?)
|
||||
|
||||
suspend fun resetActiveTunnels()
|
||||
|
||||
suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?)
|
||||
|
||||
suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?)
|
||||
@@ -25,6 +27,10 @@ interface TunnelRepository {
|
||||
|
||||
suspend fun getActive(): Tunnels
|
||||
|
||||
suspend fun getDefaultTunnel(): TunnelConf?
|
||||
|
||||
suspend fun getStartTunnel(): TunnelConf?
|
||||
|
||||
suspend fun count(): Int
|
||||
|
||||
suspend fun findByTunnelName(name: String): TunnelConf?
|
||||
|
||||
+5
-39
@@ -2,20 +2,21 @@ 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.*
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.DoNothing
|
||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.Start
|
||||
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<TunnelConf, TunnelState> = emptyMap(),
|
||||
val activeTunnels: Map<Int, TunnelState> = emptyMap(),
|
||||
val networkState: NetworkState = NetworkState(),
|
||||
val settings: GeneralSettings = GeneralSettings(),
|
||||
val tunnels: List<TunnelConf> = emptyList(),
|
||||
) {
|
||||
|
||||
fun determineAutoTunnelEvent(stateChange: StateChange): AutoTunnelEvent {
|
||||
when (val change = stateChange) {
|
||||
when (stateChange) {
|
||||
is StateChange.NetworkChange,
|
||||
is StateChange.SettingsChange -> {
|
||||
// Compute desired tunnel based on network conditions
|
||||
@@ -40,7 +41,7 @@ data class AutoTunnelState(
|
||||
|
||||
// Handle tunnel start/stop/change
|
||||
if (desiredTunnel != null) {
|
||||
if (currentTunnel != desiredTunnel) {
|
||||
if (currentTunnel != desiredTunnel.id) {
|
||||
// Start or switch to the desired tunnel (overrides any kill switch)
|
||||
return Start(desiredTunnel)
|
||||
}
|
||||
@@ -54,12 +55,6 @@ data class AutoTunnelState(
|
||||
}
|
||||
}
|
||||
}
|
||||
is StateChange.MonitoringChange -> {
|
||||
val bounceTunnels = bounceOnPingFailed()
|
||||
if (bounceTunnels.isNotEmpty()) {
|
||||
return Bounce(bounceTunnels)
|
||||
}
|
||||
}
|
||||
|
||||
is StateChange.ActiveTunnelsChange -> Unit
|
||||
}
|
||||
@@ -96,41 +91,12 @@ 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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.zaneschepke.wireguardautotunnel.domain.state
|
||||
|
||||
data class LogHealthState(val isHealthy: Boolean, val timestamp: Long = System.currentTimeMillis())
|
||||
@@ -9,5 +9,48 @@ data class TunnelState(
|
||||
val backendState: BackendMode = BackendMode.Inactive,
|
||||
val statistics: TunnelStatistics? = null,
|
||||
val pingStates: Map<Key, PingState>? = null,
|
||||
val handshakeSuccessLogs: Boolean? = null,
|
||||
)
|
||||
val logHealthState: LogHealthState? = null,
|
||||
) {
|
||||
|
||||
fun health(): Health {
|
||||
val now = System.currentTimeMillis()
|
||||
|
||||
if (pingStates == null && logHealthState == null && statistics == null)
|
||||
return Health.UNKNOWN
|
||||
|
||||
// Logs check take precedent
|
||||
logHealthState?.let { log ->
|
||||
if (!log.isHealthy) return Health.UNHEALTHY
|
||||
val recent = (now - log.timestamp) <= LOG_HEALTH_SUCCESS_TIMEOUT_MS
|
||||
if (recent) {
|
||||
// Logs healthy but override if pings are unhealthy
|
||||
if (pingStates?.any { !it.value.isReachable } == true) return Health.UNHEALTHY
|
||||
return Health.HEALTHY
|
||||
}
|
||||
}
|
||||
|
||||
// Ping health if no logs
|
||||
pingStates?.let { pings ->
|
||||
if (pings.any { !it.value.isReachable }) return Health.UNHEALTHY
|
||||
return Health.HEALTHY
|
||||
}
|
||||
|
||||
// Stats health if no logs or pings
|
||||
statistics?.let { stats ->
|
||||
return if (stats.isTunnelStale()) Health.STALE else 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
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -46,12 +46,12 @@ fun IconSurfaceButton(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.Companion.CenterVertically,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
||||
verticalAlignment = Alignment.Companion.CenterVertically,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier.padding(vertical = if (description == null) 10.dp else 0.dp),
|
||||
) {
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -3,6 +3,7 @@ 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(
|
||||
@@ -11,5 +12,9 @@ data class SelectionItem(
|
||||
val title: (@Composable () -> Unit),
|
||||
val description: (@Composable () -> Unit)? = null,
|
||||
val onClick: (() -> Unit)? = null,
|
||||
val modifier: Modifier = Modifier.height(64.dp),
|
||||
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,
|
||||
)
|
||||
|
||||
+83
-35
@@ -1,65 +1,113 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
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),
|
||||
) {
|
||||
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),
|
||||
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,
|
||||
),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.weight(4f, false).fillMaxWidth(),
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),
|
||||
) {
|
||||
item.leading?.invoke()
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement =
|
||||
Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
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
|
||||
),
|
||||
Modifier.weight(4f, false)
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = item.padding),
|
||||
) {
|
||||
item.title()
|
||||
item.description?.let { it() }
|
||||
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.trailing?.let {
|
||||
Box(
|
||||
contentAlignment = Alignment.CenterEnd,
|
||||
modifier = Modifier.padding(start = 16.dp),
|
||||
) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-78
@@ -1,78 +0,0 @@
|
||||
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,58 +1,65 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.navigation
|
||||
|
||||
import androidx.annotation.Keep
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Keep
|
||||
@Serializable
|
||||
sealed class Route {
|
||||
@Serializable data object TunnelsGraph : Route()
|
||||
|
||||
@Serializable data object AutoTunnelGraph : Route()
|
||||
@Keep @Serializable data object TunnelsGraph : Route()
|
||||
|
||||
@Serializable data object SettingsGraph : Route()
|
||||
@Keep @Serializable data object AutoTunnelGraph : Route()
|
||||
|
||||
@Serializable data object SupportGraph : Route()
|
||||
@Keep @Serializable data object SettingsGraph : Route()
|
||||
|
||||
@Serializable data object Support : Route()
|
||||
@Keep @Serializable data object SupportGraph : Route()
|
||||
|
||||
@Serializable data object Lock : Route()
|
||||
@Keep @Serializable data object Support : Route()
|
||||
|
||||
@Serializable data object License : Route()
|
||||
@Keep @Serializable data object Lock : Route()
|
||||
|
||||
@Serializable data object Logs : Route()
|
||||
@Keep @Serializable data object License : Route()
|
||||
|
||||
@Serializable data object Appearance : Route()
|
||||
@Keep @Serializable data object Logs : Route()
|
||||
|
||||
@Serializable data object Language : Route()
|
||||
@Keep @Serializable data object Appearance : Route()
|
||||
|
||||
@Serializable data object Display : Route()
|
||||
@Keep @Serializable data object Language : Route()
|
||||
|
||||
@Serializable data object Tunnels : Route()
|
||||
@Keep @Serializable data object Display : Route()
|
||||
|
||||
@Serializable data class TunnelOptions(val id: Int) : Route()
|
||||
@Keep @Serializable data object Tunnels : Route()
|
||||
|
||||
@Serializable data class Config(val id: Int?) : Route()
|
||||
@Keep @Serializable data class TunnelOptions(val id: Int) : Route()
|
||||
|
||||
@Serializable data class SplitTunnel(val id: Int) : Route()
|
||||
@Keep @Serializable data class Config(val id: Int?) : Route()
|
||||
|
||||
@Serializable data class TunnelAutoTunnel(val id: Int) : Route()
|
||||
@Keep @Serializable data class SplitTunnel(val id: Int) : Route()
|
||||
|
||||
@Serializable data object Sort : Route()
|
||||
@Keep @Serializable data class TunnelAutoTunnel(val id: Int) : Route()
|
||||
|
||||
@Serializable data object Settings : Route()
|
||||
@Keep @Serializable data object Sort : Route()
|
||||
|
||||
@Serializable data object TunnelMonitoring : Route()
|
||||
@Keep @Serializable data object Settings : Route()
|
||||
|
||||
@Serializable data object SystemFeatures : Route()
|
||||
@Keep @Serializable data object TunnelMonitoring : Route()
|
||||
|
||||
@Serializable data object Dns : Route()
|
||||
@Keep @Serializable data object SystemFeatures : Route()
|
||||
|
||||
@Serializable data object ProxySettings : Route()
|
||||
@Keep @Serializable data object Dns : Route()
|
||||
|
||||
@Serializable data object AutoTunnel : Route()
|
||||
@Keep @Serializable data object ProxySettings : Route()
|
||||
|
||||
@Serializable data object AdvancedAutoTunnel : Route()
|
||||
@Keep @Serializable data object AutoTunnel : Route()
|
||||
|
||||
@Serializable data object WifiDetectionMethod : Route()
|
||||
@Keep @Serializable data object AdvancedAutoTunnel : Route()
|
||||
|
||||
@Serializable data object LocationDisclosure : 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()
|
||||
}
|
||||
|
||||
+28
-17
@@ -25,7 +25,6 @@ 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?> {
|
||||
@@ -54,44 +53,56 @@ 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 = { navigateToDebounced(Route.TunnelsGraph) },
|
||||
onClick = {
|
||||
navController.navigate(Route.TunnelsGraph) {
|
||||
popUpTo(navController.graph.findStartDestination().id) { saveState = true }
|
||||
launchSingleTop = true
|
||||
restoreState = true
|
||||
}
|
||||
},
|
||||
route = Route.TunnelsGraph,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.auto_tunnel),
|
||||
icon = Icons.Rounded.Bolt,
|
||||
onClick = { navigateToDebounced(Route.AutoTunnelGraph) },
|
||||
onClick = {
|
||||
navController.navigate(Route.AutoTunnelGraph) {
|
||||
popUpTo(navController.graph.findStartDestination().id) { saveState = true }
|
||||
launchSingleTop = true
|
||||
restoreState = true
|
||||
}
|
||||
},
|
||||
route = Route.AutoTunnelGraph,
|
||||
active = isAutoTunnelActive,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.settings),
|
||||
icon = Icons.Rounded.Settings,
|
||||
onClick = { navigateToDebounced(Route.SettingsGraph) },
|
||||
onClick = {
|
||||
navController.navigate(Route.SettingsGraph) {
|
||||
popUpTo(navController.graph.findStartDestination().id) { saveState = true }
|
||||
launchSingleTop = true
|
||||
restoreState = true
|
||||
}
|
||||
},
|
||||
route = Route.SettingsGraph,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.support),
|
||||
icon = Icons.Rounded.QuestionMark,
|
||||
onClick = { navigateToDebounced(Route.SupportGraph) },
|
||||
onClick = {
|
||||
navController.navigate(Route.SupportGraph) {
|
||||
popUpTo(navController.graph.findStartDestination().id) { saveState = true }
|
||||
launchSingleTop = true
|
||||
restoreState = true
|
||||
}
|
||||
},
|
||||
route = Route.SupportGraph,
|
||||
),
|
||||
)
|
||||
|
||||
+325
@@ -0,0 +1,325 @@
|
||||
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.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 selectedCount by
|
||||
rememberSaveable(sharedState.selectedTunnels) {
|
||||
mutableIntStateOf(sharedState.selectedTunnels.size)
|
||||
}
|
||||
|
||||
return produceState(initialValue = NavbarState(), route, selectedCount) {
|
||||
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 =
|
||||
if (!sharedState.isLocationDisclosureShown) null
|
||||
else {
|
||||
{ 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(Route.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()
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-13
@@ -17,12 +17,11 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
|
||||
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
|
||||
@@ -32,7 +31,6 @@ 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
|
||||
@@ -41,21 +39,16 @@ 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)) },
|
||||
)
|
||||
)
|
||||
LaunchedEffect(autoTunnelState.stateInitialized) {
|
||||
if (!autoTunnelState.isLocationDisclosureShown && autoTunnelState.stateInitialized) {
|
||||
navController.navigate(Route.LocationDisclosure)
|
||||
}
|
||||
}
|
||||
|
||||
if (!autoTunnelState.stateInitialized) return
|
||||
var showLocationDialog by remember { mutableStateOf(false) }
|
||||
|
||||
val showLocationServicesWarning by
|
||||
|
||||
-14
@@ -12,7 +12,6 @@ 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
|
||||
@@ -20,25 +19,12 @@ 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,7 +36,6 @@ fun networkTunnelingItems(
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
enabled = !autoTunnelState.generalSettings.isAlwaysOnVpnEnabled,
|
||||
checked = autoTunnelState.generalSettings.isTunnelOnMobileDataEnabled,
|
||||
onClick = { viewModel.setTunnelOnCellular(it) },
|
||||
)
|
||||
@@ -77,7 +76,6 @@ fun networkTunnelingItems(
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
enabled = !autoTunnelState.generalSettings.isAlwaysOnVpnEnabled,
|
||||
checked = autoTunnelState.generalSettings.isTunnelOnEthernetEnabled,
|
||||
onClick = { viewModel.setTunnelOnEthernet(it) },
|
||||
)
|
||||
|
||||
-1
@@ -60,7 +60,6 @@ fun wifiTunnelingItems(
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
enabled = !autoTunnelState.generalSettings.isAlwaysOnVpnEnabled,
|
||||
checked = autoTunnelState.generalSettings.isTunnelOnWifiEnabled,
|
||||
onClick = { viewModel.setAutoTunnelOnWifiEnabled(it) },
|
||||
)
|
||||
|
||||
-14
@@ -4,21 +4,16 @@ 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
|
||||
@@ -30,15 +25,6 @@ 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),
|
||||
|
||||
+33
-6
@@ -1,29 +1,45 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
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.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
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.navigation.Route
|
||||
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
|
||||
val context = LocalContext.current
|
||||
|
||||
LaunchedEffect(Unit) { sharedViewModel.updateNavbarState(NavbarState(showBottomItems = true)) }
|
||||
fun goToAutoTunnel() {
|
||||
navController.navigate(Route.AutoTunnel) {
|
||||
popUpTo(Route.LocationDisclosure) { inclusive = true }
|
||||
}
|
||||
}
|
||||
|
||||
val settingsLauncher =
|
||||
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { _ ->
|
||||
goToAutoTunnel()
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) { viewModel.setLocationDisclosureShown() }
|
||||
|
||||
@@ -33,7 +49,18 @@ fun LocationDisclosureScreen(viewModel: AutoTunnelViewModel) {
|
||||
modifier = Modifier.fillMaxSize().padding(top = 18.dp).padding(horizontal = 24.dp),
|
||||
) {
|
||||
LocationDisclosureHeader()
|
||||
SurfaceSelectionGroupButton(items = listOf(appSettingsItem()))
|
||||
SurfaceSelectionGroupButton(items = listOf(skipItem(navController)))
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
listOf(
|
||||
appSettingsItem {
|
||||
val intent =
|
||||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
|
||||
data = Uri.fromParts("package", context.packageName, null)
|
||||
}
|
||||
settingsLauncher.launch(intent)
|
||||
}
|
||||
)
|
||||
)
|
||||
SurfaceSelectionGroupButton(items = listOf(skipItem { goToAutoTunnel() }))
|
||||
}
|
||||
}
|
||||
|
||||
+4
-8
@@ -4,19 +4,15 @@ import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.LocationOn
|
||||
import androidx.compose.material3.Icon
|
||||
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.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.util.extensions.launchAppSettings
|
||||
|
||||
@Composable
|
||||
fun appSettingsItem(): SelectionItem {
|
||||
val context = LocalContext.current
|
||||
|
||||
fun appSettingsItem(onClick: () -> Unit): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.LocationOn, contentDescription = null) },
|
||||
title = {
|
||||
@@ -25,7 +21,7 @@ fun appSettingsItem(): SelectionItem {
|
||||
labelType = SelectionLabelType.TITLE,
|
||||
)
|
||||
},
|
||||
trailing = { ForwardButton { context.launchAppSettings() } },
|
||||
onClick = { context.launchAppSettings() },
|
||||
trailing = { LaunchButton { onClick() } },
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
+3
-5
@@ -2,21 +2,19 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.com
|
||||
|
||||
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 skipItem(navController: NavController): SelectionItem {
|
||||
fun skipItem(onClick: () -> Unit): SelectionItem {
|
||||
return SelectionItem(
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.skip), labelType = SelectionLabelType.TITLE)
|
||||
},
|
||||
trailing = { ForwardButton { navController.navigate(Route.AutoTunnelGraph) } },
|
||||
onClick = { navController.navigate(Route.AutoTunnelGraph) },
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
+4
-4
@@ -3,14 +3,16 @@ 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.*
|
||||
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.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
|
||||
@@ -22,8 +24,6 @@ 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(
|
||||
|
||||
+53
-35
@@ -8,18 +8,14 @@ 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.hilt.lifecycle.viewmodel.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
|
||||
@@ -27,13 +23,15 @@ 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.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asString
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.SettingsViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
|
||||
@Composable
|
||||
@@ -47,11 +45,23 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel()) {
|
||||
|
||||
val settingsState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
if (!settingsState.stateInitialized) return
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
val showProxySettings by
|
||||
remember(settingsState.settings.appMode) {
|
||||
derivedStateOf {
|
||||
@@ -62,21 +72,7 @@ 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 (showBackupSheet) BackupBottomSheet { showBackupSheet = false }
|
||||
if (showAppModeSheet)
|
||||
AppModeBottomSheet(sharedViewModel::setAppMode, settingsState.settings.appMode) {
|
||||
showAppModeSheet = false
|
||||
@@ -103,40 +99,62 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel()) {
|
||||
),
|
||||
) {
|
||||
SurfaceSelectionGroupButton(
|
||||
buildList {
|
||||
add(backendModeItem(settingsState.settings.appMode) { showAppModeSheet = true })
|
||||
}
|
||||
listOf(appModeItem(settingsState.settings.appMode) { showAppModeSheet = true })
|
||||
)
|
||||
SectionDivider()
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
buildList {
|
||||
if (settingsState.settings.appMode == AppMode.LOCK_DOWN) {
|
||||
if (appMode == AppMode.LOCK_DOWN) {
|
||||
add(
|
||||
lanTrafficItem(settingsState.settings.isLanOnKillSwitchEnabled) {
|
||||
viewModel.setLanKillSwitchEnabled(it)
|
||||
}
|
||||
)
|
||||
}
|
||||
add(tunnelMonitoringItem(navController))
|
||||
add(dnsSettingsItem(navController))
|
||||
// TODO changing these settings won't work in certain app states
|
||||
if (showProxySettings) add(proxYSettingsItem(navController))
|
||||
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) })
|
||||
}
|
||||
)
|
||||
SectionDivider()
|
||||
SurfaceSelectionGroupButton(listOf(systemFeaturesItem(navController)))
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(systemFeaturesItem { navController.navigate(Route.SystemFeatures) })
|
||||
)
|
||||
SectionDivider()
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
buildList {
|
||||
add(appearanceItem(navController))
|
||||
add(appearanceItem { navController.navigate(Route.Appearance) })
|
||||
add(
|
||||
localLoggingItem(settingsState.isLocalLoggingEnabled) {
|
||||
viewModel.setLocalLogging(it)
|
||||
}
|
||||
)
|
||||
if (settingsState.isLocalLoggingEnabled) add(readLogsItem(navController))
|
||||
if (settingsState.isLocalLoggingEnabled)
|
||||
add(readLogsItem { navController.navigate(Route.Logs) })
|
||||
add(
|
||||
pinLockItem(settingsState.isPinLockEnabled) { enabled ->
|
||||
if (enabled) {
|
||||
|
||||
-16
@@ -4,36 +4,20 @@ 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.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.LaunchButton
|
||||
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 = { ForwardButton { context.launchNotificationSettings() } },
|
||||
trailing = { LaunchButton { context.launchNotificationSettings() } },
|
||||
onClick = { context.launchNotificationSettings() },
|
||||
)
|
||||
}
|
||||
|
||||
-12
@@ -4,9 +4,7 @@ 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
|
||||
@@ -16,7 +14,6 @@ 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
|
||||
@@ -26,15 +23,6 @@ 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,7 +5,6 @@ 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
|
||||
@@ -18,7 +17,6 @@ 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.*
|
||||
@@ -29,15 +27,6 @@ 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
@@ -1,27 +0,0 @@
|
||||
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)
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
+12
-2
@@ -1,9 +1,12 @@
|
||||
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
|
||||
@@ -13,15 +16,22 @@ 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 backendModeItem(appMode: AppMode, onClick: () -> Unit): SelectionItem {
|
||||
fun appModeItem(appMode: AppMode, onClick: () -> Unit): SelectionItem {
|
||||
val context = LocalContext.current
|
||||
return SelectionItem(
|
||||
leading = { Icon(ImageVector.vectorResource(R.drawable.sdk), contentDescription = null) },
|
||||
trailing = {
|
||||
Icon(Icons.Outlined.ExpandMore, contentDescription = stringResource(R.string.select))
|
||||
IconButton(onClick = onClick) {
|
||||
Icon(
|
||||
Icons.Outlined.ExpandMore,
|
||||
contentDescription = stringResource(R.string.select),
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
}
|
||||
},
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.backend_mode), SelectionLabelType.TITLE)
|
||||
+3
-5
@@ -7,14 +7,12 @@ 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(navController: NavController): SelectionItem {
|
||||
fun appearanceItem(onClick: () -> Unit): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.AutoMirrored.Outlined.ViewQuilt, contentDescription = null) },
|
||||
title = {
|
||||
@@ -24,7 +22,7 @@ fun appearanceItem(navController: NavController): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
trailing = { ForwardButton { navController.navigate(Route.Appearance) } },
|
||||
onClick = { navController.navigate(Route.Appearance) },
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
+18
-5
@@ -6,18 +6,29 @@ 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.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asString
|
||||
|
||||
@Composable
|
||||
fun dnsSettingsItem(navController: NavController): SelectionItem {
|
||||
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
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Dns, null) },
|
||||
trailing = { ForwardButton { navController.navigate(Route.Dns) } },
|
||||
trailing = { ForwardButton { click() } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.dns_settings),
|
||||
@@ -25,6 +36,8 @@ fun dnsSettingsItem(navController: NavController): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { navController.navigate(Route.Dns) },
|
||||
onClick = click,
|
||||
disabledReason =
|
||||
context.getString(R.string.mode_disabled_template, appMode.asString(context)),
|
||||
)
|
||||
}
|
||||
|
||||
+3
-5
@@ -7,17 +7,15 @@ 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(navController: NavController): SelectionItem {
|
||||
fun proxYSettingsItem(onClick: () -> Unit): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(ImageVector.vectorResource(R.drawable.proxy), null) },
|
||||
trailing = { ForwardButton { navController.navigate(Route.ProxySettings) } },
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.proxy_settings),
|
||||
@@ -25,6 +23,6 @@ fun proxYSettingsItem(navController: NavController): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { navController.navigate(Route.ProxySettings) },
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
+3
-5
@@ -5,22 +5,20 @@ 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(navController: NavController): SelectionItem {
|
||||
fun readLogsItem(onClick: () -> Unit): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Filled.ViewTimeline, contentDescription = null) },
|
||||
title = {
|
||||
SelectionItemLabel(stringResource(R.string.read_logs), SelectionLabelType.TITLE)
|
||||
},
|
||||
trailing = { ForwardButton { navController.navigate(Route.Logs) } },
|
||||
onClick = { navController.navigate(Route.Logs) },
|
||||
trailing = { ForwardButton { onClick() } },
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
+3
-5
@@ -7,17 +7,15 @@ 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(navController: NavController): SelectionItem {
|
||||
fun systemFeaturesItem(onClick: () -> Unit): SelectionItem {
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.Android, null) },
|
||||
trailing = { ForwardButton { navController.navigate(Route.SystemFeatures) } },
|
||||
trailing = { ForwardButton { onClick.invoke() } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.system_features),
|
||||
@@ -25,6 +23,6 @@ fun systemFeaturesItem(navController: NavController): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { navController.navigate(Route.SystemFeatures) },
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
+22
-5
@@ -6,18 +6,32 @@ 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.ui.navigation.Route
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asString
|
||||
|
||||
@Composable
|
||||
fun tunnelMonitoringItem(navController: NavController): SelectionItem {
|
||||
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
|
||||
return SelectionItem(
|
||||
leading = { Icon(Icons.Outlined.MonitorHeart, null) },
|
||||
trailing = { ForwardButton { navController.navigate(Route.TunnelMonitoring) } },
|
||||
trailing = { ForwardButton { click() } },
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(R.string.tunnel_monitoring),
|
||||
@@ -25,6 +39,9 @@ fun tunnelMonitoringItem(navController: NavController): SelectionItem {
|
||||
MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { navController.navigate(Route.TunnelMonitoring) },
|
||||
isEnabled = enabled,
|
||||
onClick = click,
|
||||
disabledReason =
|
||||
context.getString(R.string.mode_disabled_template, appMode.asString(context)),
|
||||
)
|
||||
}
|
||||
|
||||
-13
@@ -14,7 +14,6 @@ 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
|
||||
@@ -25,26 +24,14 @@ 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),
|
||||
|
||||
+7
-20
@@ -4,8 +4,6 @@ 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.*
|
||||
@@ -15,39 +13,28 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontStyle
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.hilt.lifecycle.viewmodel.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.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.viewmodel.LoggerViewModel
|
||||
import org.orbitmvi.orbit.compose.collectSideEffect
|
||||
|
||||
@Composable
|
||||
fun LogsScreen(viewModel: LoggerViewModel = hiltViewModel()) {
|
||||
val sharedViewModel = LocalSharedVm.current
|
||||
val sharedAppViewModel = LocalSharedVm.current
|
||||
val loggerState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
|
||||
|
||||
val lazyColumnListState = rememberLazyListState()
|
||||
var isAutoScrolling by rememberSaveable { mutableStateOf(true) }
|
||||
var lastScrollPosition by rememberSaveable() { mutableIntStateOf(0) }
|
||||
var lastScrollPosition by rememberSaveable { mutableIntStateOf(0) }
|
||||
var showLogsSheet by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
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
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
sharedAppViewModel.collectSideEffect { sideEffect ->
|
||||
if (sideEffect is LocalSideEffect.Sheet.LoggerActions) showLogsSheet = true
|
||||
}
|
||||
|
||||
LaunchedEffect(isAutoScrolling) {
|
||||
|
||||
-13
@@ -14,7 +14,6 @@ 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
|
||||
@@ -22,28 +21,16 @@ 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),
|
||||
|
||||
+81
-58
@@ -1,14 +1,15 @@
|
||||
import androidx.compose.foundation.layout.*
|
||||
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.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
|
||||
@@ -18,13 +19,12 @@ import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.text.input.VisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
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,53 +32,68 @@ 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.state.NavbarState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
|
||||
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()
|
||||
|
||||
val proxySettings by remember { derivedStateOf { proxySettingsState.proxySettings } }
|
||||
if (!proxySettingsState.stateInitialized) return
|
||||
|
||||
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 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) }
|
||||
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
|
||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||
|
||||
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,
|
||||
)
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
sharedViewModel.collectSideEffect { sideEffect ->
|
||||
when (sideEffect) {
|
||||
LocalSideEffect.SaveChanges -> {
|
||||
viewModel.save(
|
||||
AppProxySettings(
|
||||
socks5ProxyEnabled = socks5Enabled,
|
||||
socks5ProxyBindAddress = socksBindAddress,
|
||||
httpProxyEnabled = httpEnabled,
|
||||
httpProxyBindAddress = httpBindAddress,
|
||||
proxyUsername = proxyUsername,
|
||||
proxyPassword = proxyPassword,
|
||||
)
|
||||
)
|
||||
}
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
SecureScreenFromRecording()
|
||||
@@ -99,17 +114,15 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
checked = proxySettings.socks5ProxyEnabled,
|
||||
onClick = { viewModel.setEnableSocks5(it) },
|
||||
)
|
||||
ScaledSwitch(checked = socks5Enabled, onClick = { socks5Enabled = it })
|
||||
},
|
||||
onClick = { viewModel.setEnableSocks5(!proxySettings.socks5ProxyEnabled) },
|
||||
onClick = { socks5Enabled = !socks5Enabled },
|
||||
)
|
||||
)
|
||||
)
|
||||
if (proxySettings.socks5ProxyEnabled) {
|
||||
if (socks5Enabled) {
|
||||
ConfigurationTextBox(
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.defaults_to_template,
|
||||
@@ -118,7 +131,11 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
label = stringResource(R.string.socks_5_bind_address),
|
||||
value = socksBindAddress,
|
||||
isError = proxySettingsState.isSocks5BindAddressError,
|
||||
onValueChange = { socksBindAddress = it },
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isSocks5BindAddressError)
|
||||
viewModel.clearSocks5BindError()
|
||||
socksBindAddress = it
|
||||
},
|
||||
)
|
||||
}
|
||||
SurfaceSelectionGroupButton(
|
||||
@@ -132,16 +149,13 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
checked = proxySettings.httpProxyEnabled,
|
||||
onClick = { viewModel.setEnableHttp(it) },
|
||||
)
|
||||
ScaledSwitch(checked = httpEnabled, onClick = { httpEnabled = it })
|
||||
},
|
||||
onClick = { viewModel.setEnableHttp(!proxySettings.httpProxyEnabled) },
|
||||
onClick = { httpEnabled = !httpEnabled },
|
||||
)
|
||||
)
|
||||
)
|
||||
if (proxySettings.httpProxyEnabled) {
|
||||
if (httpEnabled) {
|
||||
ConfigurationTextBox(
|
||||
hint =
|
||||
stringResource(
|
||||
@@ -151,14 +165,17 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
label = stringResource(R.string.http_bind_address),
|
||||
value = httpBindAddress,
|
||||
isError = proxySettingsState.isHttpBindAddressError,
|
||||
onValueChange = { httpBindAddress = it },
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isSocks5BindAddressError) viewModel.clearHttpBindError()
|
||||
httpBindAddress = it
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
)
|
||||
}
|
||||
if (proxySettings.httpProxyEnabled || proxySettings.socks5ProxyEnabled) {
|
||||
if (socks5Enabled || httpEnabled) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
) {
|
||||
GroupLabel(
|
||||
stringResource(
|
||||
@@ -168,23 +185,29 @@ fun ProxySettingsScreen(viewModel: ProxySettingsViewModel = hiltViewModel()) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = proxyUsername,
|
||||
onValueChange = { proxyUsername = it },
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isUserNameError) viewModel.clearUsernameError()
|
||||
proxyUsername = it
|
||||
},
|
||||
label = stringResource(R.string.username),
|
||||
isError = proxySettingsState.isUserNameError,
|
||||
hint = "",
|
||||
keyboardActions = keyboardActions,
|
||||
keyboardOptions = keyboardOptions,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = proxyPassword,
|
||||
onValueChange = { proxyPassword = it },
|
||||
onValueChange = {
|
||||
if (proxySettingsState.isUserNameError) viewModel.clearPasswordError()
|
||||
proxyPassword = it
|
||||
},
|
||||
label = stringResource(R.string.password),
|
||||
isError = proxySettingsState.isPasswordError,
|
||||
hint = "",
|
||||
keyboardActions = keyboardActions,
|
||||
keyboardOptions = keyboardOptions,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
trailing = {
|
||||
IconButton(onClick = { passwordVisible = !passwordVisible }) {
|
||||
Icon(
|
||||
|
||||
-16
@@ -6,41 +6,25 @@ 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.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.LaunchButton
|
||||
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 = { ForwardButton { context.launchVpnSettings() } },
|
||||
trailing = { LaunchButton { context.launchVpnSettings() } },
|
||||
onClick = { context.launchVpnSettings() },
|
||||
)
|
||||
}
|
||||
|
||||
+9
-14
@@ -6,8 +6,11 @@ import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.LinearProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
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.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.StrokeCap
|
||||
@@ -20,14 +23,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
|
||||
@@ -37,21 +40,11 @@ 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() },
|
||||
@@ -143,7 +136,9 @@ fun SupportScreen(viewModel: SupportViewModel) {
|
||||
stringResource(R.string.thank_you),
|
||||
modifier = Modifier.padding(horizontal = 12.dp).padding(bottom = 12.dp),
|
||||
)
|
||||
UpdateSection(onUpdateCheck = { viewModel.checkForUpdate() })
|
||||
UpdateSection { viewModel.checkForUpdate() }
|
||||
SectionDivider()
|
||||
DonateSection { navController.navigate(Route.Donate) }
|
||||
SectionDivider()
|
||||
GeneralSupportOptions(navController)
|
||||
SectionDivider()
|
||||
|
||||
+72
-104
@@ -2,7 +2,6 @@ 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
|
||||
@@ -10,15 +9,13 @@ 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.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.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchSupportEmail
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
|
||||
@@ -26,106 +23,77 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
fun ContactSupportOptions(context: android.content.Context) {
|
||||
SurfaceSelectionGroupButton(
|
||||
items =
|
||||
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)) },
|
||||
listOf(
|
||||
SelectionItem(
|
||||
leading = {
|
||||
Icon(
|
||||
ImageVector.vectorResource(R.drawable.matrix),
|
||||
contentDescription = null,
|
||||
Modifier.size(iconSize),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
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
@@ -0,0 +1,29 @@
|
||||
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,
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
+3
-2
@@ -11,6 +11,7 @@ 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
|
||||
@@ -34,7 +35,7 @@ fun GeneralSupportOptions(navController: NavController) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton {
|
||||
LaunchButton {
|
||||
context.openWebUrl(context.getString(R.string.docs_url))
|
||||
}
|
||||
},
|
||||
@@ -51,7 +52,7 @@ fun GeneralSupportOptions(navController: NavController) {
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton {
|
||||
LaunchButton {
|
||||
context.openWebUrl(context.getString(R.string.privacy_policy_url))
|
||||
}
|
||||
},
|
||||
|
||||
+10
-6
@@ -5,6 +5,7 @@ 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
|
||||
@@ -12,9 +13,15 @@ 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 = {}) {
|
||||
fun UpdateSection(onUpdateCheck: () -> Unit) {
|
||||
val clipboardManager = rememberClipboardHelper()
|
||||
val version = remember {
|
||||
"v${BuildConfig.VERSION_NAME +
|
||||
if(BuildConfig.DEBUG) "-debug" else "" }"
|
||||
}
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
@@ -28,11 +35,7 @@ fun UpdateSection(onUpdateCheck: () -> Unit = {}) {
|
||||
description = {
|
||||
Column {
|
||||
SelectionItemLabel(
|
||||
stringResource(
|
||||
R.string.version_template,
|
||||
"v${BuildConfig.VERSION_NAME +
|
||||
if(BuildConfig.DEBUG) "-debug" else "" }",
|
||||
),
|
||||
stringResource(R.string.version_template, version),
|
||||
SelectionLabelType.DESCRIPTION,
|
||||
)
|
||||
SelectionItemLabel(
|
||||
@@ -42,6 +45,7 @@ fun UpdateSection(onUpdateCheck: () -> Unit = {}) {
|
||||
}
|
||||
},
|
||||
onClick = onUpdateCheck,
|
||||
onLongPress = { clipboardManager.copy(version) },
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
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
@@ -0,0 +1,80 @@
|
||||
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
@@ -0,0 +1,97 @@
|
||||
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
@@ -0,0 +1,42 @@
|
||||
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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
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,
|
||||
address = R.string.litecoin_address,
|
||||
icon = R.drawable.ltc,
|
||||
),
|
||||
Address(
|
||||
name = R.string.ecash,
|
||||
address = R.string.ecash_address,
|
||||
icon = R.drawable.ecash,
|
||||
),
|
||||
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,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user