diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/data')
3 files changed, 58 insertions, 58 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt b/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt index 273b5a2..f3a7ea8 100644 --- a/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt +++ b/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt | |||
@@ -7,18 +7,18 @@ import org.apache.commons.configuration2.ex.ConversionException | |||
7 | */ | 7 | */ |
8 | data class CidrAddress(val address: String, val prefix: Int) { | 8 | data class CidrAddress(val address: String, val prefix: Int) { |
9 | 9 | ||
10 | companion object { | 10 | companion object { |
11 | 11 | ||
12 | private val SEPARATOR = "/" | 12 | private val SEPARATOR = "/" |
13 | |||
14 | fun fromSlashSeparated(s: String) = try { | ||
15 | CidrAddress(s.substringBefore(SEPARATOR), Integer.parseInt(s.substringAfter(SEPARATOR))) | ||
16 | } catch (e: Exception) { | ||
17 | throw ConversionException(e.message, e) | ||
18 | } | ||
19 | 13 | ||
14 | fun fromSlashSeparated(s: String) = try { | ||
15 | CidrAddress(s.substringBefore(SEPARATOR), Integer.parseInt(s.substringAfter(SEPARATOR))) | ||
16 | } catch (e: Exception) { | ||
17 | throw ConversionException(e.message, e) | ||
20 | } | 18 | } |
21 | 19 | ||
22 | fun toSlashSeparated() = address + SEPARATOR + prefix | 20 | } |
21 | |||
22 | fun toSlashSeparated() = address + SEPARATOR + prefix | ||
23 | 23 | ||
24 | } | 24 | } |
diff --git a/app/src/main/java/org/pacien/tincapp/data/TincConfiguration.kt b/app/src/main/java/org/pacien/tincapp/data/TincConfiguration.kt index 66496c4..5a8a254 100644 --- a/app/src/main/java/org/pacien/tincapp/data/TincConfiguration.kt +++ b/app/src/main/java/org/pacien/tincapp/data/TincConfiguration.kt | |||
@@ -11,16 +11,16 @@ import java.io.File | |||
11 | data class TincConfiguration(val ed25519PrivateKeyFile: File? = null, | 11 | data class TincConfiguration(val ed25519PrivateKeyFile: File? = null, |
12 | val privateKeyFile: File? = null) { | 12 | val privateKeyFile: File? = null) { |
13 | 13 | ||
14 | companion object { | 14 | companion object { |
15 | 15 | ||
16 | private val KEY_ED25519_PRIVATE_KEY_FILE = "Ed25519PrivateKeyFile" | 16 | private val KEY_ED25519_PRIVATE_KEY_FILE = "Ed25519PrivateKeyFile" |
17 | private val KEY_PRIVATE_KEY_FILE = "PrivateKeyFile" | 17 | private val KEY_PRIVATE_KEY_FILE = "PrivateKeyFile" |
18 | 18 | ||
19 | fun fromTincConfiguration(f: File) = fromTincConfiguration(Configurations().properties(f)) | 19 | fun fromTincConfiguration(f: File) = fromTincConfiguration(Configurations().properties(f)) |
20 | fun fromTincConfiguration(c: Configuration) = TincConfiguration( | 20 | fun fromTincConfiguration(c: Configuration) = TincConfiguration( |
21 | c.getFile(KEY_ED25519_PRIVATE_KEY_FILE), | 21 | c.getFile(KEY_ED25519_PRIVATE_KEY_FILE), |
22 | c.getFile(KEY_PRIVATE_KEY_FILE)) | 22 | c.getFile(KEY_PRIVATE_KEY_FILE)) |
23 | 23 | ||
24 | } | 24 | } |
25 | 25 | ||
26 | } | 26 | } |
diff --git a/app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt b/app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt index 70c8b96..7b699c6 100644 --- a/app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt +++ b/app/src/main/java/org/pacien/tincapp/data/VpnInterfaceConfiguration.kt | |||
@@ -26,53 +26,53 @@ data class VpnInterfaceConfiguration(val addresses: List<CidrAddress> = emptyLis | |||
26 | val blocking: Boolean = false, | 26 | val blocking: Boolean = false, |
27 | val mtu: Int? = null) { | 27 | val mtu: Int? = null) { |
28 | 28 | ||
29 | companion object { | 29 | companion object { |
30 | 30 | ||
31 | private val KEY_ADDRESSES = "Address" | 31 | private val KEY_ADDRESSES = "Address" |
32 | private val KEY_ROUTES = "Route" | 32 | private val KEY_ROUTES = "Route" |
33 | private val KEY_DNS_SERVERS = "DNSServer" | 33 | private val KEY_DNS_SERVERS = "DNSServer" |
34 | private val KEY_SEARCH_DOMAINS = "SearchDomain" | 34 | private val KEY_SEARCH_DOMAINS = "SearchDomain" |
35 | private val KEY_ALLOWED_APPLICATIONS = "AllowApplication" | 35 | private val KEY_ALLOWED_APPLICATIONS = "AllowApplication" |
36 | private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication" | 36 | private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication" |
37 | private val KEY_ALLOWED_FAMILIES = "AllowFamily" | 37 | private val KEY_ALLOWED_FAMILIES = "AllowFamily" |
38 | private val KEY_ALLOW_BYPASS = "AllowBypass" | 38 | private val KEY_ALLOW_BYPASS = "AllowBypass" |
39 | private val KEY_BLOCKING = "Blocking" | 39 | private val KEY_BLOCKING = "Blocking" |
40 | private val KEY_MTU = "MTU" | 40 | private val KEY_MTU = "MTU" |
41 | 41 | ||
42 | private val INVITATION_KEY_ADDRESSES = "Ifconfig" | 42 | private val INVITATION_KEY_ADDRESSES = "Ifconfig" |
43 | private val INVITATION_KEY_ROUTES = "Route" | 43 | private val INVITATION_KEY_ROUTES = "Route" |
44 | 44 | ||
45 | fun fromIfaceConfiguration(f: File) = fromIfaceConfiguration(Configurations().properties(f)) | 45 | fun fromIfaceConfiguration(f: File) = fromIfaceConfiguration(Configurations().properties(f)) |
46 | fun fromIfaceConfiguration(c: Configuration) = VpnInterfaceConfiguration( | 46 | fun fromIfaceConfiguration(c: Configuration) = VpnInterfaceConfiguration( |
47 | c.getCidrList(KEY_ADDRESSES), | 47 | c.getCidrList(KEY_ADDRESSES), |
48 | c.getCidrList(KEY_ROUTES), | 48 | c.getCidrList(KEY_ROUTES), |
49 | c.getStringList(KEY_DNS_SERVERS), | 49 | c.getStringList(KEY_DNS_SERVERS), |
50 | c.getStringList(KEY_SEARCH_DOMAINS), | 50 | c.getStringList(KEY_SEARCH_DOMAINS), |
51 | c.getStringList(KEY_ALLOWED_APPLICATIONS), | 51 | c.getStringList(KEY_ALLOWED_APPLICATIONS), |
52 | c.getStringList(KEY_DISALLOWED_APPLICATIONS), | 52 | c.getStringList(KEY_DISALLOWED_APPLICATIONS), |
53 | c.getIntList(KEY_ALLOWED_FAMILIES), | 53 | c.getIntList(KEY_ALLOWED_FAMILIES), |
54 | c.getBoolean(KEY_ALLOW_BYPASS, false), | 54 | c.getBoolean(KEY_ALLOW_BYPASS, false), |
55 | c.getBoolean(KEY_BLOCKING, false), | 55 | c.getBoolean(KEY_BLOCKING, false), |
56 | c.getInteger(KEY_MTU, null)) | 56 | c.getInteger(KEY_MTU, null)) |
57 | 57 | ||
58 | fun fromInvitation(f: File) = fromInvitation(Configurations().properties(f)) | 58 | fun fromInvitation(f: File) = fromInvitation(Configurations().properties(f)) |
59 | fun fromInvitation(c: Configuration) = VpnInterfaceConfiguration( | 59 | fun fromInvitation(c: Configuration) = VpnInterfaceConfiguration( |
60 | c.getStringList(INVITATION_KEY_ADDRESSES) | 60 | c.getStringList(INVITATION_KEY_ADDRESSES) |
61 | .map { applyIgnoringException(CidrAddress.Companion::fromSlashSeparated, it) } | 61 | .map { applyIgnoringException(CidrAddress.Companion::fromSlashSeparated, it) } |
62 | .filterNotNull(), | 62 | .filterNotNull(), |
63 | c.getStringList(INVITATION_KEY_ROUTES) | 63 | c.getStringList(INVITATION_KEY_ROUTES) |
64 | .map { it.substringBefore(' ') } | 64 | .map { it.substringBefore(' ') } |
65 | .map { CidrAddress.fromSlashSeparated(it) }) | 65 | .map { CidrAddress.fromSlashSeparated(it) }) |
66 | 66 | ||
67 | } | 67 | } |
68 | 68 | ||
69 | fun write(f: File) = FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration::class.java) | 69 | fun write(f: File) = FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration::class.java) |
70 | .configure(Parameters().properties().setFile(f.apply { createNewFile() })).let { builder -> | 70 | .configure(Parameters().properties().setFile(f.apply { createNewFile() })).let { builder -> |
71 | builder.configuration.let { cfg -> | 71 | builder.configuration.let { cfg -> |
72 | addresses.forEach { cfg.addProperty(KEY_ADDRESSES, it.toSlashSeparated()) } | 72 | addresses.forEach { cfg.addProperty(KEY_ADDRESSES, it.toSlashSeparated()) } |
73 | routes.forEach { cfg.addProperty(KEY_ROUTES, it.toSlashSeparated()) } | 73 | routes.forEach { cfg.addProperty(KEY_ROUTES, it.toSlashSeparated()) } |
74 | } | ||
75 | builder.save() | ||
76 | } | 74 | } |
75 | builder.save() | ||
76 | } | ||
77 | 77 | ||
78 | } | 78 | } |