aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Tinc.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tinc.kt72
1 files changed, 72 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
new file mode 100644
index 0000000..7f684c4
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
@@ -0,0 +1,72 @@
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 java8.util.concurrent.CompletableFuture
22import org.pacien.tincapp.context.AppPaths
23
24/**
25 * @author pacien
26 */
27object Tinc {
28 private fun newCommand(netName: String): Command =
29 Command(AppPaths.tinc().absolutePath)
30 .withOption("config", AppPaths.confDir(netName).absolutePath)
31 .withOption("pidfile", AppPaths.pidFile(netName).absolutePath)
32
33 fun stop(netName: String): CompletableFuture<Unit> =
34 Executor.call(newCommand(netName).withArguments("stop"))
35 .thenApply { }
36
37 fun pid(netName: String): CompletableFuture<Int> =
38 Executor.call(newCommand(netName).withArguments("pid"))
39 .thenApply { Integer.parseInt(it.first()) }
40
41 fun dumpNodes(netName: String, reachable: Boolean = false): CompletableFuture<List<String>> =
42 Executor.call(
43 if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes")
44 else newCommand(netName).withArguments("dump", "nodes"))
45
46 fun info(netName: String, node: String): CompletableFuture<String> =
47 Executor.call(newCommand(netName).withArguments("info", node))
48 .thenApply<String> { it.joinToString("\n") }
49
50 fun init(netName: String, nodeName: String): CompletableFuture<String> =
51 if (netName.isBlank())
52 CompletableFuture.failedFuture(IllegalArgumentException("Network name cannot be blank."))
53 else
54 Executor.call(Command(AppPaths.tinc().absolutePath)
55 .withOption("config", AppPaths.confDir(netName).absolutePath)
56 .withArguments("init", nodeName))
57 .thenApply<String> { it.joinToString("\n") }
58
59 fun join(netName: String, invitationUrl: String): CompletableFuture<String> =
60 if (netName.isBlank())
61 CompletableFuture.failedFuture(IllegalArgumentException("Network name cannot be blank."))
62 else
63 Executor.call(Command(AppPaths.tinc().absolutePath)
64 .withOption("config", AppPaths.confDir(netName).absolutePath)
65 .withArguments("join", invitationUrl))
66 .thenApply<String> { it.joinToString("\n") }
67
68 fun log(netName: String, level: Int? = null): Process =
69 Executor.run(newCommand(netName)
70 .withArguments("log")
71 .apply { if (level != null) withArguments(level.toString()) })
72}