aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt56
1 files changed, 56 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt
new file mode 100644
index 0000000..520d68c
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt
@@ -0,0 +1,56 @@
1package org.pacien.tincapp.service
2
3/**
4 * @author pacien
5 */
6
7import org.apache.commons.configuration2.Configuration
8import org.apache.commons.configuration2.builder.fluent.Configurations
9import java.io.File
10
11private val KEY_ADDRESSES = "Address"
12private val KEY_ROUTES = "Route"
13private val KEY_DNS_SERVERS = "DNSServer"
14private val KEY_SEARCH_DOMAINS = "SearchDomain"
15private val KEY_ALLOWED_APPLICATIONS = "AllowApplication"
16private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication"
17private val KEY_ALLOWED_FAMILIES = "AllowFamily"
18private val KEY_ALLOW_BYPASS = "AllowBypass"
19private val KEY_BLOCKING = "Blocking"
20private val KEY_MTU = "MTU"
21
22private fun Configuration.getStringList(key: String): List<String> = getList(String::class.java, key, emptyList())
23private fun Configuration.getCidrList(key: String): List<CidrAddress> = getStringList(key).map { CidrAddress(it) }
24private fun Configuration.getIntList(key: String): List<Int> = getList(Int::class.java, key, emptyList())
25
26data class CidrAddress(val address: String, val prefix: Int) {
27 constructor(slashSeparated: String) :
28 this(slashSeparated.substringBefore("/"), Integer.parseInt(slashSeparated.substringAfter("/")))
29}
30
31data class VpnInterfaceConfiguration(val addresses: List<CidrAddress>,
32 val routes: List<CidrAddress>,
33 val dnsServers: List<String>,
34 val searchDomains: List<String>,
35 val allowedApplications: List<String>,
36 val disallowedApplications: List<String>,
37 val allowedFamilies: List<Int>,
38 val allowBypass: Boolean,
39 val blocking: Boolean,
40 val mtu: Int?) {
41
42 constructor(cfg: Configuration) : this(
43 cfg.getCidrList(KEY_ADDRESSES),
44 cfg.getCidrList(KEY_ROUTES),
45 cfg.getStringList(KEY_DNS_SERVERS),
46 cfg.getStringList(KEY_SEARCH_DOMAINS),
47 cfg.getStringList(KEY_ALLOWED_APPLICATIONS),
48 cfg.getStringList(KEY_DISALLOWED_APPLICATIONS),
49 cfg.getIntList(KEY_ALLOWED_FAMILIES),
50 cfg.getBoolean(KEY_ALLOW_BYPASS, false),
51 cfg.getBoolean(KEY_BLOCKING, false),
52 cfg.getInteger(KEY_MTU, null))
53
54 constructor(cfgFile: File) : this(Configurations().properties(cfgFile))
55
56}