diff --git a/dyndns/handler/handler.go b/dyndns/handler/handler.go index 6d0842c..22723d9 100644 --- a/dyndns/handler/handler.go +++ b/dyndns/handler/handler.go @@ -116,6 +116,10 @@ func (h *Handler) InitDB() (err error) { h.DB.CreateTable(&model.Host{}) } + if !h.DB.HasTable(&model.CName{}) { + h.DB.CreateTable(&model.CName{}) + } + if !h.DB.HasTable(&model.Log{}) { h.DB.CreateTable(&model.Log{}) } diff --git a/dyndns/handler/host.go b/dyndns/handler/host.go index f0ef076..ce66716 100644 --- a/dyndns/handler/host.go +++ b/dyndns/handler/host.go @@ -106,6 +106,10 @@ func (h *Handler) CreateHost(c echo.Context) (err error) { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } + if err = h.checkUniqueHostname(host.Hostname, host.Domain); err != nil { + return c.JSON(http.StatusBadRequest, &Error{err.Error()}) + } + if err = h.DB.Create(host).Error; err != nil { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } @@ -198,6 +202,10 @@ func (h *Handler) DeleteHost(c echo.Context) (err error) { return c.JSON(http.StatusBadRequest, &Error{err.Error()}) } + if err = tx.Where(&model.CName{TargetID: uint(id)}).Delete(&model.CName{}).Error; err != nil { + return c.JSON(http.StatusBadRequest, &Error{err.Error()}) + } + return nil }) if err != nil { @@ -282,3 +290,28 @@ func (h *Handler) UpdateIP(c echo.Context) (err error) { return c.String(http.StatusOK, "good\n") } + +func (h *Handler) checkUniqueHostname(hostname, domain string) error { + fmt.Println(hostname, domain) + hosts := new([]model.Host) + if err := h.DB.Where(&model.Host{Hostname: hostname, Domain: domain}).Find(hosts).Error; err != nil { + return err + } + + if len(*hosts) > 0 { + return fmt.Errorf("hostname already exists") + } + + cnames := new([]model.CName) + if err := h.DB.Preload("Target").Where(&model.CName{Hostname: hostname}).Find(cnames).Error; err != nil { + return err + } + + for _, cname := range *cnames { + if cname.Target.Domain == domain { + return fmt.Errorf("hostname already exists") + } + } + + return nil +} diff --git a/dyndns/main.go b/dyndns/main.go index f5c6e7a..cd08fcf 100644 --- a/dyndns/main.go +++ b/dyndns/main.go @@ -6,7 +6,6 @@ import ( "github.com/benjaminbear/docker-ddns-server/dyndns/handler" "github.com/foolin/goview/supports/echoview-v4" "github.com/go-playground/validator/v10" - _ "github.com/jinzhu/gorm/dialects/sqlite" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -53,6 +52,8 @@ func main() { e.GET("/hosts/add", h.AddHost) e.GET("/hosts/edit/:id", h.EditHost) e.GET("/hosts", h.ListHosts) + e.GET("/cnames/add", h.AddCName) + e.GET("/cnames", h.ListCNames) e.GET("/logs", h.ShowLogs) e.GET("/logs/host/:id", h.ShowHostLogs) @@ -60,6 +61,8 @@ func main() { e.POST("/hosts/add", h.CreateHost) e.POST("/hosts/edit/:id", h.UpdateHost) e.GET("/hosts/delete/:id", h.DeleteHost) + e.POST("/cnames/add", h.CreateCName) + e.GET("/cnames/delete/:id", h.DeleteCName) // dyndns compatible api e.GET("/update", h.UpdateIP) diff --git a/dyndns/nswrapper/update.go b/dyndns/nswrapper/update.go index c30cb97..c0287c6 100644 --- a/dyndns/nswrapper/update.go +++ b/dyndns/nswrapper/update.go @@ -10,8 +10,8 @@ import ( ) // UpdateRecord builds a nsupdate file and updates a record by executing it with nsupdate. -func UpdateRecord(hostname string, ipAddr string, addrType string, zone string, ttl int) error { - fmt.Printf("%s record update request: %s -> %s\n", addrType, hostname, ipAddr) +func UpdateRecord(hostname string, target string, addrType string, zone string, ttl int) error { + fmt.Printf("%s record update request: %s -> %s\n", addrType, hostname, target) f, err := ioutil.TempFile(os.TempDir(), "dyndns") if err != nil { @@ -24,7 +24,7 @@ func UpdateRecord(hostname string, ipAddr 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)) - w.WriteString(fmt.Sprintf("update add %s.%s %v %s %s\n", hostname, zone, ttl, addrType, ipAddr)) + w.WriteString(fmt.Sprintf("update add %s.%s %v %s %s\n", hostname, zone, ttl, addrType, target)) w.WriteString("send\n") w.Flush() diff --git a/dyndns/static/js/actions.js b/dyndns/static/js/actions.js index 318c87e..ad14ca2 100644 --- a/dyndns/static/js/actions.js +++ b/dyndns/static/js/actions.js @@ -38,15 +38,24 @@ $("button.add, button.edit").click(function () { action = "edit"; } + let type; + if ($(this).hasClass("host")) { + type = "hosts"; + } + + if ($(this).hasClass("cname")) { + type = "cnames"; + } + $('#domain').prop('disabled', false); $.ajax({ contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: $('#editHostForm').serialize(), type: 'POST', - url: '/hosts/'+action+id, + url: '/'+type+'/'+action+id, }).done(function(data, textStatus, jqXHR) { - location.href="/hosts"; + location.href="/"+type; }).fail(function(jqXHR, textStatus, errorThrown) { alert("Error: " + $.parseJSON(jqXHR.responseText).message); }); @@ -79,6 +88,29 @@ $("#logout").click(function (){ } }); +$("button.addCName").click(function () { + location.href='/cnames/add'; +}); + +$("button.deleteCName").click(function () { + $.ajax({ + contentType: 'application/x-www-form-urlencoded; charset=UTF-8', + type: 'GET', + url: "/cnames/delete/" + $(this).attr('id') + }).done(function(data, textStatus, jqXHR) { + location.href="/cnames"; + }).fail(function(jqXHR, textStatus, errorThrown) { + alert("Error: " + $.parseJSON(jqXHR.responseText).message); + location.reload() + }); +}); + +function newTargetSelected() { + var sel = document.getElementById("target_id"); + var x = sel.options[sel.selectedIndex].label.replace(sel.options[sel.selectedIndex].text, ''); + document.getElementById("domain_mirror").value = x; +} + $("button.copyToClipboard").click(function () { let id; if ($(this).hasClass('username')) { @@ -122,4 +154,10 @@ $(document).ready(function(){ return $(this).prop('title'); } }); + + urlPath = new URL(window.location.href).pathname.split("/")[1]; + if (urlPath === "") { + urlPath = "hosts" + } + document.getElementsByClassName("nav-"+urlPath)[0].classList.add("active"); }); diff --git a/dyndns/views/edithost.html b/dyndns/views/edithost.html index 792dc10..cdd43e5 100644 --- a/dyndns/views/edithost.html +++ b/dyndns/views/edithost.html @@ -68,7 +68,7 @@
-
+
diff --git a/dyndns/views/layouts/master.html b/dyndns/views/layouts/master.html index 36be9f5..29fe6fe 100644 --- a/dyndns/views/layouts/master.html +++ b/dyndns/views/layouts/master.html @@ -27,13 +27,16 @@ @@ -44,7 +47,7 @@ {{template "content" .}}