aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt
blob: b564b6a799f60f4b35f449d4e706b1a3224d32e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.pacien.tincapp.commands

import java8.util.concurrent.CompletableFuture
import org.pacien.tincapp.R
import org.pacien.tincapp.commands.Executor.runAsyncTask
import org.pacien.tincapp.context.App
import org.pacien.tincapp.context.AppPaths
import org.pacien.tincapp.data.TincConfiguration
import org.pacien.tincapp.data.VpnInterfaceConfiguration
import org.pacien.tincapp.utils.PemUtils
import java.io.FileNotFoundException

/**
 * @author pacien
 */
object TincApp {

    private val SCRIPT_SUFFIXES = listOf("-up", "-down", "-created", "-accepted")
    private val STATIC_SCRIPTS = listOf("tinc", "host", "subnet", "invitation").flatMap { s -> SCRIPT_SUFFIXES.map { s + it } }

    private fun listScripts(netName: String) = AppPaths.confDir(netName).listFiles { f -> f.name in STATIC_SCRIPTS } +
            AppPaths.hostsDir(netName).listFiles { f -> SCRIPT_SUFFIXES.any { f.name.endsWith(it) } }

    fun listPrivateKeys(netName: String) = try {
        TincConfiguration.fromTincConfiguration(AppPaths.existing(AppPaths.tincConfFile(netName))).let {
            listOf(
                    it.privateKeyFile ?: AppPaths.defaultRsaPrivateKeyFile(netName),
                    it.ed25519PrivateKeyFile ?: AppPaths.defaultEd25519PrivateKeyFile(netName))
        }
    } catch (e: FileNotFoundException) {
        throw FileNotFoundException(App.getResources().getString(R.string.message_network_config_not_found_format, e.message!!))
    }

    fun removeScripts(netName: String): CompletableFuture<Void> = runAsyncTask {
        listScripts(netName).forEach { it.delete() }
    }

    fun generateIfaceCfg(netName: String): CompletableFuture<Void> = runAsyncTask {
        VpnInterfaceConfiguration
                .fromInvitation(AppPaths.invitationFile(netName))
                .write(AppPaths.netConfFile(netName))
    }

    fun setPassphrase(netName: String, currentPassphrase: String? = null, newPassphrase: String?): CompletableFuture<Void> = runAsyncTask {
        listPrivateKeys(netName)
                .filter { it.exists() }
                .map { Pair(PemUtils.read(it), it) }
                .map { Pair(PemUtils.decrypt(it.first, currentPassphrase), it.second) }
                .map { Pair(if (newPassphrase?.isNotEmpty() == true) PemUtils.encrypt(it.first, newPassphrase) else it.first, it.second) }
                .forEach { PemUtils.write(it.first, it.second.writer()) }
    }

}