diff options
author | Pacien TRAN-GIRARD | 2017-07-01 15:27:20 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2017-07-01 15:27:20 +0200 |
commit | 3619cf121b5b496ede1a0588f77b44f9bdb8386b (patch) | |
tree | 6cd06b128d7b7ca53457d98db2ba9bfe98b9f886 /app/src/main/java/org/pacien | |
parent | 82ea8deab12d664dc4f941f329285ce4315057ad (diff) | |
download | tincapp-3619cf121b5b496ede1a0588f77b44f9bdb8386b.tar.gz |
Refactor VPN interface configuration loading
Diffstat (limited to 'app/src/main/java/org/pacien')
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 | |||
7 | import org.pacien.tincapp.commands.Tinc | 7 | import org.pacien.tincapp.commands.Tinc |
8 | import org.pacien.tincapp.commands.Tincd | 8 | import org.pacien.tincapp.commands.Tincd |
9 | import org.pacien.tincapp.context.AppPaths | 9 | import org.pacien.tincapp.context.AppPaths |
10 | import org.pacien.tincapp.utils.applyIgnoringException | ||
10 | import java.io.IOException | 11 | import 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 @@ | |||
1 | package org.pacien.tincapp.service | ||
2 | |||
3 | /** | ||
4 | * @author pacien | ||
5 | */ | ||
6 | |||
7 | import org.apache.commons.configuration2.Configuration | ||
8 | import org.apache.commons.configuration2.builder.fluent.Configurations | ||
9 | import java.io.File | ||
10 | |||
11 | private val KEY_ADDRESSES = "Address" | ||
12 | private val KEY_ROUTES = "Route" | ||
13 | private val KEY_DNS_SERVERS = "DNSServer" | ||
14 | private val KEY_SEARCH_DOMAINS = "SearchDomain" | ||
15 | private val KEY_ALLOWED_APPLICATIONS = "AllowApplication" | ||
16 | private val KEY_DISALLOWED_APPLICATIONS = "DisallowApplication" | ||
17 | private val KEY_ALLOWED_FAMILIES = "AllowFamily" | ||
18 | private val KEY_ALLOW_BYPASS = "AllowBypass" | ||
19 | private val KEY_BLOCKING = "Blocking" | ||
20 | private val KEY_MTU = "MTU" | ||
21 | |||
22 | private fun Configuration.getStringList(key: String): List<String> = getList(String::class.java, key, emptyList()) | ||
23 | private fun Configuration.getCidrList(key: String): List<CidrAddress> = getStringList(key).map { CidrAddress(it) } | ||
24 | private fun Configuration.getIntList(key: String): List<Int> = getList(Int::class.java, key, emptyList()) | ||
25 | |||
26 | data class CidrAddress(val address: String, val prefix: Int) { | ||
27 | constructor(slashSeparated: String) : | ||
28 | this(slashSeparated.substringBefore("/"), Integer.parseInt(slashSeparated.substringAfter("/"))) | ||
29 | } | ||
30 | |||
31 | data 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 @@ | |||
1 | package org.pacien.tincapp.service | 1 | package org.pacien.tincapp.service |
2 | 2 | ||
3 | import android.net.VpnService | ||
4 | |||
5 | /** | 3 | /** |
6 | * @author pacien | 4 | * @author pacien |
7 | */ | 5 | */ |
8 | 6 | ||
9 | data class CidrAddress(val address: String, val prefix: Int) { | 7 | import android.net.VpnService |
10 | constructor(slashSeparated: String) : | 8 | import org.pacien.tincapp.utils.applyIgnoringException |
11 | this(slashSeparated.substringBefore("/"), Integer.parseInt(slashSeparated.substringAfter("/"))) | ||
12 | } | ||
13 | |||
14 | |||
15 | fun <A, R> applyIgnoringException(f: (A) -> R, x: A, alt: R? = null) = try { | ||
16 | f(x) | ||
17 | } catch (_: Exception) { | ||
18 | alt | ||
19 | } | ||
20 | 9 | ||
21 | fun VpnService.Builder.addAddress(cidr: CidrAddress) = addAddress(cidr.address, cidr.prefix) | 10 | fun VpnService.Builder.addAddress(cidr: CidrAddress): VpnService.Builder = addAddress(cidr.address, cidr.prefix) |
22 | fun VpnService.Builder.addRoute(cidr: CidrAddress) = addRoute(cidr.address, cidr.prefix) | 11 | fun VpnService.Builder.addRoute(cidr: CidrAddress): VpnService.Builder = addRoute(cidr.address, cidr.prefix) |
23 | fun VpnService.Builder.allowBypass(allow: Boolean) = if (allow) allowBypass() else this | 12 | fun VpnService.Builder.allowBypass(allow: Boolean): VpnService.Builder = if (allow) allowBypass() else this |
24 | fun VpnService.Builder.overrideMtu(mtu: Int?) = if (mtu != null) setMtu(mtu) else this | 13 | fun VpnService.Builder.overrideMtu(mtu: Int?): VpnService.Builder = if (mtu != null) setMtu(mtu) else this |
25 | 14 | ||
26 | fun VpnService.Builder.addAddresses(cidrList: List<CidrAddress>) = | 15 | fun 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 | ||
29 | fun VpnService.Builder.addRoutes(cidrList: List<CidrAddress>) = | 18 | fun 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 | ||
32 | fun VpnService.Builder.addDnsServers(dnsList: List<String>) = | 21 | fun 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 | ||
35 | fun VpnService.Builder.addSearchDomains(domainList: List<String>) = | 24 | fun 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 | ||
38 | fun VpnService.Builder.allowFamilies(familyList: List<Int>) = | 27 | fun 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 | ||
41 | fun VpnService.Builder.addAllowedApplications(apps: List<String>) = | 30 | fun 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 | ||
44 | fun VpnService.Builder.addDisallowedApplications(apps: List<String>) = | 33 | fun 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 | |||
36 | fun 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 @@ | |||
1 | package org.pacien.tincapp.utils | ||
2 | |||
3 | /** | ||
4 | * @author pacien | ||
5 | */ | ||
6 | |||
7 | fun <A, R> applyIgnoringException(f: (A) -> R, x: A, alt: R? = null) = try { | ||
8 | f(x) | ||
9 | } catch (_: Exception) { | ||
10 | alt | ||
11 | } | ||