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 | 17 |
1 files changed, 10 insertions, 7 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 278d20a..ec0512a 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | |||
@@ -6,6 +6,7 @@ import android.net.Uri | |||
6 | import android.net.VpnService | 6 | import android.net.VpnService |
7 | import android.os.ParcelFileDescriptor | 7 | import android.os.ParcelFileDescriptor |
8 | import android.util.Log | 8 | import android.util.Log |
9 | import java8.util.concurrent.CompletableFuture | ||
9 | import org.apache.commons.configuration2.ex.ConversionException | 10 | import org.apache.commons.configuration2.ex.ConversionException |
10 | import org.bouncycastle.openssl.PEMException | 11 | import org.bouncycastle.openssl.PEMException |
11 | import org.pacien.tincapp.BuildConfig | 12 | import org.pacien.tincapp.BuildConfig |
@@ -82,8 +83,8 @@ class TincVpnService : VpnService() { | |||
82 | return reportError(resources.getString(R.string.message_could_not_decrypt_private_keys_format, e.message)) | 83 | return reportError(resources.getString(R.string.message_could_not_decrypt_private_keys_format, e.message)) |
83 | } | 84 | } |
84 | 85 | ||
85 | Tincd.start(netName, deviceFd!!.fd, privateKeys.first?.fd, privateKeys.second?.fd) | 86 | val daemon = Tincd.start(netName, deviceFd!!.fd, privateKeys.first?.fd, privateKeys.second?.fd) |
86 | setState(true, netName, interfaceCfg, deviceFd) | 87 | setState(netName, interfaceCfg, deviceFd, daemon) |
87 | Log.i(TAG, "tinc daemon started.") | 88 | Log.i(TAG, "tinc daemon started.") |
88 | } | 89 | } |
89 | 90 | ||
@@ -111,16 +112,17 @@ class TincVpnService : VpnService() { | |||
111 | 112 | ||
112 | val TAG = this::class.java.canonicalName!! | 113 | val TAG = this::class.java.canonicalName!! |
113 | 114 | ||
114 | private var connected: Boolean = false | ||
115 | private var netName: String? = null | 115 | private var netName: String? = null |
116 | private var interfaceCfg: VpnInterfaceConfiguration? = null | 116 | private var interfaceCfg: VpnInterfaceConfiguration? = null |
117 | private var fd: ParcelFileDescriptor? = null | 117 | private var fd: ParcelFileDescriptor? = null |
118 | private var daemon: CompletableFuture<Void>? = null | ||
118 | 119 | ||
119 | private fun setState(connected: Boolean, netName: String?, interfaceCfg: VpnInterfaceConfiguration?, fd: ParcelFileDescriptor?) { | 120 | private fun setState(netName: String?, interfaceCfg: VpnInterfaceConfiguration?, |
120 | TincVpnService.connected = connected | 121 | fd: ParcelFileDescriptor?, daemon: CompletableFuture<Void>?) { |
121 | TincVpnService.netName = netName | 122 | TincVpnService.netName = netName |
122 | TincVpnService.interfaceCfg = interfaceCfg | 123 | TincVpnService.interfaceCfg = interfaceCfg |
123 | TincVpnService.fd = fd | 124 | TincVpnService.fd = fd |
125 | TincVpnService.daemon = daemon | ||
124 | } | 126 | } |
125 | 127 | ||
126 | fun startVpn(netName: String, passphrase: String? = null) { | 128 | fun startVpn(netName: String, passphrase: String? = null) { |
@@ -132,18 +134,19 @@ class TincVpnService : VpnService() { | |||
132 | try { | 134 | try { |
133 | Log.i(TAG, "Stopping any running tinc daemon.") | 135 | Log.i(TAG, "Stopping any running tinc daemon.") |
134 | if (netName != null) Tinc.stop(netName!!) | 136 | if (netName != null) Tinc.stop(netName!!) |
137 | daemon?.get() | ||
135 | fd?.close() | 138 | fd?.close() |
136 | Log.i(TAG, "All tinc daemons stopped.") | 139 | Log.i(TAG, "All tinc daemons stopped.") |
137 | } catch (e: IOException) { | 140 | } catch (e: IOException) { |
138 | Log.wtf(TAG, e) | 141 | Log.wtf(TAG, e) |
139 | } finally { | 142 | } finally { |
140 | setState(false, null, null, null) | 143 | setState(null, null, null, null) |
141 | } | 144 | } |
142 | } | 145 | } |
143 | 146 | ||
144 | fun getCurrentNetName() = netName | 147 | fun getCurrentNetName() = netName |
145 | fun getCurrentInterfaceCfg() = interfaceCfg | 148 | fun getCurrentInterfaceCfg() = interfaceCfg |
146 | fun isConnected() = connected | 149 | fun isConnected() = !(daemon?.isDone ?: true) |
147 | 150 | ||
148 | } | 151 | } |
149 | 152 | ||