From b93bf5513a1c43ac11acef409c81dc7238324d69 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 26 Sep 2018 00:36:40 +0200 Subject: Trigger tinc reconnection on network state change --- .../main/java/org/pacien/tincapp/commands/Tinc.kt | 4 ++ .../tincapp/service/ConnectivityChangeReceiver.kt | 60 ++++++++++++++++++++++ .../org/pacien/tincapp/service/TincVpnService.kt | 6 +++ 3 files changed, 70 insertions(+) create mode 100644 app/src/main/java/org/pacien/tincapp/service/ConnectivityChangeReceiver.kt (limited to 'app') diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt index 4d00805..dc23935 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt @@ -34,6 +34,10 @@ object Tinc { Executor.call(newCommand(netName).withArguments("stop")) .thenApply { } + fun retry(netName: String): CompletableFuture = + Executor.call(newCommand(netName).withArguments("retry")) + .thenApply { } + fun pid(netName: String): CompletableFuture = Executor.call(newCommand(netName).withArguments("pid")) .thenApply { Integer.parseInt(it.first()) } diff --git a/app/src/main/java/org/pacien/tincapp/service/ConnectivityChangeReceiver.kt b/app/src/main/java/org/pacien/tincapp/service/ConnectivityChangeReceiver.kt new file mode 100644 index 0000000..ba9aa95 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/service/ConnectivityChangeReceiver.kt @@ -0,0 +1,60 @@ +/* + * 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.service + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.net.ConnectivityManager +import org.pacien.tincapp.commands.Tinc +import org.slf4j.LoggerFactory + +/** + * @author pacien + */ +object ConnectivityChangeReceiver : BroadcastReceiver() { + private val log by lazy { LoggerFactory.getLogger(this.javaClass)!! } + private val tincCtl = Tinc + private val tincVpnService = TincVpnService + + override fun onReceive(context: Context, intent: Intent) { + log.info("Connectivity change intent received: {}", intent.toString()) + if (isNetworkAvailable(intent)) attemptReconnect() + } + + private fun attemptReconnect() { + tincVpnService.getCurrentNetName()?.let { netName -> + log.info("Sending immediate reconnection request to the tinc daemon.") + tincCtl.retry(netName) + } + } + + private fun isNetworkAvailable(intent: Intent) = + !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false) + + fun registerWatcher(context: Context) { + val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) + context.registerReceiver(this, filter) + } + + fun unregisterWatcher(context: Context) { + context.unregisterReceiver(this) + } +} diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt index 45058e2..28f1347 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -49,6 +49,7 @@ import java.io.FileNotFoundException */ class TincVpnService : VpnService() { private val log by lazy { LoggerFactory.getLogger(this.javaClass)!! } + private val connectivityChangeReceiver = ConnectivityChangeReceiver override fun onDestroy() { stopVpn() @@ -150,11 +151,16 @@ class TincVpnService : VpnService() { log.info("tinc daemon started.") broadcastEvent(Actions.EVENT_CONNECTED) } + + connectivityChangeReceiver.registerWatcher(this) } } private fun stopVpn(): Unit = synchronized(this) { log.info("Stopping any running tinc daemon.") + + connectivityChangeReceiver.unregisterWatcher(this) + getCurrentNetName()?.let { Tinc.stop(it).thenRun { log.info("All tinc daemons stopped.") -- cgit v1.2.3