aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRoger Chuang <lchuang@mozilla.com>2020-01-16 12:00:18 +0800
committerRoger Chuang <lchuang@mozilla.com>2020-02-03 11:22:23 +0800
commit15b96aa5203364ee09125d76b9cb3695e7a8145f (patch)
tree6dfe7db4103bb7f525e391e6996cc41ffd3b1c73
parentAdd WireGuardVpnService for app to customize vpn service (diff)
downloadwireguard-android-rc/libwg_statistic.tar.xz
wireguard-android-rc/libwg_statistic.zip
Support to allow only certain apps to use VPNrc/libwg_statistic
Signed-off-by: Roger Chuang <lchuang@mozilla.com>
-rw-r--r--config/src/main/java/com/wireguard/config/Interface.java24
-rw-r--r--libwg/src/main/java/com/wireguard/android/backend/WireGuardVpnService.kt1
2 files changed, 25 insertions, 0 deletions
diff --git a/config/src/main/java/com/wireguard/config/Interface.java b/config/src/main/java/com/wireguard/config/Interface.java
index 5494442..d1fa283 100644
--- a/config/src/main/java/com/wireguard/config/Interface.java
+++ b/config/src/main/java/com/wireguard/config/Interface.java
@@ -42,6 +42,7 @@ public final class Interface {
private final Set<InetNetwork> addresses;
private final Set<InetAddress> dnsServers;
private final Set<String> excludedApplications;
+ private final Set<String> includedApplications;
private final KeyPair keyPair;
private final Optional<Integer> listenPort;
private final Optional<Integer> mtu;
@@ -51,6 +52,7 @@ public final class Interface {
addresses = Collections.unmodifiableSet(new LinkedHashSet<>(builder.addresses));
dnsServers = Collections.unmodifiableSet(new LinkedHashSet<>(builder.dnsServers));
excludedApplications = Collections.unmodifiableSet(new LinkedHashSet<>(builder.excludedApplications));
+ includedApplications = Collections.unmodifiableSet(new LinkedHashSet<>(builder.includedApplications));
keyPair = Objects.requireNonNull(builder.keyPair, "Interfaces must have a private key");
listenPort = builder.listenPort;
mtu = builder.mtu;
@@ -141,6 +143,16 @@ public final class Interface {
}
/**
+ * Returns the set of applications allowed to use the interface.
+ *
+ * @return a set of package names
+ */
+ public Set<String> getIncludedApplications() {
+ // The collection is already immutable.
+ return includedApplications;
+ }
+
+ /**
* Returns the public/private key pair used by the interface.
*
* @return a key pair
@@ -239,6 +251,8 @@ public final class Interface {
private final Set<InetAddress> dnsServers = new LinkedHashSet<>();
// Defaults to an empty set.
private final Set<String> excludedApplications = new LinkedHashSet<>();
+ // Defaults to an empty set.
+ private final Set<String> includedApplications = new LinkedHashSet<>();
// No default; must be provided before building.
@Nullable private KeyPair keyPair;
// Defaults to not present.
@@ -283,6 +297,16 @@ public final class Interface {
return this;
}
+ public Builder includeApplication(final String application) {
+ includedApplications.add(application);
+ return this;
+ }
+
+ public Builder includeApplications(final Collection<String> applications) {
+ includedApplications.addAll(applications);
+ return this;
+ }
+
public Builder parseAddresses(final CharSequence addresses) throws BadConfigException {
try {
for (final String address : Attribute.split(addresses))
diff --git a/libwg/src/main/java/com/wireguard/android/backend/WireGuardVpnService.kt b/libwg/src/main/java/com/wireguard/android/backend/WireGuardVpnService.kt
index 6c05cfb..41de016 100644
--- a/libwg/src/main/java/com/wireguard/android/backend/WireGuardVpnService.kt
+++ b/libwg/src/main/java/com/wireguard/android/backend/WireGuardVpnService.kt
@@ -113,6 +113,7 @@ abstract class WireGuardVpnService : android.net.VpnService(), ServiceProxy {
fun VpnService.Builder.applyConfig(config: Config): VpnService.Builder {
config.`interface`.apply {
excludedApplications.forEach { addDisallowedApplication(it) }
+ includedApplications.forEach { addAllowedApplication(it) }
addresses.forEach { addAddress(it.address, it.mask) }
dnsServers.forEach { addDnsServer(it.hostAddress) }
setMtu(mtu.orElse(1280))