diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt new file mode 100644 index 0000000..06725dc --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt | |||
@@ -0,0 +1,99 @@ | |||
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.activities.status.nodes | ||
20 | |||
21 | import org.pacien.tincapp.R | ||
22 | import org.pacien.tincapp.context.App | ||
23 | import java.util.regex.Matcher | ||
24 | import java.util.regex.Pattern | ||
25 | |||
26 | /** | ||
27 | * @author pacien | ||
28 | */ | ||
29 | data class NodeInfo(val name: String, | ||
30 | val id: String, | ||
31 | val ip: String, | ||
32 | val port: String, | ||
33 | val cipher: Int, | ||
34 | val digest: Int, | ||
35 | val macLength: Int, | ||
36 | val compression: Int, | ||
37 | val options: Int, | ||
38 | val status: Int, | ||
39 | val nextHop: String, | ||
40 | val via: String, | ||
41 | val distance: Int, | ||
42 | val pMtu: Int, | ||
43 | val minMtu: Int, | ||
44 | val maxMtu: Int) { | ||
45 | |||
46 | // https://github.com/gsliepen/tinc/blob/950bbc8f2f9c580ac85bef7bab9a3ae36ea99c4b/src/info.c#L174 | ||
47 | fun reachabilityText(): String = when { | ||
48 | ip == "MYSELF" -> RESOURCES.getString(R.string.status_node_reachability_this_node) | ||
49 | distance == -1 -> RESOURCES.getString(R.string.status_node_reachability_unreachable) | ||
50 | minMtu > 0 || nextHop == name -> RESOURCES.getString(R.string.status_node_reachability_direct_connection) | ||
51 | distance > 1 -> RESOURCES.getString(R.string.status_node_reachability_via_format, nextHop) | ||
52 | else -> RESOURCES.getString(R.string.status_node_reachability_unknown) | ||
53 | } | ||
54 | |||
55 | companion object { | ||
56 | private const val NODE_DUMP_PATTERN_STRING = | ||
57 | "(\\S+) " + | ||
58 | "id (\\S+) " + | ||
59 | "at (\\S+) " + | ||
60 | "port (\\S+) " + | ||
61 | "cipher (\\S+) " + | ||
62 | "digest (\\S+) " + | ||
63 | "maclength (\\S+) " + | ||
64 | "compression (\\S+) " + | ||
65 | "options (\\S+) " + | ||
66 | "status (\\S+) " + | ||
67 | "nexthop (\\S+) " + | ||
68 | "via (\\S+) " + | ||
69 | "distance (\\S+) " + | ||
70 | "pmtu (\\S+) \\(min (\\S+) max (\\S+)\\)" | ||
71 | |||
72 | private val NODE_DUMP_PATTERN by lazy { Pattern.compile(NODE_DUMP_PATTERN_STRING) } | ||
73 | private val RESOURCES by lazy { App.getResources() } | ||
74 | |||
75 | fun ofNodeDump(line: String) = | ||
76 | ofNodeDump(NODE_DUMP_PATTERN.matcher(line).apply { find() }) | ||
77 | |||
78 | private fun ofNodeDump(matcher: Matcher) = NodeInfo( | ||
79 | name = matcher[1], | ||
80 | id = matcher[2], | ||
81 | ip = matcher[3], | ||
82 | port = matcher[4], | ||
83 | cipher = matcher[5].toInt(), | ||
84 | digest = matcher[6].toInt(), | ||
85 | macLength = matcher[7].toInt(), | ||
86 | compression = matcher[8].toInt(), | ||
87 | options = matcher[9].toInt(16), | ||
88 | status = matcher[10].toInt(16), | ||
89 | nextHop = matcher[11], | ||
90 | via = matcher[12], | ||
91 | distance = matcher[13].toInt(), | ||
92 | pMtu = matcher[14].toInt(), | ||
93 | minMtu = matcher[15].toInt(), | ||
94 | maxMtu = matcher[16].toInt() | ||
95 | ) | ||
96 | |||
97 | private operator fun Matcher.get(index: Int) = group(index) | ||
98 | } | ||
99 | } | ||