diff --git a/README.md b/README.md index ff1f934..713881a 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ dnsRecords: This project uses Cloudflare APIs only. It uses cloudflares cgi tracing endpoint to resolve the current public address and then handles changes accordingly. ## How to run -- Build with maven (java 11) +- Build with maven (java 8) - Run on a machine like a server or a Raspberry Pi - start once, edit configuration - start again diff --git a/pom.xml b/pom.xml index d7ec8ab..15b6b41 100644 --- a/pom.xml +++ b/pom.xml @@ -66,8 +66,8 @@ maven-compiler-plugin 3.8.1 - 11 - 11 + 8 + 8 diff --git a/src/main/java/de/tobiasgrether/dyndns/DynDNS.java b/src/main/java/de/tobiasgrether/dyndns/DynDNS.java index 6ba5eb4..5eea1c0 100644 --- a/src/main/java/de/tobiasgrether/dyndns/DynDNS.java +++ b/src/main/java/de/tobiasgrether/dyndns/DynDNS.java @@ -69,15 +69,19 @@ private void triggerDNSRewrite(String currentIp) { DNSListResult parsedResult = this.mapper.readValue(result.getBody().toString(), DNSListResult.class); if (parsedResult.isSuccess()) { for (DNSRecord record : parsedResult.getResult()) { - if (this.model.getDnsRecords().contains(new RecordEntry(record.getName(), "")) && !record.getContent().equals(currentIp)) { - record.setContent(currentIp); - HttpResponse update = Unirest.put("https://api.cloudflare.com/client/v4/zones/" + this.model.getZoneId() + "/dns_records/" + record.getId()) - .body(this.mapper.writeValue(record)) - .header("X-Auth-Key", this.model.getApiToken()) - .header("X-Auth-Email", this.model.getCfEmail()) - .asJson(); - if (update.getStatus() == 200) { - this.logger.info("DNS Record updated: {} (Record type {}) now has IP {}", record.getName(), record.getType(), currentIp); + RecordEntry entry = this.findConfigEntry(record.getName(), record.getType()); + if(entry != null){ + if(!record.getContent().equals(currentIp)){ + this.logger.info("Attempting to rewrite dns entry {}", record.getName()); + record.setContent(currentIp); + HttpResponse update = Unirest.put("https://api.cloudflare.com/client/v4/zones/" + this.model.getZoneId() + "/dns_records/" + record.getId()) + .body(this.mapper.writeValue(record)) + .header("X-Auth-Key", this.model.getApiToken()) + .header("X-Auth-Email", this.model.getCfEmail()) + .asJson(); + if (update.getStatus() == 200) { + this.logger.info("DNS Record updated: {} (Record type {}) now has IP {}", record.getName(), record.getType(), currentIp); + } } } } @@ -85,6 +89,15 @@ private void triggerDNSRewrite(String currentIp) { this.logger.info("Record rewrite complete, new public ip is " + currentIp); } + public RecordEntry findConfigEntry(String name, String type){ + for(RecordEntry record : this.getModel().getDnsRecords()){ + if(record.compareValues(name, type)){ + return record; + } + } + return null; + } + public Logger getLogger() { return logger; } diff --git a/src/main/java/de/tobiasgrether/dyndns/model/config/RecordEntry.java b/src/main/java/de/tobiasgrether/dyndns/model/config/RecordEntry.java index a26fbbb..bf7a9e4 100644 --- a/src/main/java/de/tobiasgrether/dyndns/model/config/RecordEntry.java +++ b/src/main/java/de/tobiasgrether/dyndns/model/config/RecordEntry.java @@ -1,12 +1,11 @@ package de.tobiasgrether.dyndns.model.config; +import de.tobiasgrether.dyndns.model.DNSRecord; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.ToString; -import java.util.Objects; - @Getter @ToString @AllArgsConstructor @@ -20,8 +19,15 @@ public class RecordEntry { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - RecordEntry that = (RecordEntry) o; - return Objects.equals(name, that.name) && Objects.equals(type, that.type); + if (o instanceof DNSRecord) { + DNSRecord that = (DNSRecord) o; + return this.name.equals(that.getName()) && this.type.equals(that.getType()); + } + return false; + } + + public boolean compareValues(String otherName, String otherType) { + return this.name.equals(otherName) && this.type.equals(otherType); } }