use of com.google.common.net.InternetDomainName in project graylog2-server by Graylog2.
the class DnsClientTest method testExtractShortDomain.
/**
* This test ensures that Google Guava domain parsing is working as expected (helps add protection for if
* the Guava dependency is upgraded in the future.)
*/
@Test
public void testExtractShortDomain() {
// Verify that Google Guava correctly returns just the top domain for multi-level TLDs.
// Eg. lb01.store.amazon.co.uk should resolve to just amazon.co.uk
InternetDomainName internetDomainName = InternetDomainName.from("lb01.store.amazon.co.uk");
assertEquals("amazon.co.uk", internetDomainName.topDomainUnderRegistrySuffix().toString());
}
use of com.google.common.net.InternetDomainName in project graylog2-server by Graylog2.
the class DnsClient method parseReverseLookupDomain.
/**
* Extract the domain name (without subdomain). The Guava {@link InternetDomainName} implementation
* provides a method to correctly handle this (and handles special cases for TLDs with multiple
* names. eg. for lb01.store.amazon.co.uk, only amazon.co.uk would be extracted).
* It uses https://publicsuffix.org behind the scenes.
* <p>
* Some domains (eg. completely randomly defined PTR domains) are not considered to have a public
* suffix according to Guava. For those, the only option is to manually extract the domain with
* string operations. This should be a rare case.
*/
public static void parseReverseLookupDomain(PtrDnsAnswer.Builder dnsAnswerBuilder, String hostname) {
dnsAnswerBuilder.fullDomain(hostname);
final InternetDomainName internetDomainName = InternetDomainName.from(hostname);
if (internetDomainName.hasPublicSuffix()) {
// Use Guava to extract domain name.
final InternetDomainName topDomainName = internetDomainName.topDomainUnderRegistrySuffix();
dnsAnswerBuilder.domain(topDomainName.toString());
} else {
/* Manually extract domain name.
* Eg. for hostname test.some-domain.com, only some-domain.com will be extracted. */
String[] split = hostname.split("\\.");
if (split.length > 1) {
dnsAnswerBuilder.domain(split[split.length - 2] + "." + split[split.length - 1]);
} else if (split.length == 1) {
// Domain is a single word with no dots.
dnsAnswerBuilder.domain(hostname);
} else {
// Domain is blank.
dnsAnswerBuilder.domain("");
}
}
}
use of com.google.common.net.InternetDomainName in project domain_hunter_pro by bit4woo.
the class DomainManager method isTLDDomain.
/**
* 是否是TLD域名。比如 baidu.net 是baidu.com的TLD域名
* 注意:www.baidu.com不是baidu.com的TLD域名,但是是子域名!!!
* @param domain
* @param rootDomain
*/
public static boolean isTLDDomain(String domain, String rootDomain) {
try {
InternetDomainName suffixDomain = InternetDomainName.from(domain).publicSuffix();
InternetDomainName suffixRootDomain = InternetDomainName.from(rootDomain).publicSuffix();
if (suffixDomain != null && suffixRootDomain != null) {
String suffixOfDomain = suffixDomain.toString();
String suffixOfRootDomain = suffixRootDomain.toString();
if (suffixOfDomain.equalsIgnoreCase(suffixOfRootDomain)) {
return false;
}
String tmpDomain = Commons.replaceLast(domain, suffixOfDomain, "");
String tmpRootdomain = Commons.replaceLast(rootDomain, suffixOfRootDomain, "");
if (tmpDomain.endsWith("." + tmpRootdomain) || tmpDomain.equalsIgnoreCase(tmpRootdomain)) {
return true;
}
}
return false;
} catch (java.lang.IllegalArgumentException e) {
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Aggregations