diff options
author | Pacien TRAN-GIRARD | 2017-05-05 01:02:16 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2017-05-05 01:02:16 +0200 |
commit | 2495d8032eb6839a55080b79ac818383c2f75b79 (patch) | |
tree | 6dfbab541368d10ddf1903e3ad93c44ea81080be /app/src/main/java/org/pacien/tincapp/service | |
parent | 6fcd36fb73b2351a58f812532ce846414ed77117 (diff) | |
download | tincapp-2495d8032eb6839a55080b79ac818383c2f75b79.tar.gz |
Import unversioned prototypev0.1-preview
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/service')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/service/TincVpnService.java | 51 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java | 81 |
2 files changed, 132 insertions, 0 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 new file mode 100644 index 0000000..1a6f58f --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.java | |||
@@ -0,0 +1,51 @@ | |||
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/VpnInterfaceConfigurator.java b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java new file mode 100644 index 0000000..0bf6ada --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java | |||
@@ -0,0 +1,81 @@ | |||
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 | } | ||