diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands')
5 files changed, 0 insertions, 314 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.kt b/app/src/main/java/org/pacien/tincapp/commands/Command.kt deleted file mode 100644 index 132cda9..0000000 --- a/app/src/main/java/org/pacien/tincapp/commands/Command.kt +++ /dev/null | |||
@@ -1,46 +0,0 @@ | |||
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 java.util.* | ||
22 | |||
23 | /** | ||
24 | * @author pacien | ||
25 | */ | ||
26 | internal class Command(private val cmd: String) { | ||
27 | private data class Option(val key: String, val value: String?) { | ||
28 | fun toCommandLineOption(): String = if (value != null) "--$key=$value" else "--$key" | ||
29 | } | ||
30 | |||
31 | private val opts: MutableList<Option> = LinkedList() | ||
32 | private val args: MutableList<String> = LinkedList() | ||
33 | |||
34 | fun withOption(key: String, value: String? = null): Command { | ||
35 | this.opts.add(Option(key, value)) | ||
36 | return this | ||
37 | } | ||
38 | |||
39 | fun withArguments(vararg args: String): Command { | ||
40 | this.args.addAll(Arrays.asList(*args)) | ||
41 | return this | ||
42 | } | ||
43 | |||
44 | fun asList(): List<String> = listOf(cmd) + opts.map { it.toCommandLineOption() } + args | ||
45 | fun asArray(): Array<String> = this.asList().toTypedArray() | ||
46 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt deleted file mode 100644 index 29e011f..0000000 --- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt +++ /dev/null | |||
@@ -1,85 +0,0 @@ | |||
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 android.os.AsyncTask | ||
22 | import java8.util.concurrent.CompletableFuture | ||
23 | import java8.util.function.Supplier | ||
24 | import java.io.BufferedReader | ||
25 | import java.io.IOException | ||
26 | import java.io.InputStream | ||
27 | import java.io.InputStreamReader | ||
28 | |||
29 | /** | ||
30 | * @author pacien | ||
31 | */ | ||
32 | internal object Executor { | ||
33 | private const val FAILED = -1 | ||
34 | private const val SUCCESS = 0 | ||
35 | |||
36 | class CommandExecutionException(msg: String) : Exception(msg) | ||
37 | |||
38 | init { | ||
39 | System.loadLibrary("exec") | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * @return FAILED (-1) on error, forked child PID otherwise | ||
44 | */ | ||
45 | private external fun forkExec(args: Array<String>): Int | ||
46 | |||
47 | /** | ||
48 | * @return FAILED (-1) on error, the exit status of the process otherwise | ||
49 | */ | ||
50 | private external fun wait(pid: Int): Int | ||
51 | |||
52 | private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() | ||
53 | |||
54 | fun forkExec(cmd: Command): CompletableFuture<Unit> { | ||
55 | val pid = forkExec(cmd.asArray()).also { | ||
56 | if (it == FAILED) throw CommandExecutionException("Could not fork child process.") | ||
57 | } | ||
58 | |||
59 | return runAsyncTask { | ||
60 | val exitCode = wait(pid) | ||
61 | when (exitCode) { | ||
62 | SUCCESS -> Unit | ||
63 | FAILED -> throw CommandExecutionException("Process terminated abnormally.") | ||
64 | else -> throw CommandExecutionException("Non-zero exit status code ($exitCode).") | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | fun run(cmd: Command): Process = try { | ||
70 | ProcessBuilder(cmd.asList()).start() | ||
71 | } catch (e: IOException) { | ||
72 | throw CommandExecutionException(e.message ?: "Could not start process.") | ||
73 | } | ||
74 | |||
75 | fun call(cmd: Command): CompletableFuture<List<String>> = run(cmd).let { process -> | ||
76 | supplyAsyncTask<List<String>> { | ||
77 | val exitCode = process.waitFor() | ||
78 | if (exitCode == 0) read(process.inputStream) | ||
79 | else throw CommandExecutionException(read(process.errorStream).lastOrNull() ?: "Non-zero exit status ($exitCode).") | ||
80 | } | ||
81 | } | ||
82 | |||
83 | fun runAsyncTask(r: () -> Unit) = CompletableFuture.supplyAsync(Supplier(r), AsyncTask.THREAD_POOL_EXECUTOR)!! | ||
84 | fun <U> supplyAsyncTask(s: () -> U) = CompletableFuture.supplyAsync(Supplier(s), AsyncTask.THREAD_POOL_EXECUTOR)!! | ||
85 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt deleted file mode 100644 index 7f684c4..0000000 --- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt +++ /dev/null | |||
@@ -1,72 +0,0 @@ | |||
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 java8.util.concurrent.CompletableFuture | ||
22 | import org.pacien.tincapp.context.AppPaths | ||
23 | |||
24 | /** | ||
25 | * @author pacien | ||
26 | */ | ||
27 | object 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()) }) |