aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt
blob: 06725dcbf7d6a4bb6dba4a045773af5320f137a1 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.pacien.tincapp.activities.status.nodes

import org.pacien.tincapp.R
import org.pacien.tincapp.context.App
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * @author pacien
 */
data class NodeInfo(val name: String,
                    val id: String,
                    val ip: String,
                    val port: String,
                    val cipher: Int,
                    val digest: Int,
                    val macLength: Int,
                    val compression: Int,
                    val options: Int,
                    val status: Int,
                    val nextHop: String,
                    val via: String,
                    val distance: Int,
                    val pMtu: Int,
                    val minMtu: Int,
                    val maxMtu: Int) {

  // https://github.com/gsliepen/tinc/blob/950bbc8f2f9c580ac85bef7bab9a3ae36ea99c4b/src/info.c#L174
  fun reachabilityText(): String = when {
    ip == "MYSELF" -> RESOURCES.getString(R.string.status_node_reachability_this_node)
    distance == -1 -> RESOURCES.getString(R.string.status_node_reachability_unreachable)
    minMtu > 0 || nextHop == name -> RESOURCES.getString(R.string.status_node_reachability_direct_connection)
    distance > 1 -> RESOURCES.getString(R.string.status_node_reachability_via_format, nextHop)
    else -> RESOURCES.getString(R.string.status_node_reachability_unknown)
  }

  companion object {
    private const val NODE_DUMP_PATTERN_STRING =
      "(\\S+) " +
        "id (\\S+) " +
        "at (\\S+) " +
        "port (\\S+) " +
        "cipher (\\S+) " +
        "digest (\\S+) " +
        "maclength (\\S+) " +
        "compression (\\S+) " +
        "options (\\S+) " +
        "status (\\S+) " +
        "nexthop (\\S+) " +
        "via (\\S+) " +
        "distance (\\S+) " +
        "pmtu (\\S+) \\(min (\\S+) max (\\S+)\\)"

    private val NODE_DUMP_PATTERN by lazy { Pattern.compile(NODE_DUMP_PATTERN_STRING) }
    private val RESOURCES by lazy { App.getResources() }

    fun ofNodeDump(line: String) =
      ofNodeDump(NODE_DUMP_PATTERN.matcher(line).apply { find() })

    private fun ofNodeDump(matcher: Matcher) = NodeInfo(
      name = matcher[1],
      id = matcher[2],
      ip = matcher[3],
      port = matcher[4],
      cipher = matcher[5].toInt(),
      digest = matcher[6].toInt(),
      macLength = matcher[7].toInt(),
      compression = matcher[8].toInt(),
      options = matcher[9].toInt(16),
      status = matcher[10].toInt(16),
      nextHop = matcher[11],
      via = matcher[12],
      distance = matcher[13].toInt(),
      pMtu = matcher[14].toInt(),
      minMtu = matcher[15].toInt(),
      maxMtu = matcher[16].toInt()
    )

    private operator fun Matcher.get(index: Int) = group(index)
  }
}