diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/extensions')
4 files changed, 0 insertions, 192 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/extensions/Android.kt b/app/src/main/java/org/pacien/tincapp/extensions/Android.kt deleted file mode 100644 index e954778..0000000 --- a/app/src/main/java/org/pacien/tincapp/extensions/Android.kt +++ /dev/null | |||
@@ -1,41 +0,0 @@ | |||
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.extensions | ||
20 | |||
21 | import android.widget.ArrayAdapter | ||
22 | import android.widget.TextView | ||
23 | import org.pacien.tincapp.R | ||
24 | import org.pacien.tincapp.context.App | ||
25 | |||
26 | /** | ||
27 | * @author pacien | ||
28 | */ | ||
29 | object Android { | ||
30 | fun <T> ArrayAdapter<T>.setElements(elements: Collection<T>) { | ||
31 | setNotifyOnChange(false) | ||
32 | clear() | ||
33 | addAll(elements) | ||
34 | notifyDataSetChanged() | ||
35 | setNotifyOnChange(true) | ||
36 | } | ||
37 | |||
38 | fun TextView.setText(list: List<String>) { | ||
39 | text = if (list.isNotEmpty()) list.joinToString("\n") else App.getContext().getString(R.string.value_none) | ||
40 | } | ||
41 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt b/app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt deleted file mode 100644 index f364e14..0000000 --- a/app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt +++ /dev/null | |||
@@ -1,33 +0,0 @@ | |||
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.extensions | ||
20 | |||
21 | import org.apache.commons.configuration2.Configuration | ||
22 | import org.pacien.tincapp.data.CidrAddress | ||
23 | import java.io.File | ||
24 | |||
25 | /** | ||
26 | * @author pacien | ||
27 | */ | ||
28 | object ApacheConfiguration { | ||
29 | fun Configuration.getStringList(key: String): List<String> = getList(String::class.java, key, emptyList()) | ||
30 | fun Configuration.getCidrList(key: String): List<CidrAddress> = getStringList(key).map { CidrAddress.fromSlashSeparated(it) } | ||
31 | fun Configuration.getIntList(key: String): List<Int> = getList(Int::class.java, key, emptyList()) | ||
32 | fun Configuration.getFile(key: String): File? = getString(key)?.let { File(it) } | ||
33 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/extensions/Java.kt b/app/src/main/java/org/pacien/tincapp/extensions/Java.kt deleted file mode 100644 index bc2b3be..0000000 --- a/app/src/main/java/org/pacien/tincapp/extensions/Java.kt +++ /dev/null | |||
@@ -1,38 +0,0 @@ | |||
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.extensions | ||
20 | |||
21 | import java8.util.concurrent.CompletableFuture | ||
22 | |||
23 | /** | ||
24 | * @author pacien | ||
25 | */ | ||
26 | object Java { | ||
27 | |||
28 | fun <T> CompletableFuture<T>.exceptionallyAccept(fn: (Throwable) -> Unit) = exceptionally { fn(it); null }!! | ||
29 | |||
30 | fun <A, R> applyIgnoringException(f: (A) -> R, x: A, alt: R? = null) = try { | ||
31 | f(x) | ||
32 | } catch (_: Exception) { | ||
33 | alt | ||
34 | } | ||
35 | |||
36 | fun Throwable.defaultMessage() = this.message ?: "null" | ||
37 | |||
38 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt b/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt deleted file mode 100644 index c41d018..0000000 --- a/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt +++ /dev/null | |||
@@ -1,80 +0,0 @@ | |||
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.extensions | ||
20 | |||
21 | import android.net.VpnService | ||
22 | import org.pacien.tincapp.data.CidrAddress | ||
23 | import org.pacien.tincapp.data.VpnInterfaceConfiguration | ||
24 | import org.pacien.tincapp.extensions.Java.applyIgnoringException | ||
25 | |||
26 | /** | ||
27 | * @author pacien | ||
28 | */ | ||
29 | object VpnServiceBuilder { | ||
30 | private fun <T> exceptWithCidr(cidr: CidrAddress, func: () -> T) = try { | ||
31 | func() | ||
32 | } catch (e: IllegalArgumentException) { | ||
33 | throw IllegalArgumentException("${e.message}: $cidr") | ||
34 | } | ||
35 | |||
36 | private fun VpnService.Builder.addAddress(cidr: CidrAddress): VpnService.Builder = | ||
37 | exceptWithCidr(cidr) { addAddress(cidr.address, cidr.prefix) } | ||
38 | |||
39 | private fun VpnService.Builder.addRoute(cidr: CidrAddress): VpnService.Builder = | ||
40 | exceptWithCidr(cidr) { addRoute(cidr.address, cidr.prefix) } | ||
41 | |||
42 | private fun VpnService.Builder.allowBypass(allow: Boolean): VpnService.Builder = | ||
43 | if (allow) allowBypass() else this | ||
44 | |||
45 | private fun VpnService.Builder.overrideMtu(mtu: Int?): VpnService.Builder = | ||
46 | if (mtu != null) setMtu(mtu) else this | ||
47 | |||
48 | private fun VpnService.Builder.addAddresses(cidrList: List<CidrAddress>): VpnService.Builder = | ||
49 | cidrList.fold(this) { net, cidr -> net.addAddress(cidr) } | ||
50 | |||
51 | private fun VpnService.Builder.addRoutes(cidrList: List<CidrAddress>): VpnService.Builder = | ||
52 | cidrList.fold(this) { net, cidr -> net.addRoute(cidr) } | ||
53 | |||
54 | private fun VpnService.Builder.addDnsServers(dnsList: List<String>): VpnService.Builder = | ||
55 | dnsList.fold(this) { net, dns -> net.addDnsServer(dns) } | ||
56 | |||
57 | private fun VpnService.Builder.addSearchDomains(domainList: List<String>): VpnService.Builder = | ||
58 | domainList.fold(this) { net, domain -> net.addSearchDomain(domain) } | ||
59 | |||
60 | private fun VpnService.Builder.allowFamilies(familyList: List<Int>): VpnService.Builder = | ||
61 | familyList.fold(this) { net, family -> net.allowFamily(family) } | ||
62 | |||
63 | private fun VpnService.Builder.addAllowedApplications(apps: List<String>): VpnService.Builder = | ||
64 | apps.fold(this) { net, app -> applyIgnoringException(net::addAllowedApplication, app, net)!! } | ||
65 | |||
66 | private fun VpnService.Builder.addDisallowedApplications(apps: List<String>): VpnService.Builder = | ||
67 | apps.fold(this) { net, app -> applyIgnoringException(net::addDisallowedApplication, app, net)!! } | ||
68 | |||
69 | fun VpnService.Builder.applyCfg(cfg: VpnInterfaceConfiguration): VpnService.Builder = this | ||
70 | .addAddresses(cfg.addresses) | ||
71 | .addRoutes(cfg.routes) | ||
72 | .addDnsServers(cfg.dnsServers) | ||
73 | .addSearchDomains(cfg.searchDomains) | ||
74 | .addAllowedApplications(cfg.allowedApplications) | ||
75 | .addDisallowedApplications(cfg.disallowedApplications) | ||
76 | .allowFamilies(cfg.allowedFamilies) | ||
77 | .allowBypass(cfg.allowBypass) | ||
78 | .setBlocking(cfg.blocking) | ||
79 | .overrideMtu(cfg.mtu) | ||
80 | } | ||