From 9d8846e105904b31478ed19d3a34c0d62708abcf Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 7 Aug 2018 01:08:22 +0200 Subject: Revert "Rename source directory" This reverts commit dbba24e --- .../java/org/pacien/tincapp/extensions/Android.kt | 41 +++++++++++ .../tincapp/extensions/ApacheConfiguration.kt | 33 +++++++++ .../java/org/pacien/tincapp/extensions/Java.kt | 38 ++++++++++ .../pacien/tincapp/extensions/VpnServiceBuilder.kt | 80 ++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 app/src/main/java/org/pacien/tincapp/extensions/Android.kt create mode 100644 app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt create mode 100644 app/src/main/java/org/pacien/tincapp/extensions/Java.kt create mode 100644 app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt (limited to 'app/src/main/java/org/pacien/tincapp/extensions') diff --git a/app/src/main/java/org/pacien/tincapp/extensions/Android.kt b/app/src/main/java/org/pacien/tincapp/extensions/Android.kt new file mode 100644 index 0000000..e954778 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/extensions/Android.kt @@ -0,0 +1,41 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.extensions + +import android.widget.ArrayAdapter +import android.widget.TextView +import org.pacien.tincapp.R +import org.pacien.tincapp.context.App + +/** + * @author pacien + */ +object Android { + fun ArrayAdapter.setElements(elements: Collection) { + setNotifyOnChange(false) + clear() + addAll(elements) + notifyDataSetChanged() + setNotifyOnChange(true) + } + + fun TextView.setText(list: List) { + text = if (list.isNotEmpty()) list.joinToString("\n") else App.getContext().getString(R.string.value_none) + } +} diff --git a/app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt b/app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt new file mode 100644 index 0000000..f364e14 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/extensions/ApacheConfiguration.kt @@ -0,0 +1,33 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.extensions + +import org.apache.commons.configuration2.Configuration +import org.pacien.tincapp.data.CidrAddress +import java.io.File + +/** + * @author pacien + */ +object ApacheConfiguration { + fun Configuration.getStringList(key: String): List = getList(String::class.java, key, emptyList()) + fun Configuration.getCidrList(key: String): List = getStringList(key).map { CidrAddress.fromSlashSeparated(it) } + fun Configuration.getIntList(key: String): List = getList(Int::class.java, key, emptyList()) + fun Configuration.getFile(key: String): File? = getString(key)?.let { File(it) } +} diff --git a/app/src/main/java/org/pacien/tincapp/extensions/Java.kt b/app/src/main/java/org/pacien/tincapp/extensions/Java.kt new file mode 100644 index 0000000..bc2b3be --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/extensions/Java.kt @@ -0,0 +1,38 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.extensions + +import java8.util.concurrent.CompletableFuture + +/** + * @author pacien + */ +object Java { + + fun CompletableFuture.exceptionallyAccept(fn: (Throwable) -> Unit) = exceptionally { fn(it); null }!! + + fun applyIgnoringException(f: (A) -> R, x: A, alt: R? = null) = try { + f(x) + } catch (_: Exception) { + alt + } + + fun Throwable.defaultMessage() = this.message ?: "null" + +} diff --git a/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt b/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt new file mode 100644 index 0000000..c41d018 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt @@ -0,0 +1,80 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.extensions + +import android.net.VpnService +import org.pacien.tincapp.data.CidrAddress +import org.pacien.tincapp.data.VpnInterfaceConfiguration +import org.pacien.tincapp.extensions.Java.applyIgnoringException + +/** + * @author pacien + */ +object VpnServiceBuilder { + private fun exceptWithCidr(cidr: CidrAddress, func: () -> T) = try { + func() + } catch (e: IllegalArgumentException) { + throw IllegalArgumentException("${e.message}: $cidr") + } + + private fun VpnService.Builder.addAddress(cidr: CidrAddress): VpnService.Builder = + exceptWithCidr(cidr) { addAddress(cidr.address, cidr.prefix) } + + private fun VpnService.Builder.addRoute(cidr: CidrAddress): VpnService.Builder = + exceptWithCidr(cidr) { addRoute(cidr.address, cidr.prefix) } + + private fun VpnService.Builder.allowBypass(allow: Boolean): VpnService.Builder = + if (allow) allowBypass() else this + + private fun VpnService.Builder.overrideMtu(mtu: Int?): VpnService.Builder = + if (mtu != null) setMtu(mtu) else this + + private fun VpnService.Builder.addAddresses(cidrList: List): VpnService.Builder = + cidrList.fold(this) { net, cidr -> net.addAddress(cidr) } + + private fun VpnService.Builder.addRoutes(cidrList: List): VpnService.Builder = + cidrList.fold(this) { net, cidr -> net.addRoute(cidr) } + + private fun VpnService.Builder.addDnsServers(dnsList: List): VpnService.Builder = + dnsList.fold(this) { net, dns -> net.addDnsServer(dns) } + + private fun VpnService.Builder.addSearchDomains(domainList: List): VpnService.Builder = + domainList.fold(this) { net, domain -> net.addSearchDomain(domain) } + + private fun VpnService.Builder.allowFamilies(familyList: List): VpnService.Builder = + familyList.fold(this) { net, family -> net.allowFamily(family) } + + private fun VpnService.Builder.addAllowedApplications(apps: List): VpnService.Builder = + apps.fold(this) { net, app -> applyIgnoringException(net::addAllowedApplication, app, net)!! } + + private fun VpnService.Builder.addDisallowedApplications(apps: List): VpnService.Builder = + apps.fold(this) { net, app -> applyIgnoringException(net::addDisallowedApplication, app, net)!! } + + fun VpnService.Builder.applyCfg(cfg: VpnInterfaceConfiguration): VpnService.Builder = this + .addAddresses(cfg.addresses) + .addRoutes(cfg.routes) + .addDnsServers(cfg.dnsServers) + .addSearchDomains(cfg.searchDomains) + .addAllowedApplications(cfg.allowedApplications) + .addDisallowedApplications(cfg.disallowedApplications) + .allowFamilies(cfg.allowedFamilies) + .allowBypass(cfg.allowBypass) + .setBlocking(cfg.blocking) + .overrideMtu(cfg.mtu) +} -- cgit v1.2.3