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 | 21 |
1 files changed, 10 insertions, 11 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 e2eae00..12ac17f 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | |||
@@ -2,6 +2,7 @@ package org.pacien.tincapp.service | |||
2 | 2 | ||
3 | import android.app.Service | 3 | import android.app.Service |
4 | import android.content.Intent | 4 | import android.content.Intent |
5 | import android.net.Uri | ||
5 | import android.net.VpnService | 6 | import android.net.VpnService |
6 | import android.os.ParcelFileDescriptor | 7 | import android.os.ParcelFileDescriptor |
7 | import org.pacien.tincapp.BuildConfig | 8 | import org.pacien.tincapp.BuildConfig |
@@ -12,6 +13,9 @@ import org.pacien.tincapp.context.AppPaths | |||
12 | import org.pacien.tincapp.data.VpnInterfaceConfiguration | 13 | import org.pacien.tincapp.data.VpnInterfaceConfiguration |
13 | import org.pacien.tincapp.extensions.Java.applyIgnoringException | 14 | import org.pacien.tincapp.extensions.Java.applyIgnoringException |
14 | import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg | 15 | import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg |
16 | import org.pacien.tincapp.intent.action.ACTION_START_SERVICE | ||
17 | import org.pacien.tincapp.intent.action.ACTION_STOP_SERVICE | ||
18 | import org.pacien.tincapp.intent.action.TINC_SCHEME | ||
15 | import java.io.IOException | 19 | import java.io.IOException |
16 | 20 | ||
17 | /** | 21 | /** |
@@ -20,9 +24,9 @@ import java.io.IOException | |||
20 | class TincVpnService : VpnService() { | 24 | class TincVpnService : VpnService() { |
21 | 25 | ||
22 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | 26 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { |
23 | when (intent.getSerializableExtra(INTENT_EXTRA_ACTION)) { | 27 | when (intent.action) { |
24 | Action.START -> startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!) | 28 | ACTION_START_SERVICE -> startVpn(intent.data.schemeSpecificPart) |
25 | Action.STOP -> onDestroy() | 29 | ACTION_STOP_SERVICE -> onDestroy() |
26 | } | 30 | } |
27 | 31 | ||
28 | return Service.START_STICKY | 32 | return Service.START_STICKY |
@@ -64,11 +68,6 @@ class TincVpnService : VpnService() { | |||
64 | 68 | ||
65 | companion object { | 69 | companion object { |
66 | 70 | ||
67 | private val INTENT_EXTRA_ACTION = "action" | ||
68 | private val INTENT_EXTRA_NET_NAME = "netName" | ||
69 | |||
70 | private enum class Action { START, STOP } | ||
71 | |||
72 | private var connected: Boolean = false | 71 | private var connected: Boolean = false |
73 | private var netName: String? = null | 72 | private var netName: String? = null |
74 | private var interfaceCfg: VpnInterfaceConfiguration? = null | 73 | private var interfaceCfg: VpnInterfaceConfiguration? = null |
@@ -76,13 +75,13 @@ class TincVpnService : VpnService() { | |||
76 | 75 | ||
77 | fun startVpn(netName: String) { | 76 | fun startVpn(netName: String) { |
78 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) | 77 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) |
79 | .putExtra(INTENT_EXTRA_ACTION, Action.START) | 78 | .setAction(ACTION_START_SERVICE) |
80 | .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) | 79 | .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).build())) |
81 | } | 80 | } |
82 | 81 | ||
83 | fun stopVpn() { | 82 | fun stopVpn() { |
84 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) | 83 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) |
85 | .putExtra(INTENT_EXTRA_ACTION, Action.STOP)) | 84 | .setAction(ACTION_STOP_SERVICE)) |
86 | } | 85 | } |
87 | 86 | ||
88 | fun getCurrentNetName() = netName | 87 | fun getCurrentNetName() = netName |