Search in sources :

Example 21 with InternetDomainName

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());
}
Also used : InternetDomainName(com.google.common.net.InternetDomainName) Test(org.junit.Test)

Example 22 with InternetDomainName

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("");
        }
    }
}
Also used : InternetDomainName(com.google.common.net.InternetDomainName)

Example 23 with InternetDomainName

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;
    }
}
Also used : InternetDomainName(com.google.common.net.InternetDomainName) MalformedURLException(java.net.MalformedURLException)

Aggregations

InternetDomainName (com.google.common.net.InternetDomainName)23 DateTime (org.joda.time.DateTime)5 Registry (google.registry.model.tld.Registry)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 AllocationToken (google.registry.model.domain.token.AllocationToken)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 DomainCommand (google.registry.model.domain.DomainCommand)2 Check (google.registry.model.domain.DomainCommand.Check)2 AllocationTokenExtension (google.registry.model.domain.token.AllocationTokenExtension)2 TldState (google.registry.model.tld.Registry.TldState)2 MalformedURLException (java.net.MalformedURLException)2 HashSet (java.util.HashSet)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Strings (com.google.common.base.Strings)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Maps (com.google.common.collect.Maps)1