diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-09-25 22:22:09 -0600 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-09-25 22:22:09 -0600 |
commit | 3935a369b866c67705f3e27944be56b94ea2b245 (patch) | |
tree | 8357994922ec99c4a6ad4a4683b8ce716bfdd203 /tunnel/src/main/java/com/wireguard/config/Interface.java | |
parent | 32fc760053606e80b66513fb06cf47f82ab1c301 (diff) |
ui,tunnel: support DNS search domains
wg-quick has supported this for a while, but not the config layer, and
not the Go backend, so wire this all up.
Requested-by: Alexis Geoffrey <alexis.geoffrey97@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tunnel/src/main/java/com/wireguard/config/Interface.java')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/config/Interface.java | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/tunnel/src/main/java/com/wireguard/config/Interface.java b/tunnel/src/main/java/com/wireguard/config/Interface.java index 01bb3699..1ece3b10 100644 --- a/tunnel/src/main/java/com/wireguard/config/Interface.java +++ b/tunnel/src/main/java/com/wireguard/config/Interface.java @@ -40,6 +40,7 @@ public final class Interface { private final Set<InetNetwork> addresses; private final Set<InetAddress> dnsServers; + private final Set<String> dnsSearchDomains; private final Set<String> excludedApplications; private final Set<String> includedApplications; private final KeyPair keyPair; @@ -50,6 +51,7 @@ public final class Interface { // Defensively copy to ensure immutability even if the Builder is reused. addresses = Collections.unmodifiableSet(new LinkedHashSet<>(builder.addresses)); dnsServers = Collections.unmodifiableSet(new LinkedHashSet<>(builder.dnsServers)); + dnsSearchDomains = Collections.unmodifiableSet(new LinkedHashSet<>(builder.dnsSearchDomains)); 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"); @@ -108,6 +110,7 @@ public final class Interface { final Interface other = (Interface) obj; return addresses.equals(other.addresses) && dnsServers.equals(other.dnsServers) + && dnsSearchDomains.equals(other.dnsSearchDomains) && excludedApplications.equals(other.excludedApplications) && includedApplications.equals(other.includedApplications) && keyPair.equals(other.keyPair) @@ -136,6 +139,16 @@ public final class Interface { } /** + * Returns the set of DNS search domains associated with the interface. + * + * @return a set of strings + */ + public Set<String> getDnsSearchDomains() { + // The collection is already immutable. + return dnsSearchDomains; + } + + /** * Returns the set of applications excluded from using the interface. * * @return a set of package names @@ -222,6 +235,7 @@ public final class Interface { sb.append("Address = ").append(Attribute.join(addresses)).append('\n'); if (!dnsServers.isEmpty()) { final List<String> dnsServerStrings = dnsServers.stream().map(InetAddress::getHostAddress).collect(Collectors.toList()); + dnsServerStrings.addAll(dnsSearchDomains); sb.append("DNS = ").append(Attribute.join(dnsServerStrings)).append('\n'); } if (!excludedApplications.isEmpty()) @@ -254,6 +268,8 @@ public final class Interface { // Defaults to an empty set. private final Set<InetAddress> dnsServers = new LinkedHashSet<>(); // Defaults to an empty set. + private final Set<String> dnsSearchDomains = 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<>(); @@ -284,6 +300,16 @@ public final class Interface { return this; } + public Builder addDnsSearchDomain(final String dnsSearchDomain) { + dnsSearchDomains.add(dnsSearchDomain); + return this; + } + + public Builder addDnsSearchDomains(final Collection<String> dnsSearchDomains) { + this.dnsSearchDomains.addAll(dnsSearchDomains); + return this; + } + public Interface build() throws BadConfigException { if (keyPair == null) throw new BadConfigException(Section.INTERFACE, Location.PRIVATE_KEY, @@ -326,8 +352,15 @@ public final class Interface { public Builder parseDnsServers(final CharSequence dnsServers) throws BadConfigException { try { - for (final String dnsServer : Attribute.split(dnsServers)) - addDnsServer(InetAddresses.parse(dnsServer)); + for (final String dnsServer : Attribute.split(dnsServers)) { + try { + addDnsServer(InetAddresses.parse(dnsServer)); + } catch (final ParseException e) { + if (e.getParsingClass() != InetAddress.class || !InetAddresses.isHostname(dnsServer)) + throw e; + addDnsSearchDomain(dnsServer); + } + } return this; } catch (final ParseException e) { throw new BadConfigException(Section.INTERFACE, Location.DNS, e); |