diff options
author | pacien | 2018-08-07 01:08:22 +0200 |
---|---|---|
committer | pacien | 2018-08-07 01:08:22 +0200 |
commit | 9d8846e105904b31478ed19d3a34c0d62708abcf (patch) | |
tree | 1b2bcaa01751d2f681e8ff847bc7cc57b42fa85e /app/src/main/java/org/pacien/tincapp/commands/TincApp.kt | |
parent | 20dce2236257a002a1f143ee4115e1849178ac78 (diff) | |
download | tincapp-9d8846e105904b31478ed19d3a34c0d62708abcf.tar.gz |
Revert "Rename source directory"
This reverts commit dbba24e
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 | 74 |
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 | |||
19 | package org.pacien.tincapp.commands | ||
20 | |||
21 | import org.pacien.tincapp.R | ||
22 | import org.pacien.tincapp.commands.Executor.runAsyncTask | ||
23 | import org.pacien.tincapp.context.App | ||
24 | import org.pacien.tincapp.context.AppPaths | ||
25 | import org.pacien.tincapp.data.TincConfiguration | ||
26 | import org.pacien.tincapp.data.VpnInterfaceConfiguration | ||
27 | import org.pacien.tincapp.utils.PemUtils | ||
28 | import java.io.FileNotFoundException | ||
29 | |||
30 | /** | ||
31 | * @author pacien | ||
32 | */ | ||
33 | object 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 | } | ||