blob: 12ac17fb651a9883b387af4313562602ccc7b2f5 (
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package org.pacien.tincapp.service
import android.app.Service
import android.content.Intent
import android.net.Uri
import android.net.VpnService
import android.os.ParcelFileDescriptor
import org.pacien.tincapp.BuildConfig
import org.pacien.tincapp.commands.Tinc
import org.pacien.tincapp.commands.Tincd
import org.pacien.tincapp.context.App
import org.pacien.tincapp.context.AppPaths
import org.pacien.tincapp.data.VpnInterfaceConfiguration
import org.pacien.tincapp.extensions.Java.applyIgnoringException
import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg
import org.pacien.tincapp.intent.action.ACTION_START_SERVICE
import org.pacien.tincapp.intent.action.ACTION_STOP_SERVICE
import org.pacien.tincapp.intent.action.TINC_SCHEME
import java.io.IOException
/**
* @author pacien
*/
class TincVpnService : VpnService() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
when (intent.action) {
ACTION_START_SERVICE -> startVpn(intent.data.schemeSpecificPart)
ACTION_STOP_SERVICE -> onDestroy()
}
return Service.START_STICKY
}
override fun onDestroy() {
connected = false
try {
if (netName != null) Tinc.stop(netName!!)
fd?.close()
} catch (e: IOException) {
e.printStackTrace()
} finally {
netName = null
interfaceCfg = null
fd = null
super.onDestroy()
}
}
private fun startVpn(netName: String) {
if (isConnected()) onDestroy()
TincVpnService.netName = netName
TincVpnService.interfaceCfg = VpnInterfaceConfiguration.fromIfaceConfiguration(AppPaths.netConfFile(netName))
val net = Builder().setSession(netName).applyCfg(TincVpnService.interfaceCfg!!)
applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID)
try {
fd = net.establish()
Tincd.start(netName, fd!!.fd)
} catch (e: IOException) {
e.printStackTrace()
}
connected = true
}
companion object {
private var connected: Boolean = false
private var netName: String? = null
private var interfaceCfg: VpnInterfaceConfiguration? = null
private var fd: ParcelFileDescriptor? = null
fun startVpn(netName: String) {
App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java)
.setAction(ACTION_START_SERVICE)
.setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).build()))
}
fun stopVpn() {
App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java)
.setAction(ACTION_STOP_SERVICE))
}
fun getCurrentNetName() = netName
fun getCurrentInterfaceCfg() = interfaceCfg
fun isConnected() = connected
}
}
|