@@ -202,18 +202,14 @@ constructor(
202
202
203
203
private suspend fun handleVpnKillSwitchChange (enabled : Boolean ) {
204
204
withContext(ioDispatcher) {
205
- if (enabled) {
206
- Timber .d(" Starting kill switch" )
207
- val allowedIps = if (appDataRepository.settings.getSettings().isLanOnKillSwitchEnabled) {
208
- TunnelConfig .IPV4_PUBLIC_NETWORKS
209
- } else {
210
- emptySet()
211
- }
212
- tunnelService.get().setBackendState(BackendState .KILL_SWITCH_ACTIVE , allowedIps)
205
+ if (! enabled) return @withContext tunnelService.get().setBackendState(BackendState .SERVICE_ACTIVE , emptySet())
206
+ Timber .d(" Starting kill switch" )
207
+ val allowedIps = if (appDataRepository.settings.getSettings().isLanOnKillSwitchEnabled) {
208
+ TunnelConfig .IPV4_PUBLIC_NETWORKS
213
209
} else {
214
- Timber .d(" Sending shutdown of kill switch" )
215
- tunnelService.get().setBackendState(BackendState .SERVICE_ACTIVE , emptySet())
210
+ emptySet()
216
211
}
212
+ tunnelService.get().setBackendState(BackendState .KILL_SWITCH_ACTIVE , allowedIps)
217
213
}
218
214
}
219
215
@@ -297,34 +293,76 @@ constructor(
297
293
}
298
294
}
299
295
300
- fun saveConfigChanges (config : TunnelConfig , peers : List <PeerProxy >? = null, `interface`: InterfaceProxy ? = null) = viewModelScope.launch(
301
- ioDispatcher,
302
- ) {
296
+ fun updateExistingTunnelConfig (
297
+ tunnelConfig : TunnelConfig ,
298
+ tunnelName : String? = null,
299
+ peers : List <PeerProxy >? = null,
300
+ `interface`: InterfaceProxy ? = null,
301
+ ) = viewModelScope.launch {
303
302
runCatching {
304
- val amConfig = config .toAmConfig()
305
- val wgConfig = config .toWgConfig()
306
- rebuildConfigsAndSave(config , amConfig, wgConfig, peers, `interface`)
303
+ val amConfig = tunnelConfig .toAmConfig()
304
+ val wgConfig = tunnelConfig .toWgConfig()
305
+ updateTunnelConfig(tunnelConfig, tunnelName , amConfig, wgConfig, peers, `interface`)
307
306
_popBackStack .emit(true )
308
307
SnackbarController .showMessage(StringValue .StringResource (R .string.config_changes_saved))
309
308
}.onFailure {
310
- Timber .e(it)
311
- SnackbarController .showMessage(
312
- it.message?.let { message ->
313
- (StringValue .DynamicString (message))
314
- } ? : StringValue .StringResource (R .string.unknown_error),
309
+ onConfigSaveError(it)
310
+ }
311
+ }
312
+
313
+ fun saveNewTunnel (tunnelName : String , peers : List <PeerProxy >, `interface`: InterfaceProxy ) = viewModelScope.launch {
314
+ runCatching {
315
+ val config = buildConfigs(peers, `interface`)
316
+ appDataRepository.tunnels.save(
317
+ TunnelConfig (
318
+ name = tunnelName,
319
+ wgQuick = config.first.toWgQuickString(true ),
320
+ amQuick = config.second.toAwgQuickString(true ),
321
+ ),
315
322
)
323
+ _popBackStack .emit(true )
324
+ SnackbarController .showMessage(StringValue .StringResource (R .string.config_changes_saved))
325
+ }.onFailure {
326
+ onConfigSaveError(it)
316
327
}
317
328
}
318
329
330
+ private fun onConfigSaveError (throwable : Throwable ) {
331
+ Timber .e(throwable)
332
+ SnackbarController .showMessage(
333
+ throwable.message?.let { message ->
334
+ (StringValue .DynamicString (message))
335
+ } ? : StringValue .StringResource (R .string.unknown_error),
336
+ )
337
+ }
338
+
339
+ private suspend fun updateTunnelConfig (
340
+ tunnelConfig : TunnelConfig ,
341
+ tunnelName : String? = null,
342
+ amConfig : org.amnezia.awg.config.Config ,
343
+ wgConfig : Config ,
344
+ peers : List <PeerProxy >? = null,
345
+ `interface`: InterfaceProxy ? = null,
346
+ ) {
347
+ val configs = rebuildConfigs(amConfig, wgConfig, peers, `interface`)
348
+ appDataRepository.tunnels.save(
349
+ tunnelConfig.copy(
350
+ name = tunnelName ? : tunnelConfig.name,
351
+ amQuick = configs.second.toAwgQuickString(true ),
352
+ wgQuick = configs.first.toWgQuickString(true ),
353
+ ),
354
+ )
355
+ }
356
+
319
357
fun cleanUpUninstalledApps (tunnelConfig : TunnelConfig , packages : List <String >) = viewModelScope.launch(ioDispatcher) {
320
358
runCatching {
321
359
val amConfig = tunnelConfig.toAmConfig()
322
360
val wgConfig = tunnelConfig.toWgConfig()
323
361
val proxy = InterfaceProxy .from(amConfig.`interface`)
324
362
if (proxy.includedApplications.isEmpty() && proxy.excludedApplications.isEmpty()) return @launch
325
363
if (proxy.includedApplications.retainAll(packages.toSet()) || proxy.excludedApplications.retainAll(packages.toSet())) {
326
- Timber .i( " Removing split tunnel package for app that no longer exists on the device " )
327
- rebuildConfigsAndSave(tunnelConfig, amConfig, wgConfig, `interface` = proxy )
364
+ updateTunnelConfig(tunnelConfig, amConfig = amConfig, wgConfig = wgConfig, `interface` = proxy )
365
+ Timber .i( " Removed split tunnel package for app that no longer exists on the device " )
328
366
}
329
367
}.onFailure {
330
368
Timber .e(it)
@@ -340,24 +378,38 @@ constructor(
340
378
)
341
379
}
342
380
343
- private suspend fun rebuildConfigsAndSave (
344
- config : TunnelConfig ,
381
+ private suspend fun rebuildConfigs (
345
382
amConfig : org.amnezia.awg.config.Config ,
346
383
wgConfig : Config ,
347
384
peers : List <PeerProxy >? = null,
348
385
`interface`: InterfaceProxy ? = null,
349
- ) {
350
- appDataRepository.tunnels.save(
351
- config.copy (
352
- wgQuick = Config .Builder ().apply {
386
+ ): Pair < Config , org.amnezia.awg.config. Config > {
387
+ return withContext(ioDispatcher) {
388
+ Pair (
389
+ Config .Builder ().apply {
353
390
addPeers(peers?.map { it.toWgPeer() } ? : wgConfig.peers)
354
391
setInterface(`interface`?.toWgInterface() ? : wgConfig.`interface`)
355
- }.build().toWgQuickString( true ) ,
356
- amQuick = org.amnezia.awg.config.Config .Builder ().apply {
392
+ }.build(),
393
+ org.amnezia.awg.config.Config .Builder ().apply {
357
394
addPeers(peers?.map { it.toAmPeer() } ? : amConfig.peers)
358
395
setInterface(`interface`?.toAmInterface() ? : amConfig.`interface`)
359
- }.build().toAwgQuickString(true ),
360
- ),
361
- )
396
+ }.build(),
397
+ )
398
+ }
399
+ }
400
+
401
+ private suspend fun buildConfigs (peers : List <PeerProxy >, `interface`: InterfaceProxy ): Pair <Config , org.amnezia.awg.config.Config > {
402
+ return withContext(ioDispatcher) {
403
+ Pair (
404
+ Config .Builder ().apply {
405
+ addPeers(peers.map { it.toWgPeer() })
406
+ setInterface(`interface`.toWgInterface())
407
+ }.build(),
408
+ org.amnezia.awg.config.Config .Builder ().apply {
409
+ addPeers(peers.map { it.toAmPeer() })
410
+ setInterface(`interface`.toAmInterface())
411
+ }.build(),
412
+ )
413
+ }
362
414
}
363
415
}
0 commit comments