diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | 25 |
1 files changed, 16 insertions, 9 deletions
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 0bf72a1..69811f4 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | |||
@@ -5,7 +5,6 @@ import android.content.Intent | |||
5 | import android.net.VpnService | 5 | import android.net.VpnService |
6 | import android.os.ParcelFileDescriptor | 6 | import android.os.ParcelFileDescriptor |
7 | import android.support.v4.content.LocalBroadcastManager | 7 | import android.support.v4.content.LocalBroadcastManager |
8 | import android.util.Log | ||
9 | import java8.util.concurrent.CompletableFuture | 8 | import java8.util.concurrent.CompletableFuture |
10 | import org.apache.commons.configuration2.ex.ConversionException | 9 | import org.apache.commons.configuration2.ex.ConversionException |
11 | import org.bouncycastle.openssl.PEMException | 10 | import org.bouncycastle.openssl.PEMException |
@@ -22,19 +21,28 @@ import org.pacien.tincapp.extensions.Java.applyIgnoringException | |||
22 | import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg | 21 | import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg |
23 | import org.pacien.tincapp.intent.Actions | 22 | import org.pacien.tincapp.intent.Actions |
24 | import org.pacien.tincapp.utils.TincKeyring | 23 | import org.pacien.tincapp.utils.TincKeyring |
24 | import org.slf4j.Logger | ||
25 | import org.slf4j.LoggerFactory | ||
25 | import java.io.FileNotFoundException | 26 | import java.io.FileNotFoundException |
26 | 27 | ||
27 | /** | 28 | /** |
28 | * @author pacien | 29 | * @author pacien |
29 | */ | 30 | */ |
30 | class TincVpnService : VpnService() { | 31 | class TincVpnService : VpnService() { |
32 | private var logger: Logger? = null | ||
33 | |||
34 | override fun onCreate() { | ||
35 | super.onCreate() | ||
36 | logger = LoggerFactory.getLogger(this.javaClass) | ||
37 | } | ||
38 | |||
31 | override fun onDestroy() { | 39 | override fun onDestroy() { |
32 | stopVpn() | 40 | stopVpn() |
33 | super.onDestroy() | 41 | super.onDestroy() |
34 | } | 42 | } |
35 | 43 | ||
36 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | 44 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { |
37 | Log.i(TAG, intent.action) | 45 | logger?.info("Intent received: {}", intent.action) |
38 | 46 | ||
39 | when { | 47 | when { |
40 | intent.action == Actions.ACTION_CONNECT && intent.scheme == Actions.TINC_SCHEME -> | 48 | intent.action == Actions.ACTION_CONNECT && intent.scheme == Actions.TINC_SCHEME -> |
@@ -61,7 +69,7 @@ class TincVpnService : VpnService() { | |||
61 | if (!AppPaths.confDir(netName).exists()) | 69 | if (!AppPaths.confDir(netName).exists()) |
62 | return reportError(resources.getString(R.string.message_no_configuration_for_network_format, netName), docTopic = "configuration") | 70 | return reportError(resources.getString(R.string.message_no_configuration_for_network_format, netName), docTopic = "configuration") |
63 | 71 | ||
64 | Log.i(TAG, "Starting tinc daemon for network \"$netName\".") | 72 | logger?.info("Starting tinc daemon for network \"$netName\".") |
65 | if (isConnected()) stopVpn() | 73 | if (isConnected()) stopVpn() |
66 | 74 | ||
67 | val interfaceCfg = try { | 75 | val interfaceCfg = try { |
@@ -108,17 +116,17 @@ class TincVpnService : VpnService() { | |||
108 | if (exception != null) { | 116 | if (exception != null) { |
109 | reportError(resources.getString(R.string.message_daemon_exited, exception.cause!!.message!!), exception) | 117 | reportError(resources.getString(R.string.message_daemon_exited, exception.cause!!.message!!), exception) |
110 | } else { | 118 | } else { |
111 | Log.i(TAG, "tinc daemon started.") | 119 | logger?.info("tinc daemon started.") |
112 | broadcastEvent(Actions.EVENT_CONNECTED) | 120 | broadcastEvent(Actions.EVENT_CONNECTED) |
113 | } | 121 | } |
114 | } | 122 | } |
115 | } | 123 | } |
116 | 124 | ||
117 | private fun stopVpn(): Unit = synchronized(this) { | 125 | private fun stopVpn(): Unit = synchronized(this) { |
118 | Log.i(TAG, "Stopping any running tinc daemon.") | 126 | logger?.info("Stopping any running tinc daemon.") |
119 | netName?.let { | 127 | netName?.let { |
120 | Tinc.stop(it).thenRun { | 128 | Tinc.stop(it).thenRun { |
121 | Log.i(TAG, "All tinc daemons stopped.") | 129 | logger?.info("All tinc daemons stopped.") |
122 | broadcastEvent(Actions.EVENT_DISCONNECTED) | 130 | broadcastEvent(Actions.EVENT_DISCONNECTED) |
123 | setState(null, null, null, null) | 131 | setState(null, null, null, null) |
124 | } | 132 | } |
@@ -127,9 +135,9 @@ class TincVpnService : VpnService() { | |||
127 | 135 | ||
128 | private fun reportError(msg: String, e: Throwable? = null, docTopic: String? = null) { | 136 | private fun reportError(msg: String, e: Throwable? = null, docTopic: String? = null) { |
129 | if (e != null) | 137 | if (e != null) |
130 | Log.e(TAG, msg, e) | 138 | logger?.error(msg, e) |
131 | else | 139 | else |
132 | Log.e(TAG, msg) | 140 | logger?.error(msg) |
133 | 141 | ||
134 | broadcastEvent(Actions.EVENT_ABORTED) | 142 | broadcastEvent(Actions.EVENT_ABORTED) |
135 | App.alert(R.string.title_unable_to_start_tinc, msg, | 143 | App.alert(R.string.title_unable_to_start_tinc, msg, |
@@ -147,7 +155,6 @@ class TincVpnService : VpnService() { | |||
147 | 155 | ||
148 | companion object { | 156 | companion object { |
149 | private const val SETUP_DELAY = 500L // ms | 157 | private const val SETUP_DELAY = 500L // ms |
150 | private val TAG = this::class.java.canonicalName!! | ||
151 | private var netName: String? = null | 158 | private var netName: String? = null |
152 | private var interfaceCfg: VpnInterfaceConfiguration? = null | 159 | private var interfaceCfg: VpnInterfaceConfiguration? = null |
153 | private var fd: ParcelFileDescriptor? = null | 160 | private var fd: ParcelFileDescriptor? = null |