diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/TincApp.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/commands/TincApp.kt | 62 |
1 files changed, 31 insertions, 31 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 b564b6a..11adb69 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt | |||
@@ -15,39 +15,39 @@ import java.io.FileNotFoundException | |||
15 | */ | 15 | */ |
16 | object TincApp { | 16 | object TincApp { |
17 | 17 | ||
18 | private val SCRIPT_SUFFIXES = listOf("-up", "-down", "-created", "-accepted") | 18 | private val SCRIPT_SUFFIXES = listOf("-up", "-down", "-created", "-accepted") |
19 | private val STATIC_SCRIPTS = listOf("tinc", "host", "subnet", "invitation").flatMap { s -> SCRIPT_SUFFIXES.map { s + it } } | 19 | private val STATIC_SCRIPTS = listOf("tinc", "host", "subnet", "invitation").flatMap { s -> SCRIPT_SUFFIXES.map { s + it } } |
20 | |||
21 | private fun listScripts(netName: String) = AppPaths.confDir(netName).listFiles { f -> f.name in STATIC_SCRIPTS } + | ||
22 | AppPaths.hostsDir(netName).listFiles { f -> SCRIPT_SUFFIXES.any { f.name.endsWith(it) } } | ||
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 | |||
34 | fun removeScripts(netName: String): CompletableFuture<Void> = runAsyncTask { | ||
35 | listScripts(netName).forEach { it.delete() } | ||
36 | } | ||
37 | 20 | ||
38 | fun generateIfaceCfg(netName: String): CompletableFuture<Void> = runAsyncTask { | 21 | private fun listScripts(netName: String) = AppPaths.confDir(netName).listFiles { f -> f.name in STATIC_SCRIPTS } + |
39 | VpnInterfaceConfiguration | 22 | AppPaths.hostsDir(netName).listFiles { f -> SCRIPT_SUFFIXES.any { f.name.endsWith(it) } } |
40 | .fromInvitation(AppPaths.invitationFile(netName)) | ||
41 | .write(AppPaths.netConfFile(netName)) | ||
42 | } | ||
43 | 23 | ||
44 | fun setPassphrase(netName: String, currentPassphrase: String? = null, newPassphrase: String?): CompletableFuture<Void> = runAsyncTask { | 24 | fun listPrivateKeys(netName: String) = try { |
45 | listPrivateKeys(netName) | 25 | TincConfiguration.fromTincConfiguration(AppPaths.existing(AppPaths.tincConfFile(netName))).let { |
46 | .filter { it.exists() } | 26 | listOf( |
47 | .map { Pair(PemUtils.read(it), it) } | 27 | it.privateKeyFile ?: AppPaths.defaultRsaPrivateKeyFile(netName), |
48 | .map { Pair(PemUtils.decrypt(it.first, currentPassphrase), it.second) } | 28 | it.ed25519PrivateKeyFile ?: AppPaths.defaultEd25519PrivateKeyFile(netName)) |
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 | } | 29 | } |
30 | } catch (e: FileNotFoundException) { | ||
31 | throw FileNotFoundException(App.getResources().getString(R.string.message_network_config_not_found_format, e.message!!)) | ||
32 | } | ||
33 | |||
34 | fun removeScripts(netName: String): CompletableFuture<Void> = runAsyncTask { | ||
35 | listScripts(netName).forEach { it.delete() } | ||
36 | } | ||
37 | |||
38 | fun generateIfaceCfg(netName: String): CompletableFuture<Void> = runAsyncTask { | ||
39 | VpnInterfaceConfiguration | ||
40 | .fromInvitation(AppPaths.invitationFile(netName)) | ||
41 | .write(AppPaths.netConfFile(netName)) | ||
42 | } | ||
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 | 52 | ||
53 | } | 53 | } |