diff options
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.kt | 56 |
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 @@ | |||
1 | package org.pacien.tincapp.service | ||
2 | |||
3 | /** | ||
4 | * @author pacien | ||
5 | */ | ||
6 | |||
7 | import org.apache.commons.configuration2.Configuration | ||
8 | import org.apache.commons.configuration2.builder.fluent.Configurations | ||
9 | import java.io.File | ||
10 | |||
11 | private val KEY_ADDRESSES = "Address" | ||
12 | private val KEY_ROUTES = "Route" | ||
13 | private val KEY_DNS_SERVERS = "DNSServer" | ||
14 | private val KEY_SEARCH_DOMAINS = "SearchDomain" | ||
15 | private val KEY_ALLOWED_APPLICATIONS = "AllowApplication" | ||
16 | private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication" | ||
17 | private val KEY_ALLOWED_FAMILIES = "AllowFamily" | ||
18 | private val KEY_ALLOW_BYPASS = "AllowBypass" | ||
19 | private val KEY_BLOCKING = "Blocking" | ||
20 | private val KEY_MTU = "MTU" | ||
21 | |||
22 | private fun Configuration.getStringList(key: String): List<String> = getList(String::class.java, key, emptyList()) | ||
23 | private fun Configuration.getCidrList(key: String): List<CidrAddress> = getStringList(key).map { CidrAddress(it) } | ||
24 | private fun Configuration.getIntList(key: String): List<Int> = getList(Int::class.java, key, emptyList()) | ||
25 | |||
26 | data class CidrAddress(val address: String, val prefix: Int) { | ||
27 | constructor(slashSeparated: String) : | ||
28 | this(slashSeparated.substringBefore("/"), Integer.parseInt(slashSeparated.substringAfter("/"))) | ||
29 | } | ||
30 | |||
31 | data 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 | } | ||