diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt b/app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt new file mode 100644 index 0000000..70c8b96 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt | |||
@@ -0,0 +1,78 @@ | |||
1 | package org.pacien.tincapp.data | ||
2 | |||
3 | import org.apache.commons.configuration2.Configuration | ||
4 | import org.apache.commons.configuration2.FileBasedConfiguration | ||
5 | import org.apache.commons.configuration2.PropertiesConfiguration | ||
6 | import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder | ||
7 | import org.apache.commons.configuration2.builder.fluent.Configurations | ||
8 | import org.apache.commons.configuration2.builder.fluent.Parameters | ||
9 | import org.pacien.tincapp.extensions.ApacheConfiguration.getCidrList | ||
10 | import org.pacien.tincapp.extensions.ApacheConfiguration.getIntList | ||
11 | import org.pacien.tincapp.extensions.ApacheConfiguration.getStringList | ||
12 | import org.pacien.tincapp.extensions.Java.applyIgnoringException | ||
13 | import java.io.File | ||
14 | |||
15 | /** | ||
16 | * @author pacien | ||
17 | */ | ||
18 | data class VpnInterfaceConfiguration(val addresses: List<CidrAddress> = emptyList(), | ||
19 | val routes: List<CidrAddress> = emptyList(), | ||
20 | val dnsServers: List<String> = emptyList(), | ||
21 | val searchDomains: List<String> = emptyList(), | ||
22 | val allowedApplications: List<String> = emptyList(), | ||
23 | val disallowedApplications: List<String> = emptyList(), | ||
24 | val allowedFamilies: List<Int> = emptyList(), | ||
25 | val allowBypass: Boolean = false, | ||
26 | val blocking: Boolean = false, | ||
27 | val mtu: Int? = null) { | ||
28 | |||
29 | companion object { | ||
30 | |||
31 | private val KEY_ADDRESSES = "Address" | ||
32 | private val KEY_ROUTES = "Route" | ||
33 | private val KEY_DNS_SERVERS = "DNSServer" | ||
34 | private val KEY_SEARCH_DOMAINS = "SearchDomain" | ||
35 | private val KEY_ALLOWED_APPLICATIONS = "AllowApplication" | ||
36 | private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication" | ||
37 | private val KEY_ALLOWED_FAMILIES = "AllowFamily" | ||
38 | private val KEY_ALLOW_BYPASS = "AllowBypass" | ||
39 | private val KEY_BLOCKING = "Blocking" | ||
40 | private val KEY_MTU = "MTU" | ||
41 | |||
42 | private val INVITATION_KEY_ADDRESSES = "Ifconfig" | ||
43 | private val INVITATION_KEY_ROUTES = "Route" | ||
44 | |||
45 | fun fromIfaceConfiguration(f: File) = fromIfaceConfiguration(Configurations().properties(f)) | ||
46 | fun fromIfaceConfiguration(c: Configuration) = VpnInterfaceConfiguration( | ||
47 | c.getCidrList(KEY_ADDRESSES), | ||
48 | c.getCidrList(KEY_ROUTES), | ||
49 | c.getStringList(KEY_DNS_SERVERS), | ||
50 | c.getStringList(KEY_SEARCH_DOMAINS), | ||
51 | c.getStringList(KEY_ALLOWED_APPLICATIONS), | ||
52 | c.getStringList(KEY_DISALLOWED_APPLICATIONS), | ||
53 | c.getIntList(KEY_ALLOWED_FAMILIES), | ||
54 | c.getBoolean(KEY_ALLOW_BYPASS, false), | ||
55 | c.getBoolean(KEY_BLOCKING, false), | ||
56 | c.getInteger(KEY_MTU, null)) | ||
57 | |||
58 | fun fromInvitation(f: File) = fromInvitation(Configurations().properties(f)) | ||
59 | fun fromInvitation(c: Configuration) = VpnInterfaceConfiguration( | ||
60 | c.getStringList(INVITATION_KEY_ADDRESSES) | ||
61 | .map { applyIgnoringException(CidrAddress.Companion::fromSlashSeparated, it) } | ||
62 | .filterNotNull(), | ||
63 | c.getStringList(INVITATION_KEY_ROUTES) | ||
64 | .map { it.substringBefore(' ') } | ||
65 | .map { CidrAddress.fromSlashSeparated(it) }) | ||
66 | |||
67 | } | ||
68 | |||
69 | fun write(f: File) = FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration::class.java) | ||
70 | .configure(Parameters().properties().setFile(f.apply { createNewFile() })).let { builder -> | ||
71 | builder.configuration.let { cfg -> | ||
72 | addresses.forEach { cfg.addProperty(KEY_ADDRESSES, it.toSlashSeparated()) } | ||
73 | routes.forEach { cfg.addProperty(KEY_ROUTES, it.toSlashSeparated()) } | ||
74 | } | ||
75 | builder.save() | ||
76 | } | ||
77 | |||
78 | } | ||