aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt
blob: 50ccb20cbc5485aa342cc09d679297b137ff6761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.pacien.tincapp.service

/**
 * @author pacien
 */

import org.apache.commons.configuration2.Configuration
import org.apache.commons.configuration2.builder.fluent.Configurations
import java.io.File

private val KEY_ADDRESSES = "Address"
private val KEY_ROUTES = "Route"
private val KEY_DNS_SERVERS = "DNSServer"
private val KEY_SEARCH_DOMAINS = "SearchDomain"
private val KEY_ALLOWED_APPLICATIONS = "AllowApplication"
private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication"
private val KEY_ALLOWED_FAMILIES = "AllowFamily"
private val KEY_ALLOW_BYPASS = "AllowBypass"
private val KEY_BLOCKING = "Blocking"
private val KEY_MTU = "MTU"

private fun Configuration.getStringList(key: String): List<String> = getList(String::class.java, key, emptyList())
private fun Configuration.getCidrList(key: String): List<CidrAddress> = getStringList(key).map { CidrAddress(it) }
private fun Configuration.getIntList(key: String): List<Int> = getList(Int::class.java, key, emptyList())

data class CidrAddress(val address: String, val prefix: Int) {
    constructor(slashSeparated: String) :
            this(slashSeparated.substringBefore(SEPARATOR), Integer.parseInt(slashSeparated.substringAfter(SEPARATOR)))

    override fun toString() = address + SEPARATOR + prefix

    companion object {
        private val SEPARATOR = "/"
    }
}

data class VpnInterfaceConfiguration(val addresses: List<CidrAddress> = emptyList(),
                                     val routes: List<CidrAddress> = emptyList(),
                                     val dnsServers: List<String> = emptyList(),
                                     val searchDomains: List<String> = emptyList(),
                                     val allowedApplications: List<String> = emptyList(),
                                     val disallowedApplications: List<String> = emptyList(),
                                     val allowedFamilies: List<Int> = emptyList(),
                                     val allowBypass: Boolean = false,
                                     val blocking: Boolean = false,
                                     val mtu: Int? = null) {

    constructor(cfg: Configuration) : this(
            cfg.getCidrList(KEY_ADDRESSES),
            cfg.getCidrList(KEY_ROUTES),
            cfg.getStringList(KEY_DNS_SERVERS),
            cfg.getStringList(KEY_SEARCH_DOMAINS),
            cfg.getStringList(KEY_ALLOWED_APPLICATIONS),
            cfg.getStringList(KEY_DISALLOWED_APPLICATIONS),
            cfg.getIntList(KEY_ALLOWED_FAMILIES),
            cfg.getBoolean(KEY_ALLOW_BYPASS, false),
            cfg.getBoolean(KEY_BLOCKING, false),
            cfg.getInteger(KEY_MTU, null))

    constructor(cfgFile: File) : this(Configurations().properties(cfgFile))

}