Correctly parse /etc/resolv.conf when contain multiple entries for searchdomain. (#8351)
Motivation:
ba594bcf4a
added a utility to parse searchdomains defined in /etc/resolv.conf but did not correctly handle the case when multiple are defined that are seperated by either whitespace or tab.
Modifications:
- Correctly parse multiple entries
- Add unit test.
Result:
Correctly parse multiple searchdomain entries.
This commit is contained in:
parent
652650b0db
commit
2a4bb346cf
@ -27,12 +27,12 @@ import java.io.FileReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider.DNS_PORT;
|
import static io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider.DNS_PORT;
|
||||||
import static io.netty.util.internal.ObjectUtil.checkNotNull;
|
import static io.netty.util.internal.ObjectUtil.checkNotNull;
|
||||||
@ -59,6 +59,7 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
|
|||||||
static final int DEFAULT_NDOTS = 1;
|
static final int DEFAULT_NDOTS = 1;
|
||||||
private final DnsServerAddresses defaultNameServerAddresses;
|
private final DnsServerAddresses defaultNameServerAddresses;
|
||||||
private final Map<String, DnsServerAddresses> domainToNameServerStreamMap;
|
private final Map<String, DnsServerAddresses> domainToNameServerStreamMap;
|
||||||
|
private static final Pattern SEARCH_DOMAIN_PATTERN = Pattern.compile("\\s+");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempt to parse {@code /etc/resolv.conf} and files in the {@code /etc/resolver} directory by default.
|
* Attempt to parse {@code /etc/resolv.conf} and files in the {@code /etc/resolver} directory by default.
|
||||||
@ -323,7 +324,10 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
|
|||||||
} else if (line.startsWith(SEARCH_ROW_LABEL)) {
|
} else if (line.startsWith(SEARCH_ROW_LABEL)) {
|
||||||
int i = indexOfNonWhiteSpace(line, SEARCH_ROW_LABEL.length());
|
int i = indexOfNonWhiteSpace(line, SEARCH_ROW_LABEL.length());
|
||||||
if (i >= 0) {
|
if (i >= 0) {
|
||||||
searchDomains.add(line.substring(i));
|
// May contain more then one entry, either seperated by whitespace or tab.
|
||||||
|
// See https://linux.die.net/man/5/resolver
|
||||||
|
String[] domains = SEARCH_DOMAIN_PATTERN.split(line.substring(i));
|
||||||
|
Collections.addAll(searchDomains, domains);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,6 +139,22 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
|
|||||||
assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains);
|
assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void searchDomainsWithMultipleSearchSeperatedByWhitespace() throws IOException {
|
||||||
|
File f = buildFile("search linecorp.local squarecorp.local\n" +
|
||||||
|
"nameserver 127.0.0.2\n");
|
||||||
|
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
|
||||||
|
assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void searchDomainsWithMultipleSearchSeperatedByTab() throws IOException {
|
||||||
|
File f = buildFile("search linecorp.local\tsquarecorp.local\n" +
|
||||||
|
"nameserver 127.0.0.2\n");
|
||||||
|
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
|
||||||
|
assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void searchDomainsPrecedence() throws IOException {
|
public void searchDomainsPrecedence() throws IOException {
|
||||||
File f = buildFile("domain linecorp.local\n" +
|
File f = buildFile("domain linecorp.local\n" +
|
||||||
|
Loading…
Reference in New Issue
Block a user