Skip to content

Commit

Permalink
Set DNS cache to null in Lookup to avoid stale data
Browse files Browse the repository at this point in the history
  • Loading branch information
th-schwarz committed Jan 31, 2025
1 parent 4c2e181 commit 1842bef
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/java/codes/thischwa/dyndrest/util/NetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.xbill.DNS.AAAARecord;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.DClass;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

/** Some network relevant utils. */
public interface NetUtil {
public class NetUtil {

static boolean isIp(String ipStr) {
private NetUtil() {
Lookup.getDefaultCache(DClass.IN).setMaxEntries(0);
}

public static boolean isIp(String ipStr) {
return NetUtil.isIpv4(ipStr) || NetUtil.isIpv6(ipStr);
}

Expand Down Expand Up @@ -73,7 +78,7 @@ static String buildBasicAuth(String user, String pwd) {
* @param forceHttps the force https
* @return the base url
*/
static String getBaseUrl(boolean forceHttps) {
public static String getBaseUrl(boolean forceHttps) {
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
if (forceHttps) {
builder.scheme("https");
Expand All @@ -88,7 +93,7 @@ static String getBaseUrl(boolean forceHttps) {
* @return the ip setting
* @throws IOException if the resolving fails
*/
static IpSetting resolve(String hostName) throws IOException {
public static IpSetting resolve(String hostName) throws IOException {
IpSetting ipSetting = new IpSetting();
Record rec = lookup(hostName, Type.A);
if (rec != null) {
Expand All @@ -105,9 +110,7 @@ static IpSetting resolve(String hostName) throws IOException {
@Nullable
private static org.xbill.DNS.Record lookup(String hostName, int type) throws IOException {
try {
Lookup lookup = new Lookup(hostName, type);
lookup.setCache(null);
org.xbill.DNS.Record[] records = lookup.run();
org.xbill.DNS.Record[] records = new Lookup(hostName, type).run();
return (records == null || records.length == 0) ? null : records[0];
} catch (TextParseException e) {
throw new IOException(String.format("Couldn't lookup for host %s", hostName), e);
Expand Down

0 comments on commit 1842bef

Please sign in to comment.