aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
blob: 366e2082ef4c24dfd322875734902a95b1eca3a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.pacien.tincapp.service

import android.app.Service
import android.content.Intent
import android.net.VpnService
import org.pacien.tincapp.BuildConfig
import org.pacien.tincapp.commands.Tinc
import org.pacien.tincapp.commands.Tincd
import org.pacien.tincapp.context.AppPaths
import org.pacien.tincapp.utils.applyIgnoringException
import java.io.IOException

/**
 * @author pacien
 */
class TincVpnService : VpnService() {

    private var netConf: AppPaths.NetConf? = null

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        netConf = intent.getSerializableExtra(INTENT_EXTRA_NET_CONF)!! as AppPaths.NetConf

        val net = Builder().setSession(netConf!!.netName)
        net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netConf!!)))
        applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID)

        try {
            Tincd.start(netConf!!, net.establish().detachFd())
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return Service.START_STICKY
    }

    override fun onDestroy() {
        try {
            Tinc.stop(netConf!!)
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }

    companion object {
        val INTENT_EXTRA_NET_CONF = "netConf"
    }

}