aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands
diff options
context:
space:
mode:
authorpacien2018-08-07 01:08:22 +0200
committerpacien2018-08-07 01:08:22 +0200
commit9d8846e105904b31478ed19d3a34c0d62708abcf (patch)
tree1b2bcaa01751d2f681e8ff847bc7cc57b42fa85e /app/src/main/java/org/pacien/tincapp/commands
parent20dce2236257a002a1f143ee4115e1849178ac78 (diff)
downloadtincapp-9d8846e105904b31478ed19d3a34c0d62708abcf.tar.gz
Revert "Rename source directory"
This reverts commit dbba24e
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Command.kt46
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt85
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tinc.kt72
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/TincApp.kt74
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tincd.kt37
5 files changed, 314 insertions, 0 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
new file mode 100644
index 0000000..132cda9
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/commands/Command.kt
@@ -0,0 +1,46 @@
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 java.util.*
22
23/**
24 * @author pacien
25 */
26internal 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
new file mode 100644
index 0000000..29e011f
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
@@ -0,0 +1,85 @@
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 android.os.AsyncTask
22import java8.util.concurrent.CompletableFuture
23import java8.util.function.Supplier
24import java.io.BufferedReader
25import java.io.IOException
26import java.io.InputStream
27import java.io.InputStreamReader
28
29/**
30 * @author pacien
31 */
32internal 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
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)