From 80394001852e8400fd2c3d30e441306957d32243 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 4 Jul 2017 18:51:57 +0200 Subject: Add status activity --- .../org/pacien/tincapp/service/TincVpnService.kt | 44 ++++++++++++++++++---- .../tincapp/service/VpnInterfaceConfiguraton.kt | 28 ++++++++------ 2 files changed, 53 insertions(+), 19 deletions(-) (limited to 'app/src/main/java/org/pacien/tincapp/service') 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 30e2956..7813601 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -3,6 +3,7 @@ package org.pacien.tincapp.service import android.app.Service import android.content.Intent import android.net.VpnService +import android.os.ParcelFileDescriptor import org.pacien.tincapp.BuildConfig import org.pacien.tincapp.commands.Tinc import org.pacien.tincapp.commands.Tincd @@ -11,48 +12,75 @@ import org.pacien.tincapp.context.AppPaths import org.pacien.tincapp.utils.applyIgnoringException import java.io.IOException + /** * @author pacien */ class TincVpnService : VpnService() { - private var netName: String? = null - override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { - startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!) + when (intent.getSerializableExtra(INTENT_EXTRA_ACTION)) { + Action.START -> startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!) + Action.STOP -> onDestroy() + } + return Service.START_STICKY } override fun onDestroy() = try { Tinc.stop(netName!!) + fd!!.close() } catch (e: IOException) { e.printStackTrace() } finally { netName = null + interfaceCfg = null + fd = null + super.onDestroy() } private fun startVpn(netName: String) { - if (netName == this.netName) onDestroy() - this.netName = netName + if (netName == TincVpnService.netName) onDestroy() + TincVpnService.netName = netName + TincVpnService.interfaceCfg = VpnInterfaceConfiguration(AppPaths.netConfFile(netName)) - val net = Builder().setSession(netName) - net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netName))) + val net = Builder().setSession(netName).apply(TincVpnService.interfaceCfg!!) applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) try { - Tincd.start(netName, net.establish().detachFd()) + fd = net.establish() + Tincd.start(netName, fd!!.fd) } catch (e: IOException) { e.printStackTrace() } } companion object { + + private val INTENT_EXTRA_ACTION = "action" private val INTENT_EXTRA_NET_NAME = "netName" + private enum class Action { START, STOP } + + private var netName: String? = null + private var interfaceCfg: VpnInterfaceConfiguration? = null + private var fd: ParcelFileDescriptor? = null + fun startVpn(netName: String) { App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) + .putExtra(INTENT_EXTRA_ACTION, Action.START) .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) } + + fun stopVpn() { + App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) + .putExtra(INTENT_EXTRA_ACTION, Action.STOP)) + } + + fun getCurrentNetName() = netName + fun getCurrentInterfaceCfg() = interfaceCfg + fun isConnected() = netName != null + } } diff --git a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt index 520d68c..50ccb20 100644 --- a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt +++ b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt @@ -25,19 +25,25 @@ private fun Configuration.getIntList(key: String): List = getList(Int::clas data class CidrAddress(val address: String, val prefix: Int) { constructor(slashSeparated: String) : - this(slashSeparated.substringBefore("/"), Integer.parseInt(slashSeparated.substringAfter("/"))) + this(slashSeparated.substringBefore(SEPARATOR), Integer.parseInt(slashSeparated.substringAfter(SEPARATOR))) + + override fun toString() = address + SEPARATOR + prefix + + companion object { + private val SEPARATOR = "/" + } } -data class VpnInterfaceConfiguration(val addresses: List, - val routes: List, - val dnsServers: List, - val searchDomains: List, - val allowedApplications: List, - val disallowedApplications: List, - val allowedFamilies: List, - val allowBypass: Boolean, - val blocking: Boolean, - val mtu: Int?) { +data class VpnInterfaceConfiguration(val addresses: List = emptyList(), + val routes: List = emptyList(), + val dnsServers: List = emptyList(), + val searchDomains: List = emptyList(), + val allowedApplications: List = emptyList(), + val disallowedApplications: List = emptyList(), + val allowedFamilies: List = emptyList(), + val allowBypass: Boolean = false, + val blocking: Boolean = false, + val mtu: Int? = null) { constructor(cfg: Configuration) : this( cfg.getCidrList(KEY_ADDRESSES), -- cgit v1.2.3