aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2017-07-01 15:27:20 +0200
committerPacien TRAN-GIRARD2017-07-01 15:27:20 +0200
commit3619cf121b5b496ede1a0588f77b44f9bdb8386b (patch)
tree6cd06b128d7b7ca53457d98db2ba9bfe98b9f886 /app/src/main
parent82ea8deab12d664dc4f941f329285ce4315057ad (diff)
downloadtincapp-3619cf121b5b496ede1a0588f77b44f9bdb8386b.tar.gz
Refactor VPN interface configuration loading
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt4
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt56
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt49
-rw-r--r--app/src/main/java/org/pacien/tincapp/utils/Functions.kt11
4 files changed, 94 insertions, 26 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
index 06213df..c5e1c51 100644
--- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
+++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
@@ -7,6 +7,7 @@ import org.pacien.tincapp.BuildConfig
7import org.pacien.tincapp.commands.Tinc 7import org.pacien.tincapp.commands.Tinc
8import org.pacien.tincapp.commands.Tincd 8import org.pacien.tincapp.commands.Tincd
9import org.pacien.tincapp.context.AppPaths 9import org.pacien.tincapp.context.AppPaths
10import org.pacien.tincapp.utils.applyIgnoringException
10import java.io.IOException 11import java.io.IOException
11 12
12/** 13/**
@@ -20,7 +21,7 @@ class TincVpnService : VpnService() {
20 this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME) 21 this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME)
21 22
22 val net = Builder().setSession(this.netName) 23 val net = Builder().setSession(this.netName)
23 VpnInterfaceConfigurator.applyConfiguration(net, AppPaths.netConfFile(this, this.netName)) 24 net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(this, this.netName)))
24 applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) 25 applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID)
25 26
26 try { 27 try {
@@ -42,7 +43,6 @@ class TincVpnService : VpnService() {
42 } 43 }
43 44
44 companion object { 45 companion object {
45
46 val INTENT_EXTRA_NET_NAME = "netName" 46 val INTENT_EXTRA_NET_NAME = "netName"
47 } 47 }
48 48
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 @@
1package org.pacien.tincapp.service
2
3/**
4 * @author pacien
5 */
6
7import org.apache.commons.configuration2.Configuration
8import org.apache.commons.configuration2.builder.fluent.Configurations
9import java.io.File
10
11private val KEY_ADDRESSES = "Address"
12private val KEY_ROUTES = "Route"
13private val KEY_DNS_SERVERS = "DNSServer"
14private val KEY_SEARCH_DOMAINS = "SearchDomain"
15private val KEY_ALLOWED_APPLICATIONS = "AllowApplication"
16private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication"
17private val KEY_ALLOWED_FAMILIES = "AllowFamily"
18private val KEY_ALLOW_BYPASS = "AllowBypass"
19private val KEY_BLOCKING = "Blocking"
20private val KEY_MTU = "MTU"
21
22private fun Configuration.getStringList(key: String): List<String> = getList(String::class.java, key, emptyList())
23private fun Configuration.getCidrList(key: String): List<CidrAddress> = getStringList(key).map { CidrAddress(it) }
24private fun Configuration.getIntList(key: String): List<Int> = getList(Int::class.java, key, emptyList())
25
26data class CidrAddress(val address: String, val prefix: Int) {
27 constructor(slashSeparated: String) :
28 this(slashSeparated.substringBefore("/"), Integer.parseInt(slashSeparated.substringAfter("/")))
29}
30
31data 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}
diff --git a/app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt b/app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt
index d94d64d..22edff9 100644
--- a/app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt
+++ b/app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt
@@ -1,45 +1,46 @@
1package org.pacien.tincapp.service 1package org.pacien.tincapp.service
2 2
3import android.net.VpnService
4
5/** 3/**
6 * @author pacien 4 * @author pacien
7 */ 5 */
8 6
9data class CidrAddress(val address: String, val prefix: Int) { 7import android.net.VpnService
10 constructor(slashSeparated: String) : 8import org.pacien.tincapp.utils.applyIgnoringException
11 this(slashSeparated.substringBefore("/"), Integer.parseInt(slashSeparated.substringAfter("/")))
12}
13
14
15fun <A, R> applyIgnoringException(f: (A) -> R, x: A, alt: R? = null) = try {
16 f(x)
17} catch (_: Exception) {
18 alt
19}
20 9
21fun VpnService.Builder.addAddress(cidr: CidrAddress) = addAddress(cidr.address, cidr.prefix) 10fun VpnService.Builder.addAddress(cidr: CidrAddress): VpnService.Builder = addAddress(cidr.address, cidr.prefix)
22fun VpnService.Builder.addRoute(cidr: CidrAddress) = addRoute(cidr.address, cidr.prefix) 11fun VpnService.Builder.addRoute(cidr: CidrAddress): VpnService.Builder = addRoute(cidr.address, cidr.prefix)
23fun VpnService.Builder.allowBypass(allow: Boolean) = if (allow) allowBypass() else this 12fun VpnService.Builder.allowBypass(allow: Boolean): VpnService.Builder = if (allow) allowBypass() else this
24fun VpnService.Builder.overrideMtu(mtu: Int?) = if (mtu != null) setMtu(mtu) else this 13fun VpnService.Builder.overrideMtu(mtu: Int?): VpnService.Builder = if (mtu != null) setMtu(mtu) else this
25 14
26fun VpnService.Builder.addAddresses(cidrList: List<CidrAddress>) = 15fun VpnService.Builder.addAddresses(cidrList: List<CidrAddress>): VpnService.Builder =
27 cidrList.fold(this, { net, cidr -> net.addAddress(cidr) }) 16 cidrList.fold(this, { net, cidr -> net.addAddress(cidr) })
28 17
29fun VpnService.Builder.addRoutes(cidrList: List<CidrAddress>) = 18fun VpnService.Builder.addRoutes(cidrList: List<CidrAddress>): VpnService.Builder =
30 cidrList.fold(this, { net, cidr -> net.addRoute(cidr) }) 19 cidrList.fold(this, { net, cidr -> net.addRoute(cidr) })
31 20
32fun VpnService.Builder.addDnsServers(dnsList: List<String>) = 21fun VpnService.Builder.addDnsServers(dnsList: List<String>): VpnService.Builder =
33 dnsList.fold(this, { net, dns -> net.addDnsServer(dns) }) 22 dnsList.fold(this, { net, dns -> net.addDnsServer(dns) })
34 23
35fun VpnService.Builder.addSearchDomains(domainList: List<String>) = 24fun VpnService.Builder.addSearchDomains(domainList: List<String>): VpnService.Builder =
36 domainList.fold(this, { net, domain -> net.addSearchDomain(domain) }) 25 domainList.fold(this, { net, domain -> net.addSearchDomain(domain) })
37 26
38fun VpnService.Builder.allowFamilies(familyList: List<Int>) = 27fun VpnService.Builder.allowFamilies(familyList: List<Int>): VpnService.Builder =
39 familyList.fold(this, { net, family -> net.allowFamily(family) }) 28 familyList.fold(this, { net, family -> net.allowFamily(family) })
40 29
41fun VpnService.Builder.addAllowedApplications(apps: List<String>) = 30fun VpnService.Builder.addAllowedApplications(apps: List<String>): VpnService.Builder =
42 apps.fold(this, { net, app -> applyIgnoringException(net::addAllowedApplication, app, net)!! }) 31 apps.fold(this, { net, app -> applyIgnoringException(net::addAllowedApplication, app, net)!! })
43 32
44fun VpnService.Builder.addDisallowedApplications(apps: List<String>) = 33fun VpnService.Builder.addDisallowedApplications(apps: List<String>): VpnService.Builder =
45 apps.fold(this, { net, app -> applyIgnoringException(net::addDisallowedApplication, app, net)!! }) 34 apps.fold(this, { net, app -> applyIgnoringException(net::addDisallowedApplication, app, net)!! })
35
36fun VpnService.Builder.apply(cfg: VpnInterfaceConfiguration): VpnService.Builder = this
37 .addAddresses(cfg.addresses)
38 .addRoutes(cfg.routes)
39 .addDnsServers(cfg.dnsServers)
40 .addSearchDomains(cfg.searchDomains)
41 .addAllowedApplications(cfg.allowedApplications)
42 .addDisallowedApplications(cfg.disallowedApplications)
43 .allowFamilies(cfg.allowedFamilies)
44 .allowBypass(cfg.allowBypass)
45 .setBlocking(cfg.blocking)
46 .overrideMtu(cfg.mtu)
diff --git a/app/src/main/java/org/pacien/tincapp/utils/Functions.kt b/app/src/main/java/org/pacien/tincapp/utils/Functions.kt
new file mode 100644
index 0000000..6ed77ce
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/utils/Functions.kt
@@ -0,0 +1,11 @@
1package org.pacien.tincapp.utils
2
3/**
4 * @author pacien
5 */
6
7fun <A, R> applyIgnoringException(f: (A) -> R, x: A, alt: R? = null) = try {
8 f(x)
9} catch (_: Exception) {
10 alt
11}