diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/service')
5 files changed, 146 insertions, 132 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.java b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.java deleted file mode 100644 index 1a6f58f..0000000 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.java +++ /dev/null | |||
@@ -1,51 +0,0 @@ | |||
1 | package org.pacien.tincapp.service; | ||
2 | |||
3 | import android.app.Service; | ||
4 | import android.content.Intent; | ||
5 | import android.net.VpnService; | ||
6 | |||
7 | import org.pacien.tincapp.BuildConfig; | ||
8 | import org.pacien.tincapp.commands.Tinc; | ||
9 | import org.pacien.tincapp.commands.Tincd; | ||
10 | import org.pacien.tincapp.context.AppPaths; | ||
11 | |||
12 | import java.io.IOException; | ||
13 | |||
14 | import static org.pacien.tincapp.util.Function.applyIgnoringExcept; | ||
15 | |||
16 | /** | ||
17 | * @author pacien | ||
18 | */ | ||
19 | public class TincVpnService extends VpnService { | ||
20 | |||
21 | static final public String INTENT_EXTRA_NET_NAME = "netName"; | ||
22 | |||
23 | private String netName; | ||
24 | |||
25 | @Override | ||
26 | public int onStartCommand(Intent intent, int flags, int startId) { | ||
27 | this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME); | ||
28 | |||
29 | Builder net = new Builder().setSession(this.netName); | ||
30 | VpnInterfaceConfigurator.applyConfiguration(net, AppPaths.netConfFile(this, this.netName)); | ||
31 | applyIgnoringExcept(net::addDisallowedApplication, BuildConfig.APPLICATION_ID); | ||
32 | |||
33 | try { | ||
34 | Tincd.start(this, this.netName, net.establish().detachFd()); | ||
35 | } catch (IOException e) { | ||
36 | e.printStackTrace(); | ||
37 | } | ||
38 | |||
39 | return Service.START_STICKY; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public void onDestroy() { | ||
44 | try { | ||
45 | Tinc.stop(this, this.netName); | ||
46 | } catch (IOException e) { | ||
47 | e.printStackTrace(); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt new file mode 100644 index 0000000..06213df --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt | |||
@@ -0,0 +1,49 @@ | |||
1 | package org.pacien.tincapp.service | ||
2 | |||
3 | import android.app.Service | ||
4 | import android.content.Intent | ||
5 | import android.net.VpnService | ||
6 | import org.pacien.tincapp.BuildConfig | ||
7 | import org.pacien.tincapp.commands.Tinc | ||
8 | import org.pacien.tincapp.commands.Tincd | ||
9 | import org.pacien.tincapp.context.AppPaths | ||
10 | import java.io.IOException | ||
11 | |||
12 | /** | ||
13 | * @author pacien | ||
14 | */ | ||
15 | class TincVpnService : VpnService() { | ||
16 | |||
17 | private var netName: String = "" | ||
18 | |||
19 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | ||
20 | this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME) | ||
21 | |||
22 | val net = Builder().setSession(this.netName) | ||
23 | VpnInterfaceConfigurator.applyConfiguration(net, AppPaths.netConfFile(this, this.netName)) | ||
24 | applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) | ||
25 | |||
26 | try { | ||
27 | Tincd.start(this, this.netName, net.establish().detachFd()) | ||
28 | } catch (e: IOException) { | ||
29 | e.printStackTrace() | ||
30 | } | ||
31 | |||
32 | return Service.START_STICKY | ||
33 | } | ||
34 | |||
35 | override fun onDestroy() { | ||
36 | try { | ||
37 | Tinc.stop(this, this.netName) | ||
38 | } catch (e: IOException) { | ||
39 | e.printStackTrace() | ||
40 | } | ||
41 | |||
42 | } | ||
43 | |||
44 | companion object { | ||
45 | |||
46 | val INTENT_EXTRA_NET_NAME = "netName" | ||
47 | } | ||
48 | |||
49 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java deleted file mode 100644 index 0bf6ada..0000000 --- a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java +++ /dev/null | |||
@@ -1,81 +0,0 @@ | |||
1 | package org.pacien.tincapp.service; | ||
2 | |||
3 | import android.net.VpnService; | ||
4 | |||
5 | import com.annimon.stream.Stream; | ||
6 | import com.annimon.stream.function.BiFunction; | ||
7 | import com.annimon.stream.function.Consumer; | ||
8 | |||
9 | import org.apache.commons.configuration2.Configuration; | ||
10 | import org.apache.commons.configuration2.builder.fluent.Configurations; | ||
11 | import org.apache.commons.configuration2.ex.ConfigurationException; | ||
12 | |||
13 | import java.io.File; | ||
14 | |||
15 | import static org.pacien.tincapp.util.Function.applyIgnoringExcept; | ||
16 | |||
17 | /** | ||
18 | * @author pacien | ||
19 | */ | ||
20 | final public class VpnInterfaceConfigurator { | ||
21 | |||
22 | private VpnInterfaceConfigurator() { | ||
23 | // static class | ||
24 | } | ||
25 | |||
26 | static final public String KEY_ADDRESSES = "Address"; | ||
27 | static final public String KEY_ROUTES = "Route"; | ||
28 | static final public String KEY_DNS_SERVERS = "DNSServer"; | ||
29 | static final public String KEY_SEARCH_DOMAINS = "SearchDomain"; | ||
30 | static final public String KEY_ALLOWED_APPLICATIONS = "AllowApplication"; | ||
31 | static final public String KEY_DISALLOWED_APPLICATIONS = "DisallowApplication"; | ||
32 | static final public String KEY_ALLOWED_FAMILIES = "AllowFamily"; | ||
33 | static final public String KEY_ALLOW_BYPASS = "AllowBypass"; | ||
34 | static final public String KEY_BLOCKING = "Blocking"; | ||
35 | static final public String KEY_MTU = "MTU"; | ||
36 | |||
37 | static private Stream<String> getStringStream(Configuration cfg, String key) { | ||
38 | return Stream.ofNullable(cfg.getList(String.class, key)); | ||
39 | } | ||
40 | |||
41 | static private Stream<Integer> getIntegerStream(Configuration cfg, String key) { | ||
42 | return Stream.ofNullable(cfg.getList(Integer.class, key)); | ||
43 | } | ||
44 | |||
45 | static private void doIf(Configuration cfg, String key, Consumer<Boolean> func) { | ||
46 | if (cfg.getBoolean(key, false)) func.accept(true); | ||
47 | } | ||
48 | |||
49 | static private void doWithInt(Configuration cfg, String key, Consumer<Integer> func) { | ||
50 | Stream.ofNullable(cfg.getInteger(key, null)).forEach(func); | ||
51 | } | ||
52 | |||
53 | static private <R> R applyAddressNetmask(BiFunction<String, Integer, R> func, String addrStr) { | ||
54 | String[] addrParts = addrStr.split("/", 2); | ||
55 | return func.apply(addrParts[0], Integer.parseInt(addrParts[1])); | ||
56 | } | ||
57 | |||
58 | static public VpnService.Builder applyConfiguration(VpnService.Builder net, Configuration cfg) { | ||
59 | getStringStream(cfg, KEY_ADDRESSES).forEach(str -> applyAddressNetmask(net::addAddress, str)); | ||
60 | getStringStream(cfg, KEY_ROUTES).forEach(str -> applyAddressNetmask(net::addRoute, str)); | ||
61 | getStringStream(cfg, KEY_DNS_SERVERS).forEach(net::addDnsServer); | ||
62 | getStringStream(cfg, KEY_SEARCH_DOMAINS).forEach(net::addSearchDomain); | ||
63 | getStringStream(cfg, KEY_ALLOWED_APPLICATIONS).forEach(str -> applyIgnoringExcept(net::addAllowedApplication, str)); | ||
64 | getStringStream(cfg, KEY_DISALLOWED_APPLICATIONS).forEach(str -> applyIgnoringExcept(net::addDisallowedApplication, str)); | ||
65 | getIntegerStream(cfg, KEY_ALLOWED_FAMILIES).forEach(net::allowFamily); | ||
66 | doIf(cfg, KEY_ALLOW_BYPASS, v -> net.allowBypass()); | ||
67 | doIf(cfg, KEY_BLOCKING, net::setBlocking); | ||
68 | doWithInt(cfg, KEY_MTU, net::setMtu); | ||
69 | |||
70 | return net; | ||
71 | } | ||
72 | |||
73 | static public VpnService.Builder applyConfiguration(VpnService.Builder net, File cfg) { | ||
74 | try { | ||
75 | return applyConfiguration(net, new Configurations().properties(cfg)); | ||
76 | } catch (ConfigurationException e) { | ||
77 | throw new IllegalArgumentException(e.getMessage()); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.kt b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.kt new file mode 100644 index 0000000..8104940 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.kt | |||
@@ -0,0 +1,52 @@ | |||
1 | package org.pacien.tincapp.service | ||
2 | |||
3 | import android.net.VpnService | ||
4 | import org.apache.commons.configuration2.Configuration | ||
5 | import org.apache.commons.configuration2.builder.fluent.Configurations | ||
6 | import org.apache.commons.configuration2.ex.ConfigurationException | ||
7 | import java.io.File | ||
8 | |||
9 | /** | ||
10 |