Skip to content

Commit c28e157

Browse files
committed
fix: proxy mode vpn permission
1 parent b2bd857 commit c28e157

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

app/src/main/java/com/zaneschepke/wireguardautotunnel/core/tunnel/TunnelManager.kt

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,39 @@ constructor(
325325
proxyUserspaceTunnel.setBackendMode(BackendMode.Inactive)
326326
}
327327

328+
private fun isVpnAuthorized(
329+
mode: AppMode,
330+
hasVpnPermission: () -> Boolean = { serviceManager.hasVpnPermission() },
331+
): Boolean {
332+
return when (mode) {
333+
AppMode.VPN,
334+
AppMode.LOCK_DOWN -> hasVpnPermission()
335+
AppMode.KERNEL,
336+
AppMode.PROXY -> true
337+
}
338+
}
339+
328340
suspend fun handleRestore() =
329341
withContext(ioDispatcher) {
330342
val settings = settingsRepository.getGeneralSettings()
331343
val autoTunnelSettings = autoTunnelSettingsRepository.getAutoTunnelSettings()
332344
val tunnels = tunnelsRepository.getAll()
333345
if (autoTunnelSettings.isAutoTunnelEnabled)
334346
return@withContext restoreAutoTunnel(autoTunnelSettings)
335-
when (val mode = settings.appMode) {
336-
AppMode.VPN,
337-
AppMode.PROXY,
338-
AppMode.LOCK_DOWN -> {
339-
if (serviceManager.hasVpnPermission()) {
347+
if (isVpnAuthorized(settings.appMode)) {
348+
when (val mode = settings.appMode) {
349+
AppMode.VPN,
350+
AppMode.PROXY,
351+
AppMode.LOCK_DOWN -> {
340352
if (mode == AppMode.LOCK_DOWN)
341353
handleLockDownModeInit(settings.isLanOnKillSwitchEnabled)
342354
tunnels.firstOrNull { it.isActive }?.let { startTunnel(it) }
343-
} else localErrorEvents.emit(null to BackendCoreException.NotAuthorized)
355+
}
356+
AppMode.KERNEL ->
357+
tunnels.filter { it.isActive }.forEach { conf -> startTunnel(conf) }
344358
}
345-
AppMode.KERNEL ->
346-
tunnels.filter { it.isActive }.forEach { conf -> startTunnel(conf) }
359+
} else {
360+
localErrorEvents.emit(null to BackendCoreException.NotAuthorized)
347361
}
348362
}
349363

@@ -361,21 +375,18 @@ constructor(
361375
return@withContext restoreAutoTunnel(autoTunnelSettings)
362376
if (settings.isRestoreOnBootEnabled) {
363377
tunnelsRepository.resetActiveTunnels()
364-
when (val mode = settings.appMode) {
365-
AppMode.VPN,
366-
AppMode.PROXY,
367-
AppMode.LOCK_DOWN -> {
368-
if (serviceManager.hasVpnPermission()) {
369-
if (mode == AppMode.LOCK_DOWN)
370-
handleLockDownModeInit(settings.isLanOnKillSwitchEnabled)
371-
} else
372-
return@withContext localErrorEvents.emit(
373-
null to BackendCoreException.NotAuthorized
374-
)
378+
if (isVpnAuthorized(settings.appMode)) {
379+
when (val mode = settings.appMode) {
380+
AppMode.LOCK_DOWN ->
381+
handleLockDownModeInit(settings.isLanOnKillSwitchEnabled)
382+
AppMode.KERNEL,
383+
AppMode.VPN,
384+
AppMode.PROXY -> Unit
375385
}
376-
AppMode.KERNEL -> Unit
386+
defaultTunnel?.let { startTunnel(it) }
387+
} else {
388+
localErrorEvents.emit(null to BackendCoreException.NotAuthorized)
377389
}
378-
defaultTunnel?.let { startTunnel(it) }
379390
}
380391
}
381392

app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunnels/components/TunnelStatisticsRow.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ fun TunnelStatisticsRow(
3535
val textColor = MaterialTheme.colorScheme.outline
3636

3737
// needs to be set as peer stats for duplicates return as a single set of stats
38-
val peers by remember(tunnel) {
39-
derivedStateOf {
40-
TunnelConfig.configFromWgQuick(tunnel.wgQuick)
41-
.peers
42-
.map { it.publicKey.toBase64() }
43-
.toSet()
38+
val peers by
39+
remember(tunnel) {
40+
derivedStateOf {
41+
TunnelConfig.configFromWgQuick(tunnel.wgQuick)
42+
.peers
43+
.map { it.publicKey.toBase64() }
44+
.toSet()
45+
}
4446
}
45-
}
4647

4748
var currentTimeMillis by remember { mutableLongStateOf(System.currentTimeMillis()) }
4849
LaunchedEffect(Unit) {

0 commit comments

Comments
 (0)