From aa147a68539d30be9205d9dfca11e8c9b2e07fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=A4rthlein?= Date: Mon, 27 Apr 2020 20:37:22 +0200 Subject: [PATCH] add support for multiple domains --- README.md | 4 ++-- dyndns/handler/handler.go | 8 ++++---- dyndns/handler/host.go | 13 ++++++------- dyndns/handler/log.go | 3 +-- dyndns/handler/update.go | 14 +++++++------- dyndns/model/host.go | 3 ++- dyndns/static/js/actions.js | 2 ++ dyndns/views/edithost.html | 9 ++++++++- dyndns/views/listhosts.html | 2 +- dyndns/views/listlogs.html | 2 +- 10 files changed, 34 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 6efe3e1..2da5604 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ docker run -it -d \ -v /somefolder:/var/cache/bind \ -v /someotherfolder:/root/dyndns/database \ -e DDNS_ADMIN_LOGIN=admin:123455546. \ - -e DDNS_DOMAIN=dyndns.example.com \ + -e DDNS_DOMAINS=dyndns.example.com \ -e DDNS_PARENT_NS=ns.example.com \ -e DDNS_DEFAULT_TTL=3600 \ --name=dyndns \ @@ -54,7 +54,7 @@ If you want to embed this into a docker-compose.yml you have to double the dolla echo $(htpasswd -nb user password) | sed -e s/\\$/\\$\\$/g ``` -`DDNS_DOMAIN` is the domain of the webservice and the domain zone of your dyndns server (see DNS Setup) i.e. `dyndns.example.com` +`DDNS_DOMAINS` are the domains of the webservice and the domain zones of your dyndns server (see DNS Setup) i.e. `dyndns.example.com,dyndns.example.org` (comma separated list) `DDNS_PARENT_NS` is the parent name server of your domain i.e. `ns.example.com` diff --git a/dyndns/handler/handler.go b/dyndns/handler/handler.go index 8b97083..dc390c4 100644 --- a/dyndns/handler/handler.go +++ b/dyndns/handler/handler.go @@ -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 diff --git a/dyndns/handler/host.go b/dyndns/handler/host.go index 2a3b18c..5446749 100644 --- a/dyndns/handler/host.go +++ b/dyndns/handler/host.go @@ -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) } diff --git a/dyndns/handler/log.go b/dyndns/handler/log.go index 9b5b167..e71564a 100644 --- a/dyndns/handler/log.go +++ b/dyndns/handler/log.go @@ -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, }) } diff --git a/dyndns/handler/update.go b/dyndns/handler/update.go index a7d7cd4..7ccb064 100644 --- a/dyndns/handler/update.go +++ b/dyndns/handler/update.go @@ -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() diff --git a/dyndns/model/host.go b/dyndns/model/host.go index 699e673..3145edb 100644 --- a/dyndns/model/host.go +++ b/dyndns/model/host.go @@ -8,7 +8,8 @@ import ( type Host struct { gorm.Model - Hostname string `gorm:"unique;not null" form:"hostname" validate:"required,hostname"` + Hostname string `gorm:"unique_index:idx_host_domain;not null" form:"hostname" validate:"required,hostname"` + Domain string `gorm:"unique_index:idx_host_domain;not null" validate:"required,hostname"` Ip string `form:"ip" validate:"omitempty,ipv4"` Ttl int `form:"ttl" validate:"required,min=20,max=86400"` LastUpdate time.Time `form:"lastupdate"` diff --git a/dyndns/static/js/actions.js b/dyndns/static/js/actions.js index 9e0e44b..48141c4 100644 --- a/dyndns/static/js/actions.js +++ b/dyndns/static/js/actions.js @@ -38,6 +38,8 @@ $("button.add, button.edit").click(function () { action = "edit"; } + $('#domain').prop('disabled', false); + $.ajax({ contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: $('#editHostForm').serialize(), diff --git a/dyndns/views/edithost.html b/dyndns/views/edithost.html index e233877..792dc10 100644 --- a/dyndns/views/edithost.html +++ b/dyndns/views/edithost.html @@ -8,7 +8,14 @@
- .{{.config.Domain}} +
diff --git a/dyndns/views/listhosts.html b/dyndns/views/listhosts.html index 0eb3a2d..6bd7f56 100644 --- a/dyndns/views/listhosts.html +++ b/dyndns/views/listhosts.html @@ -14,7 +14,7 @@ {{range .hosts}} - {{.Hostname}}.{{$.config.Domain}} + {{.Hostname}}.{{.Domain}} {{.Ip}} {{.Ttl}} {{.LastUpdate.Format "01/02/2006 15:04 MEZ"}} diff --git a/dyndns/views/listlogs.html b/dyndns/views/listlogs.html index 1f2a70a..9440aee 100644 --- a/dyndns/views/listlogs.html +++ b/dyndns/views/listlogs.html @@ -16,7 +16,7 @@ {{range .logs}}
- {{.Host.Hostname}}.{{$.config.Domain}} + {{.Host.Hostname}}.{{.Host.Domain}} {{.SentIP}} {{.CreatedAt.Format "01/02/2006 15:04"}} {{.UserAgent}}