Skip to content

Commit

Permalink
Added configurable properties in oxalis conf for locator and global d…
Browse files Browse the repository at this point in the history
…ns server vs company dns
  • Loading branch information
aaron-kumar committed Oct 25, 2024
1 parent c34997d commit 96de5d0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import network.oxalis.vefa.peppol.lookup.util.DynamicHostnameGenerator;
import network.oxalis.vefa.peppol.lookup.util.EncodingUtils;
import network.oxalis.vefa.peppol.mode.Mode;
import org.apache.commons.lang3.StringUtils;
import org.xbill.DNS.*;

import java.net.InetAddress;
Expand All @@ -44,8 +45,9 @@
*/
public class BdxlLocator extends AbstractLocator {

private long timeout = 30L;
private int maxRetries = 3;
private final long timeout;
private final int maxRetries;
private final boolean enablePublicDNS;

private static final List<InetAddress> customDNSServers = new ArrayList<>();
//Google DNS: faster, supported by multiple data centers all around the world
Expand All @@ -62,10 +64,11 @@ public BdxlLocator(Mode mode) {
mode.getString("lookup.locator.bdxl.prefix"),
mode.getString("lookup.locator.hostname"),
mode.getString("lookup.locator.bdxl.algorithm"),
EncodingUtils.get(mode.getString("lookup.locator.bdxl.encoding"))
EncodingUtils.get(mode.getString("lookup.locator.bdxl.encoding")),
Long.parseLong(mode.getString("lookup.locator.bdxl.timeout")),
Integer.parseInt(mode.getString("lookup.locator.bdxl.maxRetries")),
Boolean.parseBoolean(mode.getString("lookup.locator.bdxl.enablePublicDNS"))
);
maxRetries = Integer.parseInt(mode.getString("lookup.locator.bdxl.maxRetries"));
timeout = Long.parseLong(mode.getString("lookup.locator.bdxl.timeout"));

try {
GOOGLE_PRIMARY_DNS = InetAddress.getByAddress((new byte[]{(byte) (8 & 0xff), (byte) (8 & 0xff), (byte) (8 & 0xff), (byte) (8 & 0xff)}));
Expand All @@ -77,10 +80,12 @@ public BdxlLocator(Mode mode) {
//Unable to initialize Custom DNS server
}

customDNSServers.add(GOOGLE_PRIMARY_DNS);
customDNSServers.add(GOOGLE_SECONDARY_DNS);
customDNSServers.add(CLOUDFLARE_PRIMARY_DNS);
customDNSServers.add(CLOUDFLARE_SECONDARY_DNS);
if (enablePublicDNS) {
customDNSServers.add(GOOGLE_PRIMARY_DNS);
customDNSServers.add(GOOGLE_SECONDARY_DNS);
customDNSServers.add(CLOUDFLARE_PRIMARY_DNS);
customDNSServers.add(CLOUDFLARE_SECONDARY_DNS);
}
}

/**
Expand All @@ -90,7 +95,7 @@ public BdxlLocator(Mode mode) {
*/
@SuppressWarnings("unused")
public BdxlLocator(String hostname) {
this(hostname, "SHA-256");
this("", hostname, "SHA-256", 30L, 3, false);
}

/**
Expand All @@ -100,7 +105,7 @@ public BdxlLocator(String hostname) {
* @param digestAlgorithm Algorithm used for generation of hostname.
*/
public BdxlLocator(String hostname, String digestAlgorithm) {
this("", hostname, digestAlgorithm);
this("", hostname, digestAlgorithm, 30L, 3, false);
}

/**
Expand All @@ -109,9 +114,12 @@ public BdxlLocator(String hostname, String digestAlgorithm) {
* @param prefix Value attached in front of calculated hash.
* @param hostname Hostname used as base for lookup.
* @param digestAlgorithm Algorithm used for generation of hostname.
* @param timeout Lookup timeout
* @param maxRetries Maximum number of retries
* @param enablePublicDNS Enable custom DNS lookup
*/
public BdxlLocator(String prefix, String hostname, String digestAlgorithm) {
this(prefix, hostname, digestAlgorithm, BaseEncoding.base32());
public BdxlLocator(String prefix, String hostname, String digestAlgorithm, long timeout, int maxRetries, boolean enablePublicDNS) {
this(prefix, hostname, digestAlgorithm, BaseEncoding.base32(), timeout, maxRetries, enablePublicDNS);
}

/**
Expand All @@ -121,8 +129,14 @@ public BdxlLocator(String prefix, String hostname, String digestAlgorithm) {
* @param hostname Hostname used as base for lookup.
* @param digestAlgorithm Algorithm used for generation of hostname.
* @param encoding Encoding of hash for hostname.
* @param timeout Lookup timeout
* @param maxRetries Maximum number of retries
* @param enablePublicDNS Enable custom DNS lookup
*/
public BdxlLocator(String prefix, String hostname, String digestAlgorithm, BaseEncoding encoding) {
public BdxlLocator(String prefix, String hostname, String digestAlgorithm, BaseEncoding encoding, long timeout, int maxRetries, boolean enablePublicDNS) {
this.timeout = timeout;
this.maxRetries = maxRetries;
this.enablePublicDNS = enablePublicDNS;
hostnameGenerator = new DynamicHostnameGenerator(prefix, hostname, digestAlgorithm, encoding);
}

Expand All @@ -131,8 +145,21 @@ public URI lookup(ParticipantIdentifier participantIdentifier) throws LookupExce
// Create hostname for participant identifier.
String hostname = hostnameGenerator.generate(participantIdentifier).replaceAll("=*", "");

ExtendedResolver extendedResolver;
try {
ExtendedResolver extendedResolver = CustomExtendedDNSResolver.createExtendedResolver(customDNSServers, timeout, maxRetries);
if(enablePublicDNS) {
extendedResolver = CustomExtendedDNSResolver.createExtendedResolver(customDNSServers, timeout, maxRetries);
} else {
extendedResolver = new ExtendedResolver();
try {
if (StringUtils.isNotBlank(hostname)) {
extendedResolver.addResolver(new SimpleResolver(hostname));
}
} catch (final UnknownHostException ex) {
//Primary DNS lookup fail, now try with default resolver
}
extendedResolver.addResolver (Lookup.getDefaultResolver ());
}
extendedResolver.setRetries(maxRetries);
extendedResolver.setTimeout(Duration.ofSeconds(timeout));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import network.oxalis.vefa.peppol.lookup.api.NotFoundException;
import network.oxalis.vefa.peppol.lookup.util.DynamicHostnameGenerator;
import network.oxalis.vefa.peppol.mode.Mode;
import org.apache.commons.lang3.StringUtils;
import org.xbill.DNS.ExtendedResolver;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TextParseException;

import java.net.InetAddress;
Expand All @@ -37,8 +39,9 @@

public class BusdoxLocator extends AbstractLocator {

private long timeout = 30L;
private int maxRetries = 3;
private final long timeout;
private final int maxRetries;
private final boolean enablePublicDNS;

private static final List<InetAddress> customDNSServers = new ArrayList<>();
//Google DNS: faster, supported by multiple data centers all around the world
Expand All @@ -54,10 +57,11 @@ public BusdoxLocator(Mode mode) {
this(
mode.getString("lookup.locator.busdox.prefix"),
mode.getString("lookup.locator.hostname"),
mode.getString("lookup.locator.busdox.algorithm")
mode.getString("lookup.locator.busdox.algorithm"),
Long.parseLong(mode.getString("lookup.locator.busdox.timeout")),
Integer.parseInt(mode.getString("lookup.locator.busdox.maxRetries")),
Boolean.parseBoolean(mode.getString("lookup.locator.busdox.enablePublicDNS"))
);
maxRetries = Integer.parseInt(mode.getString("lookup.locator.busdox.maxRetries"));
timeout = Long.parseLong(mode.getString("lookup.locator.busdox.timeout"));

try {
GOOGLE_PRIMARY_DNS = InetAddress.getByAddress((new byte[]{(byte) (8 & 0xff), (byte) (8 & 0xff), (byte) (8 & 0xff), (byte) (8 & 0xff)}));
Expand All @@ -69,18 +73,23 @@ public BusdoxLocator(Mode mode) {
//Unable to initialize Custom DNS server
}

customDNSServers.add(GOOGLE_PRIMARY_DNS);
customDNSServers.add(GOOGLE_SECONDARY_DNS);
customDNSServers.add(CLOUDFLARE_PRIMARY_DNS);
customDNSServers.add(CLOUDFLARE_SECONDARY_DNS);
if (enablePublicDNS) {
customDNSServers.add(GOOGLE_PRIMARY_DNS);
customDNSServers.add(GOOGLE_SECONDARY_DNS);
customDNSServers.add(CLOUDFLARE_PRIMARY_DNS);
customDNSServers.add(CLOUDFLARE_SECONDARY_DNS);
}
}

@SuppressWarnings("unused")
public BusdoxLocator(String hostname) {
this("B-", hostname, "MD5");
this("B-", hostname, "MD5", 30L, 3, false);
}

public BusdoxLocator(String prefix, String hostname, String algorithm) {
public BusdoxLocator(String prefix, String hostname, String algorithm, long timeout, int maxRetries, boolean enablePublicDNS) {
this.timeout = timeout;
this.maxRetries = maxRetries;
this.enablePublicDNS = enablePublicDNS;
hostnameGenerator = new DynamicHostnameGenerator(prefix, hostname, algorithm);
}

Expand All @@ -89,8 +98,21 @@ public URI lookup(ParticipantIdentifier participantIdentifier) throws LookupExce
// Create hostname for participant identifier.
String hostname = hostnameGenerator.generate(participantIdentifier);

ExtendedResolver extendedResolver;
try {
ExtendedResolver extendedResolver = CustomExtendedDNSResolver.createExtendedResolver(customDNSServers, timeout, maxRetries);
if(enablePublicDNS) {
extendedResolver = CustomExtendedDNSResolver.createExtendedResolver(customDNSServers, timeout, maxRetries);
} else {
extendedResolver = new ExtendedResolver();
try {
if (StringUtils.isNotBlank(hostname)) {
extendedResolver.addResolver(new SimpleResolver(hostname));
}
} catch (final UnknownHostException ex) {
//Primary DNS lookup fail, now try with default resolver
}
extendedResolver.addResolver (Lookup.getDefaultResolver ());
}
extendedResolver.setRetries(maxRetries);
extendedResolver.setTimeout(Duration.ofSeconds(timeout));

Expand Down
6 changes: 4 additions & 2 deletions peppol-lookup/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ mode.default.lookup.locator = {
prefix: ""
algorithm: SHA-256
encoding: base32
maxRetries: 3
timeout: 30
maxRetries: 3
enablePublicDNS: false
}

busdox: {
prefix: "B-"
algorithm: MD5
maxRetries: 3
timeout: 30
maxRetries: 3
enablePublicDNS: false
}
}

Expand Down

0 comments on commit 96de5d0

Please sign in to comment.