diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/service')
3 files changed, 5 insertions, 112 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 45f901b..e2eae00 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | |||
@@ -9,10 +9,11 @@ import org.pacien.tincapp.commands.Tinc | |||
9 | import org.pacien.tincapp.commands.Tincd | 9 | import org.pacien.tincapp.commands.Tincd |
10 | import org.pacien.tincapp.context.App | 10 | import org.pacien.tincapp.context.App |
11 | import org.pacien.tincapp.context.AppPaths | 11 | import org.pacien.tincapp.context.AppPaths |
12 | import org.pacien.tincapp.utils.applyIgnoringException | 12 | import org.pacien.tincapp.data.VpnInterfaceConfiguration |
13 | import org.pacien.tincapp.extensions.Java.applyIgnoringException | ||
14 | import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg | ||
13 | import java.io.IOException | 15 | import java.io.IOException |
14 | 16 | ||
15 | |||
16 | /** | 17 | /** |
17 | * @author pacien | 18 | * @author pacien |
18 | */ | 19 | */ |
@@ -46,9 +47,9 @@ class TincVpnService : VpnService() { | |||
46 | private fun startVpn(netName: String) { | 47 | private fun startVpn(netName: String) { |
47 | if (isConnected()) onDestroy() | 48 | if (isConnected()) onDestroy() |
48 | TincVpnService.netName = netName | 49 | TincVpnService.netName = netName |
49 | TincVpnService.interfaceCfg = VpnInterfaceConfiguration(AppPaths.netConfFile(netName)) | 50 | TincVpnService.interfaceCfg = VpnInterfaceConfiguration.fromIfaceConfiguration(AppPaths.netConfFile(netName)) |
50 | 51 | ||
51 | val net = Builder().setSession(netName).apply(TincVpnService.interfaceCfg!!) | 52 | val net = Builder().setSession(netName).applyCfg(TincVpnService.interfaceCfg!!) |
52 | applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) | 53 | applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) |
53 | 54 | ||
54 | try { | 55 | try { |
diff --git a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt deleted file mode 100644 index 50ccb20..0000000 --- a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfiguraton.kt +++ /dev/null | |||
@@ -1,62 +0,0 @@ | |||
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(SEPARATOR), Integer.parseInt(slashSeparated.substringAfter(SEPARATOR))) | ||
29 | |||
30 | override fun toString() = address + SEPARATOR + prefix | ||
31 | |||
32 | companion object { | ||
33 | private val SEPARATOR = "/" | ||
34 | } | ||
35 | } | ||
36 | |||
37 | data class VpnInterfaceConfiguration(val addresses: List<CidrAddress> = emptyList(), | ||
38 | val routes: List<CidrAddress> = emptyList(), | ||
39 | val dnsServers: List<String> = emptyList(), | ||
40 | val searchDomains: List<String> = emptyList(), | ||
41 | val allowedApplications: List<String> = emptyList(), | ||
42 | val disallowedApplications: List<String> = emptyList(), | ||
43 | val allowedFamilies: List<Int> = emptyList(), | ||
44 | val allowBypass: Boolean = false, | ||
45 | val blocking: Boolean = false, | ||
46 | val mtu: Int? = null) { | ||
47 | |||
48 | constructor(cfg: Configuration) : this( | ||
49 | cfg.getCidrList(KEY_ADDRESSES), | ||
50 | cfg.getCidrList(KEY_ROUTES), | ||
51 | cfg.getStringList(KEY_DNS_SERVERS), | ||
52 | cfg.getStringList(KEY_SEARCH_DOMAINS), | ||
53 | cfg.getStringList(KEY_ALLOWED_APPLICATIONS), | ||
54 | cfg.getStringList(KEY_DISALLOWED_APPLICATIONS), | ||
55 | cfg.getIntList(KEY_ALLOWED_FAMILIES), | ||
56 | cfg.getBoolean(KEY_ALLOW_BYPASS, false), | ||
57 | cfg.getBoolean(KEY_BLOCKING, false), | ||
58 | cfg.getInteger(KEY_MTU, null)) | ||
59 | |||
60 | constructor(cfgFile: File) : this(Configurations().properties(cfgFile)) | ||
61 | |||
62 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt b/app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt deleted file mode 100644 index 22edff9..0000000 --- a/app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt +++ /dev/null | |||
@@ -1,46 +0,0 @@ | |||
1 | package org.pacien.tincapp.service | ||
2 | |||
3 | /** | ||
4 | * @author pacien | ||
5 | */ | ||
6 | |||
7 | import android.net.VpnService | ||
8 | import org.pacien.tincapp.utils.applyIgnoringException | ||
9 | |||
10 | fun VpnService.Builder.addAddress(cidr: CidrAddress): VpnService.Builder = addAddress(cidr.address, cidr.prefix) | ||
11 | fun VpnService.Builder.addRoute(cidr: CidrAddress): VpnService.Builder = addRoute(cidr.address, cidr.prefix) | ||
12 | fun VpnService.Builder.allowBypass(allow: Boolean): VpnService.Builder = if (allow) allowBypass() else this | ||
13 | fun VpnService.Builder.overrideMtu(mtu: Int?): VpnService.Builder = if (mtu != null) setMtu(mtu) else this | ||
14 | |||
15 | fun VpnService.Builder.addAddresses(cidrList: List<CidrAddress>): VpnService.Builder = | ||
16 | cidrList.fold(this, { net, cidr -> net.addAddress(cidr) }) | ||
17 | |||
18 | fun VpnService.Builder.addRoutes(cidrList: List<CidrAddress>): VpnService.Builder = | ||
19 | cidrList.fold(this, { net, cidr -> net.addRoute(cidr) }) | ||
20 | |||
21 | fun VpnService.Builder.addDnsServers(dnsList: List<String>): VpnService.Builder = | ||
22 | dnsList.fold(this, { net, dns -> net.addDnsServer(dns) }) | ||
23 | |||
24 | fun VpnService.Builder.addSearchDomains(domainList: List<String>): VpnService.Builder = | ||
25 | domainList.fold(this, { net, domain -> net.addSearchDomain(domain) }) | ||
26 | |||
27 | fun VpnService.Builder.allowFamilies(familyList: List<Int>): VpnService.Builder = | ||
28 | familyList.fold(this, { net, family -> net.allowFamily(family) }) | ||
29 | |||
30 | fun VpnService.Builder.addAllowedApplications(apps: List<String>): VpnService.Builder = | ||
31 | apps.fold(this, { net, app -> applyIgnoringException(net::addAllowedApplication, app, net)!! }) | ||
32 | |||
33 | fun VpnService.Builder.addDisallowedApplications(apps: List<String>): VpnService.Builder = | ||
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) | ||