/** * @see org.apache.james.protocols.smtp.DNSService#getAllByName(java.lang.String) */ public InetAddress[] getAllByName(String host) throws UnknownHostException { return dns.getAllByName(host); }
private static Stream<Domain> getDomainIpStream(Domain domain, DNSService dns, Logger log) { try { return dns.getAllByName(domain.name()).stream() .map(InetAddress::getHostAddress) .map(Domain::of) .distinct(); } catch (UnknownHostException e) { log.error("Cannot get IP address(es) for {}", domain); return Stream.of(); } }
/** * @see #getDomainsIP(List, DNSService, Logger) */ private static List<String> getDomainIP(String domain, DNSService dns, Log log) { List<String> domainIP = new ArrayList<String>(); try { InetAddress[] addrs = dns.getAllByName(domain); for (int j = 0; j < addrs.length ; j++) { String ip = addrs[j].getHostAddress(); if (domainIP.contains(ip) == false) { domainIP.add(ip); } } } catch (UnknownHostException e) { log.error("Cannot get IP address(es) for " + domain); } return domainIP; }
public MXHostAddressIterator(Iterator<String> hosts, int defaultPort, DNSService dns, boolean useSingleIP) { checkNotNull(hosts, "Hosts is null"); checkNotNull(dns, "Dns is null"); final List<HostAddress> hAddresses = Lists.newArrayList(); while (hosts.hasNext()) { String nextHostname = hosts.next(); Map.Entry<String, String> hostAndPort = extractHostAndPort(nextHostname, defaultPort); try { final Collection<InetAddress> addrs; if (useSingleIP) { addrs = ImmutableList.of(dns.getByName(hostAndPort.getKey())); } else { addrs = dns.getAllByName(hostAndPort.getKey()); } for (InetAddress addr : addrs) { hAddresses.add(new HostAddress(hostAndPort.getKey(), "smtp://" + addr.getHostAddress() + ":" + hostAndPort.getValue())); } } catch (UnknownHostException uhe) { // this should never happen, since we just got // this host from mxHosts, which should have // already done this check. String logBuffer = "Couldn't resolve IP address for discovered host " + hostAndPort.getKey() + "."; LOGGER.error(logBuffer); } } addresses = hAddresses.iterator(); }
@Test public void containsDomainShouldReturnDetectedIp() throws Exception { String detected = "detected.tld"; String detectedIp = "148.25.32.1"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress = mock(InetAddress.class); when(detectedAddress.getHostAddress()).thenReturn(detectedIp); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress)); assertThat(domainList.containsDomain(Domain.of(detectedIp))).isTrue(); }
@Test public void getDomainsShouldContainDetectedDomainsAndIps() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(true)); String detected = "detected.tld"; String detectedIp = "148.25.32.1"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress = mock(InetAddress.class); when(detectedAddress.getHostAddress()).thenReturn(detectedIp); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress)); assertThat(domainList.getDomains()).contains(Domain.of(detected), Domain.of(detectedIp)); }
@Test public void getDomainsShouldContainDetectedDomainsAndIpsOfAddedDomains() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(true)); String added = "added.tld"; String detected = "detected.tld"; String detectedIp1 = "148.25.32.1"; String detectedIp2 = "148.25.32.2"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress1 = mock(InetAddress.class); InetAddress detectedAddress2 = mock(InetAddress.class); when(detectedAddress1.getHostAddress()).thenReturn(detectedIp1); when(detectedAddress2.getHostAddress()).thenReturn(detectedIp2); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress1)); when(dnsService.getAllByName(added)).thenReturn(ImmutableList.of(detectedAddress2)); domainList.addDomain(Domain.of(added)); assertThat(domainList.getDomains()) .extracting(Domain::name) .contains(detected, detectedIp1, added, detectedIp2); }