Use lookupHost = false by default for TCP & UDP (#3825)

* Use `lookupHost = false` by default for TCP & UDP

The applications these days more and more are deployed and managed in the containers
where DNS is not configured by default.
Having `lookupHost = true` by default leads to a bad experience when some delays happen
for reverse host lookups.

* Use `lookupHost = false` by default for both TCP & UDP to have a reliable behavior
independently of the environment.
The `hostName` is used for `connectionId` and as a header in the message -
semantically it doesn't matter for the application logic what value is present over there.

* * Fix language in docs

Co-authored-by: Gary Russell <grussell@vmware.com>

Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
Artem Bilan
2022-06-22 13:23:03 -04:00
committed by GitHub
parent 8cf5f9083b
commit 04c7a87bd9
8 changed files with 75 additions and 66 deletions

View File

@@ -119,7 +119,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
private TcpConnectionInterceptorFactoryChain interceptorFactoryChain;
private boolean lookupHost = true;
private boolean lookupHost;
private TcpSocketSupport tcpSocketSupport = new DefaultTcpSocketSupport();
@@ -456,7 +456,8 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
/**
* If true, DNS reverse lookup is done on the remote ip address.
* Default true.
* Default false: not all environments (e.g. Docker containers) perform reliable DNS
* resolution.
* @param lookupHost the lookupHost to set
*/
public void setLookupHost(boolean lookupHost) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
@@ -71,26 +72,26 @@ import org.springframework.util.StringUtils;
public class DatagramPacketMessageMapper implements InboundMessageMapper<DatagramPacket>,
OutboundMessageMapper<DatagramPacket>, BeanFactoryAware {
private volatile String charset = "UTF-8";
private boolean acknowledge = false;
private String ackAddress;
private boolean lengthCheck = false;
private boolean lookupHost = true;
private volatile MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
private volatile boolean messageBuilderFactorySet;
private static Pattern udpHeadersPattern =
private static final Pattern UDP_HEADERS_PATTERN =
Pattern.compile(RegexUtils.escapeRegexSpecials(IpHeaders.ACK_ADDRESS) +
"=" + "([^;]*);" +
RegexUtils.escapeRegexSpecials(MessageHeaders.ID) +
"=" + "([^;]*);");
private String charset = StandardCharsets.UTF_8.name();
private boolean acknowledge;
private String ackAddress;
private boolean lengthCheck;
private boolean lookupHost;
private volatile MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
private volatile boolean messageBuilderFactorySet;
private BeanFactory beanFactory;
public void setCharset(String charset) {
@@ -110,6 +111,9 @@ public class DatagramPacketMessageMapper implements InboundMessageMapper<Datagra
}
/**
* If true, DNS reverse lookup is done on the remote ip address.
* Default false: not all environments (e.g. Docker containers) perform reliable DNS
* resolution.
* @param lookupHost the lookupHost to set
*/
public void setLookupHost(boolean lookupHost) {
@@ -232,20 +236,17 @@ public class DatagramPacketMessageMapper implements InboundMessageMapper<Datagra
length -= 4; // NOSONAR magic number
}
String hostAddress = packet.getAddress().getHostAddress();
String hostName;
String hostName = hostAddress;
if (this.lookupHost) {
hostName = packet.getAddress().getHostName();
}
else {
hostName = hostAddress;
}
int port = packet.getPort();
// Peek at the message in case they didn't configure us for ack but the sending
// side expects it.
if (this.acknowledge || startsWith(buffer, IpHeaders.ACK_ADDRESS)) {
try {
String headersString = new String(packet.getData(), offset, length, this.charset);
Matcher matcher = udpHeadersPattern.matcher(headersString);
Matcher matcher = UDP_HEADERS_PATTERN.matcher(headersString);
if (matcher.find()) {
// Strip off the ack headers and put in Message headers
length = length - matcher.end();
@@ -292,8 +293,8 @@ public class DatagramPacketMessageMapper implements InboundMessageMapper<Datagra
try {
byte[] comparing;
comparing = prefix.getBytes(this.charset);
for (int i = 0; i < comparing.length; i++) {
if (buffer.get() != comparing[i]) {
for (byte b : comparing) {
if (buffer.get() != b) {
return false;
}
}

View File

@@ -663,9 +663,9 @@
<xsd:attribute name="lookup-host" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Whether or not to do a DNS reverse-lookup on the remote ip address to
Whether to perform a DNS reverse-lookup on the remote ip address to
insert the host name into the
message headers (ip_connectionId, ip_hostName). Default "true".
message headers (ip_connectionId, ip_hostName). Default "false".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
@@ -853,9 +853,9 @@
<xsd:attribute name="lookup-host" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Whether or not to do a DNS reverse-lookup on the remote ip address to
Whether to do a DNS reverse-lookup on the remote ip address to
insert the host name into the
message headers (ip_hostName). Default "true".
message headers (ip_hostName). Default "false".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>