Refactoring
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Ryan Harg 2024-11-28 10:25:06 +01:00
parent 6940072b0b
commit caa7e7a5dc
3 changed files with 43 additions and 50 deletions

14
pom.xml
View file

@ -152,18 +152,4 @@
</plugins> </plugins>
</build> </build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
</profiles>
</project> </project>

View file

@ -0,0 +1,39 @@
package de.rpr.ddnsclient.lookup;
import org.jboss.logging.Logger;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.NoSuchElementException;
enum IpLookupProviders {
IFCONFIG_ME("ifconfig.me", "ifconfig.me"),
IPIFY_ORG("ipify.org", "api64.ipify.org"),
IDENT_ME("ident.me", "ident.me"),
NNEV_DE("nnev.de", "ip.nnev.de"),
I_CAN_HAZ_IP_COM("icanhazip.com", "icanhazip.com");
private static final Logger log = Logger.getLogger(PublicIpLookup.class);
final String url;
private final String tld;
private static final SecureRandom secureRandom = new SecureRandom();
IpLookupProviders(String tld, String url) {
this.tld = tld;
this.url = url;
}
public static IpLookupProviders get(String candidate) {
return Arrays.stream(values())
.filter(it -> it.tld.equals(candidate))
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Unknown ip provider!"));
}
public static IpLookupProviders random() {
IpLookupProviders randomProvider = values()[secureRandom.nextInt(values().length)];
log.tracef("Choosing ip lookup provider %s", randomProvider);
return randomProvider;
}
}

View file

@ -10,9 +10,6 @@ import org.jboss.logging.Logger;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
@ApplicationScoped @ApplicationScoped
@ -31,42 +28,13 @@ public class PublicIpLookup {
this.configuredProvider = configuredProvider; this.configuredProvider = configuredProvider;
} }
enum Providers { private Optional<IpLookupProviders> provider = Optional.empty();
IFCONFIG_ME("ifconfig.me", "ifconfig.me"),
IPIFY_ORG("ipify.org", "api64.ipify.org"),
IDENT_ME("ident.me", "ident.me"),
NNEV_DE("nnev.de", "ip.nnev.de"),
I_CAN_HAZ_IP_COM("icanhazip.com", "icanhazip.com");
final String url;
private final String tld;
Providers(String tld, String url) {
this.tld = tld;
this.url = url;
}
public static Providers get(String candidate) {
return Arrays.stream(values())
.filter(it -> it.tld.equals(candidate))
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Unknown ip provider!"));
}
public static Providers random() {
Providers randomProvider = values()[new SecureRandom().nextInt(values().length)];
log.tracef("Choosing ip lookup provider %s", randomProvider);
return randomProvider;
}
}
private Optional<Providers> provider = Optional.empty();
void onStart(@Observes StartupEvent event) { void onStart(@Observes StartupEvent event) {
try { try {
configuredProvider.ifPresent(it -> { configuredProvider.ifPresent(it -> {
log.tracef("Setting ip lookup provider %s", it); log.tracef("Setting ip lookup provider %s", it);
provider = Optional.of(Providers.get(it)); provider = Optional.of(IpLookupProviders.get(it));
} }
); );
} catch (Exception e) { } catch (Exception e) {
@ -78,7 +46,7 @@ public class PublicIpLookup {
log.trace("Retrieving public ips."); log.trace("Retrieving public ips.");
try { try {
String provider = this.provider.map(it -> it.url).orElseGet(() -> Providers.random().url); String provider = this.provider.map(it -> it.url).orElseGet(() -> IpLookupProviders.random().url);
String v4 = getIp(provider, CurlProcessFactory.IpClass.V4); String v4 = getIp(provider, CurlProcessFactory.IpClass.V4);
String v6 = getIp(provider, CurlProcessFactory.IpClass.V6); String v6 = getIp(provider, CurlProcessFactory.IpClass.V6);