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 | 49 |
1 files changed, 49 insertions, 0 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 new file mode 100644 index 0000000..06213df --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | |||
@@ -0,0 +1,49 @@ | |||
1 | package org.pacien.tincapp.service | ||
2 | |||
3 | import android.app.Service | ||
4 | import android.content.Intent | ||
5 | import android.net.VpnService | ||
6 | import org.pacien.tincapp.BuildConfig | ||
7 | import org.pacien.tincapp.commands.Tinc | ||
8 | import org.pacien.tincapp.commands.Tincd | ||
9 | import org.pacien.tincapp.context.AppPaths | ||
10 | import java.io.IOException | ||
11 | |||
12 | /** | ||
13 | * @author pacien | ||
14 | */ | ||
15 | class TincVpnService : VpnService() { | ||
16 | |||
17 | private var netName: String = "" | ||
18 | |||
19 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | ||
20 | this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME) | ||
21 | |||
22 | val net = Builder().setSession(this.netName) | ||
23 | VpnInterfaceConfigurator.applyConfiguration(net, AppPaths.netConfFile(this, this.netName)) | ||
24 | applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) | ||
25 | |||
26 | try { | ||
27 | Tincd.start(this, this.netName, net.establish().detachFd()) | ||
28 | } catch (e: IOException) { | ||
29 | e.printStackTrace() | ||
30 | } | ||
31 | |||
32 | return Service.START_STICKY | ||
33 | } | ||
34 | |||
35 | override fun onDestroy() { | ||
36 | try { | ||
37 | Tinc.stop(this, this.netName) | ||
38 | } catch (e: IOException) { | ||
39 | e.printStackTrace() | ||
40 | } | ||
41 | |||
42 | } | ||
43 | |||
44 | companion object { | ||
45 | |||
46 | val INTENT_EXTRA_NET_NAME = "netName" | ||
47 | } | ||
48 | |||
49 | } | ||