add support for multiple domains

This commit is contained in:
Benjamin Bärthlein
2020-04-27 20:37:22 +02:00
parent 890ffed035
commit aa147a6853
10 changed files with 34 additions and 26 deletions
+4 -4
View File
@@ -20,7 +20,7 @@ type Handler struct {
type Envs struct {
AdminLogin string
Domain string
Domains []string
}
type CustomValidator struct {
@@ -83,9 +83,9 @@ func (h *Handler) ParseEnvs() error {
return fmt.Errorf("environment variable DDNS_ADMIN_LOGIN has to be set")
}
h.Config.Domain = os.Getenv("DDNS_DOMAIN")
if h.Config.Domain == "" {
return fmt.Errorf("environment variable DDNS_DOMAIN has to be set")
h.Config.Domains = strings.Split(os.Getenv("DDNS_DOMAINS"), ",")
if len(h.Config.Domains) < 1 {
return fmt.Errorf("environment variable DDNS_DOMAINS has to be set")
}
return nil
+6 -7
View File
@@ -41,8 +41,7 @@ func (h *Handler) ListHosts(c echo.Context) (err error) {
}
return c.Render(http.StatusOK, "listhosts", echo.Map{
"hosts": hosts,
"config": h.Config,
"hosts": hosts,
})
}
@@ -104,7 +103,7 @@ func (h *Handler) CreateHost(c echo.Context) (err error) {
return c.JSON(http.StatusBadRequest, &Error{fmt.Sprintf("ip %s is not a valid ip", host.Ip)})
}
if err = h.updateRecord(host.Hostname, host.Ip, ipType, host.Ttl); err != nil {
if err = h.updateRecord(host.Hostname, host.Ip, ipType, host.Domain, host.Ttl); err != nil {
return c.JSON(http.StatusBadRequest, &Error{err.Error()})
}
}
@@ -148,7 +147,7 @@ func (h *Handler) UpdateHost(c echo.Context) (err error) {
return c.JSON(http.StatusBadRequest, &Error{fmt.Sprintf("ip %s is not a valid ip", host.Ip)})
}
if err = h.updateRecord(host.Hostname, host.Ip, ipType, host.Ttl); err != nil {
if err = h.updateRecord(host.Hostname, host.Ip, ipType, host.Domain, host.Ttl); err != nil {
return c.JSON(http.StatusBadRequest, &Error{err.Error()})
}
}
@@ -186,7 +185,7 @@ func (h *Handler) DeleteHost(c echo.Context) (err error) {
return c.JSON(http.StatusBadRequest, &Error{err.Error()})
}
if err = h.deleteRecord(host.Hostname); err != nil {
if err = h.deleteRecord(host.Hostname, host.Domain); err != nil {
return c.JSON(http.StatusBadRequest, &Error{err.Error()})
}
@@ -216,7 +215,7 @@ func (h *Handler) UpdateIP(c echo.Context) (err error) {
// Validate hostname
hostname := c.QueryParam("hostname")
if hostname == "" || hostname != h.AuthHost.Hostname+"."+h.Config.Domain {
if hostname == "" || hostname != h.AuthHost.Hostname+"."+h.AuthHost.Domain {
if err = h.CreateLogEntry(log); err != nil {
fmt.Println(err)
}
@@ -239,7 +238,7 @@ func (h *Handler) UpdateIP(c echo.Context) (err error) {
}
// add/update DNS record
if err = h.updateRecord(log.Host.Hostname, log.SentIP, ipType, log.Host.Ttl); err != nil {
if err = h.updateRecord(log.Host.Hostname, log.SentIP, ipType, log.Host.Domain, log.Host.Ttl); err != nil {
if err = h.CreateLogEntry(log); err != nil {
fmt.Println(err)
}
+1 -2
View File
@@ -47,7 +47,6 @@ func (h *Handler) ShowHostLogs(c echo.Context) (err error) {
}
return c.Render(http.StatusOK, "listlogs", echo.Map{
"logs": logs,
"config": h.Config,
"logs": logs,
})
}
+7 -7
View File
@@ -14,7 +14,7 @@ import (
"strings"
)
func (h *Handler) updateRecord(hostname string, ipAddr string, addrType string, ttl int) error {
func (h *Handler) updateRecord(hostname string, ipAddr string, addrType string, zone string, ttl int) error {
fmt.Printf("%s record update request: %s -> %s\n", addrType, hostname, ipAddr)
f, err := ioutil.TempFile(os.TempDir(), "dyndns")
@@ -26,9 +26,9 @@ func (h *Handler) updateRecord(hostname string, ipAddr string, addrType string,
w := bufio.NewWriter(f)
w.WriteString(fmt.Sprintf("server %s\n", "localhost"))
w.WriteString(fmt.Sprintf("zone %s\n", h.Config.Domain))
w.WriteString(fmt.Sprintf("update delete %s.%s %s\n", hostname, h.Config.Domain, addrType))
w.WriteString(fmt.Sprintf("update add %s.%s %v %s %s\n", hostname, h.Config.Domain, ttl, addrType, ipAddr))
w.WriteString(fmt.Sprintf("zone %s\n", zone))
w.WriteString(fmt.Sprintf("update delete %s.%s %s\n", hostname, zone, addrType))
w.WriteString(fmt.Sprintf("update add %s.%s %v %s %s\n", hostname, zone, ttl, addrType, ipAddr))
w.WriteString("send\n")
w.Flush()
@@ -51,7 +51,7 @@ func (h *Handler) updateRecord(hostname string, ipAddr string, addrType string,
return nil
}
func (h *Handler) deleteRecord(hostname string) error {
func (h *Handler) deleteRecord(hostname string, zone string) error {
fmt.Printf("record delete request: %s\n", hostname)
f, err := ioutil.TempFile(os.TempDir(), "dyndns")
@@ -63,8 +63,8 @@ func (h *Handler) deleteRecord(hostname string) error {
w := bufio.NewWriter(f)
w.WriteString(fmt.Sprintf("server %s\n", "localhost"))
w.WriteString(fmt.Sprintf("zone %s\n", h.Config.Domain))
w.WriteString(fmt.Sprintf("update delete %s.%s\n", hostname, h.Config.Domain))
w.WriteString(fmt.Sprintf("zone %s\n", zone))
w.WriteString(fmt.Sprintf("update delete %s.%s\n", hostname, zone))
w.WriteString("send\n")
w.Flush()