diff options
Diffstat (limited to 'app/src/main/kotlin/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt')
-rw-r--r-- | app/src/main/kotlin/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/app/src/main/kotlin/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt b/app/src/main/kotlin/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt new file mode 100644 index 0000000..26a194c --- /dev/null +++ b/app/src/main/kotlin/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.data | ||
20 | |||
21 | import org.apache.commons.configuration2.Configuration | ||
22 | import org.apache.commons.configuration2.FileBasedConfiguration | ||
23 | import org.apache.commons.configuration2.PropertiesConfiguration | ||
24 | import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder | ||
25 | import org.apache.commons.configuration2.builder.fluent.Configurations | ||
26 | import org.apache.commons.configuration2.builder.fluent.Parameters | ||
27 | import org.pacien.tincapp.extensions.ApacheConfiguration.getCidrList | ||
28 | import org.pacien.tincapp.extensions.ApacheConfiguration.getIntList | ||
29 | import org.pacien.tincapp.extensions.ApacheConfiguration.getStringList | ||
30 | import org.pacien.tincapp.extensions.Java.applyIgnoringException | ||
31 | import java.io.File | ||
32 | |||
33 | /** | ||
34 | * @author pacien | ||
35 | */ | ||
36 | data class VpnInterfaceConfiguration(val addresses: List<CidrAddress> = emptyList(), | ||
37 | val routes: List<CidrAddress> = emptyList(), | ||
38 | val dnsServers: List<String> = emptyList(), | ||
39 | val searchDomains: List<String> = emptyList(), | ||
40 | val allowedApplications: List<String> = emptyList(), | ||
41 | val disallowedApplications: List<String> = emptyList(), | ||
42 | val allowedFamilies: List<Int> = emptyList(), | ||
43 | val allowBypass: Boolean = false, | ||
44 | val blocking: Boolean = false, | ||
45 | val mtu: Int? = null) { | ||
46 | companion object { | ||
47 | private const val KEY_ADDRESSES = "Address" | ||
48 | private const val KEY_ROUTES = "Route" | ||
49 | private const val KEY_DNS_SERVERS = "DNSServer" | ||
50 | private const val KEY_SEARCH_DOMAINS = "SearchDomain" | ||
51 | private const val KEY_ALLOWED_APPLICATIONS = "AllowApplication" | ||
52 | private const val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication" | ||
53 | private const val KEY_ALLOWED_FAMILIES = "AllowFamily" | ||
54 | private const val KEY_ALLOW_BYPASS = "AllowBypass" | ||
55 | private const val KEY_BLOCKING = "Blocking" | ||
56 | private const val KEY_MTU = "MTU" | ||
57 | |||
58 | private const val INVITATION_KEY_ADDRESSES = "Ifconfig" | ||
59 | private const val INVITATION_KEY_ROUTES = "Route" | ||
60 | |||
61 | fun fromIfaceConfiguration(f: File) = fromIfaceConfiguration(Configurations().properties(f)) | ||
62 | private fun fromIfaceConfiguration(c: Configuration) = VpnInterfaceConfiguration( | ||
63 | c.getCidrList(KEY_ADDRESSES), | ||
64 | c.getCidrList(KEY_ROUTES), | ||
65 | c.getStringList(KEY_DNS_SERVERS), | ||
66 | c.getStringList(KEY_SEARCH_DOMAINS), | ||
67 | c.getStringList(KEY_ALLOWED_APPLICATIONS), | ||
68 | c.getStringList(KEY_DISALLOWED_APPLICATIONS), | ||
69 | c.getIntList(KEY_ALLOWED_FAMILIES), | ||
70 | c.getBoolean(KEY_ALLOW_BYPASS, false), | ||
71 | c.getBoolean(KEY_BLOCKING, false), | ||
72 | c.getInteger(KEY_MTU, null)) | ||
73 | |||
74 | fun fromInvitation(f: File) = fromInvitation(Configurations().properties(f)) | ||
75 | private fun fromInvitation(c: Configuration) = VpnInterfaceConfiguration( | ||
76 | c.getStringList(INVITATION_KEY_ADDRESSES) | ||
77 | .map { applyIgnoringException(CidrAddress.Companion::fromSlashSeparated, it) } | ||
78 | .filterNotNull(), | ||
79 | c.getStringList(INVITATION_KEY_ROUTES) | ||
80 | .map { it.substringBefore(' ') } | ||
81 | .map { CidrAddress.fromSlashSeparated(it) }) | ||
82 | } | ||
83 | |||
84 | fun write(f: File) = FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration::class.java) | ||
85 | .configure(Parameters().properties().setFile(f.apply { createNewFile() })).let { builder -> | ||
86 | builder.configuration.let { cfg -> | ||
87 | addresses.forEach { cfg.addProperty(KEY_ADDRESSES, it.toSlashSeparated()) } | ||
88 | routes.forEach { cfg.addProperty(KEY_ROUTES, it.toSlashSeparated()) } | ||
89 | } | ||
90 | builder.save() | ||
91 | } | ||
92 | } | ||