1
This commit is contained in:
29
controllers/dns.go
Normal file
29
controllers/dns.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"reCoreD-UI/models"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const dnsSep = ","
|
||||
|
||||
func (c *Controller) SetupDNS(dns ...string) error {
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
settings := &models.Settings{}
|
||||
|
||||
return tx.Where(&models.Settings{Key: models.SettingsKeyDNSServer}).
|
||||
Attrs(&models.Settings{Value: strings.Join(dns, dnsSep)}).
|
||||
FirstOrCreate(&settings).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) GetDNS() ([]string, error) {
|
||||
settings := &models.Settings{}
|
||||
if err := c.DB.Where(&models.Settings{Key: models.SettingsKeyDNSServer}).Find(&settings).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strings.Split(settings.Value, dnsSep), nil
|
||||
}
|
108
controllers/domain.go
Normal file
108
controllers/domain.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"reCoreD-UI/models"
|
||||
"strconv"
|
||||
|
||||
dns "github.com/cloud66-oss/coredns_mysql"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (c *Controller) CreateDomain(d *models.Domain) error {
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(d).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := &models.RecordWithType[dns.SOARecord]{}
|
||||
r.Zone = d.DomainName
|
||||
r.Name = "@"
|
||||
r.RecordType = models.RecordTypeSOA
|
||||
r.Content.Ns = d.MainDNS
|
||||
r.Content.MBox = d.EmailSOAForamt()
|
||||
r.Content.Refresh = d.RefreshInterval
|
||||
r.Content.Retry = d.RetryInterval
|
||||
r.Content.Expire = d.ExpiryPeriod
|
||||
r.Content.MinTtl = d.NegativeTtl
|
||||
|
||||
if err := tx.Create(r.ToRecord()).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) GetDomains(domain string) ([]models.Domain, error) {
|
||||
var domains []models.Domain
|
||||
|
||||
tx := c.DB
|
||||
|
||||
if domain != "" {
|
||||
tx = tx.Where(&models.Domain{DomainName: domain})
|
||||
}
|
||||
|
||||
if err := tx.Find(&domains).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return domains, nil
|
||||
}
|
||||
|
||||
func (c *Controller) UpdateDomain(d *models.Domain) error {
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(d).Updates(d).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
record := &models.Record{}
|
||||
if err := tx.Where("record_type = ?", models.RecordTypeSOA).Where("zone = ?", d.DomainName).First(record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := &models.RecordWithType[dns.SOARecord]{}
|
||||
if err := r.FromRecord(record); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.Content.Ns = d.MainDNS
|
||||
r.Content.MBox = d.EmailSOAForamt()
|
||||
r.Content.Refresh = d.RefreshInterval
|
||||
r.Content.Retry = d.RetryInterval
|
||||
r.Content.Expire = d.ExpiryPeriod
|
||||
r.Content.MinTtl = d.NegativeTtl
|
||||
|
||||
if err := tx.Where("record_type = ?", models.RecordTypeSOA).Where("zone = ?", d.DomainName).Save(r.ToRecord()).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) DeleteDomain(id string) error {
|
||||
ID, err := strconv.Atoi(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
domain := &models.Domain{
|
||||
ID: ID,
|
||||
}
|
||||
if err := tx.First(&domain).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where("zone = ?", domain.DomainName).Delete(&models.Record{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Delete(&domain).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
27
controllers/init.go
Normal file
27
controllers/init.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"reCoreD-UI/database"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewController(DSN string) (*Controller, error) {
|
||||
db, err := database.Connect(DSN)
|
||||
return &Controller{
|
||||
DB: db,
|
||||
}, err
|
||||
}
|
||||
|
||||
func (c *Controller) Close() error {
|
||||
d, err := c.DB.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return d.Close()
|
||||
}
|
11
controllers/migrate.go
Normal file
11
controllers/migrate.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package controllers
|
||||
|
||||
import "reCoreD-UI/models"
|
||||
|
||||
func (c *Controller) Migrate() error {
|
||||
return c.DB.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(
|
||||
&models.Domain{},
|
||||
&models.Record{},
|
||||
&models.Settings{},
|
||||
)
|
||||
}
|
83
controllers/record.go
Normal file
83
controllers/record.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reCoreD-UI/models"
|
||||
|
||||
dns "github.com/cloud66-oss/coredns_mysql"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (c *Controller) SetupNSRecord(domain *models.Domain) error {
|
||||
nss, err := c.GetDNS()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
for i, ns := range nss {
|
||||
record := &models.RecordWithType[dns.NSRecord]{}
|
||||
record.Zone = domain.DomainName
|
||||
record.RecordType = models.RecordTypeNS
|
||||
record.Content.Host = ns
|
||||
record.Name = fmt.Sprintf("ns%d", i+1)
|
||||
|
||||
if err := tx.Create(record.ToRecord()).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) CreateRecord(r *models.Record) error {
|
||||
if r.RecordType != models.RecordTypeSOA {
|
||||
domains, err := c.GetDomains(r.Zone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(domains) == 0 || domains[0].DomainName == r.Zone {
|
||||
return fmt.Errorf("no such domain")
|
||||
}
|
||||
}
|
||||
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Create(r).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) CreateRecords(rs []*models.Record) error {
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
for _, r := range rs {
|
||||
if err := tx.Create(r).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) GetRecords(cond map[string]any) ([]models.Record, error) {
|
||||
var records []models.Record
|
||||
|
||||
if err := c.DB.Where(cond).Find(&records).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (c *Controller) UpdateRecord(r *models.Record) error {
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Model(r).Updates(r).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) DeleteRecord(id string) error {
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Where("record_type != ?", models.RecordTypeSOA).Where("id = ?", id).Delete(&models.Record{}).Error
|
||||
})
|
||||
}
|
41
controllers/user.go
Normal file
41
controllers/user.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"reCoreD-UI/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (c *Controller) SetupAdmin(username, password string) error {
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
settings := &models.Settings{}
|
||||
|
||||
if err := tx.Where(&models.Settings{Key: models.SettingsKeyAdminUsername}).
|
||||
Attrs(&models.Settings{Value: username}).
|
||||
FirstOrCreate(settings).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where(&models.Settings{Key: models.SettingsKeyAdminPassword}).
|
||||
Attrs(&models.Settings{Value: password}).
|
||||
FirstOrCreate(settings).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) GetAdmin() (string, string, error) {
|
||||
settings := &models.Settings{}
|
||||
if err := c.DB.Where(&models.Settings{Key: models.SettingsKeyAdminUsername}).First(settings).Error; err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
username := settings.Value
|
||||
|
||||
if err := c.DB.Where(&models.Settings{Key: models.SettingsKeyAdminPassword}).First(settings).Error; err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
password := settings.Value
|
||||
|
||||
return username, password, nil
|
||||
}
|
Reference in New Issue
Block a user