diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java | 81 |
1 files changed, 0 insertions, 81 deletions
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 | } | ||