From 523936efb4bc848be1f77a26539cff1b5aa5cf4c Mon Sep 17 00:00:00 2001 From: Ryan Harg Date: Tue, 3 Dec 2024 09:31:52 +0100 Subject: [PATCH] Remove rest extension, renaming, delay --- pom.xml | 9 +++---- .../java/de/rpr/ddnsclient/AppConfig.java | 18 +------------ src/main/java/de/rpr/ddnsclient/Schedule.java | 4 ++- src/main/java/de/rpr/ddnsclient/Updater.java | 6 ++--- .../java/de/rpr/ddnsclient/dyndns/Ddnss.java | 6 ++--- .../de/rpr/ddnsclient/dyndns/DuckDNS.java | 6 ++--- .../java/de/rpr/ddnsclient/dyndns/DynDns.java | 4 +-- .../de/rpr/ddnsclient/lookup/DnsJava.java | 6 ++--- .../de/rpr/ddnsclient/lookup/DnsResolver.java | 4 +-- .../rpr/ddnsclient/lookup/PublicIpLookup.java | 6 ++--- .../de/rpr/ddnsclient/model/Addresses.java | 26 +++++++++++++++++++ .../java/de/rpr/ddnsclient/model/IPs.java | 11 -------- src/main/resources/application.properties | 2 +- .../java/de/rpr/ddnsclient/UpdaterTest.java | 14 +++++----- .../de/rpr/ddnsclient/dyndns/DdnssTest.java | 8 +++--- .../de/rpr/ddnsclient/dyndns/DuckDNSTest.java | 8 +++--- .../de/rpr/ddnsclient/lookup/DnsJavaTest.java | 4 +-- .../ddnsclient/lookup/PublicIpLookupTest.java | 6 ++--- 18 files changed, 73 insertions(+), 75 deletions(-) create mode 100644 src/main/java/de/rpr/ddnsclient/model/Addresses.java delete mode 100644 src/main/java/de/rpr/ddnsclient/model/IPs.java diff --git a/pom.xml b/pom.xml index e5dd579..da0c71c 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 de.rpr ddnsclient @@ -50,11 +51,7 @@ io.quarkus - quarkus-rest - - - io.quarkus - quarkus-rest-jackson + quarkus-jackson io.quarkus diff --git a/src/main/java/de/rpr/ddnsclient/AppConfig.java b/src/main/java/de/rpr/ddnsclient/AppConfig.java index 46beb1c..8f6a009 100644 --- a/src/main/java/de/rpr/ddnsclient/AppConfig.java +++ b/src/main/java/de/rpr/ddnsclient/AppConfig.java @@ -3,17 +3,11 @@ package de.rpr.ddnsclient; import com.fasterxml.jackson.databind.ObjectMapper; import de.rpr.ddnsclient.model.Config; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.ws.rs.Produces; +import jakarta.enterprise.inject.Produces; import org.eclipse.microprofile.config.inject.ConfigProperty; -import javax.naming.Context; -import javax.naming.NamingException; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; import java.io.File; import java.io.IOException; -import java.text.MessageFormat; -import java.util.Properties; public class AppConfig { @@ -27,14 +21,4 @@ public class AppConfig { return objectMapper.readValue(configFile, Config.class); } - @Produces - @ApplicationScoped - public DirContext dirContext( - @ConfigProperty(name = "ddnsclient.dns.resolver", defaultValue = "9.9.9.9") String dnsServer) - throws NamingException { - Properties env = new Properties(); - env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); - env.setProperty(Context.PROVIDER_URL, MessageFormat.format("dns://{0}", dnsServer)); - return new InitialDirContext(env); - } } diff --git a/src/main/java/de/rpr/ddnsclient/Schedule.java b/src/main/java/de/rpr/ddnsclient/Schedule.java index 18d35c6..1b478ce 100644 --- a/src/main/java/de/rpr/ddnsclient/Schedule.java +++ b/src/main/java/de/rpr/ddnsclient/Schedule.java @@ -5,6 +5,8 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.util.concurrent.TimeUnit; + @ApplicationScoped public class Schedule { @@ -17,7 +19,7 @@ public class Schedule { this.updater = updater; } - @Scheduled(every = "${ddnsclient.interval:5m}") + @Scheduled(every = "${ddnsclient.interval:5m}", delay = 5, delayUnit = TimeUnit.SECONDS) void run() { log.debug("Starting run()"); updater.run(); diff --git a/src/main/java/de/rpr/ddnsclient/Updater.java b/src/main/java/de/rpr/ddnsclient/Updater.java index fcc4bce..458fdbd 100644 --- a/src/main/java/de/rpr/ddnsclient/Updater.java +++ b/src/main/java/de/rpr/ddnsclient/Updater.java @@ -4,9 +4,9 @@ import de.rpr.ddnsclient.dyndns.DynDns; import de.rpr.ddnsclient.dyndns.DyndnsProviderRegistry; import de.rpr.ddnsclient.lookup.DnsResolver; import de.rpr.ddnsclient.lookup.PublicIpLookup; +import de.rpr.ddnsclient.model.Addresses; import de.rpr.ddnsclient.model.Config; import de.rpr.ddnsclient.model.DyndnsAuth; -import de.rpr.ddnsclient.model.IPs; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.eclipse.microprofile.config.inject.ConfigProperty; @@ -51,7 +51,7 @@ public class Updater { throw new IllegalStateException("Missing configuration"); } - IPs publicIps = publicIpLookup.get(); + Addresses publicIps = publicIpLookup.get(); log.debugf("Public ips - v4: %s, v6: %s", publicIps.v4(), publicIps.v6()); config.forEach(cfg -> { @@ -64,7 +64,7 @@ public class Updater { log.debugf("Handling %s.", cfg.hostname()); - IPs registeredIps = dnsResolver.resolve(cfg.hostname()); + Addresses registeredIps = dnsResolver.resolve(cfg.hostname()); log.debugf("Registered ips - v4: %s, v6: %s", registeredIps.v4(), registeredIps.v6()); if (!publicIps.equals(registeredIps)) { diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java b/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java index 3ec5e62..78d477c 100644 --- a/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java +++ b/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java @@ -1,7 +1,7 @@ package de.rpr.ddnsclient.dyndns; +import de.rpr.ddnsclient.model.Addresses; import de.rpr.ddnsclient.model.DyndnsAuth; -import de.rpr.ddnsclient.model.IPs; import jakarta.enterprise.context.ApplicationScoped; import org.jboss.logging.Logger; @@ -26,7 +26,7 @@ public class Ddnss implements DynDns { return "ddnss"; } - public void update(String hostname, IPs currentIps, DyndnsAuth auth) { + public void update(String hostname, Addresses currentIps, DyndnsAuth auth) { log.tracef("Updating ddnss hostname %s", hostname); try (HttpClient http = httpClientFactory.create()) { @@ -41,7 +41,7 @@ public class Ddnss implements DynDns { } } - private String getUpdateUrl(String hostname, IPs currentIps, String token) { + private String getUpdateUrl(String hostname, Addresses currentIps, String token) { if (currentIps.v4().isPresent() && currentIps.v6().isPresent()) { return MessageFormat.format("https://ddnss.de/upd.php?key={0}&host={1}&ip={2}&ip6={3}", token, hostname, currentIps.v4().get(), currentIps.v6().get()); } else if (currentIps.v4().isPresent()) { diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java b/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java index b8c8658..3dea0f2 100644 --- a/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java +++ b/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java @@ -1,7 +1,7 @@ package de.rpr.ddnsclient.dyndns; +import de.rpr.ddnsclient.model.Addresses; import de.rpr.ddnsclient.model.DyndnsAuth; -import de.rpr.ddnsclient.model.IPs; import jakarta.enterprise.context.ApplicationScoped; import org.jboss.logging.Logger; @@ -27,7 +27,7 @@ public class DuckDNS implements DynDns { } @Override - public void update(String hostname, IPs currentIps, DyndnsAuth auth) { + public void update(String hostname, Addresses currentIps, DyndnsAuth auth) { log.tracef("Updating duckdns hostname %s", hostname); try (HttpClient http = httpClientFactory.create()) { @@ -44,7 +44,7 @@ public class DuckDNS implements DynDns { } } - private String getUpdateUrl(String hostname, IPs currentIps, String token) { + private String getUpdateUrl(String hostname, Addresses currentIps, String token) { if (currentIps.v4().isPresent() && currentIps.v6().isPresent()) { return MessageFormat.format("https://www.duckdns.org/update?domains={0}&token={1}&ip={2}&ipv6={3}", hostname, token, currentIps.v4().get(), currentIps.v6().get()); } else if (currentIps.v4().isPresent()) { diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/DynDns.java b/src/main/java/de/rpr/ddnsclient/dyndns/DynDns.java index 74e67e5..d0c1dca 100644 --- a/src/main/java/de/rpr/ddnsclient/dyndns/DynDns.java +++ b/src/main/java/de/rpr/ddnsclient/dyndns/DynDns.java @@ -1,7 +1,7 @@ package de.rpr.ddnsclient.dyndns; +import de.rpr.ddnsclient.model.Addresses; import de.rpr.ddnsclient.model.DyndnsAuth; -import de.rpr.ddnsclient.model.IPs; import java.net.URI; import java.net.http.HttpRequest; @@ -10,7 +10,7 @@ public interface DynDns { String name(); - void update(String hostname, IPs currentIps, DyndnsAuth auth); + void update(String hostname, Addresses currentIps, DyndnsAuth auth); default HttpRequest getRequest(String updateUrl) { return HttpRequest.newBuilder() diff --git a/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java b/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java index 485fc73..ceba660 100644 --- a/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java +++ b/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java @@ -1,6 +1,6 @@ package de.rpr.ddnsclient.lookup; -import de.rpr.ddnsclient.model.IPs; +import de.rpr.ddnsclient.model.Addresses; import jakarta.enterprise.context.ApplicationScoped; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.jboss.logging.Logger; @@ -21,10 +21,10 @@ public class DnsJava implements DnsResolver { } @Override - public IPs resolve(String hostname) { + public Addresses resolve(String hostname) { Optional v4 = getIp(hostname, RecordType.A); Optional v6 = getIp(hostname, RecordType.AAAA); - return new IPs(v4, v6); + return new Addresses(v4, v6); } private Optional getIp(String hostname, RecordType type) { diff --git a/src/main/java/de/rpr/ddnsclient/lookup/DnsResolver.java b/src/main/java/de/rpr/ddnsclient/lookup/DnsResolver.java index 757b5db..1ae0665 100644 --- a/src/main/java/de/rpr/ddnsclient/lookup/DnsResolver.java +++ b/src/main/java/de/rpr/ddnsclient/lookup/DnsResolver.java @@ -1,7 +1,7 @@ package de.rpr.ddnsclient.lookup; -import de.rpr.ddnsclient.model.IPs; +import de.rpr.ddnsclient.model.Addresses; public interface DnsResolver { - IPs resolve(String hostname); + Addresses resolve(String hostname); } diff --git a/src/main/java/de/rpr/ddnsclient/lookup/PublicIpLookup.java b/src/main/java/de/rpr/ddnsclient/lookup/PublicIpLookup.java index a87a5bf..de2169e 100644 --- a/src/main/java/de/rpr/ddnsclient/lookup/PublicIpLookup.java +++ b/src/main/java/de/rpr/ddnsclient/lookup/PublicIpLookup.java @@ -1,6 +1,6 @@ package de.rpr.ddnsclient.lookup; -import de.rpr.ddnsclient.model.IPs; +import de.rpr.ddnsclient.model.Addresses; import io.quarkus.runtime.StartupEvent; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.event.Observes; @@ -38,7 +38,7 @@ public class PublicIpLookup { } } - public IPs get() { + public Addresses get() { log.trace("Retrieving public ips."); try { @@ -47,7 +47,7 @@ public class PublicIpLookup { Optional v4 = getIp(provider, CurlProcessFactory.IpClass.V4); Optional v6 = getIp(provider, CurlProcessFactory.IpClass.V6); - return new IPs(v4, v6); + return new Addresses(v4, v6); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/main/java/de/rpr/ddnsclient/model/Addresses.java b/src/main/java/de/rpr/ddnsclient/model/Addresses.java new file mode 100644 index 0000000..df50dd9 --- /dev/null +++ b/src/main/java/de/rpr/ddnsclient/model/Addresses.java @@ -0,0 +1,26 @@ +package de.rpr.ddnsclient.model; + +import java.text.MessageFormat; +import java.util.Optional; + +public record Addresses(Optional v4, Optional v6) { + + public Addresses(String v4, String v6) { + this(Optional.ofNullable(v4), Optional.ofNullable(v6)); + } + + @SuppressWarnings("OptionalIsPresent") + @Override + public String toString() { + if (v4.isPresent() && v6.isPresent()) { + return MessageFormat.format("v4: {0}, v6: {1}", v4.get(), v6.get()); + } else if (v4.isPresent()) { + return MessageFormat.format("v4: {0}, v6: empty", v4.get()); + } else if (v6.isPresent()) { + return MessageFormat.format("v4: empty, v6: {0}", v6.get()); + } else { + return "v4: empty, v6: empty"; + } + } +} + diff --git a/src/main/java/de/rpr/ddnsclient/model/IPs.java b/src/main/java/de/rpr/ddnsclient/model/IPs.java deleted file mode 100644 index 004eee3..0000000 --- a/src/main/java/de/rpr/ddnsclient/model/IPs.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.rpr.ddnsclient.model; - -import java.util.Optional; - -public record IPs(Optional v4, Optional v6) { - - public IPs(String v4, String v6) { - this(Optional.ofNullable(v4), Optional.ofNullable(v6)); - } -} - diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0ae0f4b..bcaedde 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ quarkus.banner.path=logo-rubifont.txt quarkus.log.level=INFO quarkus.log.category."de.rpr.ddnsclient".min-level=TRACE -quarkus.log.category."de.rpr.ddnsclient".level=${DDNS_LOG_LEVEL:INFO} \ No newline at end of file +quarkus.log.category."de.rpr.ddnsclient".level=${DDNS_LOG_LEVEL:DEBUG} \ No newline at end of file diff --git a/src/test/java/de/rpr/ddnsclient/UpdaterTest.java b/src/test/java/de/rpr/ddnsclient/UpdaterTest.java index 1cb737c..6f730ec 100644 --- a/src/test/java/de/rpr/ddnsclient/UpdaterTest.java +++ b/src/test/java/de/rpr/ddnsclient/UpdaterTest.java @@ -5,9 +5,9 @@ import de.rpr.ddnsclient.dyndns.DynDns; import de.rpr.ddnsclient.dyndns.DyndnsProviderRegistry; import de.rpr.ddnsclient.lookup.DnsResolver; import de.rpr.ddnsclient.lookup.PublicIpLookup; +import de.rpr.ddnsclient.model.Addresses; import de.rpr.ddnsclient.model.Config; import de.rpr.ddnsclient.model.DyndnsAuth; -import de.rpr.ddnsclient.model.IPs; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -46,7 +46,7 @@ class UpdaterTest { @Test void should_process_all_config_entries() { - IPs ips = new IPs("ipv4", "ipv6"); + Addresses ips = new Addresses("ipv4", "ipv6"); when(publicIpLookup.get()).thenReturn(ips); when(dnsResolver.resolve(any())).thenReturn(ips); @@ -69,7 +69,7 @@ class UpdaterTest { @Test void should_not_trigger_dyndns_update_if_ips_match() { - IPs ips = new IPs("ipv4", "ipv6"); + Addresses ips = new Addresses("ipv4", "ipv6"); when(publicIpLookup.get()).thenReturn(ips); when(dnsResolver.resolve("example.org")).thenReturn(ips); @@ -88,9 +88,9 @@ class UpdaterTest { when(dyndnsProviderRegistry.get("ddnss")).thenReturn(ddnss); - IPs publicIps = new IPs("ipv4", "ipv6"); + Addresses publicIps = new Addresses("ipv4", "ipv6"); when(publicIpLookup.get()).thenReturn(publicIps); - when(dnsResolver.resolve("example.org")).thenReturn(new IPs("registered_ipv4", "registered_ipv6")); + when(dnsResolver.resolve("example.org")).thenReturn(new Addresses("registered_ipv4", "registered_ipv6")); Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, ddnssConfig, backoff); updater.run(); @@ -109,9 +109,9 @@ class UpdaterTest { when(dyndnsProviderRegistry.get("ddnss")).thenReturn(ddnss); - IPs publicIps = new IPs("ipv4", "ipv6"); + Addresses publicIps = new Addresses("ipv4", "ipv6"); when(publicIpLookup.get()).thenReturn(publicIps); - when(dnsResolver.resolve("example.org")).thenReturn(new IPs("registered_ipv4", "registered_ipv6")); + when(dnsResolver.resolve("example.org")).thenReturn(new Addresses("registered_ipv4", "registered_ipv6")); Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, ddnssConfig, backoff); updater.run(); diff --git a/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java b/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java index 6ea0323..3593166 100644 --- a/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java +++ b/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java @@ -1,7 +1,7 @@ package de.rpr.ddnsclient.dyndns; +import de.rpr.ddnsclient.model.Addresses; import de.rpr.ddnsclient.model.DyndnsAuth; -import de.rpr.ddnsclient.model.IPs; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -42,7 +42,7 @@ class DdnssTest { ddnss.update( "example.com", - new IPs("ipv4", "ipv6"), + new Addresses("ipv4", "ipv6"), new DyndnsAuth(null, null, "token") ); @@ -66,7 +66,7 @@ class DdnssTest { dynDns.update( "example.com", - new IPs("ipv4", null), + new Addresses("ipv4", null), new DyndnsAuth(null, null, "token") ); @@ -87,7 +87,7 @@ class DdnssTest { dynDns.update( "example.com", - new IPs(null, "ipv6"), + new Addresses(null, "ipv6"), new DyndnsAuth(null, null, "token") ); diff --git a/src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java b/src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java index 0c361a4..bcf144e 100644 --- a/src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java +++ b/src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java @@ -1,7 +1,7 @@ package de.rpr.ddnsclient.dyndns; +import de.rpr.ddnsclient.model.Addresses; import de.rpr.ddnsclient.model.DyndnsAuth; -import de.rpr.ddnsclient.model.IPs; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -42,7 +42,7 @@ class DuckDNSTest { dynDns.update( "example.com", - new IPs("ipv4", "ipv6"), + new Addresses("ipv4", "ipv6"), new DyndnsAuth(null, null, "token") ); @@ -66,7 +66,7 @@ class DuckDNSTest { dynDns.update( "example.com", - new IPs("ipv4", null), + new Addresses("ipv4", null), new DyndnsAuth(null, null, "token") ); @@ -87,7 +87,7 @@ class DuckDNSTest { dynDns.update( "example.com", - new IPs(null, "ipv6"), + new Addresses(null, "ipv6"), new DyndnsAuth(null, null, "token") ); diff --git a/src/test/java/de/rpr/ddnsclient/lookup/DnsJavaTest.java b/src/test/java/de/rpr/ddnsclient/lookup/DnsJavaTest.java index d910057..fba45aa 100644 --- a/src/test/java/de/rpr/ddnsclient/lookup/DnsJavaTest.java +++ b/src/test/java/de/rpr/ddnsclient/lookup/DnsJavaTest.java @@ -1,6 +1,6 @@ package de.rpr.ddnsclient.lookup; -import de.rpr.ddnsclient.model.IPs; +import de.rpr.ddnsclient.model.Addresses; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -9,7 +9,7 @@ class DnsJavaTest { @Test void test() { - IPs result = new DnsJava("9.9.9.9").resolve("example.com"); + Addresses result = new DnsJava("9.9.9.9").resolve("example.com"); assertThat(result.v4()).isPresent(); assertThat(result.v6()).isPresent(); } diff --git a/src/test/java/de/rpr/ddnsclient/lookup/PublicIpLookupTest.java b/src/test/java/de/rpr/ddnsclient/lookup/PublicIpLookupTest.java index df06f92..badda25 100644 --- a/src/test/java/de/rpr/ddnsclient/lookup/PublicIpLookupTest.java +++ b/src/test/java/de/rpr/ddnsclient/lookup/PublicIpLookupTest.java @@ -1,6 +1,6 @@ package de.rpr.ddnsclient.lookup; -import de.rpr.ddnsclient.model.IPs; +import de.rpr.ddnsclient.model.Addresses; import io.quarkus.runtime.StartupEvent; import org.apache.commons.io.IOUtils; import org.assertj.core.api.Assertions; @@ -53,9 +53,9 @@ class PublicIpLookupTest { PublicIpLookup lookup = new PublicIpLookup(curlProcessFactory, Optional.of("ifconfig.me")); lookup.onStart(new StartupEvent()); - IPs result = lookup.get(); + Addresses result = lookup.get(); - assertThat(result).isEqualTo(new IPs("ipv4", "ipv6")); + assertThat(result).isEqualTo(new Addresses("ipv4", "ipv6")); ArgumentCaptor providerCaptor = ArgumentCaptor.forClass(String.class); verify(curlProcessFactory, times(2)).create(providerCaptor.capture(), any());