diff --git a/controllers/domain.go b/controllers/domain.go index be1d321..7ea9194 100644 --- a/controllers/domain.go +++ b/controllers/domain.go @@ -22,7 +22,7 @@ func (c *Controller) CreateDomain(d *models.Domain) (*models.Domain, error) { } r := &models.RecordWithType[dns.SOARecord]{} - r.Zone = d.DomainName + r.Zone = fmt.Sprintf("%s.", d.DomainName) r.Name = "@" r.RecordType = models.RecordTypeSOA r.Content.Ns = d.MainDNS @@ -31,6 +31,9 @@ func (c *Controller) CreateDomain(d *models.Domain) (*models.Domain, error) { r.Content.Retry = d.RetryInterval r.Content.Expire = d.ExpiryPeriod r.Content.MinTtl = d.NegativeTtl + if err := r.CheckZone(); err != nil { + return err + } if err := tx.Create(r.ToRecord()).Error; err != nil { return err @@ -95,6 +98,10 @@ func (c *Controller) UpdateDomain(d *models.Domain) error { r.Content.Expire = d.ExpiryPeriod r.Content.MinTtl = d.NegativeTtl + if err := r.CheckZone(); err != nil { + return err + } + if err := tx.Where("record_type = ?", models.RecordTypeSOA).Where("zone = ?", d.DomainName).Save(r.ToRecord()).Error; err != nil { return err } diff --git a/controllers/record.go b/controllers/record.go index 3e6f5e0..11a289b 100644 --- a/controllers/record.go +++ b/controllers/record.go @@ -19,6 +19,10 @@ func (c *Controller) CreateRecord(r *models.Record) (*models.Record, error) { } } + if err := r.CheckZone(); err != nil { + return nil, err + } + if err := c.DB.Transaction(func(tx *gorm.DB) error { return tx.Create(r).Error }); err != nil { @@ -31,6 +35,10 @@ func (c *Controller) CreateRecord(r *models.Record) (*models.Record, error) { func (c *Controller) CreateRecords(rs []*models.Record) error { return c.DB.Transaction(func(tx *gorm.DB) error { for _, r := range rs { + if err := r.CheckZone(); err != nil { + return err + } + if err := tx.Create(r).Error; err != nil { return err } @@ -50,6 +58,10 @@ func (c *Controller) GetRecords(cond map[string]string) ([]models.Record, error) } func (c *Controller) UpdateRecord(r *models.Record) error { + if err := r.CheckZone(); err != nil { + return err + } + return c.DB.Transaction(func(tx *gorm.DB) error { return tx.Model(r).Updates(r).Error }) diff --git a/models/record.go b/models/record.go index d3cf4b8..d2a6f7b 100644 --- a/models/record.go +++ b/models/record.go @@ -2,6 +2,7 @@ package models import ( "fmt" + "strings" dns "github.com/cloud66-oss/coredns_mysql" ) @@ -31,6 +32,13 @@ func (Record) TableName() string { return "coredns_record" } +func (r *Record) CheckZone() error { + if strings.HasSuffix(r.Zone, ".") { + return fmt.Errorf("zone should end with '.'") + } + return nil +} + type RecordContentTypes interface { dns.ARecord | dns.AAAARecord | dns.CNAMERecord | dns.CAARecord | dns.NSRecord | dns.MXRecord | dns.SOARecord | dns.SRVRecord | dns.TXTRecord }