Skip to content

Commit

Permalink
Fixes & Compile with Java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasGrether committed Aug 7, 2021
1 parent 2424ed8 commit 8437b4d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
Expand Down
31 changes: 22 additions & 9 deletions src/main/java/de/tobiasgrether/dyndns/DynDNS.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,35 @@ 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<JsonNode> 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<JsonNode> 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);
}
}
}
}
}
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}

}

0 comments on commit 8437b4d

Please sign in to comment.