io.netty.resolver.HostsFileEntriesResolver Java Examples

The following examples show how to use io.netty.resolver.HostsFileEntriesResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpRequestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isLocalHost(String host, boolean onlyAnyOrLoopback, boolean hostsOnly) {
  if (NetUtil.isLocalhost(host)) {
    return true;
  }

  // if IP address, it is safe to use getByName (not affected by DNS rebinding)
  if (onlyAnyOrLoopback && !InetAddresses.isInetAddress(host)) {
    return false;
  }

  ThrowableNotNullFunction<InetAddress, Boolean, SocketException> isLocal =
          inetAddress -> inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress() || NetworkInterface.getByInetAddress(inetAddress) != null;

  try {
    InetAddress address = InetAddress.getByName(host);
    if (!isLocal.fun(address)) {
      return false;
    }
    // be aware - on windows hosts file doesn't contain localhost
    // hosts can contain remote addresses, so, we check it
    if (hostsOnly && !InetAddresses.isInetAddress(host)) {
      InetAddress hostInetAddress = HostsFileEntriesResolver.DEFAULT.address(host, ResolvedAddressTypes.IPV4_PREFERRED);
      return hostInetAddress != null && isLocal.fun(hostInetAddress);
    }
    else {
      return true;
    }
  }
  catch (IOException ignored) {
    return false;
  }
}
 
Example #2
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static void testRecursiveResolveCache(boolean cache)
        throws Exception {
    final String hostname = "some.record.netty.io";
    final String hostname2 = "some2.record.netty.io";

    final TestDnsServer dnsServerAuthority = new TestDnsServer(new HashSet<String>(
            Arrays.asList(hostname, hostname2)));
    dnsServerAuthority.start();

    TestDnsServer dnsServer = new RedirectingTestDnsServer(hostname,
            dnsServerAuthority.localAddress().getAddress().getHostAddress());
    dnsServer.start();

    TestDnsCache nsCache = new TestDnsCache(cache ? new DefaultDnsCache() : NoopDnsCache.INSTANCE);
    TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory =
            new TestRecursiveCacheDnsQueryLifecycleObserverFactory();
    EventLoopGroup group = new NioEventLoopGroup(1);
    DnsNameResolver resolver = new DnsNameResolver(
            group.next(), new ReflectiveChannelFactory<DatagramChannel>(NioDatagramChannel.class),
            NoopDnsCache.INSTANCE, nsCache, lifecycleObserverFactory, 3000, ResolvedAddressTypes.IPV4_ONLY, true,
            10, true, 4096, false, HostsFileEntriesResolver.DEFAULT,
            new SingletonDnsServerAddressStreamProvider(dnsServer.localAddress()),
            DnsNameResolver.DEFAULT_SEARCH_DOMAINS, 0, true) {
        @Override
        int dnsRedirectPort(InetAddress server) {
            return server.equals(dnsServerAuthority.localAddress().getAddress()) ?
                    dnsServerAuthority.localAddress().getPort() : DNS_PORT;
        }
    };

    // Java7 will strip of the "." so we need to adjust the expected dnsname. Both are valid in terms of the RFC
    // so its ok.
    String expectedDnsName = PlatformDependent.javaVersion() == 7 ?
            "dns4.some.record.netty.io" : "dns4.some.record.netty.io.";

    try {
        resolver.resolveAll(hostname).syncUninterruptibly();

        TestDnsQueryLifecycleObserver observer = lifecycleObserverFactory.observers.poll();
        assertNotNull(observer);
        assertTrue(lifecycleObserverFactory.observers.isEmpty());
        assertEquals(4, observer.events.size());
        QueryWrittenEvent writtenEvent1 = (QueryWrittenEvent) observer.events.poll();
        assertEquals(dnsServer.localAddress(), writtenEvent1.dnsServerAddress);
        QueryRedirectedEvent redirectedEvent = (QueryRedirectedEvent) observer.events.poll();

        assertEquals(expectedDnsName, redirectedEvent.nameServers.get(0).getHostName());
        assertEquals(dnsServerAuthority.localAddress(), redirectedEvent.nameServers.get(0));
        QueryWrittenEvent writtenEvent2 = (QueryWrittenEvent) observer.events.poll();
        assertEquals(dnsServerAuthority.localAddress(), writtenEvent2.dnsServerAddress);
        QuerySucceededEvent succeededEvent = (QuerySucceededEvent) observer.events.poll();

        if (cache) {
            assertNull(nsCache.cache.get("io.", null));
            assertNull(nsCache.cache.get("netty.io.", null));
            List<? extends DnsCacheEntry> entries = nsCache.cache.get("record.netty.io.", null);
            assertEquals(1, entries.size());

            assertNull(nsCache.cache.get(hostname, null));

            // Test again via cache.
            resolver.resolveAll(hostname).syncUninterruptibly();

            observer = lifecycleObserverFactory.observers.poll();
            assertNotNull(observer);
            assertTrue(lifecycleObserverFactory.observers.isEmpty());
            assertEquals(2, observer.events.size());
            writtenEvent1 = (QueryWrittenEvent) observer.events.poll();
            assertEquals(expectedDnsName, writtenEvent1.dnsServerAddress.getHostName());
            assertEquals(dnsServerAuthority.localAddress(), writtenEvent1.dnsServerAddress);
            succeededEvent = (QuerySucceededEvent) observer.events.poll();

            resolver.resolveAll(hostname2).syncUninterruptibly();

            observer = lifecycleObserverFactory.observers.poll();
            assertNotNull(observer);
            assertTrue(lifecycleObserverFactory.observers.isEmpty());
            assertEquals(2, observer.events.size());
            writtenEvent1 = (QueryWrittenEvent) observer.events.poll();
            assertEquals(expectedDnsName, writtenEvent1.dnsServerAddress.getHostName());
            assertEquals(dnsServerAuthority.localAddress(), writtenEvent1.dnsServerAddress);
            succeededEvent = (QuerySucceededEvent) observer.events.poll();

            // Check that it only queried the cache for record.netty.io.
            assertNull(nsCache.cacheHits.get("io."));
            assertNull(nsCache.cacheHits.get("netty.io."));
            assertNotNull(nsCache.cacheHits.get("record.netty.io."));
            assertNull(nsCache.cacheHits.get("some.record.netty.io."));
        }
    } finally {
        resolver.close();
        group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
        dnsServer.stop();
        dnsServerAuthority.stop();
    }
}
 
Example #3
Source File: DnsNameResolver.java    From netty-4.1.22 with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the component that tries to resolve hostnames against the hosts file prior to asking to
 * remotes DNS servers.
 */
public HostsFileEntriesResolver hostsFileEntriesResolver() {
    return hostsFileEntriesResolver;
}