setup dyndns service

This commit is contained in:
_Bastler 2023-12-13 20:39:59 +01:00
parent d38478e0a4
commit f6a1d3cb57
2 changed files with 17 additions and 7 deletions

View File

@ -5,6 +5,8 @@ package de.bstly.we.dyndns.controller;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xbill.DNS.Message;
import org.xbill.DNS.Name;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.SimpleResolver;
@ -36,7 +39,10 @@ import de.bstly.we.model.User;
@RequestMapping("/dyndns")
public class DyndnsController extends BaseController {
private Logger logger = LoggerFactory.getLogger(DyndnsController.class);
public static final String SYSTEM_PROPERTY_DYNDNS_HOSTNAME = "dyndns.hostname";
public static final String SYSTEM_PROPERTY_DYNDNS_KEY_NAME = "dyndns.keyname";
public static final String SYSTEM_PROPERTY_DYNDNS_KEY = "dyndns.key";
@Autowired
@ -78,24 +84,28 @@ public class DyndnsController extends BaseController {
}
try {
Name zone = Name.fromString(name + ".we.bstly.de.");
Name host = Name.fromString("host", zone);
Name zone = Name.fromString("we.bstly.de.");
Name host = Name.fromString(name, zone);
Update update = new Update(zone);
if (ip.isPresent()) {
update.replace(host, Type.A, 3600, ip.get());
update.replace(host, Type.A, 300, ip.get());
}
if (ipv6.isPresent()) {
update.replace(host, Type.AAAA, 3600, ipv6.get());
update.replace(host, Type.AAAA, 300, ipv6.get());
}
String hostname = systemPropertyManager.get(SYSTEM_PROPERTY_DYNDNS_HOSTNAME, "127.0.0.1");
String keyName = systemPropertyManager.get(SYSTEM_PROPERTY_DYNDNS_KEY_NAME, "");
String key = systemPropertyManager.get(SYSTEM_PROPERTY_DYNDNS_KEY, "");
Resolver res = new SimpleResolver(hostname);
res.setTSIGKey(new TSIG(TSIG.HMAC_SHA512, host, key));
res.setTSIGKey(new TSIG(TSIG.HMAC_SHA512, keyName, key));
res.setTCP(true);
res.send(update);
Message response = res.send(update);
logger.debug("\nDynDNS for " + name + "with\nIP:\t" + ip.orElse("-") + "\nIPV6:\t" + ipv6.orElse("-") + "\n\n" +
response.toString());
} catch (Exception e) {
e.printStackTrace();
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);

View File

@ -14,7 +14,7 @@
<java.version>17</java.version>
<querydsl.version>5.0.0</querydsl.version>
<nimbus.version>9.37.3</nimbus.version>
<revision>3.0.1-SNAPSHOT</revision>
<revision>3.0.2-SNAPSHOT</revision>
</properties>
<parent>