diff --git a/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BdxlLocator.java b/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BdxlLocator.java index da14bde4..ea608553 100644 --- a/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BdxlLocator.java +++ b/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BdxlLocator.java @@ -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; @@ -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 customDNSServers = new ArrayList<>(); //Google DNS: faster, supported by multiple data centers all around the world @@ -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)})); @@ -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); + } } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } @@ -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)); diff --git a/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BusdoxLocator.java b/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BusdoxLocator.java index 2dfecff8..9834257c 100644 --- a/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BusdoxLocator.java +++ b/peppol-lookup/src/main/java/network/oxalis/vefa/peppol/lookup/locator/BusdoxLocator.java @@ -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; @@ -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 customDNSServers = new ArrayList<>(); //Google DNS: faster, supported by multiple data centers all around the world @@ -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)})); @@ -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); } @@ -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)); diff --git a/peppol-lookup/src/main/resources/reference.conf b/peppol-lookup/src/main/resources/reference.conf index 4ae2758f..0cc19df4 100644 --- a/peppol-lookup/src/main/resources/reference.conf +++ b/peppol-lookup/src/main/resources/reference.conf @@ -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 } }