aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
diff options
context:
space:
mode:
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.kt44
1 files changed, 36 insertions, 8 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 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
3import android.app.Service 3import android.app.Service
4import android.content.Intent 4import android.content.Intent
5import android.net.VpnService 5import android.net.VpnService
6import android.os.ParcelFileDescriptor
6import org.pacien.tincapp.BuildConfig 7import org.pacien.tincapp.BuildConfig
7import org.pacien.tincapp.commands.Tinc 8import org.pacien.tincapp.commands.Tinc
8import org.pacien.tincapp.commands.Tincd 9import org.pacien.tincapp.commands.Tincd
@@ -11,48 +12,75 @@ import org.pacien.tincapp.context.AppPaths
11import org.pacien.tincapp.utils.applyIgnoringException 12import org.pacien.tincapp.utils.applyIgnoringException
12import java.io.IOException 13import java.io.IOException
13 14
15
14/** 16/**
15 * @author pacien 17 * @author pacien
16 */ 18 */
17class TincVpnService : VpnService() { 19class TincVpnService : VpnService() {
18 20
19 private var netName: String? = null
20
21 override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { 21 override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
22 startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!) 22 when (intent.getSerializableExtra(INTENT_EXTRA_ACTION)) {
23 Action.START -> startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!)
24 Action.STOP -> onDestroy()
25 }
26
23 return Service.START_STICKY 27 return Service.START_STICKY
24 } 28 }
25 29
26 override fun onDestroy() = try { 30 override fun onDestroy() = try {
27 Tinc.stop(netName!!) 31 Tinc.stop(netName!!)
32 fd!!.close()
28 } catch (e: IOException) { 33 } catch (e: IOException) {
29 e.printStackTrace() 34 e.printStackTrace()
30 } finally { 35 } finally {
31 netName = null 36 netName = null
37 interfaceCfg = null
38 fd = null
39 super.onDestroy()
32 } 40 }
33 41
34 private fun startVpn(netName: String) { 42 private fun startVpn(netName: String) {
35 if (netName == this.netName) onDestroy() 43 if (netName == TincVpnService.netName) onDestroy()
36 this.netName = netName 44 TincVpnService.netName = netName
45 TincVpnService.interfaceCfg = VpnInterfaceConfiguration(AppPaths.netConfFile(netName))
37 46
38 val net = Builder().setSession(netName) 47 val net = Builder().setSession(netName).apply(TincVpnService.interfaceCfg!!)
39 net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netName)))
40 applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) 48 applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID)
41 49
42 try { 50 try {
43 Tincd.start(netName, net.establish().detachFd()) 51 fd = net.establish()
52 Tincd.start(netName, fd!!.fd)
44 } catch (e: IOException) { 53 } catch (e: IOException) {
45 e.printStackTrace() 54 e.printStackTrace()
46 } 55 }
47 } 56 }
48 57
49 companion object { 58 companion object {
59
60 private val INTENT_EXTRA_ACTION = "action"
50 private val INTENT_EXTRA_NET_NAME = "netName" 61 private val INTENT_EXTRA_NET_NAME = "netName"
51 62
63 private enum class Action { START, STOP }
64
65 private var netName: String? = null
66 private var interfaceCfg: VpnInterfaceConfiguration? = null
67 private var fd: ParcelFileDescriptor? = null
68
52 fun startVpn(netName: String) { 69 fun startVpn(netName: String) {
53 App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) 70 App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java)
71 .putExtra(INTENT_EXTRA_ACTION, Action.START)
54 .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) 72 .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName))
55 } 73 }
74
75 fun stopVpn() {
76 App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java)
77 .putExtra(INTENT_EXTRA_ACTION, Action.STOP))
78 }
79
80 fun getCurrentNetName() = netName
81 fun getCurrentInterfaceCfg() = interfaceCfg
82 fun isConnected() = netName != null
83
56 } 84 }
57 85
58} 86}