diff --git a/dyndns/handler/cname.go b/dyndns/handler/cname.go index 7c1b0ed..de85f0a 100644 --- a/dyndns/handler/cname.go +++ b/dyndns/handler/cname.go @@ -79,7 +79,7 @@ func (h *Handler) CreateCName(c echo.Context) (err error) { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } - if err = nswrapper.UpdateRecord(cname.Hostname, fmt.Sprintf("%s.%s", cname.Target.Hostname, cname.Target.Domain), "CNAME", cname.Target.Domain, cname.Ttl); err != nil { + if err = nswrapper.UpdateRecord(cname.Hostname, fmt.Sprintf("%s.%s", cname.Target.Hostname, cname.Target.Domain), "CNAME", cname.Target.Domain, cname.Ttl, h.AllowWildcard); err != nil { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } @@ -114,7 +114,7 @@ func (h *Handler) DeleteCName(c echo.Context) (err error) { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } - if err = nswrapper.DeleteRecord(cname.Hostname, cname.Target.Domain); err != nil { + if err = nswrapper.DeleteRecord(cname.Hostname, cname.Target.Domain, h.AllowWildcard); err != nil { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } diff --git a/dyndns/handler/handler.go b/dyndns/handler/handler.go index 9b32ca4..fd9305e 100644 --- a/dyndns/handler/handler.go +++ b/dyndns/handler/handler.go @@ -24,6 +24,7 @@ type Handler struct { DisableAdminAuth bool LastClearedLogs time.Time ClearInterval uint64 + AllowWildcard bool } type Envs struct { @@ -112,17 +113,24 @@ func (h *Handler) ParseEnvs() (adminAuth bool, err error) { h.AuthAdmin = true h.DisableAdminAuth = true } - h.Title = os.Getenv("DDNS_TITLE") - if h.Title == "" { + var ok bool + h.Title, ok = os.LookupEnv("DDNS_TITLE") + if !ok { h.Title = "TheBBCloud DynDNS" } - + allowWildcard, ok := os.LookupEnv("DDNS_ALLOW_WILDCARD") + if ok { + h.AllowWildcard, err = strconv.ParseBool(allowWildcard) + if err == nil { + log.Info("Wildcard allowed") + } + } clearEnv := os.Getenv("DDNS_CLEAR_LOG_INTERVAL") clearInterval, err := strconv.ParseUint(clearEnv, 10, 32) if err != nil { log.Info("No log clear interval found") } else { - log.Info("log clear interval found:", clearInterval, "days") + log.Info("log clear interval found: ", clearInterval, "days") h.ClearInterval = clearInterval if clearInterval > 0 { h.LastClearedLogs = time.Now() diff --git a/dyndns/handler/host.go b/dyndns/handler/host.go index 3a5919f..b6d0e86 100644 --- a/dyndns/handler/host.go +++ b/dyndns/handler/host.go @@ -125,7 +125,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 = nswrapper.UpdateRecord(host.Hostname, host.Ip, ipType, host.Domain, host.Ttl); err != nil { + if err = nswrapper.UpdateRecord(host.Hostname, host.Ip, ipType, host.Domain, host.Ttl, h.AllowWildcard); err != nil { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } } @@ -172,7 +172,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 = nswrapper.UpdateRecord(host.Hostname, host.Ip, ipType, host.Domain, host.Ttl); err != nil { + if err = nswrapper.UpdateRecord(host.Hostname, host.Ip, ipType, host.Domain, host.Ttl, h.AllowWildcard); err != nil { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } } @@ -216,7 +216,7 @@ func (h *Handler) DeleteHost(c echo.Context) (err error) { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } - if err = nswrapper.DeleteRecord(host.Hostname, host.Domain); err != nil { + if err = nswrapper.DeleteRecord(host.Hostname, host.Domain, h.AllowWildcard); err != nil { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } @@ -276,7 +276,7 @@ func (h *Handler) UpdateIP(c echo.Context) (err error) { } // add/update DNS record - if err = nswrapper.UpdateRecord(log.Host.Hostname, log.SentIP, ipType, log.Host.Domain, log.Host.Ttl); err != nil { + if err = nswrapper.UpdateRecord(log.Host.Hostname, log.SentIP, ipType, log.Host.Domain, log.Host.Ttl, h.AllowWildcard); err != nil { log.Message = fmt.Sprintf("DNS error: %v", err) l.Error(log.Message) if err = h.CreateLogEntry(log); err != nil { diff --git a/dyndns/nswrapper/update.go b/dyndns/nswrapper/update.go index 3ccd56f..cf6e469 100644 --- a/dyndns/nswrapper/update.go +++ b/dyndns/nswrapper/update.go @@ -11,7 +11,7 @@ import ( ) // UpdateRecord builds a nsupdate file and updates a record by executing it with nsupdate. -func UpdateRecord(hostname string, target string, addrType string, zone string, ttl int) error { +func UpdateRecord(hostname string, target string, addrType string, zone string, ttl int, enableWildcard bool) error { log.Info(fmt.Sprintf("%s record update request: %s -> %s", addrType, hostname, target)) f, err := ioutil.TempFile(os.TempDir(), "dyndns") @@ -25,7 +25,13 @@ func UpdateRecord(hostname string, target string, addrType string, zone string, w.WriteString(fmt.Sprintf("server %s\n", "localhost")) w.WriteString(fmt.Sprintf("zone %s\n", zone)) w.WriteString(fmt.Sprintf("update delete %s.%s %s\n", hostname, zone, addrType)) + if enableWildcard { + 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, target)) + if enableWildcard { + w.WriteString(fmt.Sprintf("update add %s.%s %v %s %s\n", "*."+hostname, zone, ttl, addrType, target)) + } w.WriteString("send\n") w.Flush() @@ -49,7 +55,7 @@ func UpdateRecord(hostname string, target string, addrType string, zone string, } // DeleteRecord builds a nsupdate file and deletes a record by executing it with nsupdate. -func DeleteRecord(hostname string, zone string) error { +func DeleteRecord(hostname string, zone string, enableWildcard bool) error { fmt.Printf("record delete request: %s\n", hostname) f, err := ioutil.TempFile(os.TempDir(), "dyndns") @@ -63,6 +69,9 @@ func DeleteRecord(hostname string, zone string) error { w.WriteString(fmt.Sprintf("server %s\n", "localhost")) w.WriteString(fmt.Sprintf("zone %s\n", zone)) w.WriteString(fmt.Sprintf("update delete %s.%s\n", hostname, zone)) + if enableWildcard { + w.WriteString(fmt.Sprintf("update delete %s.%s\n", "*."+hostname, zone)) + } w.WriteString("send\n") w.Flush()