From 4702b7441b65173a1e0c7f0b560e672d06ee4e4d Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 21 Aug 2018 01:35:06 +0200 Subject: Show node reachability in node list --- .../tincapp/activities/status/nodes/NodeInfo.kt | 99 ++++++++++++++++++++++ .../status/nodes/NodeInfoArrayAdapter.kt | 45 ++++++++++ .../activities/status/nodes/NodeListFragment.kt | 15 +--- .../activities/status/nodes/NodeListLiveData.kt | 4 +- 4 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfo.kt create mode 100644 app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt (limited to 'app/src/main/java') 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 @@ +/* + * 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 . + */ + +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) + } +} diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt new file mode 100644 index 0000000..ee103ee --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeInfoArrayAdapter.kt @@ -0,0 +1,45 @@ +/* + * 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 . + */ + +package org.pacien.tincapp.activities.status.nodes + +import android.content.Context +import android.databinding.DataBindingUtil +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import org.pacien.tincapp.databinding.StatusNodeListItemBinding + +/** + * @author pacien + */ +class NodeInfoArrayAdapter(context: Context?, private val onItemClick: (NodeInfo) -> Unit) : ArrayAdapter(context, -1) { + private val layoutInflater = LayoutInflater.from(context)!! + + override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { + val binding = when (convertView) { + null -> StatusNodeListItemBinding.inflate(layoutInflater, parent, false) + else -> DataBindingUtil.getBinding(convertView)!! + } + + binding.nodeInfo = getItem(position) + binding.onClick = onItemClick + return binding.root + } +} diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt index 115809e..1f762c0 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt @@ -26,9 +26,6 @@ import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter -import android.widget.TextView import kotlinx.android.synthetic.main.base.* import kotlinx.android.synthetic.main.status_node_info_dialog.view.* import kotlinx.android.synthetic.main.status_node_list.* @@ -47,8 +44,8 @@ class NodeListFragment : Fragment() { private val tincCtl = Tinc private val netName by lazy { vpnService.getCurrentNetName()!! } private val nodeListViewModel by lazy { ViewModelProviders.of(this).get(NodeListViewModel::class.java) } - private val nodeListAdapter by lazy { ArrayAdapter(context, R.layout.status_node_list_item) } - private val nodeListObserver by lazy { Observer> { nodeListAdapter.setElements(it) } } + private val nodeListAdapter by lazy { NodeInfoArrayAdapter(context, this::onItemClick) } + private val nodeListObserver by lazy { Observer> { nodeListAdapter.setElements(it) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -63,15 +60,11 @@ class NodeListFragment : Fragment() { status_node_list.hideTopSeparator() status_node_list.hideBottomSeparator() status_node_list.emptyView = status_node_list_placeholder - status_node_list.onItemClickListener = AdapterView.OnItemClickListener(this::onItemClick) status_node_list.adapter = nodeListAdapter } - @Suppress("UNUSED_PARAMETER") - private fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = when (view) { - is TextView -> showNodeInfo(view.text.toString()) - else -> Unit - } + private fun onItemClick(nodeInfo: NodeInfo) = + showNodeInfo(nodeInfo.name) private fun showNodeInfo(nodeName: String) { val dialogTextView = layoutInflater.inflate(R.layout.status_node_info_dialog, main_content, false) diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt index 70ea54e..cada4bc 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt @@ -25,12 +25,12 @@ import java.util.concurrent.TimeUnit /** * @author pacien */ -class NodeListLiveData(private val netName: String) : SelfRefreshingLiveData>(1, TimeUnit.SECONDS) { +class NodeListLiveData(private val netName: String) : SelfRefreshingLiveData>(1, TimeUnit.SECONDS) { private val tincCtl = Tinc override fun onRefresh() { val nodeList = tincCtl.dumpNodes(netName) - .thenApply { list -> list.map { it.substringBefore(' ') } } + .thenApply { list -> list.map { NodeInfo.ofNodeDump(it) } } .get() postValue(nodeList) -- cgit v1.2.3