aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/extensions
diff options
context:
space:
mode:
authorpacien2018-08-07 00:19:57 +0200
committerpacien2018-08-07 00:19:57 +0200
commitdbba24ef009e5fa9ad428b0ba426b41672fa6b2e (patch)
treec7ff813d7ca0821b4866beeb59f7539cbff6bdca /app/src/main/java/org/pacien/tincapp/extensions
parent123d40dbf3921c5b39938f3b18460eb4a11bf3d6 (diff)
downloadtincapp-dbba24ef009e5fa9ad428b0ba426b41672fa6b2e.tar.gz
Rename source directory
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/extensions')
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/Android.kt41
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt33
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/Java.kt38
-rw-r--r--app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt80
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
19package org.pacien.tincapp.extensions
20
21import android.widget.ArrayAdapter
22import android.widget.TextView
23import org.pacien.tincapp.R
24import org.pacien.tincapp.context.App
25
26/**
27 * @author pacien
28 */
29object 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
19package org.pacien.tincapp.extensions
20
21import org.apache.commons.configuration2.Configuration
22import org.pacien.tincapp.data.CidrAddress
23import java.io.File
24
25/**
26 * @author pacien
27 */
28object 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
19package org.pacien.tincapp.extensions
20
21import java8.util.concurrent.CompletableFuture
22
23/**
24 * @author pacien
25 */
26object 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
19package org.pacien.tincapp.extensions
20
21import android.net.VpnService
22import org.pacien.tincapp.data.CidrAddress
23import org.pacien.tincapp.data.VpnInterfaceConfiguration
24import org.pacien.tincapp.extensions.Java.applyIgnoringException
25
26/**
27 * @author pacien
28 */
29object 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)