mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cb3422a3cd | |||
| 44353df400 | |||
| 0da6e3e82c | |||
| dbaf0312f8 | |||
| 7cf7817cea | |||
| 133bd6caf7 | |||
| 25b986ef2f | |||
| fe45a2fad9 | |||
| a3eb53f90f | |||
| 246916ab0d | |||
| 7f89dcaab0 | |||
| 1d2b305047 | |||
| 0201523262 | |||
| fcc1c1e7a1 | |||
| cd82b83789 | |||
| 9f1af0140f | |||
| 14cd49983a | |||
| 40dd14ca16 | |||
| 048bcd4ad7 | |||
| 94bd23f2a4 | |||
| 7285af5c2d | |||
| 11efad703c | |||
| b81f43c7cf | |||
| 68865843dc | |||
| 62daf138dd | |||
| 1120a8fc81 | |||
| ab10be7266 | |||
| 88f9d43e51 | |||
| 2877757396 | |||
| c657c62640 | |||
| d84d9df57d | |||
| 6762d4733e | |||
| add18d5cef | |||
| c5b42b55c3 | |||
| 670d9d680c | |||
| bbfc0e2fab |
@@ -174,7 +174,7 @@ jobs:
|
||||
|
||||
# Save the APK after the Build job is complete to publish it as a Github release in the next job
|
||||
- name: Upload APK
|
||||
uses: actions/upload-artifact@v4.4.3
|
||||
uses: actions/upload-artifact@v4.5.0
|
||||
with:
|
||||
name: wgtunnel
|
||||
path: ${{ env.APK_PATH }}
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
<activity
|
||||
android:name=".ui.MainActivity"
|
||||
android:exported="true"
|
||||
android:windowSoftInputMode="adjustResize"
|
||||
android:theme="@style/Theme.WireguardAutoTunnel"
|
||||
android:configChanges="orientation|screenSize|keyboardHidden"
|
||||
>
|
||||
@@ -78,9 +79,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
|
||||
</intent-filter>
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity>
|
||||
<activity
|
||||
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.module.MainDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||
@@ -17,6 +18,7 @@ import dagger.hilt.android.HiltAndroidApp
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -43,6 +45,10 @@ class WireGuardAutoTunnel : Application() {
|
||||
@IoDispatcher
|
||||
lateinit var ioDispatcher: CoroutineDispatcher
|
||||
|
||||
@Inject
|
||||
@MainDispatcher
|
||||
lateinit var mainDispatcher: CoroutineDispatcher
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
instance = this
|
||||
@@ -59,7 +65,11 @@ class WireGuardAutoTunnel : Application() {
|
||||
} else {
|
||||
Timber.plant(ReleaseTree())
|
||||
}
|
||||
|
||||
applicationScope.launch {
|
||||
withContext(mainDispatcher) {
|
||||
if (appStateRepository.isLocalLogsEnabled() && !isRunningOnTv()) logReader.initialize()
|
||||
}
|
||||
if (!settingsRepository.getSettings().isKernelEnabled) {
|
||||
tunnelService.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||
}
|
||||
@@ -67,14 +77,6 @@ class WireGuardAutoTunnel : Application() {
|
||||
LocaleUtil.changeLocale(it)
|
||||
}
|
||||
}
|
||||
if (!isRunningOnTv()) {
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
if (appStateRepository.isLocalLogsEnabled()) {
|
||||
Timber.d("Starting logger")
|
||||
logReader.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onTerminate() {
|
||||
|
||||
@@ -69,6 +69,10 @@ data class TunnelConfig(
|
||||
return configFromAmQuick(if (amQuick != "") amQuick else wgQuick)
|
||||
}
|
||||
|
||||
fun toWgConfig(): Config {
|
||||
return configFromWgQuick(wgQuick)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun configFromWgQuick(wgQuick: String): Config {
|
||||
|
||||
@@ -2,9 +2,11 @@ package com.zaneschepke.wireguardautotunnel.module
|
||||
|
||||
import android.content.Context
|
||||
import com.zaneschepke.logcatter.LogReader
|
||||
import com.zaneschepke.logcatter.LogcatCollector
|
||||
import com.zaneschepke.logcatter.LogcatReader
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||
import com.zaneschepke.wireguardautotunnel.service.shortcut.DynamicShortcutManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutManager
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
@@ -27,7 +29,7 @@ class AppModule {
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideLogCollect(@ApplicationContext context: Context): LogReader {
|
||||
return LogcatCollector.init(context = context)
|
||||
return LogcatReader.init(storageDir = context.filesDir.absolutePath)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@@ -35,4 +37,10 @@ class AppModule {
|
||||
fun provideNotificationService(@ApplicationContext context: Context): NotificationService {
|
||||
return WireGuardNotification(context)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideShortcutManager(@ApplicationContext context: Context, @IoDispatcher ioDispatcher: CoroutineDispatcher): ShortcutManager {
|
||||
return DynamicShortcutManager(context, ioDispatcher)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.zaneschepke.wireguardautotunnel.module
|
||||
|
||||
import javax.inject.Qualifier
|
||||
|
||||
@Qualifier
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class Wifi
|
||||
|
||||
@Qualifier
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class MobileData
|
||||
|
||||
@Qualifier
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class Ethernet
|
||||
@@ -13,15 +13,19 @@ import dagger.hilt.android.scopes.ServiceScoped
|
||||
@Module
|
||||
@InstallIn(ServiceComponent::class)
|
||||
abstract class ServiceModule {
|
||||
@Binds
|
||||
@ServiceScoped
|
||||
abstract fun provideWifiService(wifiService: WifiService): NetworkService<WifiService>
|
||||
|
||||
@Binds
|
||||
@Wifi
|
||||
@ServiceScoped
|
||||
abstract fun provideMobileDataService(mobileDataService: MobileDataService): NetworkService<MobileDataService>
|
||||
abstract fun provideWifiService(wifiService: WifiService): NetworkService
|
||||
|
||||
@Binds
|
||||
@MobileData
|
||||
@ServiceScoped
|
||||
abstract fun provideEthernetService(ethernetService: EthernetService): NetworkService<EthernetService>
|
||||
abstract fun provideMobileDataService(mobileDataService: MobileDataService): NetworkService
|
||||
|
||||
@Binds
|
||||
@Ethernet
|
||||
@ServiceScoped
|
||||
abstract fun provideEthernetService(ethernetService: EthernetService): NetworkService
|
||||
}
|
||||
|
||||
+4
-3
@@ -3,6 +3,7 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.AutoTunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.service.tile.AutoTunnelControlTile
|
||||
@@ -58,12 +59,12 @@ class ServiceManager
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun startBackgroundService() {
|
||||
suspend fun startBackgroundService(tunnelConfig: TunnelConfig?) {
|
||||
if (backgroundService.isCompleted) return
|
||||
kotlin.runCatching {
|
||||
startService(TunnelBackgroundService::class.java, true)
|
||||
backgroundService.await()
|
||||
backgroundService.getCompleted().start()
|
||||
backgroundService.getCompleted().start(tunnelConfig)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
}
|
||||
@@ -85,7 +86,7 @@ class ServiceManager
|
||||
}
|
||||
}
|
||||
|
||||
fun updateAutoTunnelTile() {
|
||||
private fun updateAutoTunnelTile() {
|
||||
if (autoTunnelTile.isCompleted) {
|
||||
autoTunnelTile.getCompleted().updateTileState()
|
||||
} else {
|
||||
|
||||
+10
-7
@@ -6,6 +6,8 @@ import android.os.IBinder
|
||||
import androidx.core.app.ServiceCompat
|
||||
import androidx.lifecycle.LifecycleService
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
@@ -24,12 +26,11 @@ class TunnelBackgroundService : LifecycleService() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
start()
|
||||
serviceManager.backgroundService.complete(this)
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
super.onBind(intent)
|
||||
// We don't provide binding, so return null
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -38,11 +39,11 @@ class TunnelBackgroundService : LifecycleService() {
|
||||
return super.onStartCommand(intent, flags, startId)
|
||||
}
|
||||
|
||||
fun start() {
|
||||
fun start(tunnelConfig: TunnelConfig?) {
|
||||
ServiceCompat.startForeground(
|
||||
this,
|
||||
NotificationService.KERNEL_SERVICE_NOTIFICATION_ID,
|
||||
createNotification(),
|
||||
createNotification(tunnelConfig),
|
||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||
)
|
||||
}
|
||||
@@ -57,11 +58,13 @@ class TunnelBackgroundService : LifecycleService() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun createNotification(): Notification {
|
||||
private fun createNotification(tunnelConfig: TunnelConfig?): Notification {
|
||||
return notificationService.createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
getString(R.string.tunnel_running),
|
||||
description = "",
|
||||
title = "${getString(R.string.tunnel_running)} - ${tunnelConfig?.name}",
|
||||
actions = listOf(
|
||||
notificationService.createNotificationAction(NotificationAction.TUNNEL_OFF),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+33
-42
@@ -1,7 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.NetworkCapabilities
|
||||
import android.os.IBinder
|
||||
import android.os.PowerManager
|
||||
import androidx.core.app.ServiceCompat
|
||||
@@ -13,17 +12,16 @@ import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.AppShell
|
||||
import com.zaneschepke.wireguardautotunnel.module.Ethernet
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.module.MainImmediateDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.module.MobileData
|
||||
import com.zaneschepke.wireguardautotunnel.module.Wifi
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.AutoTunnelEvent
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.AutoTunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.NetworkState
|
||||
import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
|
||||
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkStatus
|
||||
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||
@@ -31,19 +29,21 @@ import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.cancelWithMessage
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.onNotRunning
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.debounce
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.filterNot
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -61,13 +61,16 @@ class AutoTunnelService : LifecycleService() {
|
||||
lateinit var rootShell: Provider<RootShell>
|
||||
|
||||
@Inject
|
||||
lateinit var wifiService: NetworkService<WifiService>
|
||||
@Wifi
|
||||
lateinit var wifiService: NetworkService
|
||||
|
||||
@Inject
|
||||
lateinit var mobileDataService: NetworkService<MobileDataService>
|
||||
@MobileData
|
||||
lateinit var mobileDataService: NetworkService
|
||||
|
||||
@Inject
|
||||
lateinit var ethernetService: NetworkService<EthernetService>
|
||||
@Ethernet
|
||||
lateinit var ethernetService: NetworkService
|
||||
|
||||
@Inject
|
||||
lateinit var appDataRepository: Provider<AppDataRepository>
|
||||
@@ -89,7 +92,9 @@ class AutoTunnelService : LifecycleService() {
|
||||
@MainImmediateDispatcher
|
||||
lateinit var mainImmediateDispatcher: CoroutineDispatcher
|
||||
|
||||
private val autoTunnelStateFlow = MutableStateFlow(AutoTunnelState())
|
||||
private val defaultState = AutoTunnelState()
|
||||
|
||||
private val autoTunnelStateFlow = MutableStateFlow(defaultState)
|
||||
|
||||
private var wakeLock: PowerManager.WakeLock? = null
|
||||
|
||||
@@ -183,6 +188,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
private fun startPingStateJob() = lifecycleScope.launch {
|
||||
autoTunnelStateFlow.collect {
|
||||
if (it == defaultState) return@collect
|
||||
if (it.isPingEnabled()) {
|
||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
||||
} else {
|
||||
@@ -236,8 +242,9 @@ class AutoTunnelService : LifecycleService() {
|
||||
) { double, networkState ->
|
||||
AutoTunnelState(tunnelService.get().vpnState.value, networkState, double.first, double.second)
|
||||
}.collect { state ->
|
||||
Timber.d("Network state: ${state.networkState}")
|
||||
autoTunnelStateFlow.update {
|
||||
it.copy(state.vpnState, state.networkState, state.settings, state.tunnels)
|
||||
it.copy(vpnState = state.vpnState, networkState = state.networkState, settings = state.settings, tunnels = state.tunnels)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,54 +254,38 @@ class AutoTunnelService : LifecycleService() {
|
||||
pingJob = null
|
||||
}
|
||||
|
||||
@OptIn(FlowPreview::class)
|
||||
private fun combineNetworkEventsJob(): Flow<NetworkState> {
|
||||
return combine(
|
||||
wifiService.networkStatus,
|
||||
mobileDataService.networkStatus,
|
||||
ethernetService.networkStatus,
|
||||
) { wifi, mobileData, ethernet ->
|
||||
wifiService.status,
|
||||
mobileDataService.status,
|
||||
) { wifi, mobileData ->
|
||||
NetworkState(
|
||||
wifi.isConnected,
|
||||
mobileData.isConnected,
|
||||
ethernet.isConnected,
|
||||
when (wifi) {
|
||||
is NetworkStatus.CapabilitiesChanged -> getWifiSSID(wifi.networkCapabilities)
|
||||
is NetworkStatus.Available -> autoTunnelStateFlow.value.networkState.wifiName
|
||||
is NetworkStatus.Unavailable -> null
|
||||
},
|
||||
wifi.available,
|
||||
mobileData.available,
|
||||
false,
|
||||
wifi.name,
|
||||
)
|
||||
}.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null }
|
||||
}.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null }.debounce(500L)
|
||||
}
|
||||
|
||||
private fun combineSettings(): Flow<Pair<Settings, TunnelConfigs>> {
|
||||
return combine(
|
||||
appDataRepository.get().settings.getSettingsFlow(),
|
||||
appDataRepository.get().tunnels.getTunnelConfigsFlow().distinctUntilChanged { old, new ->
|
||||
old.map { it.isActive } != new.map { it.isActive }
|
||||
appDataRepository.get().tunnels.getTunnelConfigsFlow().map { tunnels ->
|
||||
// isActive is ignored for equality checks so user can manually toggle off tunnel with auto-tunnel
|
||||
tunnels.map { it.copy(isActive = false) }
|
||||
},
|
||||
) { settings, tunnels ->
|
||||
Pair(settings, tunnels)
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
|
||||
private suspend fun getWifiSSID(networkCapabilities: NetworkCapabilities): String? {
|
||||
return withContext(ioDispatcher) {
|
||||
with(autoTunnelStateFlow.value.settings) {
|
||||
if (isWifiNameByShellEnabled) return@withContext rootShell.get().getCurrentWifiName()
|
||||
wifiService.getNetworkName(networkCapabilities)
|
||||
}.also {
|
||||
if (it?.contains(Constants.UNREADABLE_SSID) == true) {
|
||||
Timber.w("SSID unreadable: missing permissions")
|
||||
} else {
|
||||
Timber.i("Detected valid SSID")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(FlowPreview::class)
|
||||
private fun startAutoTunnelJob() = lifecycleScope.launch(ioDispatcher) {
|
||||
Timber.i("Starting auto-tunnel network event watcher")
|
||||
autoTunnelStateFlow.collect { watcherState ->
|
||||
autoTunnelStateFlow.debounce(1000L).collect { watcherState ->
|
||||
if (watcherState == defaultState) return@collect
|
||||
Timber.d("New auto tunnel state emitted")
|
||||
when (val event = watcherState.asAutoTunnelEvent()) {
|
||||
is AutoTunnelEvent.Start -> tunnelService.get().startTunnel(
|
||||
|
||||
-2
@@ -5,7 +5,6 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
||||
import timber.log.Timber
|
||||
|
||||
data class AutoTunnelState(
|
||||
val vpnState: VpnState = VpnState(),
|
||||
@@ -82,7 +81,6 @@ data class AutoTunnelState(
|
||||
}
|
||||
|
||||
private fun startOnUntrustedWifi(): Boolean {
|
||||
Timber.d("Is tunnel on wifi enabled ${settings.isTunnelOnWifiEnabled}")
|
||||
return isWifiActive() && settings.isTunnelOnWifiEnabled && vpnState.status.isDown() && !isCurrentSSIDTrusted()
|
||||
}
|
||||
|
||||
|
||||
-116
@@ -1,116 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.network
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkRequest
|
||||
import android.net.wifi.WifiManager
|
||||
import android.os.Build
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.conflate
|
||||
import kotlinx.coroutines.flow.map
|
||||
import timber.log.Timber
|
||||
|
||||
abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
||||
val context: Context,
|
||||
networkCapability: Int,
|
||||
) : NetworkService<T> {
|
||||
private val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
val wifiManager =
|
||||
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||
|
||||
fun checkHasCapability(networkCapability: Int): Boolean {
|
||||
val network = connectivityManager.activeNetwork
|
||||
val networkCapabilities = connectivityManager.getNetworkCapabilities(network)
|
||||
return networkCapabilities?.hasTransport(networkCapability) == true
|
||||
}
|
||||
|
||||
override val networkStatus =
|
||||
callbackFlow {
|
||||
if (!checkHasCapability(networkCapability)) {
|
||||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
val networkStatusCallback =
|
||||
when (Build.VERSION.SDK_INT) {
|
||||
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
||||
object :
|
||||
ConnectivityManager.NetworkCallback(
|
||||
FLAG_INCLUDE_LOCATION_INFO,
|
||||
) {
|
||||
override fun onAvailable(network: Network) {
|
||||
trySend(NetworkStatus.Available(network))
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
networkCapabilities,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
trySend(NetworkStatus.Available(network))
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
networkCapabilities,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val request =
|
||||
NetworkRequest.Builder()
|
||||
.addTransportType(networkCapability)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||
.build()
|
||||
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||
|
||||
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||
}.catch {
|
||||
Timber.e(it)
|
||||
// conflate for backpressure
|
||||
}.conflate()
|
||||
}
|
||||
|
||||
inline fun <Result> Flow<NetworkStatus>.map(
|
||||
crossinline onUnavailable: suspend () -> Result,
|
||||
crossinline onAvailable: suspend (network: Network) -> Result,
|
||||
crossinline onCapabilitiesChanged:
|
||||
suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result,
|
||||
): Flow<Result> = map { status ->
|
||||
when (status) {
|
||||
is NetworkStatus.Unavailable -> onUnavailable()
|
||||
is NetworkStatus.Available -> onAvailable(status.network)
|
||||
is NetworkStatus.CapabilitiesChanged ->
|
||||
onCapabilitiesChanged(
|
||||
status.network,
|
||||
status.networkCapabilities,
|
||||
)
|
||||
}
|
||||
}
|
||||
+50
-4
@@ -1,18 +1,64 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.network
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkRequest
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
class EthernetService
|
||||
@Inject
|
||||
constructor(
|
||||
@ApplicationContext context: Context,
|
||||
) :
|
||||
BaseNetworkService<EthernetService>(context, NetworkCapabilities.TRANSPORT_ETHERNET) {
|
||||
) : NetworkService {
|
||||
|
||||
override fun isNetworkSecure(): Boolean {
|
||||
return true
|
||||
private val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
override val status = callbackFlow {
|
||||
val networkStatusCallback = object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
trySend(NetworkStatus.Available(network))
|
||||
}
|
||||
override fun onLost(network: Network) {
|
||||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
networkCapabilities,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
val request =
|
||||
NetworkRequest.Builder()
|
||||
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||
.build()
|
||||
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||
|
||||
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||
}.onStart {
|
||||
// needed for services that are not yet available as it will impact later combine flows if we don't emit
|
||||
emit(NetworkStatus.Unavailable())
|
||||
}.catch {
|
||||
Timber.e(it)
|
||||
emit(NetworkStatus.Unavailable())
|
||||
}.map {
|
||||
when (it) {
|
||||
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null)
|
||||
is NetworkStatus.Unavailable -> Status(false, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+50
-4
@@ -1,17 +1,63 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.network
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkRequest
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
class MobileDataService
|
||||
@Inject
|
||||
constructor(
|
||||
@ApplicationContext context: Context,
|
||||
) :
|
||||
BaseNetworkService<MobileDataService>(context, NetworkCapabilities.TRANSPORT_CELLULAR) {
|
||||
override fun isNetworkSecure(): Boolean {
|
||||
return false
|
||||
) : NetworkService {
|
||||
private val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
override val status = callbackFlow {
|
||||
val networkStatusCallback = object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
trySend(NetworkStatus.Available(network))
|
||||
}
|
||||
override fun onLost(network: Network) {
|
||||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
networkCapabilities,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
val request =
|
||||
NetworkRequest.Builder()
|
||||
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||
.build()
|
||||
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||
|
||||
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||
}.onStart {
|
||||
// needed for services that are not yet available as it will impact later combine flows if we don't emit
|
||||
emit(NetworkStatus.Unavailable())
|
||||
}.catch {
|
||||
Timber.e(it)
|
||||
emit(NetworkStatus.Unavailable())
|
||||
}.map {
|
||||
when (it) {
|
||||
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null)
|
||||
is NetworkStatus.Unavailable -> Status(false, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
-8
@@ -1,14 +1,27 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.network
|
||||
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
interface NetworkService<T> {
|
||||
fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
||||
return null
|
||||
}
|
||||
|
||||
fun isNetworkSecure(): Boolean
|
||||
|
||||
val networkStatus: Flow<NetworkStatus>
|
||||
interface NetworkService {
|
||||
val status: Flow<Status>
|
||||
}
|
||||
|
||||
inline fun <Result> Flow<NetworkStatus>.map(
|
||||
crossinline onUnavailable: suspend () -> Result,
|
||||
crossinline onAvailable: suspend (network: Network) -> Result,
|
||||
crossinline onCapabilitiesChanged:
|
||||
suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result,
|
||||
): Flow<Result> = map { status ->
|
||||
when (status) {
|
||||
is NetworkStatus.Unavailable -> onUnavailable()
|
||||
is NetworkStatus.Available -> onAvailable(status.network)
|
||||
is NetworkStatus.CapabilitiesChanged ->
|
||||
onCapabilitiesChanged(
|
||||
status.network,
|
||||
status.networkCapabilities,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.network
|
||||
|
||||
data class Status(
|
||||
val available: Boolean,
|
||||
val name: String?,
|
||||
)
|
||||
+116
-9
@@ -1,22 +1,134 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.network
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.ConnectivityManager.NetworkCallback.FLAG_INCLUDE_LOCATION_INFO
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkRequest
|
||||
import android.net.wifi.SupplicantState
|
||||
import android.net.wifi.WifiManager
|
||||
import android.os.Build
|
||||
import com.wireguard.android.util.RootShell
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.AppShell
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.flow.transform
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
|
||||
class WifiService
|
||||
@Inject
|
||||
constructor(
|
||||
@ApplicationContext context: Context,
|
||||
) :
|
||||
BaseNetworkService<WifiService>(context, NetworkCapabilities.TRANSPORT_WIFI) {
|
||||
@ApplicationContext private val context: Context,
|
||||
private val settingsRepository: SettingsRepository,
|
||||
@AppShell private val rootShell: Provider<RootShell>,
|
||||
) : NetworkService {
|
||||
|
||||
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
||||
val mutex = Mutex()
|
||||
|
||||
private var ssid: String? = null
|
||||
private var available: Boolean = false
|
||||
|
||||
private val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
override val status = callbackFlow {
|
||||
val networkStatusCallback =
|
||||
when (Build.VERSION.SDK_INT) {
|
||||
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
||||
object :
|
||||
ConnectivityManager.NetworkCallback(
|
||||
FLAG_INCLUDE_LOCATION_INFO,
|
||||
) {
|
||||
override fun onAvailable(network: Network) {
|
||||
trySend(NetworkStatus.Available(network))
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
networkCapabilities,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
trySend(NetworkStatus.Available(network))
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
networkCapabilities,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val request =
|
||||
NetworkRequest.Builder()
|
||||
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||
.build()
|
||||
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||
|
||||
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||
}.onStart {
|
||||
// needed for services that are not yet available as it will impact later combine flows if we don't emit
|
||||
emit(NetworkStatus.Unavailable())
|
||||
}.catch {
|
||||
Timber.e(it)
|
||||
emit(NetworkStatus.Unavailable())
|
||||
}.transform {
|
||||
when (it) {
|
||||
is NetworkStatus.Available -> mutex.withLock {
|
||||
available = true
|
||||
}
|
||||
is NetworkStatus.CapabilitiesChanged -> mutex.withLock {
|
||||
if (available) {
|
||||
available = false
|
||||
Timber.d("Getting SSID from capabilities")
|
||||
ssid = getNetworkName(it.networkCapabilities)
|
||||
}
|
||||
emit(Status(true, ssid))
|
||||
}
|
||||
is NetworkStatus.Unavailable -> emit(Status(false, null))
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
||||
if (settingsRepository.getSettings().isWifiNameByShellEnabled) return rootShell.get().getCurrentWifiName()
|
||||
var ssid = networkCapabilities.getWifiName()
|
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) {
|
||||
val wifiManager =
|
||||
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
val info = wifiManager.connectionInfo
|
||||
if (info.supplicantState === SupplicantState.COMPLETED) {
|
||||
ssid = info.ssid
|
||||
@@ -24,9 +136,4 @@ constructor(
|
||||
}
|
||||
return ssid?.trim('"')
|
||||
}
|
||||
|
||||
override fun isNetworkSecure(): Boolean {
|
||||
// TODO
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ interface NotificationService {
|
||||
onlyAlertOnce: Boolean = true,
|
||||
): Notification
|
||||
|
||||
fun createNotificationAction(action: NotificationAction): NotificationCompat.Action
|
||||
fun createNotificationAction(notificationAction: NotificationAction): NotificationCompat.Action
|
||||
|
||||
fun remove(notificationId: Int)
|
||||
|
||||
|
||||
+13
-4
@@ -14,6 +14,7 @@ import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.receiver.NotificationActionReceiver
|
||||
import com.zaneschepke.wireguardautotunnel.ui.MainActivity
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -46,6 +47,14 @@ constructor(
|
||||
addAction(it)
|
||||
}
|
||||
setContentTitle(title)
|
||||
setContentIntent(
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
0,
|
||||
Intent(context, MainActivity::class.java),
|
||||
PendingIntent.FLAG_IMMUTABLE,
|
||||
),
|
||||
)
|
||||
setContentText(description)
|
||||
setOnlyAlertOnce(onlyAlertOnce)
|
||||
setOngoing(onGoing)
|
||||
@@ -84,15 +93,15 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun NotificationChannels.asBuilder(): NotificationCompat.Builder {
|
||||
private fun NotificationChannels.asBuilder(): NotificationCompat.Builder {
|
||||
return when (this) {
|
||||
NotificationChannels.VPN -> {
|
||||
NotificationChannels.AUTO_TUNNEL -> {
|
||||
NotificationCompat.Builder(
|
||||
context,
|
||||
context.getString(R.string.auto_tunnel_channel_id),
|
||||
)
|
||||
}
|
||||
NotificationChannels.AUTO_TUNNEL -> {
|
||||
NotificationChannels.VPN -> {
|
||||
NotificationCompat.Builder(
|
||||
context,
|
||||
context.getString(R.string.vpn_channel_id),
|
||||
@@ -101,7 +110,7 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun NotificationChannels.asChannel(): NotificationChannel {
|
||||
private fun NotificationChannels.asChannel(): NotificationChannel {
|
||||
return when (this) {
|
||||
NotificationChannels.VPN -> {
|
||||
NotificationChannel(
|
||||
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.shortcut
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.core.content.pm.ShortcutInfoCompat
|
||||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
import androidx.core.graphics.drawable.IconCompat
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class DynamicShortcutManager(private val context: Context, @IoDispatcher private val ioDispatcher: CoroutineDispatcher) : ShortcutManager {
|
||||
override suspend fun addShortcuts() {
|
||||
withContext(ioDispatcher) {
|
||||
ShortcutManagerCompat.setDynamicShortcuts(context, createShortcuts())
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun removeShortcuts() {
|
||||
withContext(ioDispatcher) {
|
||||
ShortcutManagerCompat.removeDynamicShortcuts(context, createShortcuts().map { it.id })
|
||||
}
|
||||
}
|
||||
|
||||
private fun createShortcuts(): List<ShortcutInfoCompat> {
|
||||
return listOf(
|
||||
buildShortcut(
|
||||
context.getString(R.string.vpn_off),
|
||||
context.getString(R.string.vpn_off),
|
||||
context.getString(R.string.vpn_off),
|
||||
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||
putExtra("className", "WireGuardTunnelService")
|
||||
action = ShortcutsActivity.Action.STOP.name
|
||||
},
|
||||
shortcutIcon = R.drawable.vpn_off,
|
||||
),
|
||||
buildShortcut(
|
||||
context.getString(R.string.vpn_on),
|
||||
context.getString(R.string.vpn_on),
|
||||
context.getString(R.string.vpn_on),
|
||||
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||
putExtra("className", "WireGuardTunnelService")
|
||||
action = ShortcutsActivity.Action.START.name
|
||||
},
|
||||
shortcutIcon = R.drawable.vpn_on,
|
||||
),
|
||||
buildShortcut(
|
||||
context.getString(R.string.start_auto),
|
||||
context.getString(R.string.start_auto),
|
||||
context.getString(R.string.start_auto),
|
||||
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||
putExtra("className", "WireGuardConnectivityWatcherService")
|
||||
action = ShortcutsActivity.Action.START.name
|
||||
},
|
||||
shortcutIcon = R.drawable.auto_play,
|
||||
),
|
||||
buildShortcut(
|
||||
context.getString(R.string.stop_auto),
|
||||
context.getString(R.string.stop_auto),
|
||||
context.getString(R.string.stop_auto),
|
||||
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||
putExtra("className", "WireGuardConnectivityWatcherService")
|
||||
action = ShortcutsActivity.Action.STOP.name
|
||||
},
|
||||
shortcutIcon = R.drawable.auto_pause,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun buildShortcut(id: String, shortLabel: String, longLabel: String, intent: Intent, shortcutIcon: Int): ShortcutInfoCompat {
|
||||
return ShortcutInfoCompat.Builder(context, id)
|
||||
.setShortLabel(shortLabel)
|
||||
.setLongLabel(longLabel)
|
||||
.setIntent(intent)
|
||||
.setIcon(IconCompat.createWithResource(context, shortcutIcon))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.shortcut
|
||||
|
||||
interface ShortcutManager {
|
||||
suspend fun addShortcuts()
|
||||
suspend fun removeShortcuts()
|
||||
}
|
||||
+41
-44
@@ -2,6 +2,7 @@ package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||
|
||||
import com.wireguard.android.backend.Backend
|
||||
import com.wireguard.android.backend.Tunnel.State
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||
@@ -9,7 +10,9 @@ import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.module.Kernel
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService.Companion.VPN_NOTIFICATION_ID
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.AmneziaStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
||||
@@ -29,9 +32,6 @@ import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.amnezia.awg.backend.Tunnel
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService.Companion.VPN_NOTIFICATION_ID
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
@@ -102,6 +102,7 @@ constructor(
|
||||
else -> throw NotImplementedError()
|
||||
}
|
||||
}.onFailure {
|
||||
// TODO add better error message to user, especially for kernel as exceptions contain no details
|
||||
Timber.e(it)
|
||||
}
|
||||
}
|
||||
@@ -113,26 +114,15 @@ constructor(
|
||||
}
|
||||
|
||||
override suspend fun startTunnel(tunnelConfig: TunnelConfig?, background: Boolean) {
|
||||
if (tunnelConfig == null) return
|
||||
withContext(ioDispatcher) {
|
||||
if (isTunnelAlreadyRunning(tunnelConfig)) return@withContext
|
||||
if (tunnelConfig == null || isTunnelAlreadyRunning(tunnelConfig)) return@withContext
|
||||
updateTunnelConfig(tunnelConfig) // need to update this here
|
||||
withServiceActive {
|
||||
onBeforeStart(background)
|
||||
tunnelControlMutex.withLock {
|
||||
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
||||
startActiveTunnelJobs()
|
||||
if (it.isUp()) appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||
with(notificationService) {
|
||||
val notification = createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = "${context.getString(R.string.tunnel_running)} - ${tunnelConfig.name}",
|
||||
actions = listOf(
|
||||
notificationService.createNotificationAction(NotificationAction.TUNNEL_OFF),
|
||||
),
|
||||
)
|
||||
show(VPN_NOTIFICATION_ID, notification)
|
||||
}
|
||||
updateTunnelState(it, tunnelConfig)
|
||||
onTunnelStart(tunnelConfig, background)
|
||||
}
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
@@ -148,10 +138,8 @@ constructor(
|
||||
if (tunnelConfig == null) return@withContext
|
||||
tunnelControlMutex.withLock {
|
||||
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
||||
onTunnelStop(tunnelConfig)
|
||||
updateTunnelState(it, null)
|
||||
onStop(tunnelConfig)
|
||||
notificationService.remove(VPN_NOTIFICATION_ID)
|
||||
stopBackgroundService()
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
}
|
||||
@@ -216,33 +204,44 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun shutDownActiveTunnel() {
|
||||
private suspend fun onBeforeStart(background: Boolean) {
|
||||
with(_vpnState.value) {
|
||||
if (status.isUp()) {
|
||||
stopTunnel()
|
||||
}
|
||||
if (status.isUp()) stopTunnel() else clearJobsAndStats()
|
||||
if (isKernelBackend == true || background) serviceManager.startBackgroundService(tunnelConfig)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startBackgroundService() {
|
||||
serviceManager.startBackgroundService()
|
||||
serviceManager.updateTunnelTile()
|
||||
private suspend fun onTunnelStart(tunnelConfig: TunnelConfig, background: Boolean) {
|
||||
startActiveTunnelJobs()
|
||||
if (_vpnState.value.status.isUp()) {
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||
}
|
||||
if (isKernelBackend == false && !background) launchUserspaceTunnelNotification()
|
||||
}
|
||||
|
||||
private fun stopBackgroundService() {
|
||||
serviceManager.stopBackgroundService()
|
||||
serviceManager.updateTunnelTile()
|
||||
private fun launchUserspaceTunnelNotification() {
|
||||
with(notificationService) {
|
||||
val notification = createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = "${context.getString(R.string.tunnel_running)} - ${_vpnState.value.tunnelConfig?.name}",
|
||||
actions = listOf(
|
||||
notificationService.createNotificationAction(NotificationAction.TUNNEL_OFF),
|
||||
),
|
||||
)
|
||||
show(VPN_NOTIFICATION_ID, notification)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun onBeforeStart(background: Boolean) {
|
||||
shutDownActiveTunnel()
|
||||
resetBackendStatistics()
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (background || settings.isKernelEnabled) startBackgroundService()
|
||||
private suspend fun onTunnelStop(tunnelConfig: TunnelConfig) {
|
||||
runCatching {
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
||||
serviceManager.stopBackgroundService()
|
||||
notificationService.remove(VPN_NOTIFICATION_ID)
|
||||
clearJobsAndStats()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun onStop(tunnelConfig: TunnelConfig) {
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
||||
private fun clearJobsAndStats() {
|
||||
cancelActiveTunnelJobs()
|
||||
resetBackendStatistics()
|
||||
}
|
||||
@@ -301,13 +300,11 @@ constructor(
|
||||
is Backend -> updateBackendStatistics(
|
||||
WireGuardStatistics(backend.getStatistics(this@WireGuardTunnel)),
|
||||
)
|
||||
is org.amnezia.awg.backend.Backend -> {
|
||||
updateBackendStatistics(
|
||||
AmneziaStatistics(
|
||||
backend.getStatistics(this@WireGuardTunnel),
|
||||
),
|
||||
)
|
||||
}
|
||||
is org.amnezia.awg.backend.Backend -> updateBackendStatistics(
|
||||
AmneziaStatistics(
|
||||
backend.getStatistics(this@WireGuardTunnel),
|
||||
),
|
||||
)
|
||||
}
|
||||
delay(VPN_STATISTIC_CHECK_INTERVAL)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.wireguard.android.backend.WgQuickBackend
|
||||
import com.wireguard.android.util.RootShell
|
||||
import com.wireguard.config.Config
|
||||
import com.zaneschepke.logcatter.LogReader
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||
@@ -16,13 +18,19 @@ import com.zaneschepke.wireguardautotunnel.service.tunnel.BackendState
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.InterfaceProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.splittunnel.SplitTunnelApp
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getAllInternetCapablePackages
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.combine
|
||||
@@ -50,6 +58,18 @@ constructor(
|
||||
private val logReader: LogReader,
|
||||
) : ViewModel() {
|
||||
|
||||
private val _popBackStack = MutableSharedFlow<Boolean>()
|
||||
val popBackStack = _popBackStack.asSharedFlow()
|
||||
|
||||
private val _isAppReady = MutableStateFlow(false)
|
||||
val isAppReady = _isAppReady.asStateFlow()
|
||||
|
||||
private val _configurationChange = MutableStateFlow(false)
|
||||
val configurationChange = _configurationChange.asStateFlow()
|
||||
|
||||
private val _splitTunnelApps = MutableStateFlow<List<SplitTunnelApp>>(emptyList())
|
||||
val splitTunnelApps = _splitTunnelApps.asStateFlow()
|
||||
|
||||
val uiState =
|
||||
combine(
|
||||
appDataRepository.settings.getSettingsFlow(),
|
||||
@@ -71,17 +91,13 @@ constructor(
|
||||
AppUiState(),
|
||||
)
|
||||
|
||||
private val _isAppReady = MutableStateFlow(false)
|
||||
val isAppReady = _isAppReady.asStateFlow()
|
||||
|
||||
private val _configurationChange = MutableStateFlow(false)
|
||||
val configurationChange = _configurationChange.asStateFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
initPin()
|
||||
initServices()
|
||||
initTunnel()
|
||||
launch {
|
||||
initTunnel()
|
||||
}
|
||||
appReadyCheck()
|
||||
}
|
||||
}
|
||||
@@ -141,7 +157,6 @@ constructor(
|
||||
}
|
||||
|
||||
private suspend fun onLoggerStop() {
|
||||
logReader.stop()
|
||||
logReader.deleteAndClearLogs()
|
||||
}
|
||||
|
||||
@@ -238,6 +253,7 @@ constructor(
|
||||
if (!isKernelEnabled) {
|
||||
requestRoot().onSuccess {
|
||||
if (!isKernelSupported()) return@onSuccess SnackbarController.showMessage(StringValue.StringResource(R.string.kernel_not_supported))
|
||||
tunnelService.get().setBackendState(BackendState.INACTIVE, emptyList())
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isKernelEnabled = true,
|
||||
@@ -257,7 +273,20 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun requestRoot(): Result<Unit> {
|
||||
suspend fun getEmitSplitTunnelApps(context: Context) {
|
||||
withContext(ioDispatcher) {
|
||||
val apps = context.getAllInternetCapablePackages().filter { it.applicationInfo != null }.map { pack ->
|
||||
SplitTunnelApp(
|
||||
context.packageManager.getApplicationIcon(pack.applicationInfo!!),
|
||||
context.packageManager.getApplicationLabel(pack.applicationInfo!!).toString(),
|
||||
pack.packageName,
|
||||
)
|
||||
}
|
||||
_splitTunnelApps.emit(apps)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun requestRoot(): Result<Unit> {
|
||||
return withContext(ioDispatcher) {
|
||||
kotlin.runCatching {
|
||||
rootShell.get().start()
|
||||
@@ -267,4 +296,59 @@ constructor(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun saveConfigChanges(config: TunnelConfig, peers: List<PeerProxy>? = null, `interface`: InterfaceProxy? = null) = viewModelScope.launch(
|
||||
ioDispatcher,
|
||||
) {
|
||||
runCatching {
|
||||
val amConfig = config.toAmConfig()
|
||||
val wgConfig = config.toWgConfig()
|
||||
rebuildConfigsAndSave(config, amConfig, wgConfig, peers, `interface`)
|
||||
_popBackStack.emit(true)
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.config_changes_saved))
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
SnackbarController.showMessage(
|
||||
it.message?.let { message ->
|
||||
(StringValue.DynamicString(message))
|
||||
} ?: StringValue.StringResource(R.string.unknown_error),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun cleanUpUninstalledApps(tunnelConfig: TunnelConfig, packages: List<String>) = viewModelScope.launch(ioDispatcher) {
|
||||
runCatching {
|
||||
val amConfig = tunnelConfig.toAmConfig()
|
||||
val wgConfig = tunnelConfig.toWgConfig()
|
||||
val proxy = InterfaceProxy.from(amConfig.`interface`)
|
||||
if (proxy.includedApplications.isEmpty() && proxy.excludedApplications.isEmpty()) return@launch
|
||||
if (proxy.includedApplications.retainAll(packages.toSet()) || proxy.excludedApplications.retainAll(packages.toSet())) {
|
||||
Timber.i("Removing split tunnel package for app that no longer exists on the device")
|
||||
rebuildConfigsAndSave(tunnelConfig, amConfig, wgConfig, `interface` = proxy)
|
||||
}
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun rebuildConfigsAndSave(
|
||||
config: TunnelConfig,
|
||||
amConfig: org.amnezia.awg.config.Config,
|
||||
wgConfig: Config,
|
||||
peers: List<PeerProxy>? = null,
|
||||
`interface`: InterfaceProxy? = null,
|
||||
) {
|
||||
appDataRepository.tunnels.save(
|
||||
config.copy(
|
||||
wgQuick = Config.Builder().apply {
|
||||
addPeers(peers?.map { it.toWgPeer() } ?: wgConfig.peers)
|
||||
setInterface(`interface`?.toWgInterface() ?: wgConfig.`interface`)
|
||||
}.build().toWgQuickString(true),
|
||||
amQuick = org.amnezia.awg.config.Config.Builder().apply {
|
||||
addPeers(peers?.map { it.toAmPeer() } ?: amConfig.peers)
|
||||
setInterface(`interface`?.toAmInterface() ?: amConfig.`interface`)
|
||||
}.build().toAwgQuickString(true),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.Color.TRANSPARENT
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import androidx.activity.SystemBarStyle
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.compose.animation.core.tween
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
@@ -35,15 +40,14 @@ import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.toRoute
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||
import com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.options.OptionsScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.pinlock.PinLockScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.scanner.ScannerScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
||||
@@ -55,6 +59,10 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.settings.disclosure.Locati
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.killswitch.KillSwitchScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.OptionsScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.ConfigScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.splittunnel.SplitTunnelScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.tunnelautotunnel.TunnelAutoTunnelScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||
@@ -71,7 +79,18 @@ class MainActivity : AppCompatActivity() {
|
||||
@Inject
|
||||
lateinit var tunnelService: TunnelService
|
||||
|
||||
@Inject
|
||||
lateinit var shortcutManager: ShortcutManager
|
||||
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
enableEdgeToEdge(
|
||||
statusBarStyle = SystemBarStyle.auto(TRANSPARENT, TRANSPARENT),
|
||||
navigationBarStyle = SystemBarStyle.auto(TRANSPARENT, TRANSPARENT),
|
||||
)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
window.isNavigationBarContrastEnforced = false
|
||||
}
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
val viewModel by viewModels<AppViewModel>()
|
||||
@@ -96,6 +115,10 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.getEmitSplitTunnelApps(this@MainActivity)
|
||||
}
|
||||
|
||||
LaunchedEffect(appUiState.autoTunnelActive) {
|
||||
requestAutoTunnelTileServiceUpdate()
|
||||
}
|
||||
@@ -104,13 +127,17 @@ class MainActivity : AppCompatActivity() {
|
||||
LaunchedEffect(isAutoTunnelEnabled) {
|
||||
this@MainActivity.requestAutoTunnelTileServiceUpdate()
|
||||
}
|
||||
LaunchedEffect(isShortcutsEnabled) {
|
||||
if (!isShortcutsEnabled) return@LaunchedEffect shortcutManager.removeShortcuts()
|
||||
shortcutManager.addShortcuts()
|
||||
}
|
||||
}
|
||||
|
||||
CompositionLocalProvider(LocalNavController provides navController) {
|
||||
SnackbarControllerProvider { host ->
|
||||
WireguardAutoTunnelTheme(theme = appUiState.generalState.theme) {
|
||||
Scaffold(
|
||||
contentWindowInsets = WindowInsets(0.dp),
|
||||
contentWindowInsets = WindowInsets(0),
|
||||
snackbarHost = {
|
||||
SnackbarHost(host) { snackbarData: SnackbarData ->
|
||||
CustomSnackBar(
|
||||
@@ -190,11 +217,13 @@ class MainActivity : AppCompatActivity() {
|
||||
composable<Route.Config> {
|
||||
val args = it.toRoute<Route.Config>()
|
||||
ConfigScreen(
|
||||
appUiState,
|
||||
tunnelId = args.id,
|
||||
appViewModel = viewModel,
|
||||
)
|
||||
}
|
||||
composable<Route.Option> {
|
||||
val args = it.toRoute<Route.Option>()
|
||||
composable<Route.TunnelOptions> {
|
||||
val args = it.toRoute<Route.TunnelOptions>()
|
||||
OptionsScreen(
|
||||
tunnelId = args.id,
|
||||
appUiState = appUiState,
|
||||
@@ -211,6 +240,14 @@ class MainActivity : AppCompatActivity() {
|
||||
composable<Route.KillSwitch> {
|
||||
KillSwitchScreen(appUiState, viewModel)
|
||||
}
|
||||
composable<Route.SplitTunnel> {
|
||||
val args = it.toRoute<Route.SplitTunnel>()
|
||||
SplitTunnelScreen(appUiState, args.id, viewModel)
|
||||
}
|
||||
composable<Route.TunnelAutoTunnel> {
|
||||
val args = it.toRoute<Route.SplitTunnel>()
|
||||
TunnelAutoTunnelScreen(appUiState, args.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ sealed class Route {
|
||||
data object Main : Route()
|
||||
|
||||
@Serializable
|
||||
data class Option(
|
||||
data class TunnelOptions(
|
||||
val id: Int,
|
||||
) : Route()
|
||||
|
||||
@@ -46,6 +46,16 @@ sealed class Route {
|
||||
val id: Int,
|
||||
) : Route()
|
||||
|
||||
@Serializable
|
||||
data class SplitTunnel(
|
||||
val id: Int,
|
||||
) : Route()
|
||||
|
||||
@Serializable
|
||||
data class TunnelAutoTunnel(
|
||||
val id: Int,
|
||||
) : Route()
|
||||
|
||||
@Serializable
|
||||
data object Logs : Route()
|
||||
}
|
||||
|
||||
+7
-1
@@ -55,7 +55,13 @@ fun ExpandingRowListItem(
|
||||
modifier = Modifier.fillMaxWidth(13 / 20f),
|
||||
) {
|
||||
leading()
|
||||
Text(text, maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.labelLarge)
|
||||
Text(
|
||||
text,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
)
|
||||
}
|
||||
trailing()
|
||||
}
|
||||
|
||||
@@ -2,36 +2,22 @@ package com.zaneschepke.wireguardautotunnel.ui.common
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.CheckBox
|
||||
import androidx.compose.material3.Icon
|
||||
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.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
|
||||
@Composable
|
||||
fun SelectedLabel() {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(
|
||||
stringResource(id = R.string.selected),
|
||||
modifier =
|
||||
Modifier.padding(
|
||||
horizontal = 24.dp.scaledWidth(),
|
||||
vertical = 16.dp.scaledHeight(),
|
||||
),
|
||||
color =
|
||||
MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
)
|
||||
val icon = Icons.Outlined.CheckBox
|
||||
Icon(icon, icon.name)
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -1,6 +1,8 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
||||
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.SwitchDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.scale
|
||||
@@ -14,5 +16,13 @@ fun ScaledSwitch(checked: Boolean, onClick: (checked: Boolean) -> Unit, enabled:
|
||||
{ onClick(it) },
|
||||
modifier.scale((52.dp.scaledHeight() / 52.dp)),
|
||||
enabled = enabled,
|
||||
colors = SwitchDefaults.colors().copy(
|
||||
checkedThumbColor = MaterialTheme.colorScheme.background,
|
||||
checkedIconColor = MaterialTheme.colorScheme.background,
|
||||
uncheckedTrackColor = MaterialTheme.colorScheme.surface,
|
||||
uncheckedBorderColor = MaterialTheme.colorScheme.outline,
|
||||
uncheckedThumbColor = MaterialTheme.colorScheme.outline,
|
||||
uncheckedIconColor = MaterialTheme.colorScheme.outline,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
+13
-3
@@ -5,7 +5,9 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
@@ -17,8 +19,10 @@ import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
|
||||
@Composable
|
||||
fun SelectionItemButton(
|
||||
@@ -30,7 +34,8 @@ fun SelectionItemButton(
|
||||
) {
|
||||
Card(
|
||||
modifier =
|
||||
Modifier.clip(RoundedCornerShape(8.dp))
|
||||
Modifier
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.clickable(
|
||||
indication = if (ripple) ripple() else null,
|
||||
interactionSource = remember { MutableInteractionSource() },
|
||||
@@ -45,15 +50,20 @@ fun SelectionItemButton(
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.Start,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(end = 10.dp.scaledWidth()),
|
||||
) {
|
||||
leading?.let {
|
||||
it()
|
||||
}
|
||||
Text(
|
||||
buttonText,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
modifier = Modifier.fillMaxWidth(3 / 4f),
|
||||
maxLines = 2,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
trailing?.let {
|
||||
it()
|
||||
|
||||
+2
-1
@@ -52,6 +52,7 @@ fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
||||
icon,
|
||||
icon.name,
|
||||
modifier = Modifier.size(iconSize),
|
||||
tint = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
}
|
||||
Column(
|
||||
@@ -80,7 +81,7 @@ fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (index + 1 != items.size) HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
|
||||
if (index + 1 != items.size) HorizontalDivider(color = MaterialTheme.colorScheme.outline.copy(0.30f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -3,6 +3,7 @@ package com.zaneschepke.wireguardautotunnel.ui.common.config
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
@@ -23,19 +24,20 @@ fun ConfigurationTextBox(
|
||||
capitalization = KeyboardCapitalization.None,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
trailing: @Composable () -> Unit = {},
|
||||
trailing: (@Composable () -> Unit)? = null,
|
||||
interactionSource: MutableInteractionSource? = null,
|
||||
) {
|
||||
OutlinedTextField(
|
||||
isError = isError,
|
||||
textStyle = MaterialTheme.typography.labelLarge,
|
||||
modifier = modifier,
|
||||
value = value,
|
||||
singleLine = true,
|
||||
interactionSource = interactionSource,
|
||||
onValueChange = { onValueChange(it) },
|
||||
label = { Text(label) },
|
||||
label = { Text(label, color = MaterialTheme.colorScheme.onSurface, style = MaterialTheme.typography.labelMedium) },
|
||||
maxLines = 1,
|
||||
placeholder = { Text(hint) },
|
||||
placeholder = { Text(hint, color = MaterialTheme.colorScheme.outline, style = MaterialTheme.typography.labelLarge) },
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
trailingIcon = trailing,
|
||||
|
||||
+3
-3
@@ -4,12 +4,12 @@ import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||
|
||||
@Composable
|
||||
fun ConfigurationToggle(
|
||||
@@ -38,11 +38,11 @@ fun ConfigurationToggle(
|
||||
),
|
||||
softWrap = true,
|
||||
)
|
||||
Switch(
|
||||
ScaledSwitch(
|
||||
modifier = modifier,
|
||||
enabled = enabled,
|
||||
checked = checked,
|
||||
onCheckedChange = { onCheckChanged(it) },
|
||||
onClick = { onCheckChanged(it) },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ fun GroupLabel(title: String) {
|
||||
Text(
|
||||
title,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -4,12 +4,14 @@ import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.NavigationBar
|
||||
import androidx.compose.material3.NavigationBarItem
|
||||
import androidx.compose.material3.NavigationBarItemDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
@@ -57,6 +59,13 @@ fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavIte
|
||||
contentDescription = "${item.name} Icon",
|
||||
)
|
||||
},
|
||||
colors = NavigationBarItemDefaults.colors().copy(
|
||||
selectedIndicatorColor = Color.Transparent,
|
||||
selectedIconColor = MaterialTheme.colorScheme.primary,
|
||||
selectedTextColor = MaterialTheme.colorScheme.primary,
|
||||
unselectedTextColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.55f),
|
||||
unselectedIconColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.55f),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -28,8 +28,8 @@ fun CustomTextField(
|
||||
onValueChange: (value: String) -> Unit = {},
|
||||
singleLine: Boolean = false,
|
||||
placeholder: @Composable (() -> Unit)? = null,
|
||||
keyboardOptions: KeyboardOptions,
|
||||
keyboardActions: KeyboardActions,
|
||||
keyboardOptions: KeyboardOptions = KeyboardOptions(),
|
||||
keyboardActions: KeyboardActions = KeyboardActions(),
|
||||
supportingText: @Composable (() -> Unit)? = null,
|
||||
leading: @Composable (() -> Unit)? = null,
|
||||
trailing: @Composable (() -> Unit)? = null,
|
||||
|
||||
-601
@@ -1,601 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.focusGroup
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.IntrinsicSize
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.ContentCopy
|
||||
import androidx.compose.material.icons.rounded.Delete
|
||||
import androidx.compose.material.icons.rounded.Refresh
|
||||
import androidx.compose.material.icons.rounded.Save
|
||||
import androidx.compose.material3.FabPosition
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.platform.ClipboardManager
|
||||
import androidx.compose.ui.platform.LocalClipboardManager
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.buildAnnotatedString
|
||||
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.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.components.ApplicationSelectionDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
@Composable
|
||||
fun ConfigScreen(tunnelId: Int) {
|
||||
val viewModel = hiltViewModel<ConfigViewModel, ConfigViewModel.ConfigViewModelFactory> { factory ->
|
||||
factory.create(tunnelId)
|
||||
}
|
||||
|
||||
val context = LocalContext.current
|
||||
val snackbar = SnackbarController.current
|
||||
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
val navController = LocalNavController.current
|
||||
|
||||
var showApplicationsDialog by remember { mutableStateOf(false) }
|
||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||
var isAuthenticated by remember { mutableStateOf(false) }
|
||||
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
var configType by remember { mutableStateOf<ConfigType?>(null) }
|
||||
val derivedConfigType = remember {
|
||||
derivedStateOf {
|
||||
configType ?: if (!uiState.isAmneziaEnabled) ConfigType.WIREGUARD else ConfigType.AMNEZIA
|
||||
}
|
||||
}
|
||||
val saved by viewModel.saved.collectAsStateWithLifecycle(null)
|
||||
|
||||
LaunchedEffect(saved) {
|
||||
if (saved == true) {
|
||||
navController.navigate(Route.Main)
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
delay(2_000L)
|
||||
viewModel.cleanUpUninstalledApps()
|
||||
}
|
||||
|
||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||
|
||||
val fillMaxHeight = .85f
|
||||
val fillMaxWidth = .85f
|
||||
val screenPadding = 5.dp
|
||||
|
||||
val applicationButtonText =
|
||||
buildAnnotatedString {
|
||||
append(stringResource(id = R.string.tunneling_apps))
|
||||
append(": ")
|
||||
if (uiState.isAllApplicationsEnabled) {
|
||||
append(stringResource(id = R.string.all))
|
||||
} else {
|
||||
append("${uiState.checkedPackageNames.size} ")
|
||||
(
|
||||
if (uiState.include) {
|
||||
append(stringResource(id = R.string.included))
|
||||
} else {
|
||||
append(
|
||||
stringResource(id = R.string.excluded),
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showAuthPrompt) {
|
||||
AuthorizationPrompt(
|
||||
onSuccess = {
|
||||
showAuthPrompt = false
|
||||
isAuthenticated = true
|
||||
},
|
||||
onError = {
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
context.getString(R.string.error_authentication_failed),
|
||||
)
|
||||
},
|
||||
onFailure = {
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
context.getString(R.string.error_authorization_failed),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if (showApplicationsDialog) {
|
||||
ApplicationSelectionDialog(viewModel, uiState) {
|
||||
showApplicationsDialog = false
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopNavBar(stringResource(R.string.edit_tunnel))
|
||||
},
|
||||
floatingActionButtonPosition = FabPosition.End,
|
||||
floatingActionButton = {
|
||||
FloatingActionButton(
|
||||
onClick = {
|
||||
viewModel.onSaveAllChanges()
|
||||
},
|
||||
containerColor = MaterialTheme.colorScheme.primary,
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Rounded.Save,
|
||||
contentDescription = stringResource(id = R.string.save_changes),
|
||||
tint = MaterialTheme.colorScheme.background,
|
||||
)
|
||||
}
|
||||
},
|
||||
) { padding ->
|
||||
Column(Modifier.padding(padding)) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier =
|
||||
Modifier
|
||||
.verticalScroll(rememberScrollState())
|
||||
.weight(1f, true)
|
||||
.fillMaxSize(),
|
||||
) {
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
(
|
||||
if (context.isRunningOnTv()) {
|
||||
Modifier
|
||||
.fillMaxHeight(fillMaxHeight)
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
} else {
|
||||
Modifier.fillMaxWidth(fillMaxWidth)
|
||||
}
|
||||
)
|
||||
.padding(bottom = 10.dp.scaledHeight()).padding(top = 24.dp.scaledHeight()),
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(15.dp)
|
||||
.focusGroup(),
|
||||
) {
|
||||
SectionTitle(
|
||||
stringResource(R.string.interface_),
|
||||
padding = screenPadding,
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(id = R.string.show_amnezia_properties),
|
||||
checked = derivedConfigType.value == ConfigType.AMNEZIA,
|
||||
onCheckChanged = { configType = if (it) ConfigType.AMNEZIA else ConfigType.WIREGUARD },
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.tunnelName,
|
||||
onValueChange = viewModel::onTunnelNameChange,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.name),
|
||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
OutlinedTextField(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { showAuthPrompt = true },
|
||||
value = uiState.interfaceProxy.privateKey,
|
||||
visualTransformation =
|
||||
if ((tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated) {
|
||||
VisualTransformation.None
|
||||
} else {
|
||||
PasswordVisualTransformation()
|
||||
},
|
||||
enabled = (tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated,
|
||||
onValueChange = { value -> viewModel.onPrivateKeyChange(value) },
|
||||
trailingIcon = {
|
||||
IconButton(
|
||||
modifier = Modifier.focusRequester(FocusRequester.Default),
|
||||
onClick = { viewModel.generateKeyPair() },
|
||||
) {
|
||||
Icon(
|
||||
Icons.Rounded.Refresh,
|
||||
stringResource(R.string.rotate_keys),
|
||||
tint = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
}
|
||||
},
|
||||
label = { Text(stringResource(R.string.private_key)) },
|
||||
singleLine = true,
|
||||
placeholder = { Text(stringResource(R.string.base64_key)) },
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
OutlinedTextField(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.focusRequester(FocusRequester.Default),
|
||||
value = uiState.interfaceProxy.publicKey,
|
||||
enabled = false,
|
||||
onValueChange = {},
|
||||
trailingIcon = {
|
||||
IconButton(
|
||||
modifier = Modifier.focusRequester(FocusRequester.Default),
|
||||
onClick = {
|
||||
clipboardManager.setText(
|
||||
AnnotatedString(uiState.interfaceProxy.publicKey),
|
||||
)
|
||||
},
|
||||
) {
|
||||
Icon(
|
||||
Icons.Rounded.ContentCopy,
|
||||
stringResource(R.string.copy_public_key),
|
||||
tint = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
}
|
||||
},
|
||||
label = { Text(stringResource(R.string.public_key)) },
|
||||
singleLine = true,
|
||||
placeholder = { Text(stringResource(R.string.base64_key)) },
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.addresses,
|
||||
onValueChange = viewModel::onAddressesChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.addresses),
|
||||
hint = stringResource(R.string.comma_separated_list),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(end = 5.dp),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.listenPort,
|
||||
onValueChange = viewModel::onListenPortChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.listen_port),
|
||||
hint = stringResource(R.string.random),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Row(modifier = Modifier.fillMaxWidth()) {
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.dnsServers,
|
||||
onValueChange = viewModel::onDnsServersChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.dns_servers),
|
||||
hint = stringResource(R.string.comma_separated_list),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(3 / 5f)
|
||||
.padding(end = 5.dp),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.mtu,
|
||||
onValueChange = viewModel::onMtuChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.mtu),
|
||||
hint = stringResource(R.string.auto),
|
||||
modifier = Modifier.width(IntrinsicSize.Min),
|
||||
)
|
||||
}
|
||||
if (derivedConfigType.value == ConfigType.AMNEZIA) {
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.junkPacketCount,
|
||||
onValueChange = viewModel::onJunkPacketCountChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_count),
|
||||
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.junkPacketMinSize,
|
||||
onValueChange = viewModel::onJunkPacketMinSizeChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_minimum_size),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.junk_packet_minimum_size,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.junkPacketMaxSize,
|
||||
onValueChange = viewModel::onJunkPacketMaxSizeChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_maximum_size),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.junk_packet_maximum_size,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.initPacketJunkSize,
|
||||
onValueChange = viewModel::onInitPacketJunkSizeChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.init_packet_junk_size),
|
||||
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.responsePacketJunkSize,
|
||||
onValueChange = viewModel::onResponsePacketJunkSize,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.response_packet_junk_size),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.response_packet_junk_size,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.initPacketMagicHeader,
|
||||
onValueChange = viewModel::onInitPacketMagicHeader,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.init_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.init_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.responsePacketMagicHeader,
|
||||
onValueChange = viewModel::onResponsePacketMagicHeader,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.response_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.response_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.underloadPacketMagicHeader,
|
||||
onValueChange = viewModel::onUnderloadPacketMagicHeader,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.underload_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.underload_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.transportPacketMagicHeader,
|
||||
onValueChange = viewModel::onTransportPacketMagicHeader,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.transport_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.transport_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(onClick = { showApplicationsDialog = true }) {
|
||||
Text(applicationButtonText.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
uiState.proxyPeers.forEachIndexed { index, peer ->
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
(
|
||||
if (context.isRunningOnTv()) {
|
||||
Modifier
|
||||
.fillMaxHeight(fillMaxHeight)
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
} else {
|
||||
Modifier.fillMaxWidth(fillMaxWidth)
|
||||
}
|
||||
)
|
||||
.padding(top = 10.dp, bottom = 10.dp),
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(horizontal = 15.dp)
|
||||
.padding(bottom = 10.dp),
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 5.dp),
|
||||
) {
|
||||
SectionTitle(
|
||||
stringResource(R.string.peer),
|
||||
padding = screenPadding,
|
||||
)
|
||||
IconButton(onClick = { viewModel.onDeletePeer(index) }) {
|
||||
val icon = Icons.Rounded.Delete
|
||||
Icon(icon, icon.name)
|
||||
}
|
||||
}
|
||||
|
||||
ConfigurationTextBox(
|
||||
value = peer.publicKey,
|
||||
onValueChange = { value ->
|
||||
viewModel.onPeerPublicKeyChange(index, value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.public_key),
|
||||
hint = stringResource(R.string.base64_key),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = peer.preSharedKey,
|
||||
onValueChange = { value ->
|
||||
viewModel.onPreSharedKeyChange(index, value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.preshared_key),
|
||||
hint = stringResource(R.string.optional),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
OutlinedTextField(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
value = peer.persistentKeepalive,
|
||||
enabled = true,
|
||||
onValueChange = { value ->
|
||||
viewModel.onPersistentKeepaliveChanged(index, value)
|
||||
},
|
||||
trailingIcon = {
|
||||
Text(
|
||||
stringResource(R.string.seconds),
|
||||
modifier = Modifier.padding(end = 10.dp),
|
||||
)
|
||||
},
|
||||
label = { Text(stringResource(R.string.persistent_keepalive)) },
|
||||
singleLine = true,
|
||||
placeholder = {
|
||||
Text(stringResource(R.string.optional_no_recommend))
|
||||
},
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = peer.endpoint,
|
||||
onValueChange = { value ->
|
||||
viewModel.onEndpointChange(index, value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.endpoint),
|
||||
hint = stringResource(R.string.endpoint).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
OutlinedTextField(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
value = peer.allowedIps,
|
||||
enabled = true,
|
||||
onValueChange = { value ->
|
||||
viewModel.onAllowedIpsChange(index, value)
|
||||
},
|
||||
label = { Text(stringResource(R.string.allowed_ips)) },
|
||||
singleLine = true,
|
||||
placeholder = {
|
||||
Text(stringResource(R.string.comma_separated_list))
|
||||
},
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(bottom = 140.dp),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(onClick = { viewModel.addEmptyPeer() }) {
|
||||
Text(stringResource(R.string.add_peer))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-81
@@ -1,81 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
||||
|
||||
import com.wireguard.config.Config
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.InterfaceProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.Packages
|
||||
|
||||
data class ConfigUiState(
|
||||
val proxyPeers: List<PeerProxy> = arrayListOf(PeerProxy()),
|
||||
val interfaceProxy: InterfaceProxy = InterfaceProxy(),
|
||||
val packages: Packages = emptyList(),
|
||||
val checkedPackageNames: List<String> = emptyList(),
|
||||
val include: Boolean = true,
|
||||
val isAllApplicationsEnabled: Boolean = false,
|
||||
val loading: Boolean = true,
|
||||
val tunnel: TunnelConfig? = null,
|
||||
var tunnelName: String = "",
|
||||
val isAmneziaEnabled: Boolean = false,
|
||||
) {
|
||||
companion object {
|
||||
fun from(config: Config): ConfigUiState {
|
||||
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
||||
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
||||
var include = true
|
||||
var isAllApplicationsEnabled = false
|
||||
val checkedPackages =
|
||||
if (config.`interface`.includedApplications.isNotEmpty()) {
|
||||
config.`interface`.includedApplications
|
||||
} else if (config.`interface`.excludedApplications.isNotEmpty()) {
|
||||
include = false
|
||||
config.`interface`.excludedApplications
|
||||
} else {
|
||||
isAllApplicationsEnabled = true
|
||||
emptySet()
|
||||
}
|
||||
return ConfigUiState(
|
||||
proxyPeers,
|
||||
proxyInterface,
|
||||
emptyList(),
|
||||
checkedPackages.toList(),
|
||||
include,
|
||||
isAllApplicationsEnabled,
|
||||
)
|
||||
}
|
||||
|
||||
fun from(config: org.amnezia.awg.config.Config): ConfigUiState {
|
||||
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
||||
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
||||
var include = true
|
||||
var isAllApplicationsEnabled = false
|
||||
val checkedPackages =
|
||||
if (config.`interface`.includedApplications.isNotEmpty()) {
|
||||
config.`interface`.includedApplications
|
||||
} else if (config.`interface`.excludedApplications.isNotEmpty()) {
|
||||
include = false
|
||||
config.`interface`.excludedApplications
|
||||
} else {
|
||||
isAllApplicationsEnabled = true
|
||||
emptySet()
|
||||
}
|
||||
return ConfigUiState(
|
||||
proxyPeers,
|
||||
proxyInterface,
|
||||
emptyList(),
|
||||
checkedPackages.toList(),
|
||||
include,
|
||||
isAllApplicationsEnabled,
|
||||
)
|
||||
}
|
||||
|
||||
fun from(tunnel: TunnelConfig): ConfigUiState {
|
||||
val config = tunnel.toAmConfig()
|
||||
return from(config).copy(
|
||||
tunnelName = tunnel.name,
|
||||
tunnel = tunnel,
|
||||
isAmneziaEnabled = config.`interface`.junkPacketCount.isPresent,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
-591
@@ -1,591 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.wireguard.config.Config
|
||||
import com.wireguard.config.Interface
|
||||
import com.wireguard.config.Peer
|
||||
import com.wireguard.crypto.Key
|
||||
import com.wireguard.crypto.KeyPair
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.removeAt
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.update
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.plus
|
||||
import timber.log.Timber
|
||||
|
||||
@HiltViewModel(assistedFactory = ConfigViewModel.ConfigViewModelFactory::class)
|
||||
class ConfigViewModel
|
||||
@AssistedInject
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
@Assisted val id: Int,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
private val packageManager = WireGuardAutoTunnel.instance.packageManager
|
||||
|
||||
private val _saved = MutableSharedFlow<Boolean>()
|
||||
val saved = _saved.asSharedFlow()
|
||||
|
||||
private val _uiState = MutableStateFlow(ConfigUiState())
|
||||
val uiState = _uiState.onStart {
|
||||
appDataRepository.tunnels.getById(id)?.let {
|
||||
val packages = getQueriedPackages()
|
||||
_uiState.value = ConfigUiState.from(it).copy(
|
||||
packages = packages,
|
||||
)
|
||||
}
|
||||
}.stateIn(
|
||||
viewModelScope + ioDispatcher,
|
||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||
ConfigUiState(),
|
||||
)
|
||||
|
||||
fun onTunnelNameChange(name: String) {
|
||||
_uiState.update {
|
||||
it.copy(tunnelName = name)
|
||||
}
|
||||
}
|
||||
|
||||
fun onIncludeChange(include: Boolean) {
|
||||
_uiState.update {
|
||||
it.copy(include = include)
|
||||
}
|
||||
}
|
||||
|
||||
fun cleanUpUninstalledApps() = viewModelScope.launch(ioDispatcher) {
|
||||
uiState.value.tunnel?.let {
|
||||
val config = it.toAmConfig()
|
||||
val packages = getQueriedPackages()
|
||||
val packageSet = packages.map { pack -> pack.packageName }.toSet()
|
||||
val includedApps = config.`interface`.includedApplications.toMutableList()
|
||||
val excludedApps = config.`interface`.excludedApplications.toMutableList()
|
||||
if (includedApps.isEmpty() && excludedApps.isEmpty()) return@launch
|
||||
if (includedApps.retainAll(packageSet) || excludedApps.retainAll(packageSet)) {
|
||||
Timber.i("Removing split tunnel package name that no longer exists on the device")
|
||||
_uiState.update { state ->
|
||||
state.copy(
|
||||
checkedPackageNames = if (_uiState.value.include) includedApps else excludedApps,
|
||||
)
|
||||
}
|
||||
val wgQuick = buildConfig().toWgQuickString(true)
|
||||
val amQuick = buildAmConfig().toAwgQuickString(true)
|
||||
saveConfig(
|
||||
it.copy(
|
||||
amQuick = amQuick,
|
||||
wgQuick = wgQuick,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onAddCheckedPackage(packageName: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
checkedPackageNames = it.checkedPackageNames + packageName,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onAllApplicationsChange(isAllApplicationsEnabled: Boolean) {
|
||||
_uiState.update {
|
||||
it.copy(isAllApplicationsEnabled = isAllApplicationsEnabled)
|
||||
}
|
||||
}
|
||||
|
||||
fun onRemoveCheckedPackage(packageName: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
checkedPackageNames = it.checkedPackageNames - packageName,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getQueriedPackages(query: String = ""): List<PackageInfo> {
|
||||
return getAllInternetCapablePackages().filter {
|
||||
getPackageLabel(it).lowercase().contains(query.lowercase())
|
||||
}
|
||||
}
|
||||
|
||||
fun getPackageLabel(packageInfo: PackageInfo): String {
|
||||
return packageInfo.applicationInfo?.loadLabel(packageManager).toString()
|
||||
}
|
||||
|
||||
private fun getAllInternetCapablePackages(): List<PackageInfo> {
|
||||
return getPackagesHoldingPermissions(arrayOf(Manifest.permission.INTERNET))
|
||||
}
|
||||
|
||||
private fun getPackagesHoldingPermissions(permissions: Array<String>): List<PackageInfo> {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
packageManager.getPackagesHoldingPermissions(
|
||||
permissions,
|
||||
PackageManager.PackageInfoFlags.of(0L),
|
||||
)
|
||||
} else {
|
||||
packageManager.getPackagesHoldingPermissions(permissions, 0)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isAllApplicationsEnabled(): Boolean {
|
||||
return _uiState.value.isAllApplicationsEnabled
|
||||
}
|
||||
|
||||
private fun saveConfig(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(tunnelConfig)
|
||||
}
|
||||
|
||||
private fun updateTunnelConfig(tunnelConfig: TunnelConfig?) = viewModelScope.launch {
|
||||
if (tunnelConfig != null) {
|
||||
saveConfig(tunnelConfig).join()
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildPeerListFromProxyPeers(): List<Peer> {
|
||||
return _uiState.value.proxyPeers.map {
|
||||
val builder = Peer.Builder()
|
||||
if (it.allowedIps.isNotEmpty()) builder.parseAllowedIPs(it.allowedIps.trim())
|
||||
if (it.publicKey.isNotEmpty()) builder.parsePublicKey(it.publicKey.trim())
|
||||
if (it.preSharedKey.isNotEmpty()) builder.parsePreSharedKey(it.preSharedKey.trim())
|
||||
if (it.endpoint.isNotEmpty()) builder.parseEndpoint(it.endpoint.trim())
|
||||
if (it.persistentKeepalive.isNotEmpty()) {
|
||||
builder.parsePersistentKeepalive(it.persistentKeepalive.trim())
|
||||
}
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildAmPeerListFromProxyPeers(): List<org.amnezia.awg.config.Peer> {
|
||||
return _uiState.value.proxyPeers.map {
|
||||
val builder = org.amnezia.awg.config.Peer.Builder()
|
||||
if (it.allowedIps.isNotEmpty()) builder.parseAllowedIPs(it.allowedIps.trim())
|
||||
if (it.publicKey.isNotEmpty()) builder.parsePublicKey(it.publicKey.trim())
|
||||
if (it.preSharedKey.isNotEmpty()) builder.parsePreSharedKey(it.preSharedKey.trim())
|
||||
if (it.endpoint.isNotEmpty()) builder.parseEndpoint(it.endpoint.trim())
|
||||
if (it.persistentKeepalive.isNotEmpty()) {
|
||||
builder.parsePersistentKeepalive(it.persistentKeepalive.trim())
|
||||
}
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
|
||||
private fun emptyCheckedPackagesList() {
|
||||
_uiState.update {
|
||||
it.copy(checkedPackageNames = emptyList())
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildInterfaceListFromProxyInterface(): Interface {
|
||||
val builder = Interface.Builder()
|
||||
with(_uiState.value.interfaceProxy) {
|
||||
builder.parsePrivateKey(this.privateKey.trim())
|
||||
builder.parseAddresses(this.addresses.trim())
|
||||
if (this.dnsServers.isNotEmpty()) {
|
||||
builder.parseDnsServers(this.dnsServers.trim())
|
||||
}
|
||||
if (this.mtu.isNotEmpty()) {
|
||||
builder.parseMtu(this.mtu.trim())
|
||||
}
|
||||
if (this.listenPort.isNotEmpty()) {
|
||||
builder.parseListenPort(this.listenPort.trim())
|
||||
}
|
||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||
if (_uiState.value.include) {
|
||||
builder.includeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (!_uiState.value.include) {
|
||||
builder.excludeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
private fun buildAmInterfaceListFromProxyInterface(): org.amnezia.awg.config.Interface {
|
||||
val builder = org.amnezia.awg.config.Interface.Builder()
|
||||
with(_uiState.value.interfaceProxy) {
|
||||
builder.parsePrivateKey(this.privateKey.trim())
|
||||
builder.parseAddresses(this.addresses.trim())
|
||||
if (this.dnsServers.isNotEmpty()) {
|
||||
builder.parseDnsServers(this.dnsServers.trim())
|
||||
}
|
||||
if (this.mtu.isNotEmpty()) {
|
||||
builder.parseMtu(this.mtu.trim())
|
||||
}
|
||||
if (this.listenPort.isNotEmpty()) {
|
||||
builder.parseListenPort(this.listenPort.trim())
|
||||
}
|
||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||
if (_uiState.value.include) {
|
||||
builder.includeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (!_uiState.value.include) {
|
||||
builder.excludeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (this.junkPacketCount.isNotEmpty()) {
|
||||
builder.setJunkPacketCount(
|
||||
this.junkPacketCount.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.junkPacketMinSize.isNotEmpty()) {
|
||||
builder.setJunkPacketMinSize(
|
||||
this.junkPacketMinSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.junkPacketMaxSize.isNotEmpty()) {
|
||||
builder.setJunkPacketMaxSize(
|
||||
this.junkPacketMaxSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.initPacketJunkSize.isNotEmpty()) {
|
||||
builder.setInitPacketJunkSize(
|
||||
this.initPacketJunkSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.responsePacketJunkSize.isNotEmpty()) {
|
||||
builder.setResponsePacketJunkSize(
|
||||
this.responsePacketJunkSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.initPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setInitPacketMagicHeader(
|
||||
this.initPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (this.responsePacketMagicHeader.isNotEmpty()) {
|
||||
builder.setResponsePacketMagicHeader(
|
||||
this.responsePacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (this.transportPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setTransportPacketMagicHeader(
|
||||
this.transportPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (this.underloadPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setUnderloadPacketMagicHeader(
|
||||
this.underloadPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
private fun buildConfig(): Config {
|
||||
val peerList = buildPeerListFromProxyPeers()
|
||||
val wgInterface = buildInterfaceListFromProxyInterface()
|
||||
return Config.Builder().addPeers(peerList).setInterface(wgInterface).build()
|
||||
}
|
||||
|
||||
private fun buildAmConfig(): org.amnezia.awg.config.Config {
|
||||
val peerList = buildAmPeerListFromProxyPeers()
|
||||
val amInterface = buildAmInterfaceListFromProxyInterface()
|
||||
return org.amnezia.awg.config.Config.Builder().addPeers(
|
||||
peerList,
|
||||
).setInterface(amInterface)
|
||||
.build()
|
||||
}
|
||||
|
||||
fun onSaveAllChanges() = viewModelScope.launch {
|
||||
kotlin.runCatching {
|
||||
val wgQuick = buildConfig().toWgQuickString(true)
|
||||
val amQuick = buildAmConfig().toAwgQuickString(true)
|
||||
val tunnelConfig = uiState.value.tunnel?.copy(
|
||||
name = _uiState.value.tunnelName,
|
||||
amQuick = amQuick,
|
||||
wgQuick = wgQuick,
|
||||
) ?: TunnelConfig(
|
||||
name = _uiState.value.tunnelName,
|
||||
wgQuick = wgQuick,
|
||||
amQuick = amQuick,
|
||||
)
|
||||
updateTunnelConfig(tunnelConfig)
|
||||
SnackbarController.showMessage(
|
||||
StringValue.StringResource(R.string.config_changes_saved),
|
||||
)
|
||||
_saved.emit(true)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
val message = it.message?.substringAfter(":", missingDelimiterValue = "")
|
||||
val stringValue = if (message.isNullOrBlank()) {
|
||||
StringValue.StringResource(R.string.unknown_error)
|
||||
} else {
|
||||
StringValue.DynamicString(message)
|
||||
}
|
||||
SnackbarController.showMessage(stringValue)
|
||||
}
|
||||
}
|
||||
|
||||
fun onPeerPublicKeyChange(index: Int, value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
proxyPeers =
|
||||
_uiState.value.proxyPeers.update(
|
||||
index,
|
||||
_uiState.value.proxyPeers[index].copy(publicKey = value),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onPreSharedKeyChange(index: Int, value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
proxyPeers =
|
||||
_uiState.value.proxyPeers.update(
|
||||
index,
|
||||
_uiState.value.proxyPeers[index].copy(preSharedKey = value),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onEndpointChange(index: Int, value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
proxyPeers =
|
||||
_uiState.value.proxyPeers.update(
|
||||
index,
|
||||
_uiState.value.proxyPeers[index].copy(endpoint = value),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onAllowedIpsChange(index: Int, value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
proxyPeers =
|
||||
_uiState.value.proxyPeers.update(
|
||||
index,
|
||||
_uiState.value.proxyPeers[index].copy(allowedIps = value),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onPersistentKeepaliveChanged(index: Int, value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
proxyPeers =
|
||||
_uiState.value.proxyPeers.update(
|
||||
index,
|
||||
_uiState.value.proxyPeers[index].copy(persistentKeepalive = value),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onDeletePeer(index: Int) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
proxyPeers = _uiState.value.proxyPeers.removeAt(index),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun addEmptyPeer() {
|
||||
_uiState.update {
|
||||
it.copy(proxyPeers = _uiState.value.proxyPeers + PeerProxy())
|
||||
}
|
||||
}
|
||||
|
||||
fun generateKeyPair() {
|
||||
val keyPair = KeyPair()
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
privateKey = keyPair.privateKey.toBase64(),
|
||||
publicKey = keyPair.publicKey.toBase64(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onAddressesChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(addresses = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onListenPortChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(listenPort = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onDnsServersChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(dnsServers = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onMtuChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(interfaceProxy = it.interfaceProxy.copy(mtu = value))
|
||||
}
|
||||
}
|
||||
|
||||
private fun onInterfacePublicKeyChange(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(publicKey = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onPrivateKeyChange(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(privateKey = value),
|
||||
)
|
||||
}
|
||||
if (NumberUtils.isValidKey(value)) {
|
||||
val pair = KeyPair(Key.fromBase64(value))
|
||||
onInterfacePublicKeyChange(pair.publicKey.toBase64())
|
||||
} else {
|
||||
onInterfacePublicKeyChange("")
|
||||
}
|
||||
}
|
||||
|
||||
fun emitQueriedPackages(query: String) {
|
||||
val packages =
|
||||
getAllInternetCapablePackages().filter {
|
||||
getPackageLabel(it).lowercase().contains(query.lowercase())
|
||||
}
|
||||
_uiState.update { it.copy(packages = packages) }
|
||||
}
|
||||
|
||||
fun onJunkPacketCountChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(junkPacketCount = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onJunkPacketMinSizeChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMinSize = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onJunkPacketMaxSizeChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMaxSize = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onInitPacketJunkSizeChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(initPacketJunkSize = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onResponsePacketJunkSize(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
responsePacketJunkSize = value,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onInitPacketMagicHeader(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
initPacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onResponsePacketMagicHeader(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
responsePacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onTransportPacketMagicHeader(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
transportPacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onUnderloadPacketMagicHeader(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
underloadPacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@AssistedFactory
|
||||
interface ConfigViewModelFactory {
|
||||
fun create(id: Int): ConfigViewModel
|
||||
}
|
||||
}
|
||||
-199
@@ -1,199 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config.components
|
||||
|
||||
import android.content.pm.PackageInfo
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Android
|
||||
import androidx.compose.material3.BasicAlertDialog
|
||||
import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
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 com.google.accompanist.drawablepainter.DrawablePainter
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SearchBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ApplicationSelectionDialog(viewModel: ConfigViewModel, uiState: ConfigUiState, onDismiss: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val licenseComparator = compareBy<PackageInfo> { viewModel.getPackageLabel(it) }
|
||||
|
||||
val sortedPackages = remember(uiState.packages, licenseComparator) {
|
||||
uiState.packages.sortedWith(licenseComparator)
|
||||
}
|
||||
BasicAlertDialog(
|
||||
onDismissRequest = { onDismiss() },
|
||||
) {
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.fillMaxHeight(if (uiState.isAllApplicationsEnabled) 1 / 5f else 4 / 5f),
|
||||
) {
|
||||
Column(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.tunnel_all))
|
||||
Switch(
|
||||
checked = uiState.isAllApplicationsEnabled,
|
||||
onCheckedChange = viewModel::onAllApplicationsChange,
|
||||
)
|
||||
}
|
||||
if (!uiState.isAllApplicationsEnabled) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.include))
|
||||
Checkbox(
|
||||
checked = uiState.include,
|
||||
onCheckedChange = {
|
||||
viewModel.onIncludeChange(!uiState.include)
|
||||
},
|
||||
)
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.exclude))
|
||||
Checkbox(
|
||||
checked = !uiState.include,
|
||||
onCheckedChange = {
|
||||
viewModel.onIncludeChange(!uiState.include)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
SearchBar(viewModel::emitQueriedPackages)
|
||||
}
|
||||
Spacer(Modifier.padding(5.dp))
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.fillMaxHeight(19 / 22f),
|
||||
) {
|
||||
items(sortedPackages, key = { it.packageName }) { pack ->
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(5.dp).padding(end = 25.dp),
|
||||
) {
|
||||
Row(modifier = Modifier.fillMaxWidth().padding(start = 5.dp)) {
|
||||
val drawable =
|
||||
pack.applicationInfo?.loadIcon(context.packageManager)
|
||||
val iconSize = 35.dp
|
||||
if (drawable != null) {
|
||||
Image(
|
||||
painter = DrawablePainter(drawable),
|
||||
stringResource(id = R.string.icon),
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
} else {
|
||||
val icon = Icons.Rounded.Android
|
||||
Icon(
|
||||
icon,
|
||||
icon.name,
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
}
|
||||
Text(
|
||||
viewModel.getPackageLabel(pack),
|
||||
modifier = Modifier.padding(5.dp),
|
||||
)
|
||||
}
|
||||
Checkbox(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
checked =
|
||||
(
|
||||
uiState.checkedPackageNames.contains(
|
||||
pack.packageName,
|
||||
)
|
||||
),
|
||||
onCheckedChange = {
|
||||
if (it) {
|
||||
viewModel.onAddCheckedPackage(pack.packageName)
|
||||
} else {
|
||||
viewModel.onRemoveCheckedPackage(pack.packageName)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(onClick = { onDismiss() }) {
|
||||
Text(stringResource(R.string.done))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-121
@@ -1,121 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config.model
|
||||
|
||||
import com.wireguard.config.Interface
|
||||
|
||||
data class InterfaceProxy(
|
||||
val privateKey: String = "",
|
||||
val publicKey: String = "",
|
||||
val addresses: String = "",
|
||||
val dnsServers: String = "",
|
||||
val listenPort: String = "",
|
||||
val mtu: String = "",
|
||||
val junkPacketCount: String = "",
|
||||
val junkPacketMinSize: String = "",
|
||||
val junkPacketMaxSize: String = "",
|
||||
val initPacketJunkSize: String = "",
|
||||
val responsePacketJunkSize: String = "",
|
||||
val initPacketMagicHeader: String = "",
|
||||
val responsePacketMagicHeader: String = "",
|
||||
val underloadPacketMagicHeader: String = "",
|
||||
val transportPacketMagicHeader: String = "",
|
||||
) {
|
||||
companion object {
|
||||
fun from(i: Interface): InterfaceProxy {
|
||||
return InterfaceProxy(
|
||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||
addresses = i.addresses.joinToString(", ").trim(),
|
||||
dnsServers = listOf(
|
||||
i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
||||
i.dnsSearchDomains.joinToString(", ").trim(),
|
||||
).filter { it.length > 0 }.joinToString(", "),
|
||||
listenPort =
|
||||
if (i.listenPort.isPresent) {
|
||||
i.listenPort.get().toString().trim()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
mtu = if (i.mtu.isPresent) i.mtu.get().toString().trim() else "",
|
||||
)
|
||||
}
|
||||
|
||||
fun from(i: org.amnezia.awg.config.Interface): InterfaceProxy {
|
||||
return InterfaceProxy(
|
||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||
addresses = i.addresses.joinToString(", ").trim(),
|
||||
dnsServers = (i.dnsServers + i.dnsSearchDomains).joinToString(", ").replace("/", "").trim(),
|
||||
listenPort =
|
||||
if (i.listenPort.isPresent) {
|
||||
i.listenPort.get().toString().trim()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
mtu = if (i.mtu.isPresent) i.mtu.get().toString().trim() else "",
|
||||
junkPacketCount =
|
||||
if (i.junkPacketCount.isPresent) {
|
||||
i.junkPacketCount.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
junkPacketMinSize =
|
||||
if (i.junkPacketMinSize.isPresent) {
|
||||
i.junkPacketMinSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
junkPacketMaxSize =
|
||||
if (i.junkPacketMaxSize.isPresent) {
|
||||
i.junkPacketMaxSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
initPacketJunkSize =
|
||||
if (i.initPacketJunkSize.isPresent) {
|
||||
i.initPacketJunkSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
responsePacketJunkSize =
|
||||
if (i.responsePacketJunkSize.isPresent) {
|
||||
i.responsePacketJunkSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
initPacketMagicHeader =
|
||||
if (i.initPacketMagicHeader.isPresent) {
|
||||
i.initPacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
responsePacketMagicHeader =
|
||||
if (i.responsePacketMagicHeader.isPresent) {
|
||||
i.responsePacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
transportPacketMagicHeader =
|
||||
if (i.transportPacketMagicHeader.isPresent) {
|
||||
i.transportPacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
underloadPacketMagicHeader =
|
||||
if (i.underloadPacketMagicHeader.isPresent) {
|
||||
i.underloadPacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,6 @@ import androidx.compose.ui.platform.LocalClipboardManager
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.os.ConfigurationCompat
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
@@ -59,6 +58,7 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import java.text.Collator
|
||||
import java.util.Locale
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
@@ -74,8 +74,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
||||
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
||||
val isRunningOnTv = remember { context.isRunningOnTv() }
|
||||
|
||||
val currentLocale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
|
||||
val collator = Collator.getInstance(currentLocale)
|
||||
val collator = Collator.getInstance(Locale.getDefault())
|
||||
|
||||
val sortedTunnels = remember(uiState.tunnels) {
|
||||
uiState.tunnels.sortedWith(compareBy(collator) { it.name })
|
||||
@@ -199,10 +198,10 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
||||
)
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(5.dp.scaledHeight(), Alignment.Top),
|
||||
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.Top),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize().padding(padding)
|
||||
.fillMaxSize().padding(padding).padding(top = 24.dp.scaledHeight())
|
||||
.overscroll(ScrollableDefaults.overscrollEffect())
|
||||
.nestedScroll(nestedScrollConnection),
|
||||
state = rememberLazyListState(0, uiState.tunnels.count()),
|
||||
|
||||
+2
-2
@@ -95,7 +95,7 @@ fun TunnelRowItem(
|
||||
IconButton(
|
||||
onClick = {
|
||||
navController.navigate(
|
||||
Route.Option(tunnel.id),
|
||||
Route.TunnelOptions(tunnel.id),
|
||||
)
|
||||
},
|
||||
) {
|
||||
@@ -128,7 +128,7 @@ fun TunnelRowItem(
|
||||
onClick = {
|
||||
onHold()
|
||||
navController.navigate(
|
||||
Route.Option(tunnel.id),
|
||||
Route.TunnelOptions(tunnel.id),
|
||||
)
|
||||
},
|
||||
) {
|
||||
|
||||
+18
-15
@@ -21,22 +21,21 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.toThreeDecimalPlaceSt
|
||||
@Composable
|
||||
fun TunnelStatisticsRow(statistics: TunnelStatistics?, tunnelConfig: TunnelConfig) {
|
||||
val config = TunnelConfig.configFromAmQuick(tunnelConfig.wgQuick)
|
||||
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(end = 10.dp, bottom = 10.dp, start = 45.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(30.dp, Alignment.Start),
|
||||
) {
|
||||
config.peers.forEach {
|
||||
val peerId = it.publicKey.toBase64().subSequence(0, 3).toString() + "***"
|
||||
val peerRx = statistics?.peerStats(it.publicKey)?.rxBytes ?: 0
|
||||
val peerTx = statistics?.peerStats(it.publicKey)?.txBytes ?: 0
|
||||
config.peers.forEach { peer ->
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(end = 10.dp, bottom = 10.dp, start = 45.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(5.dp, Alignment.Start),
|
||||
) {
|
||||
val peerId = peer.publicKey.toBase64().subSequence(0, 3).toString() + "***"
|
||||
val peerRx = statistics?.peerStats(peer.publicKey)?.rxBytes ?: 0
|
||||
val peerTx = statistics?.peerStats(peer.publicKey)?.txBytes ?: 0
|
||||
val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString()
|
||||
val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString()
|
||||
val handshake = statistics?.peerStats(it.publicKey)?.latestHandshakeEpochMillis?.let {
|
||||
val handshake = statistics?.peerStats(peer.publicKey)?.latestHandshakeEpochMillis?.let {
|
||||
if (it == 0L) {
|
||||
stringResource(R.string.never)
|
||||
} else {
|
||||
@@ -56,7 +55,11 @@ fun TunnelStatisticsRow(statistics: TunnelStatistics?, tunnelConfig: TunnelConfi
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||
) {
|
||||
Text(stringResource(R.string.handshake) + ": $handshake", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
|
||||
Text(
|
||||
stringResource(R.string.handshake) + ": $handshake",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.outline,
|
||||
)
|
||||
Text("rx: $peerRxMB MB", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
|
||||
}
|
||||
}
|
||||
|
||||
+48
-44
@@ -5,8 +5,11 @@ import androidx.compose.foundation.focusable
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.imePadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
@@ -46,7 +49,6 @@ import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavControll
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.topPadding
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchNotificationSettings
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
@@ -54,6 +56,7 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.showToast
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel: AppViewModel, uiState: AppUiState) {
|
||||
val context = LocalContext.current
|
||||
@@ -92,8 +95,8 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
||||
modifier =
|
||||
Modifier
|
||||
.verticalScroll(rememberScrollState())
|
||||
.fillMaxSize()
|
||||
.padding(top = topPadding)
|
||||
.fillMaxSize().systemBarsPadding().imePadding()
|
||||
.padding(top = 24.dp.scaledHeight())
|
||||
.padding(bottom = 40.dp.scaledHeight())
|
||||
.padding(horizontal = 24.dp.scaledWidth())
|
||||
.then(
|
||||
@@ -151,50 +154,51 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
||||
),
|
||||
)
|
||||
if (!isRunningOnTv) {
|
||||
addAll(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
Icons.Outlined.VpnLock,
|
||||
{
|
||||
ScaledSwitch(
|
||||
enabled = !(
|
||||
(
|
||||
uiState.settings.isTunnelOnWifiEnabled ||
|
||||
uiState.settings.isTunnelOnEthernetEnabled ||
|
||||
uiState.settings.isTunnelOnMobileDataEnabled
|
||||
) &&
|
||||
uiState.settings.isAutoTunnelEnabled
|
||||
),
|
||||
onClick = { appViewModel.onToggleAlwaysOnVPN() },
|
||||
checked = uiState.settings.isAlwaysOnVpnEnabled,
|
||||
)
|
||||
},
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.always_on_vpn_support),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { appViewModel.onToggleAlwaysOnVPN() },
|
||||
),
|
||||
SelectionItem(
|
||||
Icons.Outlined.VpnKeyOff,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.kill_switch_options),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
navController.navigate(Route.KillSwitch)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton { navController.navigate(Route.KillSwitch) }
|
||||
},
|
||||
),
|
||||
add(
|
||||
SelectionItem(
|
||||
Icons.Outlined.VpnLock,
|
||||
{
|
||||
ScaledSwitch(
|
||||
enabled = !(
|
||||
(
|
||||
uiState.settings.isTunnelOnWifiEnabled ||
|
||||
uiState.settings.isTunnelOnEthernetEnabled ||
|
||||
uiState.settings.isTunnelOnMobileDataEnabled
|
||||
) &&
|
||||
uiState.settings.isAutoTunnelEnabled
|
||||
),
|
||||
onClick = { appViewModel.onToggleAlwaysOnVPN() },
|
||||
checked = uiState.settings.isAlwaysOnVpnEnabled,
|
||||
)
|
||||
},
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.always_on_vpn_support),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { appViewModel.onToggleAlwaysOnVPN() },
|
||||
),
|
||||
)
|
||||
}
|
||||
add(
|
||||
SelectionItem(
|
||||
Icons.Outlined.VpnKeyOff,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.kill_switch_options),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
navController.navigate(Route.KillSwitch)
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton { navController.navigate(Route.KillSwitch) }
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
add(
|
||||
SelectionItem(
|
||||
Icons.Outlined.Restore,
|
||||
|
||||
+18
@@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Contrast
|
||||
import androidx.compose.material.icons.outlined.Notifications
|
||||
import androidx.compose.material.icons.outlined.Translate
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
@@ -13,6 +14,7 @@ import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
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 com.zaneschepke.wireguardautotunnel.R
|
||||
@@ -22,12 +24,14 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelec
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchNotificationSettings
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
|
||||
@Composable
|
||||
fun AppearanceScreen() {
|
||||
val navController = LocalNavController.current
|
||||
val context = LocalContext.current
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
@@ -55,6 +59,20 @@ fun AppearanceScreen() {
|
||||
),
|
||||
),
|
||||
)
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
Icons.Outlined.Notifications,
|
||||
title = { Text(stringResource(R.string.notifications), style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface)) },
|
||||
onClick = {
|
||||
context.launchNotificationSettings()
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton { context.launchNotificationSettings() }
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
|
||||
+4
-2
@@ -60,8 +60,10 @@ fun LanguageScreen(appUiState: AppUiState, appViewModel: AppViewModel) {
|
||||
appViewModel.onLocaleChange(LocaleUtil.OPTION_PHONE_LANGUAGE)
|
||||
},
|
||||
trailing = {
|
||||
if (appUiState.generalState.locale == LocaleUtil.OPTION_PHONE_LANGUAGE) {
|
||||
SelectedLabel()
|
||||
with(appUiState.generalState.locale) {
|
||||
if (this == LocaleUtil.OPTION_PHONE_LANGUAGE || this == null) {
|
||||
SelectedLabel()
|
||||
}
|
||||
}
|
||||
},
|
||||
ripple = false,
|
||||
|
||||
+1
-1
@@ -106,7 +106,7 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltV
|
||||
showLocationServicesAlertDialog,
|
||||
onDismiss = { showLocationServicesAlertDialog = false },
|
||||
onAttest = {
|
||||
viewModel.onToggleTunnelOnWifi()
|
||||
showLocationServicesAlertDialog = false
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
+3
-2
@@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.LocationOn
|
||||
import androidx.compose.material.icons.rounded.PermScanWifi
|
||||
@@ -27,7 +28,6 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionIte
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.topPadding
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.goFromRoot
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchAppSettings
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
@@ -46,7 +46,8 @@ fun LocationDisclosureScreen(appViewModel: AppViewModel, appUiState: AppUiState)
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
||||
modifier =
|
||||
Modifier.fillMaxSize().padding(top = topPadding).padding(horizontal = 24.dp.scaledWidth()),
|
||||
Modifier.fillMaxSize().systemBarsPadding().padding(top = 24.dp.scaledHeight())
|
||||
.padding(horizontal = 24.dp.scaledWidth()),
|
||||
) {
|
||||
val icon = Icons.Rounded.PermScanWifi
|
||||
Icon(
|
||||
|
||||
+23
-16
@@ -25,7 +25,10 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionIte
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.permission.vpn.withVpnPermission
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchVpnSettings
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
@@ -38,6 +41,8 @@ fun KillSwitchScreen(uiState: AppUiState, appViewModel: AppViewModel) {
|
||||
|
||||
fun toggleVpnKillSwitch() {
|
||||
with(uiState.settings) {
|
||||
// TODO improve this error message
|
||||
if (isKernelEnabled) return SnackbarController.showMessage(StringValue.StringResource(R.string.kernel_not_supported))
|
||||
if (isVpnKillSwitchEnabled) {
|
||||
appViewModel.onToggleVpnKillSwitch(false)
|
||||
} else {
|
||||
@@ -66,23 +71,25 @@ fun KillSwitchScreen(uiState: AppUiState, appViewModel: AppViewModel) {
|
||||
.padding(top = 24.dp.scaledHeight())
|
||||
.padding(horizontal = 24.dp.scaledWidth()),
|
||||
) {
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
Icons.Outlined.AdminPanelSettings,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.native_kill_switch),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { context.launchVpnSettings() },
|
||||
trailing = {
|
||||
ForwardButton { context.launchVpnSettings() }
|
||||
},
|
||||
if (!context.isRunningOnTv()) {
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
Icons.Outlined.AdminPanelSettings,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.native_kill_switch),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = { context.launchVpnSettings() },
|
||||
trailing = {
|
||||
ForwardButton { context.launchVpnSettings() }
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
}
|
||||
SurfaceSelectionGroupButton(
|
||||
buildList {
|
||||
add(
|
||||
|
||||
+3
-3
@@ -4,6 +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.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
@@ -40,7 +41,6 @@ import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.label.VersionLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.topPadding
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchSupportEmail
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
@@ -71,7 +71,7 @@ fun SupportScreen(appUiState: AppUiState, appViewModel: AppViewModel) {
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = topPadding)
|
||||
.systemBarsPadding().padding(top = 24.dp.scaledHeight())
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(horizontal = 24.dp.scaledWidth()),
|
||||
) {
|
||||
@@ -174,7 +174,7 @@ fun SupportScreen(appUiState: AppUiState, appViewModel: AppViewModel) {
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.chat_description),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.outlined.CallSplit
|
||||
import androidx.compose.material.icons.outlined.Bolt
|
||||
import androidx.compose.material.icons.outlined.Edit
|
||||
import androidx.compose.material.icons.outlined.Star
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
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.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
|
||||
@Composable
|
||||
fun OptionsScreen(tunnelOptionsViewModel: TunnelOptionsViewModel = hiltViewModel(), appUiState: AppUiState, tunnelId: Int) {
|
||||
val navController = LocalNavController.current
|
||||
val config = remember { appUiState.tunnels.first { it.id == tunnelId } }
|
||||
|
||||
var currentText by remember { mutableStateOf("") }
|
||||
|
||||
LaunchedEffect(config.tunnelNetworks) {
|
||||
currentText = ""
|
||||
}
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopNavBar(config.name)
|
||||
},
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(it)
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(top = 24.dp.scaledHeight())
|
||||
.padding(horizontal = 24.dp.scaledWidth()),
|
||||
) {
|
||||
SurfaceSelectionGroupButton(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
Icons.Outlined.Star,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.primary_tunnel),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
description = {
|
||||
Text(
|
||||
stringResource(R.string.set_primary_tunnel),
|
||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
config.isPrimaryTunnel,
|
||||
onClick = { tunnelOptionsViewModel.onTogglePrimaryTunnel(config) },
|
||||
)
|
||||
},
|
||||
onClick = { tunnelOptionsViewModel.onTogglePrimaryTunnel(config) },
|
||||
),
|
||||
SelectionItem(
|
||||
Icons.Outlined.Bolt,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.auto_tunneling),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
description = {
|
||||
Text(
|
||||
stringResource(R.string.tunnel_specific_settings),
|
||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
navController.navigate(Route.TunnelAutoTunnel(id = tunnelId))
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton { navController.navigate(Route.TunnelAutoTunnel(id = tunnelId)) }
|
||||
},
|
||||
),
|
||||
SelectionItem(
|
||||
Icons.Outlined.Edit,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.edit_tunnel),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
navController.navigate(Route.Config(id = tunnelId))
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton { navController.navigate(Route.Config(id = tunnelId)) }
|
||||
},
|
||||
),
|
||||
SelectionItem(
|
||||
Icons.AutoMirrored.Outlined.CallSplit,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.splt_tunneling),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
navController.navigate(Route.SplitTunnel(id = tunnelId))
|
||||
},
|
||||
trailing = {
|
||||
ForwardButton { navController.navigate(Route.SplitTunnel(id = tunnelId)) }
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
// GroupLabel(stringResource(R.string.quick_actions))
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.options
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
@@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class OptionsViewModel
|
||||
class TunnelOptionsViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
+662
@@ -0,0 +1,662 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.focusGroup
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.IntrinsicSize
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Save
|
||||
import androidx.compose.material.icons.rounded.ContentCopy
|
||||
import androidx.compose.material.icons.rounded.Delete
|
||||
import androidx.compose.material.icons.rounded.Refresh
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.toMutableStateList
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.platform.ClipboardManager
|
||||
import androidx.compose.ui.platform.LocalClipboardManager
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
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.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.InterfaceProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
import kotlinx.coroutines.launch
|
||||
import org.amnezia.awg.crypto.KeyPair
|
||||
|
||||
@Composable
|
||||
fun ConfigScreen(appUiState: AppUiState, appViewModel: AppViewModel, tunnelId: Int) {
|
||||
val context = LocalContext.current
|
||||
val snackbar = SnackbarController.current
|
||||
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
val navController = LocalNavController.current
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
val popBackStack by appViewModel.popBackStack.collectAsStateWithLifecycle(false)
|
||||
|
||||
val tunnelConfig by remember {
|
||||
derivedStateOf {
|
||||
appUiState.tunnels.first { it.id == tunnelId }
|
||||
}
|
||||
}
|
||||
|
||||
val configPair by remember {
|
||||
derivedStateOf {
|
||||
Pair(tunnelConfig.name, tunnelConfig.toAmConfig())
|
||||
}
|
||||
}
|
||||
|
||||
var tunnelName by remember {
|
||||
mutableStateOf(configPair.first)
|
||||
}
|
||||
|
||||
var interfaceState by remember {
|
||||
mutableStateOf(InterfaceProxy.from(configPair.second.`interface`))
|
||||
}
|
||||
|
||||
var showAmneziaValues by remember {
|
||||
mutableStateOf(configPair.second.`interface`.junkPacketCount.isPresent)
|
||||
}
|
||||
|
||||
var showScripts by remember {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
|
||||
val peersState = remember {
|
||||
configPair.second.peers.map { PeerProxy.from(it) }.toMutableStateList()
|
||||
}
|
||||
|
||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||
var isAuthenticated by remember { mutableStateOf(false) }
|
||||
|
||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||
|
||||
if (showAuthPrompt) {
|
||||
AuthorizationPrompt(
|
||||
onSuccess = {
|
||||
showAuthPrompt = false
|
||||
isAuthenticated = true
|
||||
},
|
||||
onError = {
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
context.getString(R.string.error_authentication_failed),
|
||||
)
|
||||
},
|
||||
onFailure = {
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
context.getString(R.string.error_authorization_failed),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(popBackStack) {
|
||||
if (popBackStack) navController.popBackStack()
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopNavBar(stringResource(R.string.edit_tunnel), trailing = {
|
||||
IconButton(onClick = {
|
||||
appViewModel.saveConfigChanges(
|
||||
tunnelConfig.copy(
|
||||
name = tunnelName,
|
||||
),
|
||||
peers = peersState,
|
||||
`interface` = interfaceState,
|
||||
)
|
||||
}) {
|
||||
val icon = Icons.Outlined.Save
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = icon.name,
|
||||
)
|
||||
}
|
||||
})
|
||||
},
|
||||
) { padding ->
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(padding)
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(top = 24.dp.scaledHeight())
|
||||
.padding(horizontal = 24.dp.scaledWidth()),
|
||||
) {
|
||||
Surface(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.Top),
|
||||
modifier = Modifier
|
||||
.padding(16.dp.scaledWidth())
|
||||
.focusGroup(),
|
||||
) {
|
||||
GroupLabel(
|
||||
stringResource(R.string.interface_),
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(id = R.string.show_amnezia_properties),
|
||||
checked = showAmneziaValues,
|
||||
onCheckChanged = {
|
||||
if (appUiState.settings.isKernelEnabled) {
|
||||
snackbar.showMessage(context.getString(R.string.amnezia_kernel_message))
|
||||
} else {
|
||||
showAmneziaValues = it
|
||||
}
|
||||
},
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(id = R.string.show_scripts),
|
||||
checked = showScripts,
|
||||
onCheckChanged = { checked ->
|
||||
if (appUiState.settings.isKernelEnabled) {
|
||||
showScripts = checked
|
||||
} else {
|
||||
scope.launch {
|
||||
appViewModel.requestRoot().onSuccess {
|
||||
showScripts = checked
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = tunnelName,
|
||||
onValueChange = { tunnelName = it },
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.name),
|
||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
OutlinedTextField(
|
||||
textStyle = MaterialTheme.typography.labelLarge,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { showAuthPrompt = true },
|
||||
value = interfaceState.privateKey,
|
||||
visualTransformation =
|
||||
if ((tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated) {
|
||||
VisualTransformation.None
|
||||
} else {
|
||||
PasswordVisualTransformation()
|
||||
},
|
||||
enabled = (tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated,
|
||||
onValueChange = { interfaceState = interfaceState.copy(privateKey = it) },
|
||||
trailingIcon = {
|
||||
IconButton(
|
||||
enabled = isAuthenticated,
|
||||
modifier = Modifier.focusRequester(FocusRequester.Default),
|
||||
onClick = {
|
||||
val keypair = KeyPair()
|
||||
interfaceState = interfaceState.copy(
|
||||
privateKey = keypair.privateKey.toBase64(),
|
||||
publicKey = keypair.publicKey.toBase64(),
|
||||
)
|
||||
},
|
||||
) {
|
||||
Icon(
|
||||
Icons.Rounded.Refresh,
|
||||
stringResource(R.string.rotate_keys),
|
||||
tint = if (isAuthenticated) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.outline,
|
||||
)
|
||||
}
|
||||
},
|
||||
label = { Text(stringResource(R.string.private_key)) },
|
||||
singleLine = true,
|
||||
placeholder = {
|
||||
Text(
|
||||
stringResource(R.string.base64_key),
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.outline,
|
||||
)
|
||||
},
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
OutlinedTextField(
|
||||
textStyle = MaterialTheme.typography.labelLarge,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.focusRequester(FocusRequester.Default),
|
||||
value = interfaceState.publicKey,
|
||||
enabled = false,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(publicKey = it)
|
||||
},
|
||||
trailingIcon = {
|
||||
IconButton(
|
||||
modifier = Modifier.focusRequester(FocusRequester.Default),
|
||||
onClick = {
|
||||
clipboardManager.setText(
|
||||
AnnotatedString(interfaceState.publicKey),
|
||||
)
|
||||
},
|
||||
) {
|
||||
Icon(
|
||||
Icons.Rounded.ContentCopy,
|
||||
stringResource(R.string.copy_public_key),
|
||||
tint = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
}
|
||||
},
|
||||
label = { Text(stringResource(R.string.public_key)) },
|
||||
singleLine = true,
|
||||
placeholder = {
|
||||
Text(
|
||||
stringResource(R.string.base64_key),
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.outline,
|
||||
)
|
||||
},
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.addresses,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(addresses = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.addresses),
|
||||
hint = stringResource(R.string.comma_separated_list),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(end = 5.dp),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.listenPort,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(listenPort = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.listen_port),
|
||||
hint = stringResource(R.string.random),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||
) {
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.dnsServers,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(dnsServers = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.dns_servers),
|
||||
hint = stringResource(R.string.comma_separated_list),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(3 / 5f)
|
||||
.padding(end = 5.dp),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.mtu,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(mtu = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.mtu),
|
||||
hint = stringResource(R.string.auto),
|
||||
modifier = Modifier.width(IntrinsicSize.Min),
|
||||
)
|
||||
}
|
||||
if (showScripts) {
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.preUp,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(preUp = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.pre_up),
|
||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.postUp,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(postUp = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.post_up),
|
||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.preDown,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(preDown = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.pre_down),
|
||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.postDown,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(postDown = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.post_down),
|
||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
if (showAmneziaValues) {
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.junkPacketCount,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(junkPacketCount = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_count),
|
||||
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.junkPacketMinSize,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(junkPacketMinSize = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_minimum_size),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.junk_packet_minimum_size,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.junkPacketMaxSize,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(junkPacketMaxSize = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_maximum_size),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.junk_packet_maximum_size,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.initPacketJunkSize,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(initPacketJunkSize = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.init_packet_junk_size),
|
||||
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.responsePacketJunkSize,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(responsePacketJunkSize = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.response_packet_junk_size),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.response_packet_junk_size,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.initPacketMagicHeader,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(initPacketMagicHeader = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.init_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.init_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.responsePacketMagicHeader,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(responsePacketMagicHeader = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.response_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.response_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.underloadPacketMagicHeader,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(underloadPacketMagicHeader = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.underload_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.underload_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = interfaceState.transportPacketMagicHeader,
|
||||
onValueChange = {
|
||||
interfaceState = interfaceState.copy(transportPacketMagicHeader = it)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.transport_packet_magic_header),
|
||||
hint =
|
||||
stringResource(
|
||||
R.string.transport_packet_magic_header,
|
||||
).lowercase(),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
peersState.forEachIndexed { index, peer ->
|
||||
Surface(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.Top),
|
||||
modifier = Modifier
|
||||
.padding(16.dp.scaledWidth())
|
||||
.focusGroup(),
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier.fillMaxWidth(),
|
||||
) {
|
||||
GroupLabel(
|
||||
stringResource(R.string.peer),
|
||||
)
|
||||
IconButton(onClick = {
|
||||
peersState.removeAt(index)
|
||||
}) {
|
||||
val icon = Icons.Rounded.Delete
|
||||
Icon(icon, icon.name)
|
||||
}
|
||||
}
|
||||
|
||||
ConfigurationTextBox(
|
||||
value = peer.publicKey,
|
||||
onValueChange = { value ->
|
||||
peersState[index] = peersState[index].copy(publicKey = value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.public_key),
|
||||
hint = stringResource(R.string.base64_key),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = peer.preSharedKey,
|
||||
onValueChange = { value ->
|
||||
peersState[index] = peersState[index].copy(preSharedKey = value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.preshared_key),
|
||||
hint = stringResource(R.string.optional),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
OutlinedTextField(
|
||||
textStyle = MaterialTheme.typography.labelLarge,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
value = peer.persistentKeepalive,
|
||||
enabled = true,
|
||||
onValueChange = { value ->
|
||||
peersState[index] = peersState[index].copy(persistentKeepalive = value)
|
||||
},
|
||||
trailingIcon = {
|
||||
Text(
|
||||
stringResource(R.string.seconds),
|
||||
modifier = Modifier.padding(end = 10.dp),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
)
|
||||
},
|
||||
label = { Text(stringResource(R.string.persistent_keepalive), style = MaterialTheme.typography.labelMedium) },
|
||||
singleLine = true,
|
||||
placeholder = {
|
||||
Text(stringResource(R.string.optional_no_recommend), style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.outline)
|
||||
},
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = peer.endpoint,
|
||||
onValueChange = { value ->
|
||||
peersState[index] = peersState[index].copy(endpoint = value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.endpoint),
|
||||
hint = stringResource(R.string.endpoint).lowercase(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
OutlinedTextField(
|
||||
textStyle = MaterialTheme.typography.labelLarge,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
value = peer.allowedIps,
|
||||
enabled = true,
|
||||
onValueChange = { value ->
|
||||
peersState[index] = peersState[index].copy(allowedIps = value)
|
||||
},
|
||||
label = { Text(stringResource(R.string.allowed_ips)) },
|
||||
singleLine = true,
|
||||
placeholder = {
|
||||
Text(stringResource(R.string.comma_separated_list), style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.outline)
|
||||
},
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(bottom = 140.dp),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(onClick = {
|
||||
peersState.add(PeerProxy())
|
||||
}) {
|
||||
Text(stringResource(R.string.add_peer))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model
|
||||
|
||||
import com.wireguard.config.Interface
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.joinAndTrim
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toTrimmedList
|
||||
|
||||
data class InterfaceProxy(
|
||||
val privateKey: String = "",
|
||||
val publicKey: String = "",
|
||||
val addresses: String = "",
|
||||
val dnsServers: String = "",
|
||||
val listenPort: String = "",
|
||||
val mtu: String = "",
|
||||
val includedApplications: MutableSet<String> = mutableSetOf(),
|
||||
val excludedApplications: MutableSet<String> = mutableSetOf(),
|
||||
val junkPacketCount: String = "",
|
||||
val junkPacketMinSize: String = "",
|
||||
val junkPacketMaxSize: String = "",
|
||||
val initPacketJunkSize: String = "",
|
||||
val responsePacketJunkSize: String = "",
|
||||
val initPacketMagicHeader: String = "",
|
||||
val responsePacketMagicHeader: String = "",
|
||||
val underloadPacketMagicHeader: String = "",
|
||||
val transportPacketMagicHeader: String = "",
|
||||
val preUp: String = "",
|
||||
val postUp: String = "",
|
||||
val preDown: String = "",
|
||||
val postDown: String = "",
|
||||
) {
|
||||
|
||||
fun toWgInterface(): Interface {
|
||||
return Interface.Builder().apply {
|
||||
parseAddresses(addresses)
|
||||
parsePrivateKey(privateKey)
|
||||
if (dnsServers.isNotBlank()) parseDnsServers(dnsServers)
|
||||
if (mtu.isNotBlank()) parseMtu(mtu)
|
||||
if (listenPort.isNotBlank()) parseListenPort(listenPort)
|
||||
includeApplications(includedApplications)
|
||||
excludeApplications(excludedApplications)
|
||||
preUp.toTrimmedList().forEach { parsePreUp(it) }
|
||||
postUp.toTrimmedList().forEach { parsePostUp(it) }
|
||||
preDown.toTrimmedList().forEach { parsePreDown(it) }
|
||||
postDown.toTrimmedList().forEach { parsePostDown(it) }
|
||||
}.build()
|
||||
}
|
||||
|
||||
fun toAmInterface(): org.amnezia.awg.config.Interface {
|
||||
return org.amnezia.awg.config.Interface.Builder().apply {
|
||||
parseAddresses(addresses)
|
||||
parsePrivateKey(privateKey)
|
||||
if (dnsServers.isNotBlank()) parseDnsServers(dnsServers)
|
||||
if (mtu.isNotBlank()) parseMtu(mtu)
|
||||
if (listenPort.isNotBlank()) parseListenPort(listenPort)
|
||||
includeApplications(includedApplications)
|
||||
excludeApplications(excludedApplications)
|
||||
preUp.toTrimmedList().forEach { parsePreUp(it) }
|
||||
postUp.toTrimmedList().forEach { parsePostUp(it) }
|
||||
preDown.toTrimmedList().forEach { parsePreDown(it) }
|
||||
postDown.toTrimmedList().forEach { parsePostDown(it) }
|
||||
if (junkPacketCount.isNotBlank()) parseJunkPacketCount(junkPacketCount)
|
||||
if (junkPacketMinSize.isNotBlank()) parseJunkPacketMinSize(junkPacketMinSize)
|
||||
if (junkPacketMaxSize.isNotBlank()) parseJunkPacketMaxSize(junkPacketMaxSize)
|
||||
if (initPacketJunkSize.isNotBlank()) parseInitPacketJunkSize(initPacketJunkSize)
|
||||
if (responsePacketJunkSize.isNotBlank()) parseResponsePacketJunkSize(responsePacketJunkSize)
|
||||
if (initPacketMagicHeader.isNotBlank()) parseInitPacketMagicHeader(initPacketMagicHeader)
|
||||
if (responsePacketMagicHeader.isNotBlank()) parseResponsePacketMagicHeader(responsePacketMagicHeader)
|
||||
if (underloadPacketMagicHeader.isNotBlank()) parseUnderloadPacketMagicHeader(underloadPacketMagicHeader)
|
||||
if (transportPacketMagicHeader.isNotBlank()) parseTransportPacketMagicHeader(transportPacketMagicHeader)
|
||||
}.build()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(i: Interface): InterfaceProxy {
|
||||
return InterfaceProxy(
|
||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||
addresses = i.addresses.joinToString(", ").trim(),
|
||||
dnsServers = listOf(
|
||||
i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
||||
i.dnsSearchDomains.joinAndTrim(),
|
||||
).filter { it.isNotEmpty() }.joinToString(", "),
|
||||
listenPort =
|
||||
if (i.listenPort.isPresent) {
|
||||
i.listenPort.get().toString().trim()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
mtu = if (i.mtu.isPresent) i.mtu.get().toString().trim() else "",
|
||||
includedApplications = i.includedApplications.toMutableSet(),
|
||||
excludedApplications = i.excludedApplications.toMutableSet(),
|
||||
preUp = i.preUp.joinAndTrim(),
|
||||
postUp = i.postUp.joinAndTrim(),
|
||||
preDown = i.preDown.joinAndTrim(),
|
||||
postDown = i.postDown.joinAndTrim(),
|
||||
)
|
||||
}
|
||||
|
||||
fun from(i: org.amnezia.awg.config.Interface): InterfaceProxy {
|
||||
return InterfaceProxy(
|
||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||
addresses = i.addresses.joinToString(", ").trim(),
|
||||
dnsServers = (i.dnsServers + i.dnsSearchDomains).joinToString(", ").replace("/", "").trim(),
|
||||
listenPort =
|
||||
if (i.listenPort.isPresent) {
|
||||
i.listenPort.get().toString().trim()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
mtu = if (i.mtu.isPresent) i.mtu.get().toString().trim() else "",
|
||||
includedApplications = i.includedApplications.toMutableSet(),
|
||||
excludedApplications = i.excludedApplications.toMutableSet(),
|
||||
preUp = i.preUp.joinAndTrim(),
|
||||
postUp = i.postUp.joinAndTrim(),
|
||||
preDown = i.preDown.joinAndTrim(),
|
||||
postDown = i.postDown.joinAndTrim(),
|
||||
junkPacketCount =
|
||||
if (i.junkPacketCount.isPresent) {
|
||||
i.junkPacketCount.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
junkPacketMinSize =
|
||||
if (i.junkPacketMinSize.isPresent) {
|
||||
i.junkPacketMinSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
junkPacketMaxSize =
|
||||
if (i.junkPacketMaxSize.isPresent) {
|
||||
i.junkPacketMaxSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
initPacketJunkSize =
|
||||
if (i.initPacketJunkSize.isPresent) {
|
||||
i.initPacketJunkSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
responsePacketJunkSize =
|
||||
if (i.responsePacketJunkSize.isPresent) {
|
||||
i.responsePacketJunkSize.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
initPacketMagicHeader =
|
||||
if (i.initPacketMagicHeader.isPresent) {
|
||||
i.initPacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
responsePacketMagicHeader =
|
||||
if (i.responsePacketMagicHeader.isPresent) {
|
||||
i.responsePacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
transportPacketMagicHeader =
|
||||
if (i.transportPacketMagicHeader.isPresent) {
|
||||
i.transportPacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
underloadPacketMagicHeader =
|
||||
if (i.underloadPacketMagicHeader.isPresent) {
|
||||
i.underloadPacketMagicHeader.get()
|
||||
.toString()
|
||||
} else {
|
||||
""
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
-2
@@ -1,14 +1,34 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config.model
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model
|
||||
|
||||
import com.wireguard.config.Peer
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.joinAndTrim
|
||||
|
||||
data class PeerProxy(
|
||||
val publicKey: String = "",
|
||||
val preSharedKey: String = "",
|
||||
val persistentKeepalive: String = "",
|
||||
val endpoint: String = "",
|
||||
val allowedIps: String = IPV4_WILDCARD.joinToString(", ").trim(),
|
||||
val allowedIps: String = IPV4_WILDCARD.joinAndTrim(),
|
||||
) {
|
||||
fun toWgPeer(): Peer {
|
||||
return Peer.Builder().apply {
|
||||
parsePublicKey(publicKey)
|
||||
if (preSharedKey.isNotBlank()) parsePreSharedKey(preSharedKey)
|
||||
if (persistentKeepalive.isNotBlank()) parsePersistentKeepalive(persistentKeepalive)
|
||||
parseEndpoint(endpoint)
|
||||
parseAllowedIPs(allowedIps)
|
||||
}.build()
|
||||
}
|
||||
fun toAmPeer(): org.amnezia.awg.config.Peer {
|
||||
return org.amnezia.awg.config.Peer.Builder().apply {
|
||||
parsePublicKey(publicKey)
|
||||
if (preSharedKey.isNotBlank()) parsePreSharedKey(preSharedKey)
|
||||
if (persistentKeepalive.isNotBlank()) parsePersistentKeepalive(persistentKeepalive)
|
||||
parseEndpoint(endpoint)
|
||||
parseAllowedIPs(allowedIps)
|
||||
}.build()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(peer: Peer): PeerProxy {
|
||||
return PeerProxy(
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.splittunnel
|
||||
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.AllInclusive
|
||||
import androidx.compose.material.icons.filled.Remove
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
|
||||
enum class SplitOptions {
|
||||
INCLUDE,
|
||||
ALL,
|
||||
EXCLUDE,
|
||||
;
|
||||
|
||||
fun icon(): ImageVector {
|
||||
return when (this) {
|
||||
ALL -> Icons.Filled.AllInclusive
|
||||
INCLUDE -> Icons.Filled.Add
|
||||
EXCLUDE -> Icons.Filled.Remove
|
||||
}
|
||||
}
|
||||
|
||||
fun text(): StringValue {
|
||||
return when (this) {
|
||||
ALL -> StringValue.StringResource(R.string.all)
|
||||
INCLUDE -> StringValue.StringResource(R.string.include)
|
||||
EXCLUDE -> StringValue.StringResource(R.string.exclude)
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.splittunnel
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
|
||||
data class SplitTunnelApp(
|
||||
val icon: Drawable,
|
||||
val name: String,
|
||||
val `package`: String,
|
||||
)
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.splittunnel
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Check
|
||||
import androidx.compose.material.icons.outlined.Save
|
||||
import androidx.compose.material.icons.outlined.Search
|
||||
import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.MultiChoiceSegmentedButtonRow
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.SegmentedButton
|
||||
import androidx.compose.material3.SegmentedButtonDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.toMutableStateList
|
||||
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.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.SelectionItemButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.InterfaceProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
import java.text.Collator
|
||||
import java.util.Locale
|
||||
|
||||
@Composable
|
||||
fun SplitTunnelScreen(appUiState: AppUiState, tunnelId: Int, viewModel: AppViewModel) {
|
||||
val context = LocalContext.current
|
||||
val navController = LocalNavController.current
|
||||
|
||||
val inputHeight = 45.dp
|
||||
|
||||
val collator = Collator.getInstance(Locale.getDefault())
|
||||
|
||||
val popBackStack by viewModel.popBackStack.collectAsStateWithLifecycle(false)
|
||||
|
||||
LaunchedEffect(popBackStack) {
|
||||
if (popBackStack) navController.popBackStack()
|
||||
}
|
||||
|
||||
val config by remember { derivedStateOf { appUiState.tunnels.first { it.id == tunnelId } } }
|
||||
|
||||
val splitTunnelApps by viewModel.splitTunnelApps.collectAsStateWithLifecycle()
|
||||
|
||||
var proxyInterface by remember { mutableStateOf(InterfaceProxy()) }
|
||||
|
||||
var selectedSplitOption by remember { mutableStateOf(SplitOptions.ALL) }
|
||||
|
||||
val selectedPackages = remember { mutableStateListOf<String>() }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
proxyInterface = InterfaceProxy.from(config.toWgConfig().`interface`)
|
||||
val pair = when {
|
||||
proxyInterface.excludedApplications.isNotEmpty() -> Pair(SplitOptions.EXCLUDE, proxyInterface.excludedApplications)
|
||||
proxyInterface.includedApplications.isNotEmpty() -> Pair(SplitOptions.INCLUDE, proxyInterface.includedApplications)
|
||||
else -> Pair(SplitOptions.ALL, mutableSetOf())
|
||||
}
|
||||
selectedSplitOption = pair.first
|
||||
selectedPackages.addAll(pair.second)
|
||||
}
|
||||
|
||||
var query: String by remember { mutableStateOf("") }
|
||||
|
||||
val sortedPackages by remember {
|
||||
derivedStateOf {
|
||||
splitTunnelApps.sortedWith(compareBy(collator) { it.name }).filter { it.name.contains(query) }.toMutableStateList()
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
// clean up any split tunnel packages for apps that were uninstalled
|
||||
viewModel.cleanUpUninstalledApps(config, splitTunnelApps.map { it.`package` })
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopNavBar(stringResource(R.string.tunneling_apps), trailing = {
|
||||
IconButton(onClick = {
|
||||
proxyInterface.apply {
|
||||
includedApplications.clear()
|
||||
excludedApplications.clear()
|
||||
}
|
||||
when (selectedSplitOption) {
|
||||
SplitOptions.INCLUDE -> proxyInterface.includedApplications.apply {
|
||||
addAll(selectedPackages)
|
||||
}
|
||||
SplitOptions.EXCLUDE -> proxyInterface.excludedApplications.apply {
|
||||
addAll(selectedPackages)
|
||||
}
|
||||
SplitOptions.ALL -> Unit
|
||||
}
|
||||
viewModel.saveConfigChanges(config, `interface` = proxyInterface)
|
||||
}) {
|
||||
val icon = Icons.Outlined.Save
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = icon.name,
|
||||
)
|
||||
}
|
||||
})
|
||||
},
|
||||
) { padding ->
|
||||
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.CenterVertically),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(padding)
|
||||
.padding(top = 24.dp.scaledHeight()),
|
||||
) {
|
||||
MultiChoiceSegmentedButtonRow(
|
||||
modifier = Modifier.background(color = MaterialTheme.colorScheme.background).fillMaxWidth()
|
||||
.padding(horizontal = 24.dp.scaledWidth()).height(inputHeight),
|
||||
) {
|
||||
SplitOptions.entries.forEachIndexed { index, entry ->
|
||||
val active = selectedSplitOption == entry
|
||||
SegmentedButton(
|
||||
shape = SegmentedButtonDefaults.itemShape(index = index, count = SplitOptions.entries.size, baseShape = RoundedCornerShape(8.dp)),
|
||||
icon = {
|
||||
SegmentedButtonDefaults.Icon(active = active, activeContent = {
|
||||
val icon = Icons.Outlined.Check
|
||||
Icon(imageVector = icon, icon.name, tint = MaterialTheme.colorScheme.primary, modifier = Modifier.size(SegmentedButtonDefaults.IconSize))
|
||||
}) {
|
||||
Icon(
|
||||
imageVector = entry.icon(),
|
||||
contentDescription = entry.icon().name,
|
||||
modifier = Modifier.size(SegmentedButtonDefaults.IconSize),
|
||||
)
|
||||
}
|
||||
},
|
||||
colors = SegmentedButtonDefaults.colors().copy(
|
||||
activeContainerColor = MaterialTheme.colorScheme.surface,
|
||||
inactiveContainerColor = MaterialTheme.colorScheme.background,
|
||||
),
|
||||
onCheckedChange = {
|
||||
selectedSplitOption = entry
|
||||
},
|
||||
checked = active,
|
||||
) {
|
||||
Text(
|
||||
entry.text().asString(context)
|
||||
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() },
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (selectedSplitOption != SplitOptions.ALL) {
|
||||
Column(
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(),
|
||||
) {
|
||||
CustomTextField(
|
||||
textStyle = MaterialTheme.typography.labelMedium.copy(
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
),
|
||||
value = query,
|
||||
onValueChange = { input ->
|
||||
query = input
|
||||
},
|
||||
interactionSource = remember { MutableInteractionSource() },
|
||||
label = {},
|
||||
leading = {
|
||||
val icon = Icons.Outlined.Search
|
||||
Icon(icon, icon.name)
|
||||
},
|
||||
containerColor = MaterialTheme.colorScheme.background,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth().height(inputHeight).padding(horizontal = 24.dp.scaledWidth()),
|
||||
singleLine = true,
|
||||
keyboardOptions =
|
||||
KeyboardOptions(
|
||||
capitalization = KeyboardCapitalization.None,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
keyboardActions = KeyboardActions(),
|
||||
)
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
contentPadding = PaddingValues(top = 10.dp),
|
||||
) {
|
||||
items(sortedPackages, key = { it.`package` }) { app ->
|
||||
val checked = selectedPackages.contains(app.`package`)
|
||||
val onClick = {
|
||||
if (checked) selectedPackages.remove(app.`package`) else selectedPackages.add(app.`package`)
|
||||
}
|
||||
SelectionItemButton(
|
||||
{
|
||||
Image(
|
||||
rememberDrawablePainter(app.icon),
|
||||
app.name,
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(horizontal = 24.dp.scaledWidth())
|
||||
.size(
|
||||
iconSize,
|
||||
),
|
||||
)
|
||||
},
|
||||
buttonText = app.name,
|
||||
onClick = {
|
||||
onClick()
|
||||
},
|
||||
trailing = {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Checkbox(
|
||||
checked = checked,
|
||||
onCheckedChange = {
|
||||
onClick()
|
||||
},
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
-62
@@ -1,4 +1,4 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.options
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.tunnelautotunnel
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
@@ -11,14 +11,11 @@ import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Edit
|
||||
import androidx.compose.material.icons.outlined.NetworkPing
|
||||
import androidx.compose.material.icons.outlined.PhoneAndroid
|
||||
import androidx.compose.material.icons.outlined.Security
|
||||
import androidx.compose.material.icons.outlined.SettingsEthernet
|
||||
import androidx.compose.material.icons.outlined.Star
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
@@ -37,13 +34,10 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
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.SurfaceSelectionGroupButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.SubmitConfigurationTextBox
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.TrustedNetworkTextBox
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.WildcardsLabel
|
||||
@@ -54,9 +48,8 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||
|
||||
@Composable
|
||||
fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiState: AppUiState, tunnelId: Int) {
|
||||
val navController = LocalNavController.current
|
||||
val config = appUiState.tunnels.first { it.id == tunnelId }
|
||||
fun TunnelAutoTunnelScreen(appUiState: AppUiState, tunnelId: Int, tunnelAutoTunnelViewModel: TunnelAutoTunnelViewModel = hiltViewModel()) {
|
||||
val config = remember { appUiState.tunnels.first { it.id == tunnelId } }
|
||||
|
||||
var currentText by remember { mutableStateOf("") }
|
||||
|
||||
@@ -65,59 +58,24 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
}
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopNavBar(config.name, trailing = {
|
||||
IconButton(onClick = {
|
||||
navController.navigate(
|
||||
Route.Config(config.id),
|
||||
)
|
||||
}) {
|
||||
val icon = Icons.Outlined.Edit
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = icon.name,
|
||||
)
|
||||
}
|
||||
})
|
||||
TopNavBar(config.name)
|
||||
},
|
||||
) {
|
||||
) { padding ->
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(it)
|
||||
.padding(padding)
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(top = 24.dp.scaledHeight())
|
||||
.padding(horizontal = 24.dp.scaledWidth()),
|
||||
) {
|
||||
GroupLabel(stringResource(R.string.auto_tunneling))
|
||||
SurfaceSelectionGroupButton(
|
||||
buildList {
|
||||
addAll(
|
||||
listOf(
|
||||
SelectionItem(
|
||||
Icons.Outlined.Star,
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.primary_tunnel),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||
)
|
||||
},
|
||||
description = {
|
||||
Text(
|
||||
stringResource(R.string.set_primary_tunnel),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.outline),
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
config.isPrimaryTunnel,
|
||||
onClick = { optionsViewModel.onTogglePrimaryTunnel(config) },
|
||||
)
|
||||
},
|
||||
onClick = { optionsViewModel.onTogglePrimaryTunnel(config) },
|
||||
),
|
||||
SelectionItem(
|
||||
Icons.Outlined.PhoneAndroid,
|
||||
title = {
|
||||
@@ -129,16 +87,16 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
description = {
|
||||
Text(
|
||||
stringResource(R.string.mobile_data_tunnel),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.outline),
|
||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
config.isMobileDataTunnel,
|
||||
onClick = { optionsViewModel.onToggleIsMobileDataTunnel(config) },
|
||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsMobileDataTunnel(config) },
|
||||
)
|
||||
},
|
||||
onClick = { optionsViewModel.onToggleIsMobileDataTunnel(config) },
|
||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsMobileDataTunnel(config) },
|
||||
),
|
||||
SelectionItem(
|
||||
Icons.Outlined.SettingsEthernet,
|
||||
@@ -151,16 +109,16 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
description = {
|
||||
Text(
|
||||
stringResource(R.string.set_ethernet_tunnel),
|
||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.outline),
|
||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||
)
|
||||
},
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
config.isEthernetTunnel,
|
||||
onClick = { optionsViewModel.onToggleIsEthernetTunnel(config) },
|
||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsEthernetTunnel(config) },
|
||||
)
|
||||
},
|
||||
onClick = { optionsViewModel.onToggleIsEthernetTunnel(config) },
|
||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsEthernetTunnel(config) },
|
||||
),
|
||||
SelectionItem(
|
||||
Icons.Outlined.NetworkPing,
|
||||
@@ -173,10 +131,10 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
trailing = {
|
||||
ScaledSwitch(
|
||||
checked = config.isPingEnabled,
|
||||
onClick = { optionsViewModel.onToggleRestartOnPing(config) },
|
||||
onClick = { tunnelAutoTunnelViewModel.onToggleRestartOnPing(config) },
|
||||
)
|
||||
},
|
||||
onClick = { optionsViewModel.onToggleRestartOnPing(config) },
|
||||
onClick = { tunnelAutoTunnelViewModel.onToggleRestartOnPing(config) },
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -191,7 +149,7 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
stringResource(R.string.default_ping_ip),
|
||||
isErrorValue = { !it.isNullOrBlank() && !it.isValidIpv4orIpv6Address() },
|
||||
onSubmit = {
|
||||
optionsViewModel.saveTunnelChanges(
|
||||
tunnelAutoTunnelViewModel.saveTunnelChanges(
|
||||
config.copy(pingIp = it.ifBlank { null }),
|
||||
)
|
||||
},
|
||||
@@ -209,7 +167,7 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
),
|
||||
isErrorValue = ::isSecondsError,
|
||||
onSubmit = {
|
||||
optionsViewModel.saveTunnelChanges(
|
||||
tunnelAutoTunnelViewModel.saveTunnelChanges(
|
||||
config.copy(pingInterval = if (it.isBlank()) null else it.toLong() * 1000),
|
||||
)
|
||||
},
|
||||
@@ -223,7 +181,7 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
),
|
||||
isErrorValue = ::isSecondsError,
|
||||
onSubmit = {
|
||||
optionsViewModel.saveTunnelChanges(
|
||||
tunnelAutoTunnelViewModel.saveTunnelChanges(
|
||||
config.copy(pingCooldown = if (it.isBlank()) null else it.toLong() * 1000),
|
||||
)
|
||||
},
|
||||
@@ -237,7 +195,9 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
title = {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp.scaledHeight()),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 4.dp.scaledHeight()),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
@@ -270,9 +230,9 @@ fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiSta
|
||||
description = {
|
||||
TrustedNetworkTextBox(
|
||||
config.tunnelNetworks,
|
||||
onDelete = { optionsViewModel.onDeleteRunSSID(it, config) },
|
||||
onDelete = { tunnelAutoTunnelViewModel.onDeleteRunSSID(it, config) },
|
||||
currentText = currentText,
|
||||
onSave = { optionsViewModel.onSaveRunSSID(it, config) },
|
||||
onSave = { tunnelAutoTunnelViewModel.onSaveRunSSID(it, config) },
|
||||
onValueChange = { currentText = it },
|
||||
supporting = {
|
||||
if (appUiState.settings.isWildcardsEnabled) {
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.tunnelautotunnel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class TunnelAutoTunnelViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
) : ViewModel() {
|
||||
|
||||
fun onDeleteRunSSID(ssid: String, tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(
|
||||
tunnelConfig =
|
||||
tunnelConfig.copy(
|
||||
tunnelNetworks = (tunnelConfig.tunnelNetworks - ssid).toMutableList(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun saveTunnelChanges(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(tunnelConfig)
|
||||
}
|
||||
|
||||
fun onSaveRunSSID(ssid: String, tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
if (ssid.isBlank()) return@launch
|
||||
val trimmed = ssid.trim()
|
||||
val tunnelsWithName = appDataRepository.tunnels.findByTunnelNetworksName(trimmed)
|
||||
|
||||
if (!tunnelConfig.tunnelNetworks.contains(trimmed) &&
|
||||
tunnelsWithName.isEmpty()
|
||||
) {
|
||||
saveTunnelChanges(
|
||||
tunnelConfig.copy(
|
||||
tunnelNetworks = (tunnelConfig.tunnelNetworks + ssid).toMutableList(),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
SnackbarController.showMessage(
|
||||
StringValue.StringResource(
|
||||
R.string.error_ssid_exists,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onToggleIsMobileDataTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
if (tunnelConfig.isMobileDataTunnel) {
|
||||
appDataRepository.tunnels.updateMobileDataTunnel(null)
|
||||
} else {
|
||||
appDataRepository.tunnels.updateMobileDataTunnel(tunnelConfig)
|
||||
}
|
||||
}
|
||||
|
||||
fun onTogglePrimaryTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.updatePrimaryTunnel(
|
||||
when (tunnelConfig.isPrimaryTunnel) {
|
||||
true -> null
|
||||
false -> tunnelConfig
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleRestartOnPing(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(
|
||||
tunnelConfig.copy(
|
||||
isPingEnabled = !tunnelConfig.isPingEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleIsEthernetTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
if (tunnelConfig.isEthernetTunnel) {
|
||||
appDataRepository.tunnels.updateEthernetTunnel(null)
|
||||
} else {
|
||||
appDataRepository.tunnels.updateEthernetTunnel(tunnelConfig)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,9 @@ package com.zaneschepke.wireguardautotunnel.ui.theme
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val OffWhite = Color(0xFFE5E1E5)
|
||||
val LightGrey = Color(0xFFCAC4D0)
|
||||
val OffWhite = Color(0xFFF2F2F4)
|
||||
val CoolGray = Color(0xFF8D9D9F)
|
||||
val LightGrey = Color(0xFFECEDEF)
|
||||
val Aqua = Color(0xFF76BEBD)
|
||||
val SilverTree = Color(0xFF6DB58B)
|
||||
val Plantation = Color(0xFF264A49)
|
||||
@@ -18,14 +19,18 @@ sealed class ThemeColors(
|
||||
val primary: Color,
|
||||
val secondary: Color,
|
||||
val onSurface: Color,
|
||||
val onBackground: Color,
|
||||
val outline: Color,
|
||||
) {
|
||||
|
||||
data object Light : ThemeColors(
|
||||
background = LightGrey,
|
||||
background = LightGrey.copy(alpha = 0.95f),
|
||||
surface = OffWhite,
|
||||
primary = Plantation,
|
||||
secondary = OffWhite,
|
||||
primary = Aqua,
|
||||
secondary = LightGrey,
|
||||
onSurface = BalticSea,
|
||||
outline = Plantation.copy(alpha = .75f),
|
||||
onBackground = BalticSea,
|
||||
)
|
||||
|
||||
data object Dark : ThemeColors(
|
||||
@@ -34,5 +39,7 @@ sealed class ThemeColors(
|
||||
primary = Aqua,
|
||||
secondary = Plantation,
|
||||
onSurface = OffWhite,
|
||||
outline = CoolGray,
|
||||
onBackground = OffWhite,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,4 +4,3 @@ import androidx.compose.ui.unit.dp
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||
|
||||
val iconSize = 24.dp.scaledHeight()
|
||||
val topPadding = 80.dp.scaledHeight()
|
||||
|
||||
@@ -21,9 +21,11 @@ private val DarkColorScheme =
|
||||
primary = ThemeColors.Dark.primary,
|
||||
surface = ThemeColors.Dark.surface,
|
||||
background = ThemeColors.Dark.background,
|
||||
secondaryContainer = ThemeColors.Dark.secondary,
|
||||
secondary = ThemeColors.Dark.secondary,
|
||||
onSurface = ThemeColors.Dark.onSurface,
|
||||
onSecondaryContainer = ThemeColors.Dark.primary,
|
||||
outline = ThemeColors.Dark.outline,
|
||||
onBackground = ThemeColors.Dark.onBackground,
|
||||
)
|
||||
|
||||
private val LightColorScheme =
|
||||
@@ -31,9 +33,11 @@ private val LightColorScheme =
|
||||
primary = ThemeColors.Light.primary,
|
||||
surface = ThemeColors.Light.surface,
|
||||
background = ThemeColors.Light.background,
|
||||
secondaryContainer = ThemeColors.Light.secondary,
|
||||
secondary = ThemeColors.Light.secondary,
|
||||
onSurface = ThemeColors.Light.onSurface,
|
||||
onSecondaryContainer = ThemeColors.Light.primary,
|
||||
outline = ThemeColors.Light.outline,
|
||||
onBackground = ThemeColors.Light.onBackground,
|
||||
)
|
||||
|
||||
enum class Theme {
|
||||
@@ -70,14 +74,18 @@ fun WireguardAutoTunnelTheme(theme: Theme = Theme.AUTOMATIC, content: @Composabl
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val view = LocalView.current
|
||||
if (!view.isInEditMode) {
|
||||
SideEffect {
|
||||
val window = (view.context as Activity).window
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
window.statusBarColor = Color.Transparent.toArgb()
|
||||
window.navigationBarColor = Color.Transparent.toArgb()
|
||||
WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars = !isDark
|
||||
window.statusBarColor = Color.Transparent.toArgb()
|
||||
WindowCompat.getInsetsController(window, window.decorView).apply {
|
||||
isAppearanceLightStatusBars = !isDark
|
||||
isAppearanceLightNavigationBars = !isDark
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+15
-18
@@ -1,12 +1,15 @@
|
||||
package com.zaneschepke.wireguardautotunnel.util.extensions
|
||||
|
||||
import android.Manifest
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Context.POWER_SERVICE
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.location.LocationManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.PowerManager
|
||||
import android.provider.Settings
|
||||
import android.service.quicksettings.TileService
|
||||
@@ -168,24 +171,6 @@ fun Context.launchAppSettings() {
|
||||
}
|
||||
}
|
||||
|
||||
// fun Context.startTunnelBackground(tunnelId: Int) {
|
||||
// sendBroadcast(
|
||||
// Intent(this, BackgroundActionReceiver::class.java).apply {
|
||||
// action = BackgroundActionReceiver.ACTION_CONNECT
|
||||
// putExtra(BackgroundActionReceiver.TUNNEL_ID_EXTRA_KEY, tunnelId)
|
||||
// },
|
||||
// )
|
||||
// }
|
||||
//
|
||||
// fun Context.stopTunnelBackground(tunnelId: Int) {
|
||||
// sendBroadcast(
|
||||
// Intent(this, BackgroundActionReceiver::class.java).apply {
|
||||
// action = BackgroundActionReceiver.ACTION_DISCONNECT
|
||||
// putExtra(BackgroundActionReceiver.TUNNEL_ID_EXTRA_KEY, tunnelId)
|
||||
// },
|
||||
// )
|
||||
// }
|
||||
|
||||
fun Context.requestTunnelTileServiceStateUpdate() {
|
||||
TileService.requestListeningState(
|
||||
this,
|
||||
@@ -199,3 +184,15 @@ fun Context.requestAutoTunnelTileServiceUpdate() {
|
||||
ComponentName(this, AutoTunnelControlTile::class.java),
|
||||
)
|
||||
}
|
||||
|
||||
fun Context.getAllInternetCapablePackages(): List<PackageInfo> {
|
||||
val permissions = arrayOf(Manifest.permission.INTERNET)
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
packageManager.getPackagesHoldingPermissions(
|
||||
permissions,
|
||||
PackageManager.PackageInfoFlags.of(0L),
|
||||
)
|
||||
} else {
|
||||
packageManager.getPackagesHoldingPermissions(permissions, 0)
|
||||
}
|
||||
}
|
||||
|
||||
+7
-10
@@ -51,20 +51,17 @@ fun String.replaceUnescapedChar(charToReplace: String, replacement: String): Str
|
||||
this[matchResult.range.first - 1] != '\\' ||
|
||||
(matchResult.range.first > 1 && this[matchResult.range.first - 2] == '\\')
|
||||
) {
|
||||
replacement.toString()
|
||||
replacement
|
||||
} else {
|
||||
matchResult.value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun String.isCharacterEscaped(index: Int): Boolean {
|
||||
if (index <= 0) return false
|
||||
var backslashCount = 0
|
||||
var currentIndex = index - 1
|
||||
while (currentIndex >= 0 && this[currentIndex] == '\\') {
|
||||
backslashCount++
|
||||
currentIndex--
|
||||
}
|
||||
return backslashCount % 2 != 0
|
||||
fun Iterable<String>.joinAndTrim(): String {
|
||||
return this.joinToString(", ").trim()
|
||||
}
|
||||
|
||||
fun String.toTrimmedList(): List<String> {
|
||||
return this.split(",").map { it.trim() }.filter { it.isNotEmpty() }
|
||||
}
|
||||
|
||||
+2
-2
@@ -84,8 +84,8 @@ fun Config.toWgQuickString(): String {
|
||||
|
||||
fun RootShell.getCurrentWifiName(): String? {
|
||||
val response = mutableListOf<String>()
|
||||
this.run(response, "dumpsys wifi | grep -o \"SSID: [^,]*\" | cut -d ' ' -f2- | tr -d '\"'")
|
||||
return response.lastOrNull()
|
||||
this.run(response, "dumpsys wifi | grep 'Supplicant state: COMPLETED' | grep -o 'SSID: [^,]*' | cut -d ' ' -f2- | tr -d '\"'")
|
||||
return response.firstOrNull()
|
||||
}
|
||||
|
||||
fun Backend.BackendState.asBackendState(): BackendState {
|
||||
|
||||
@@ -190,4 +190,14 @@
|
||||
<string name="auto_tunnel_channel_name">Auto-tunnel Notification Channel</string>
|
||||
<string name="auto_tunnel_channel_description">A channel for auto-tunnel state notifications</string>
|
||||
<string name="stop">stop</string>
|
||||
<string name="config">Config</string>
|
||||
<string name="splt_tunneling">Split tunneling</string>
|
||||
<string name="tunnel_specific_settings">Tunnel specific settings</string>
|
||||
<string name="quick_actions">Quick actions</string>
|
||||
<string name="show_scripts">Show scripts</string>
|
||||
<string name="pre_up">Pre up</string>
|
||||
<string name="post_up">Post up</string>
|
||||
<string name="pre_down">Pre down</string>
|
||||
<string name="post_down">Post down</string>
|
||||
<string name="amnezia_kernel_message">Amnezia unavailable in kernel mode</string>
|
||||
</resources>
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/vpn_on"
|
||||
android:shortcutDisabledMessage="@string/vpn_on"
|
||||
android:shortcutId="defaultOn1"
|
||||
android:shortcutLongLabel="@string/vpn_on"
|
||||
android:shortcutShortLabel="@string/vpn_on">
|
||||
<intent
|
||||
android:action="START"
|
||||
android:targetClass="com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutsActivity"
|
||||
android:targetPackage="com.zaneschepke.wireguardautotunnel">
|
||||
<extra
|
||||
android:name="className"
|
||||
android:value="WireGuardTunnelService" />
|
||||
</intent>
|
||||
<capability-binding android:key="actions.intent.START" />
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/vpn_off"
|
||||
android:shortcutDisabledMessage="@string/vpn_off"
|
||||
android:shortcutId="defaultOff1"
|
||||
android:shortcutLongLabel="@string/vpn_off"
|
||||
android:shortcutShortLabel="@string/vpn_off">
|
||||
<intent
|
||||
android:action="STOP"
|
||||
android:targetClass="com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutsActivity"
|
||||
android:targetPackage="com.zaneschepke.wireguardautotunnel">
|
||||
<extra
|
||||
android:name="className"
|
||||
android:value="WireGuardTunnelService" />
|
||||
</intent>
|
||||
<capability-binding android:key="actions.intent.STOP" />
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/auto_play"
|
||||
android:shortcutId="autoOn1"
|
||||
android:shortcutLongLabel="@string/start_auto"
|
||||
android:shortcutShortLabel="@string/start_auto">
|
||||
<intent
|
||||
android:action="START"
|
||||
android:targetClass="com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutsActivity"
|
||||
android:targetPackage="com.zaneschepke.wireguardautotunnel">
|
||||
<extra
|
||||
android:name="className"
|
||||
android:value="WireGuardConnectivityWatcherService" />
|
||||
</intent>
|
||||
<capability-binding android:key="actions.intent.STOP" />
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/auto_pause"
|
||||
android:shortcutId="autoOff1"
|
||||
android:shortcutLongLabel="@string/stop_auto"
|
||||
android:shortcutShortLabel="@string/stop_auto">
|
||||
<intent
|
||||
android:action="STOP"
|
||||
android:targetClass="com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutsActivity"
|
||||
android:targetPackage="com.zaneschepke.wireguardautotunnel">
|
||||
<extra
|
||||
android:name="className"
|
||||
android:value="WireGuardConnectivityWatcherService" />
|
||||
</intent>
|
||||
<capability-binding android:key="actions.intent.STOP" />
|
||||
</shortcut>
|
||||
</shortcuts>
|
||||
@@ -1,7 +1,7 @@
|
||||
object Constants {
|
||||
const val VERSION_NAME = "3.6.1"
|
||||
const val VERSION_NAME = "3.6.5"
|
||||
const val JVM_TARGET = "17"
|
||||
const val VERSION_CODE = 36100
|
||||
const val VERSION_CODE = 36500
|
||||
const val TARGET_SDK = 35
|
||||
const val MIN_SDK = 26
|
||||
const val APP_ID = "com.zaneschepke.wireguardautotunnel"
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
What's new:
|
||||
- Added VPN kill switch w/LAN bypass
|
||||
- Improved auto tunnel speed and reliability
|
||||
- Improved tile sync
|
||||
- Various bug fixes and improvements
|
||||
@@ -0,0 +1,3 @@
|
||||
What's new:
|
||||
- Fixed kernel mode toggle bug
|
||||
- Fixed notification crash bug
|
||||
@@ -0,0 +1,6 @@
|
||||
What's new:
|
||||
- Auto tunnel reliability improvements
|
||||
- Split tunnel save fix and performance improvements
|
||||
- Kernel mode bug fix
|
||||
- Tile bug fix
|
||||
- Various other fixes and improvements
|
||||
@@ -8,24 +8,24 @@ biometricKtx = "1.2.0-alpha05"
|
||||
coreGoogleShortcuts = "1.1.0"
|
||||
coreKtx = "1.15.0"
|
||||
datastorePreferences = "1.1.1"
|
||||
desugar_jdk_libs = "2.1.3"
|
||||
desugar_jdk_libs = "2.1.4"
|
||||
espressoCore = "3.6.1"
|
||||
hiltAndroid = "2.53"
|
||||
hiltAndroid = "2.54"
|
||||
hiltNavigationCompose = "1.2.0"
|
||||
junit = "4.13.2"
|
||||
kotlinx-serialization-json = "1.7.3"
|
||||
lifecycle-runtime-compose = "2.8.7"
|
||||
material3 = "1.3.1"
|
||||
navigationCompose = "2.8.4"
|
||||
navigationCompose = "2.8.5"
|
||||
pinLockCompose = "1.0.4"
|
||||
roomVersion = "2.6.1"
|
||||
timber = "5.0.1"
|
||||
tunnel = "1.2.1"
|
||||
androidGradlePlugin = "8.8.0-rc01"
|
||||
androidGradlePlugin = "8.8.0-rc02"
|
||||
kotlin = "2.1.0"
|
||||
ksp = "2.1.0-1.0.29"
|
||||
composeBom = "2024.11.00"
|
||||
compose = "1.7.5"
|
||||
composeBom = "2024.12.01"
|
||||
compose = "1.7.6"
|
||||
zxingAndroidEmbedded = "4.3.0"
|
||||
coreSplashscreen = "1.0.1"
|
||||
gradlePlugins-grgit = "5.3.0"
|
||||
|
||||
@@ -49,6 +49,7 @@ dependencies {
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
androidTestImplementation(libs.androidx.espresso.core)
|
||||
|
||||
implementation(libs.androidx.lifecycle.process)
|
||||
// logging
|
||||
implementation(libs.timber)
|
||||
}
|
||||
|
||||
@@ -4,8 +4,7 @@ import com.zaneschepke.logcatter.model.LogMessage
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface LogReader {
|
||||
suspend fun start(onLogMessage: ((message: LogMessage) -> Unit)? = null)
|
||||
fun stop()
|
||||
fun initialize(onLogMessage: ((message: LogMessage) -> Unit)? = null)
|
||||
fun zipLogFiles(path: String)
|
||||
suspend fun deleteAndClearLogs()
|
||||
val bufferedLogs: Flow<LogMessage>
|
||||
|
||||
+62
-85
@@ -1,26 +1,30 @@
|
||||
package com.zaneschepke.logcatter
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.zaneschepke.logcatter.model.LogMessage
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.BufferedReader
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStreamReader
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
object LogcatCollector {
|
||||
object LogcatReader {
|
||||
|
||||
private const val MAX_FILE_SIZE = 2097152L // 2MB
|
||||
private const val MAX_FOLDER_SIZE = 10485760L // 10MB
|
||||
@@ -40,7 +44,7 @@ object LogcatCollector {
|
||||
var logcatPath = ""
|
||||
}
|
||||
|
||||
fun init(maxFileSize: Long = MAX_FILE_SIZE, maxFolderSize: Long = MAX_FOLDER_SIZE, context: Context): LogReader {
|
||||
fun init(maxFileSize: Long = MAX_FILE_SIZE, maxFolderSize: Long = MAX_FOLDER_SIZE, storageDir: String): LogReader {
|
||||
if (maxFileSize > maxFolderSize) {
|
||||
throw IllegalStateException("maxFileSize must be less than maxFolderSize")
|
||||
}
|
||||
@@ -48,13 +52,11 @@ object LogcatCollector {
|
||||
LogcatHelperInit.maxFileSize = maxFileSize
|
||||
LogcatHelperInit.maxFolderSize = maxFolderSize
|
||||
LogcatHelperInit.pID = android.os.Process.myPid()
|
||||
context.getExternalFilesDir(null)?.let {
|
||||
LogcatHelperInit.publicAppDirectory = it.absolutePath
|
||||
LogcatHelperInit.logcatPath = LogcatHelperInit.publicAppDirectory + File.separator + "logs"
|
||||
val logDirectory = File(LogcatHelperInit.logcatPath)
|
||||
if (!logDirectory.exists()) {
|
||||
logDirectory.mkdir()
|
||||
}
|
||||
LogcatHelperInit.publicAppDirectory = storageDir
|
||||
LogcatHelperInit.logcatPath = LogcatHelperInit.publicAppDirectory + File.separator + "logs"
|
||||
val logDirectory = File(LogcatHelperInit.logcatPath)
|
||||
if (!logDirectory.exists()) {
|
||||
logDirectory.mkdir()
|
||||
}
|
||||
return Logcat
|
||||
}
|
||||
@@ -62,7 +64,12 @@ object LogcatCollector {
|
||||
|
||||
internal object Logcat : LogReader {
|
||||
|
||||
private var logcatReader: LogcatReader? = null
|
||||
private lateinit var logcatReader: LogcatReader
|
||||
|
||||
override fun initialize(onLogMessage: ((message: LogMessage) -> Unit)?) {
|
||||
logcatReader = LogcatReader(LogcatHelperInit.pID.toString(), LogcatHelperInit.logcatPath, onLogMessage)
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(logcatReader)
|
||||
}
|
||||
|
||||
private fun obfuscator(log: String): String {
|
||||
return findKeyRegex.replace(log, "<crypto-key>").let { first ->
|
||||
@@ -72,22 +79,10 @@ object LogcatCollector {
|
||||
}.let { last -> findIpv4AddressRegex.replace(last, "<ipv4-address>") }
|
||||
}
|
||||
|
||||
override suspend fun start(onLogMessage: ((message: LogMessage) -> Unit)?) {
|
||||
logcatReader ?: run {
|
||||
logcatReader = LogcatReader(LogcatHelperInit.pID.toString(), LogcatHelperInit.logcatPath, onLogMessage)
|
||||
}
|
||||
logcatReader?.run()
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
logcatReader?.stop()
|
||||
logcatReader = null
|
||||
}
|
||||
|
||||
override fun zipLogFiles(path: String) {
|
||||
logcatReader?.pause()
|
||||
logcatReader.cancel()
|
||||
zipAll(path)
|
||||
logcatReader?.resume()
|
||||
logcatReader.onCreate(ProcessLifecycleOwner.get())
|
||||
}
|
||||
|
||||
private fun zipAll(zipFilePath: String) {
|
||||
@@ -110,10 +105,10 @@ object LogcatCollector {
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override suspend fun deleteAndClearLogs() {
|
||||
withContext(ioDispatcher) {
|
||||
logcatReader?.pause()
|
||||
logcatReader.cancel()
|
||||
_bufferedLogs.resetReplayCache()
|
||||
logcatReader?.deleteAllFiles()
|
||||
logcatReader?.resume()
|
||||
logcatReader.deleteAllFiles()
|
||||
logcatReader.onCreate(ProcessLifecycleOwner.get())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,57 +129,25 @@ object LogcatCollector {
|
||||
pID: String,
|
||||
private val logcatPath: String,
|
||||
private val callback: ((input: LogMessage) -> Unit)?,
|
||||
) {
|
||||
) : DefaultLifecycleObserver {
|
||||
private var logcatProc: Process? = null
|
||||
private var reader: BufferedReader? = null
|
||||
|
||||
@get:Synchronized @set:Synchronized
|
||||
private var paused = false
|
||||
|
||||
@get:Synchronized @set:Synchronized
|
||||
private var stopped = false
|
||||
private var command = ""
|
||||
private var clearLogCommand = ""
|
||||
private val command = "logcat -v epoch | grep \"($pID)\""
|
||||
private val clearLogCommand = "logcat -c"
|
||||
private var logJob: Job? = null
|
||||
private var outputStream: FileOutputStream? = null
|
||||
|
||||
init {
|
||||
try {
|
||||
outputStream = FileOutputStream(createLogFile(logcatPath))
|
||||
} catch (e: FileNotFoundException) {
|
||||
Timber.e(e)
|
||||
}
|
||||
|
||||
command = "logcat -v epoch | grep \"($pID)\""
|
||||
clearLogCommand = "logcat -c"
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
paused = true
|
||||
}
|
||||
fun stop() {
|
||||
stopped = true
|
||||
}
|
||||
|
||||
fun resume() {
|
||||
paused = false
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
Runtime.getRuntime().exec(clearLogCommand)
|
||||
}
|
||||
|
||||
suspend fun run() {
|
||||
withContext(ioDispatcher) {
|
||||
paused = false
|
||||
stopped = false
|
||||
if (outputStream == null) return@withContext
|
||||
override fun onCreate(owner: LifecycleOwner) {
|
||||
super.onCreate(owner)
|
||||
logJob = owner.lifecycleScope.launch(ioDispatcher) {
|
||||
try {
|
||||
if (outputStream == null) outputStream = createNewLogFileStream()
|
||||
clear()
|
||||
logcatProc = Runtime.getRuntime().exec(command)
|
||||
reader = BufferedReader(InputStreamReader(logcatProc!!.inputStream), 1024)
|
||||
var line: String?
|
||||
while (!stopped) {
|
||||
if (paused) continue
|
||||
var line: String? = null
|
||||
while (true) {
|
||||
line = reader?.readLine()
|
||||
if (line.isNullOrEmpty()) continue
|
||||
outputStream?.let {
|
||||
@@ -196,8 +159,8 @@ object LogcatCollector {
|
||||
deleteOldestFile()
|
||||
}
|
||||
line.let { text ->
|
||||
val obfuscated = obfuscator(text)
|
||||
it.write((obfuscated + System.lineSeparator()).toByteArray())
|
||||
val sanitized = obfuscator(text)
|
||||
it.write((sanitized + System.lineSeparator()).toByteArray())
|
||||
try {
|
||||
val logMessage = LogMessage.from(text)
|
||||
_bufferedLogs.tryEmit(logMessage)
|
||||
@@ -214,19 +177,34 @@ object LogcatCollector {
|
||||
} catch (e: IOException) {
|
||||
Timber.e(e)
|
||||
} finally {
|
||||
logcatProc?.destroy()
|
||||
logcatProc = null
|
||||
|
||||
try {
|
||||
reader?.close()
|
||||
outputStream?.close()
|
||||
reader = null
|
||||
outputStream = null
|
||||
} catch (e: IOException) {
|
||||
Timber.e(e)
|
||||
}
|
||||
reset()
|
||||
}
|
||||
}
|
||||
logJob?.invokeOnCompletion {
|
||||
reset()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
super.onDestroy(owner)
|
||||
logJob?.cancel()
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
logJob?.cancel()
|
||||
}
|
||||
|
||||
private fun reset() {
|
||||
logcatProc?.destroy()
|
||||
logcatProc = null
|
||||
reader?.close()
|
||||
outputStream?.close()
|
||||
reader = null
|
||||
outputStream = null
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
Runtime.getRuntime().exec(clearLogCommand)
|
||||
}
|
||||
|
||||
private fun getFolderSize(path: String): Long {
|
||||
@@ -266,7 +244,6 @@ object LogcatCollector {
|
||||
directory.listFiles()?.toMutableList()?.run {
|
||||
this.forEach { it.delete() }
|
||||
}
|
||||
outputStream = createNewLogFileStream()
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1 +1 @@
|
||||
12
|
||||
1
|
||||
|
||||
Reference in New Issue
Block a user