aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt
diff options
context:
space:
mode:
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.kt74
1 files changed, 74 insertions, 0 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
new file mode 100644
index 0000000..32fc1f9
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt
@@ -0,0 +1,74 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.commands
20
21import org.pacien.tincapp.R
22import org.pacien.tincapp.commands.Executor.runAsyncTask
23import org.pacien.tincapp.context.App
24import org.pacien.tincapp.context.AppPaths
25import org.pacien.tincapp.data.TincConfiguration
26import org.pacien.tincapp.data.VpnInterfaceConfiguration
27import org.pacien.tincapp.utils.PemUtils
28import java.io.FileNotFoundException
29
30/**
31 * @author pacien
32 */
33object TincApp {
34 private val SCRIPT_SUFFIXES = listOf("-up", "-down", "-created", "-accepted")
35 private val STATIC_SCRIPTS = listOf("tinc", "host", "subnet", "invitation").flatMap { s -> SCRIPT_SUFFIXES.map { s + it } }
36
37 private fun listScripts(netName: String) = AppPaths.confDir(netName).listFiles { f -> f.name in STATIC_SCRIPTS } +
38 AppPaths.hostsDir(netName).listFiles { f -> SCRIPT_SUFFIXES.any { f.name.endsWith(it) } }
39
40 fun listPrivateKeys(netName: String) = try {
41 TincConfiguration.fromTincConfiguration(AppPaths.existing(AppPaths.tincConfFile(netName))).let {
42 listOf(
43 it.privateKeyFile ?: AppPaths.defaultRsaPrivateKeyFile(netName),
44 it.ed25519PrivateKeyFile ?: AppPaths.defaultEd25519PrivateKeyFile(netName))
45 }
46 } catch (e: FileNotFoundException) {
47 throw FileNotFoundException(App.getResources().getString(R.string.message_network_config_not_found_format, e.message!!))
48 }
49
50 fun removeScripts(netName: String) = runAsyncTask {
51 listScripts(netName).forEach { it.delete() }
52 }
53
54 fun generateIfaceCfg(netName: String) = runAsyncTask {
55 VpnInterfaceConfiguration
56 .fromInvitation(AppPaths.invitationFile(netName))
57 .write(AppPaths.netConfFile(netName))
58 }
59
60 fun generateIfaceCfgTemplate(netName: String) = runAsyncTask {
61 App.getResources().openRawResource(R.raw.network).use { inputStream ->
62 AppPaths.netConfFile(netName).outputStream().use { inputStream.copyTo(it) }
63 }
64 }
65
66 fun setPassphrase(netName: String, currentPassphrase: String? = null, newPassphrase: String?) = runAsyncTask {
67 listPrivateKeys(netName)
68 .filter { it.exists() }
69 .map { Pair(PemUtils.read(it), it) }
70 .map { Pair(PemUtils.decrypt(it.first, currentPassphrase), it.second) }
71 .map { Pair(if (newPassphrase?.isNotEmpty() == true) PemUtils.encrypt(it.first, newPassphrase) else it.first, it.second) }
72 .forEach { PemUtils.write(it.first, it.second.writer()) }
73 }
74}