aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/TincApp.kt24
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tincd.kt6
2 files changed, 28 insertions, 2 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt
index b18a39b..b564b6a 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt
@@ -1,9 +1,14 @@
1package org.pacien.tincapp.commands 1package org.pacien.tincapp.commands
2 2
3import java8.util.concurrent.CompletableFuture 3import java8.util.concurrent.CompletableFuture
4import org.pacien.tincapp.R
4import org.pacien.tincapp.commands.Executor.runAsyncTask 5import org.pacien.tincapp.commands.Executor.runAsyncTask
6import org.pacien.tincapp.context.App
5import org.pacien.tincapp.context.AppPaths 7import org.pacien.tincapp.context.AppPaths
8import org.pacien.tincapp.data.TincConfiguration
6import org.pacien.tincapp.data.VpnInterfaceConfiguration 9import org.pacien.tincapp.data.VpnInterfaceConfiguration
10import org.pacien.tincapp.utils.PemUtils
11import java.io.FileNotFoundException
7 12
8/** 13/**
9 * @author pacien 14 * @author pacien
@@ -16,6 +21,16 @@ object TincApp {
16 private fun listScripts(netName: String) = AppPaths.confDir(netName).listFiles { f -> f.name in STATIC_SCRIPTS } + 21 private fun listScripts(netName: String) = AppPaths.confDir(netName).listFiles { f -> f.name in STATIC_SCRIPTS } +
17 AppPaths.hostsDir(netName).listFiles { f -> SCRIPT_SUFFIXES.any { f.name.endsWith(it) } } 22 AppPaths.hostsDir(netName).listFiles { f -> SCRIPT_SUFFIXES.any { f.name.endsWith(it) } }
18 23
24 fun listPrivateKeys(netName: String) = try {
25 TincConfiguration.fromTincConfiguration(AppPaths.existing(AppPaths.tincConfFile(netName))).let {
26 listOf(
27 it.privateKeyFile ?: AppPaths.defaultRsaPrivateKeyFile(netName),
28 it.ed25519PrivateKeyFile ?: AppPaths.defaultEd25519PrivateKeyFile(netName))
29 }
30 } catch (e: FileNotFoundException) {
31 throw FileNotFoundException(App.getResources().getString(R.string.message_network_config_not_found_format, e.message!!))
32 }
33
19 fun removeScripts(netName: String): CompletableFuture<Void> = runAsyncTask { 34 fun removeScripts(netName: String): CompletableFuture<Void> = runAsyncTask {
20 listScripts(netName).forEach { it.delete() } 35 listScripts(netName).forEach { it.delete() }
21 } 36 }
@@ -26,4 +41,13 @@ object TincApp {
26 .write(AppPaths.netConfFile(netName)) 41 .write(AppPaths.netConfFile(netName))
27 } 42 }
28 43
44 fun setPassphrase(netName: String, currentPassphrase: String? = null, newPassphrase: String?): CompletableFuture<Void> = runAsyncTask {
45 listPrivateKeys(netName)
46 .filter { it.exists() }
47 .map { Pair(PemUtils.read(it), it) }
48 .map { Pair(PemUtils.decrypt(it.first, currentPassphrase), it.second) }
49 .map { Pair(if (newPassphrase?.isNotEmpty() == true) PemUtils.encrypt(it.first, newPassphrase) else it.first, it.second) }
50 .forEach { PemUtils.write(it.first, it.second.writer()) }
51 }
52
29} 53}
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
index db113cc..d44d930 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
@@ -7,14 +7,16 @@ import org.pacien.tincapp.context.AppPaths
7 */ 7 */
8object Tincd { 8object Tincd {
9 9
10 fun start(netName: String, fd: Int) { 10 fun start(netName: String, deviceFd: Int, ed25519PrivateKeyFd: Int? = null, rsaPrivateKeyFd: Int? = null) {
11 Executor.forkExec(Command(AppPaths.tincd().absolutePath) 11 Executor.forkExec(Command(AppPaths.tincd().absolutePath)
12 .withOption("no-detach") 12 .withOption("no-detach")
13 .withOption("config", AppPaths.confDir(netName).absolutePath) 13 .withOption("config", AppPaths.confDir(netName).absolutePath)
14 .withOption("pidfile", AppPaths.pidFile(netName).absolutePath) 14 .withOption("pidfile", AppPaths.pidFile(netName).absolutePath)
15 .withOption("logfile", AppPaths.logFile(netName).absolutePath) 15 .withOption("logfile", AppPaths.logFile(netName).absolutePath)
16 .withOption("option", "DeviceType=fd") 16 .withOption("option", "DeviceType=fd")
17 .withOption("option", "Device=" + fd)) 17 .withOption("option", "Device=" + deviceFd)
18 .apply { if (ed25519PrivateKeyFd != null) withOption("option", "Ed25519PrivateKeyFile=/proc/self/fd/$ed25519PrivateKeyFd") }
19 .apply { if (rsaPrivateKeyFd != null) withOption("option", "PrivateKeyFile=/proc/self/fd/$rsaPrivateKeyFd") })
18 } 20 }
19 21
20} 22}