record should endwith .

This commit is contained in:
Sense T 2024-04-09 10:16:06 +08:00
parent c93e8107dc
commit 613ef7fdd9
3 changed files with 28 additions and 1 deletions

View File

@ -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
}

View File

@ -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
})

View File

@ -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
}