reCoreD-UI/controllers/domain.go

156 lines
3.1 KiB
Go
Raw Normal View History

2024-04-03 09:05:12 +00:00
package controllers
import (
2024-04-03 14:37:15 +00:00
"fmt"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/database"
2024-04-03 09:05:12 +00:00
"reCoreD-UI/models"
"strconv"
2024-04-19 01:44:37 +00:00
"github.com/sirupsen/logrus"
2024-04-03 09:05:12 +00:00
)
2024-04-09 08:28:18 +00:00
type domainsDAO struct {
2024-04-11 02:51:50 +00:00
database.BaseDAO[models.IDomain]
2024-04-09 08:28:18 +00:00
}
func CreateDomain(d *models.Domain) (*models.Domain, error) {
nss, err := GetDNS()
2024-04-03 14:37:15 +00:00
if err != nil {
2024-04-07 05:08:30 +00:00
return nil, err
2024-04-03 14:37:15 +00:00
}
2024-04-09 08:28:18 +00:00
tx := database.Client.Begin()
2024-04-11 02:51:50 +00:00
if _, err := (domainsDAO{}).Create(tx, d); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return nil, err
}
2024-04-03 09:05:12 +00:00
2024-04-13 02:14:45 +00:00
r := &models.Record[models.SOARecord]{}
2024-04-09 08:28:18 +00:00
r.Zone = d.WithDotEnd()
r.Name = "@"
r.RecordType = models.RecordTypeSOA
2024-04-09 13:16:19 +00:00
r.Content = d.GenerateSOA()
2024-04-19 01:44:37 +00:00
logrus.Debug(r)
2024-04-09 08:28:18 +00:00
if err := r.CheckZone(); err != nil {
tx.Rollback()
return nil, err
}
2024-04-03 09:05:12 +00:00
2024-04-09 13:16:19 +00:00
if _, err := (recordsDAO{}).Create(tx, r); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return nil, err
}
2024-04-03 09:05:12 +00:00
2024-04-09 08:28:18 +00:00
for i, ns := range nss {
2024-04-13 02:14:45 +00:00
record := &models.Record[models.NSRecord]{
2024-04-09 13:16:19 +00:00
Zone: d.WithDotEnd(),
2024-04-19 01:44:37 +00:00
RecordType: models.RecordTypeNS,
2024-04-09 13:16:19 +00:00
Name: fmt.Sprintf("ns%d", i+1),
}
2024-04-09 08:28:18 +00:00
record.Content.Host = ns
2024-04-03 14:37:15 +00:00
2024-04-09 13:16:19 +00:00
if _, err := (recordsDAO{}).Create(tx, record); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return nil, err
2024-04-03 14:37:15 +00:00
}
2024-04-07 05:08:30 +00:00
}
2024-04-11 02:51:50 +00:00
return d, tx.Commit().Error
2024-04-03 09:05:12 +00:00
}
2024-04-09 08:28:18 +00:00
func GetDomains(domain string) ([]models.Domain, error) {
2024-04-03 09:05:12 +00:00
if domain != "" {
2024-04-11 03:41:33 +00:00
r, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{DomainName: domain})
2024-04-11 02:51:50 +00:00
n := make([]models.Domain, 0)
for _, e := range r {
2024-04-11 03:41:33 +00:00
i, ok := e.(*models.Domain)
if !ok {
continue
}
n = append(n, *i)
2024-04-11 02:51:50 +00:00
}
return n, err
2024-04-09 08:28:18 +00:00
} else {
2024-04-11 03:41:33 +00:00
r, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{})
2024-04-11 02:51:50 +00:00
n := make([]models.Domain, 0)
for _, e := range r {
2024-04-11 03:41:33 +00:00
i, ok := e.(*models.Domain)
if !ok {
continue
}
n = append(n, *i)
2024-04-11 02:51:50 +00:00
}
return n, err
2024-04-03 09:05:12 +00:00
}
}
2024-04-09 08:28:18 +00:00
func UpdateDomain(d *models.Domain) error {
tx := database.Client.Begin()
2024-04-11 02:51:50 +00:00
if _, err := (domainsDAO{}).Update(tx, d); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-09 13:16:19 +00:00
soa, err := (recordsDAO{}).GetOne(tx, &models.Record[models.RecordContentDefault]{
2024-04-09 08:28:18 +00:00
RecordType: models.RecordTypeSOA, Zone: d.WithDotEnd(),
})
if err != nil {
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-13 02:14:45 +00:00
r := &models.Record[models.SOARecord]{}
2024-04-09 13:16:19 +00:00
if err := r.FromEntity(soa); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-09 13:16:19 +00:00
r.Content = d.GenerateSOA()
2024-04-09 08:28:18 +00:00
if err := r.CheckZone(); err != nil {
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-09 13:16:19 +00:00
if _, err := (recordsDAO{}).Update(tx, r); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-09 02:16:06 +00:00
2024-04-11 02:51:50 +00:00
return tx.Commit().Error
2024-04-03 09:05:12 +00:00
}
2024-04-09 08:28:18 +00:00
func DeleteDomain(id string) error {
2024-04-03 09:05:12 +00:00
ID, err := strconv.Atoi(id)
if err != nil {
return err
}
2024-04-09 08:28:18 +00:00
tx := database.Client.Begin()
2024-04-19 01:44:37 +00:00
domain, err := (domainsDAO{}).GetOne(tx, &models.Domain{ID: uint(ID)})
2024-04-09 08:28:18 +00:00
if err != nil {
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-19 01:44:37 +00:00
if err := (domainsDAO{}).Delete(tx, &models.Domain{ID: uint(ID)}); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-09 13:16:19 +00:00
if err := (recordsDAO{}).Delete(tx, &models.Record[models.RecordContentDefault]{Zone: domain.WithDotEnd()}); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-11 02:51:50 +00:00
return tx.Commit().Error
2024-04-03 09:05:12 +00:00
}
2024-04-07 02:07:26 +00:00
2024-04-09 08:28:18 +00:00
// for metrics
func getDomainCounts() (float64, error) {
2024-04-11 03:41:33 +00:00
c, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{})
2024-04-09 08:28:18 +00:00
if err != nil {
2024-04-07 02:07:26 +00:00
return 0, err
}
2024-04-09 08:28:18 +00:00
return float64(len(c)), nil
2024-04-07 02:07:26 +00:00
}