diff options
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt | 1 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt | 35 |
2 files changed, 25 insertions, 11 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt b/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt index 510ebc9..5d966c4 100644 --- a/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt +++ b/app/src/main/java/org/pacien/tincapp/data/CidrAddress.kt | |||
@@ -17,4 +17,5 @@ data class CidrAddress(val address: String, val prefix: Int) { | |||
17 | } | 17 | } |
18 | 18 | ||
19 | fun toSlashSeparated() = address + SEPARATOR + prefix | 19 | fun toSlashSeparated() = address + SEPARATOR + prefix |
20 | override fun toString(): String = "$address/$prefix" | ||
20 | } | 21 | } |
diff --git a/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt b/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt index 53a0f7a..f11115e 100644 --- a/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt +++ b/app/src/main/java/org/pacien/tincapp/extensions/VpnServiceBuilder.kt | |||
@@ -9,30 +9,43 @@ import org.pacien.tincapp.extensions.Java.applyIgnoringException | |||
9 | * @author pacien | 9 | * @author pacien |
10 | */ | 10 | */ |
11 | object VpnServiceBuilder { | 11 | object VpnServiceBuilder { |
12 | fun VpnService.Builder.addAddress(cidr: CidrAddress): VpnService.Builder = addAddress(cidr.address, cidr.prefix) | 12 | private fun <T> exceptWithCidr(cidr: CidrAddress, func: () -> T) = try { |
13 | fun VpnService.Builder.addRoute(cidr: CidrAddress): VpnService.Builder = addRoute(cidr.address, cidr.prefix) | 13 | func() |
14 | fun VpnService.Builder.allowBypass(allow: Boolean): VpnService.Builder = if (allow) allowBypass() else this | 14 | } catch (e: IllegalArgumentException) { |
15 | fun VpnService.Builder.overrideMtu(mtu: Int?): VpnService.Builder = if (mtu != null) setMtu(mtu) else this | 15 | throw IllegalArgumentException("${e.message}: $cidr") |
16 | } | ||
16 | 17 | ||
17 | fun VpnService.Builder.addAddresses(cidrList: List<CidrAddress>): VpnService.Builder = | 18 | private fun VpnService.Builder.addAddress(cidr: CidrAddress): VpnService.Builder = |
19 | exceptWithCidr(cidr, { addAddress(cidr.address, cidr.prefix) }) | ||
20 | |||
21 | private fun VpnService.Builder.addRoute(cidr: CidrAddress): VpnService.Builder = | ||
22 | exceptWithCidr(cidr, { addRoute(cidr.address, cidr.prefix) }) | ||
23 | |||
24 | private fun VpnService.Builder.allowBypass(allow: Boolean): VpnService.Builder = | ||
25 | if (allow) allowBypass() else this | ||
26 | |||
27 | private fun VpnService.Builder.overrideMtu(mtu: Int?): VpnService.Builder = | ||
28 | if (mtu != null) setMtu(mtu) else this | ||
29 | |||
30 | private fun VpnService.Builder.addAddresses(cidrList: List<CidrAddress>): VpnService.Builder = | ||
18 | cidrList.fold(this, { net, cidr -> net.addAddress(cidr) }) | 31 | cidrList.fold(this, { net, cidr -> net.addAddress(cidr) }) |
19 | 32 | ||
20 | fun VpnService.Builder.addRoutes(cidrList: List<CidrAddress>): VpnService.Builder = | 33 | private fun VpnService.Builder.addRoutes(cidrList: List<CidrAddress>): VpnService.Builder = |
21 | cidrList.fold(this, { net, cidr -> net.addRoute(cidr) }) | 34 | cidrList.fold(this, { net, cidr -> net.addRoute(cidr) }) |
22 | 35 | ||
23 | fun VpnService.Builder.addDnsServers(dnsList: List<String>): VpnService.Builder = | 36 | private fun VpnService.Builder.addDnsServers(dnsList: List<String>): VpnService.Builder = |
24 | dnsList.fold(this, { net, dns -> net.addDnsServer(dns) }) | 37 | dnsList.fold(this, { net, dns -> net.addDnsServer(dns) }) |
25 | 38 | ||
26 | fun VpnService.Builder.addSearchDomains(domainList: List<String>): VpnService.Builder = | 39 | private fun VpnService.Builder.addSearchDomains(domainList: List<String>): VpnService.Builder = |
27 | domainList.fold(this, { net, domain -> net.addSearchDomain(domain) }) | 40 | domainList.fold(this, { net, domain -> net.addSearchDomain(domain) }) |
28 | 41 | ||
29 | fun VpnService.Builder.allowFamilies(familyList: List<Int>): VpnService.Builder = | 42 | private fun VpnService.Builder.allowFamilies(familyList: List<Int>): VpnService.Builder = |
30 | familyList.fold(this, { net, family -> net.allowFamily(family) }) | 43 | familyList.fold(this, { net, family -> net.allowFamily(family) }) |
31 | 44 | ||
32 | fun VpnService.Builder.addAllowedApplications(apps: List<String>): VpnService.Builder = | 45 | private fun VpnService.Builder.addAllowedApplications(apps: List<String>): VpnService.Builder = |
33 | apps.fold(this, { net, app -> applyIgnoringException(net::addAllowedApplication, app, net)!! }) | 46 | apps.fold(this, { net, app -> applyIgnoringException(net::addAllowedApplication, app, net)!! }) |
34 | 47 | ||
35 | fun VpnService.Builder.addDisallowedApplications(apps: List<String>): VpnService.Builder = | 48 | private fun VpnService.Builder.addDisallowedApplications(apps: List<String>): VpnService.Builder = |
36 | apps.fold(this, { net, app -> applyIgnoringException(net::addDisallowedApplication, app, net)!! }) | 49 | apps.fold(this, { net, app -> applyIgnoringException(net::addDisallowedApplication, app, net)!! }) |
37 | 50 | ||
38 | fun VpnService.Builder.applyCfg(cfg: VpnInterfaceConfiguration): VpnService.Builder = this | 51 | fun VpnService.Builder.applyCfg(cfg: VpnInterfaceConfiguration): VpnService.Builder = this |