diff options
author | Pacien TRAN-GIRARD | 2017-07-03 22:52:34 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2017-07-03 22:52:34 +0200 |
commit | 862b991d9a10296dc0c419fa93babb11614c3957 (patch) | |
tree | a1c3e4ae7e0af7185c19d8df2b5ad1b5900d0afc /app/src/main/java | |
parent | 24dd15ecda923f63ea1c855059f1d3b47bd1cd10 (diff) | |
download | tincapp-862b991d9a10296dc0c419fa93babb11614c3957.tar.gz |
Only allow one VPN connection at a time
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | 31 |
1 files changed, 17 insertions, 14 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 59d9e34..30e2956 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | |||
@@ -19,28 +19,31 @@ class TincVpnService : VpnService() { | |||
19 | private var netName: String? = null | 19 | private var netName: String? = null |
20 | 20 | ||
21 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | 21 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { |
22 | netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME)!! | 22 | startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!) |
23 | return Service.START_STICKY | ||
24 | } | ||
23 | 25 | ||
24 | val net = Builder().setSession(netName) | 26 | override fun onDestroy() = try { |
25 | net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netName!!))) | 27 | Tinc.stop(netName!!) |
26 | applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) | 28 | } catch (e: IOException) { |
29 | e.printStackTrace() | ||
30 | } finally { | ||
31 | netName = null | ||
32 | } | ||
27 | 33 | ||
28 | try { | 34 | private fun startVpn(netName: String) { |
29 | Tincd.start(netName!!, net.establish().detachFd()) | 35 | if (netName == this.netName) onDestroy() |
30 | } catch (e: IOException) { | 36 | this.netName = netName |
31 | e.printStackTrace() | ||
32 | } | ||
33 | 37 | ||
34 | return Service.START_STICKY | 38 | val net = Builder().setSession(netName) |
35 | } | 39 | net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netName))) |
40 | applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) | ||
36 | 41 | ||
37 | override fun onDestroy() { | ||
38 | try { | 42 | try { |
39 | Tinc.stop(netName!!) | 43 | Tincd.start(netName, net.establish().detachFd()) |
40 | } catch (e: IOException) { | 44 | } catch (e: IOException) { |
41 | e.printStackTrace() | 45 | e.printStackTrace() |
42 | } | 46 | } |
43 | |||
44 | } | 47 | } |
45 | 48 | ||
46 | companion object { | 49 | companion object { |