2020-11-12 14:41:28 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2020-11-13 18:22:22 +08:00
|
|
|
|
"encoding/json"
|
2020-11-15 11:57:49 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
|
2020-11-12 14:41:28 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
|
|
"github.com/iwind/TeaGo/Tea"
|
|
|
|
|
|
"github.com/iwind/TeaGo/dbs"
|
2020-12-23 16:49:47 +08:00
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2020-11-12 14:41:28 +08:00
|
|
|
|
"github.com/iwind/TeaGo/types"
|
2020-12-23 16:49:47 +08:00
|
|
|
|
"strings"
|
2020-11-14 21:28:07 +08:00
|
|
|
|
"time"
|
2020-11-12 14:41:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
DNSDomainStateEnabled = 1 // 已启用
|
|
|
|
|
|
DNSDomainStateDisabled = 0 // 已禁用
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type DNSDomainDAO dbs.DAO
|
|
|
|
|
|
|
|
|
|
|
|
func NewDNSDomainDAO() *DNSDomainDAO {
|
|
|
|
|
|
return dbs.NewDAO(&DNSDomainDAO{
|
|
|
|
|
|
DAOObject: dbs.DAOObject{
|
|
|
|
|
|
DB: Tea.Env,
|
|
|
|
|
|
Table: "edgeDNSDomains",
|
|
|
|
|
|
Model: new(DNSDomain),
|
|
|
|
|
|
PkName: "id",
|
|
|
|
|
|
},
|
|
|
|
|
|
}).(*DNSDomainDAO)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var SharedDNSDomainDAO *DNSDomainDAO
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
dbs.OnReady(func() {
|
|
|
|
|
|
SharedDNSDomainDAO = NewDNSDomainDAO()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 启用条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) EnableDNSDomain(tx *dbs.Tx, id int64) error {
|
|
|
|
|
|
_, err := this.Query(tx).
|
2020-11-12 14:41:28 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
Set("state", DNSDomainStateEnabled).
|
|
|
|
|
|
Update()
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 禁用条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) DisableDNSDomain(tx *dbs.Tx, id int64) error {
|
|
|
|
|
|
_, err := this.Query(tx).
|
2020-11-12 14:41:28 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
Set("state", DNSDomainStateDisabled).
|
|
|
|
|
|
Update()
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查找启用中的条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, id int64) (*DNSDomain, error) {
|
|
|
|
|
|
result, err := this.Query(tx).
|
2020-11-12 14:41:28 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
Attr("state", DNSDomainStateEnabled).
|
|
|
|
|
|
Find()
|
|
|
|
|
|
if result == nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result.(*DNSDomain), err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据主键查找名称
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) FindDNSDomainName(tx *dbs.Tx, id int64) (string, error) {
|
|
|
|
|
|
return this.Query(tx).
|
2020-11-12 14:41:28 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
Result("name").
|
|
|
|
|
|
FindStringCol("")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建域名
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) CreateDomain(tx *dbs.Tx, adminId int64, userId int64, providerId int64, name string) (int64, error) {
|
2020-11-12 14:41:28 +08:00
|
|
|
|
op := NewDNSDomainOperator()
|
|
|
|
|
|
op.ProviderId = providerId
|
2020-11-27 09:57:21 +08:00
|
|
|
|
op.AdminId = adminId
|
|
|
|
|
|
op.UserId = userId
|
2020-11-12 14:41:28 +08:00
|
|
|
|
op.Name = name
|
|
|
|
|
|
op.State = DNSDomainStateEnabled
|
|
|
|
|
|
op.IsOn = true
|
2021-01-01 23:31:30 +08:00
|
|
|
|
err := this.Save(tx, op)
|
2020-11-12 14:41:28 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return types.Int64(op.Id), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 修改域名
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, name string, isOn bool) error {
|
2020-11-12 14:41:28 +08:00
|
|
|
|
if domainId <= 0 {
|
|
|
|
|
|
return errors.New("invalid domainId")
|
|
|
|
|
|
}
|
|
|
|
|
|
op := NewDNSDomainOperator()
|
|
|
|
|
|
op.Id = domainId
|
|
|
|
|
|
op.Name = name
|
|
|
|
|
|
op.IsOn = isOn
|
2021-01-01 23:31:30 +08:00
|
|
|
|
err := this.Save(tx, op)
|
2020-11-12 14:41:28 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查询一个服务商下面的所有域名
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (result []*DNSDomain, err error) {
|
|
|
|
|
|
_, err = this.Query(tx).
|
2020-11-12 14:41:28 +08:00
|
|
|
|
State(DNSDomainStateEnabled).
|
|
|
|
|
|
Attr("providerId", providerId).
|
|
|
|
|
|
AscPk().
|
|
|
|
|
|
Slice(&result).
|
|
|
|
|
|
FindAll()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 计算某个服务商下的域名数量
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (int64, error) {
|
|
|
|
|
|
return this.Query(tx).
|
2020-11-12 14:41:28 +08:00
|
|
|
|
State(DNSDomainStateEnabled).
|
|
|
|
|
|
Attr("providerId", providerId).
|
|
|
|
|
|
Count()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新域名数据
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) UpdateDomainData(tx *dbs.Tx, domainId int64, data string) error {
|
2020-11-12 14:41:28 +08:00
|
|
|
|
if domainId <= 0 {
|
|
|
|
|
|
return errors.New("invalid domainId")
|
|
|
|
|
|
}
|
|
|
|
|
|
op := NewDNSDomainOperator()
|
|
|
|
|
|
op.Id = domainId
|
|
|
|
|
|
op.Data = data
|
2021-01-01 23:31:30 +08:00
|
|
|
|
err := this.Save(tx, op)
|
2020-11-12 14:41:28 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-13 18:22:22 +08:00
|
|
|
|
// 更新域名解析记录
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) UpdateDomainRecords(tx *dbs.Tx, domainId int64, recordsJSON []byte) error {
|
2020-11-12 14:41:28 +08:00
|
|
|
|
if domainId <= 0 {
|
|
|
|
|
|
return errors.New("invalid domainId")
|
|
|
|
|
|
}
|
|
|
|
|
|
op := NewDNSDomainOperator()
|
|
|
|
|
|
op.Id = domainId
|
2020-11-13 18:22:22 +08:00
|
|
|
|
op.Records = recordsJSON
|
2020-11-14 21:28:07 +08:00
|
|
|
|
op.DataUpdatedAt = time.Now().Unix()
|
2021-01-01 23:31:30 +08:00
|
|
|
|
err := this.Save(tx, op)
|
2020-11-12 14:41:28 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-13 18:22:22 +08:00
|
|
|
|
// 更新线路
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) UpdateDomainRoutes(tx *dbs.Tx, domainId int64, routesJSON []byte) error {
|
2020-11-12 14:41:28 +08:00
|
|
|
|
if domainId <= 0 {
|
|
|
|
|
|
return errors.New("invalid domainId")
|
|
|
|
|
|
}
|
|
|
|
|
|
op := NewDNSDomainOperator()
|
|
|
|
|
|
op.Id = domainId
|
2020-11-13 18:22:22 +08:00
|
|
|
|
op.Routes = routesJSON
|
2020-11-14 21:28:07 +08:00
|
|
|
|
op.DataUpdatedAt = time.Now().Unix()
|
2021-01-01 23:31:30 +08:00
|
|
|
|
err := this.Save(tx, op)
|
2020-11-12 14:41:28 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-13 18:22:22 +08:00
|
|
|
|
// 查找域名线路
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) FindDomainRoutes(tx *dbs.Tx, domainId int64) ([]*dnsclients.Route, error) {
|
|
|
|
|
|
routes, err := this.Query(tx).
|
2020-11-13 18:22:22 +08:00
|
|
|
|
Pk(domainId).
|
|
|
|
|
|
Result("routes").
|
|
|
|
|
|
FindStringCol("")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2020-11-12 14:41:28 +08:00
|
|
|
|
}
|
2020-11-13 18:22:22 +08:00
|
|
|
|
if len(routes) == 0 || routes == "null" {
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
2020-11-15 11:57:49 +08:00
|
|
|
|
result := []*dnsclients.Route{}
|
2020-11-13 18:22:22 +08:00
|
|
|
|
err = json.Unmarshal([]byte(routes), &result)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result, nil
|
2020-11-12 14:41:28 +08:00
|
|
|
|
}
|
2020-11-15 11:57:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 查找线路名称
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) FindDomainRouteName(tx *dbs.Tx, domainId int64, routeCode string) (string, error) {
|
|
|
|
|
|
routes, err := this.FindDomainRoutes(tx, domainId)
|
2020-11-15 11:57:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
|
if route.Code == routeCode {
|
|
|
|
|
|
return route.Name, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return "", nil
|
|
|
|
|
|
}
|
2020-11-15 16:28:29 +08:00
|
|
|
|
|
|
|
|
|
|
// 判断是否有域名可选
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
|
|
|
|
|
|
subQuery, err := SharedDNSProviderDAO.Query(tx).
|
2020-11-15 16:28:29 +08:00
|
|
|
|
Where("state=1"). // 这里要使用非变量
|
|
|
|
|
|
ResultPk().
|
|
|
|
|
|
AsSQL()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
2021-01-01 23:31:30 +08:00
|
|
|
|
return this.Query(tx).
|
2020-11-15 16:28:29 +08:00
|
|
|
|
State(DNSDomainStateEnabled).
|
|
|
|
|
|
Attr("isOn", true).
|
|
|
|
|
|
Where("providerId IN (" + subQuery + ")").
|
|
|
|
|
|
Exist()
|
|
|
|
|
|
}
|
2020-12-23 16:49:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查域名解析记录是否存在
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordName string, recordType string, recordRoute string, recordValue string) (bool, error) {
|
2020-12-23 16:49:47 +08:00
|
|
|
|
query := maps.Map{
|
|
|
|
|
|
"name": recordName,
|
|
|
|
|
|
"type": recordType,
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(recordRoute) > 0 {
|
|
|
|
|
|
query["route"] = recordRoute
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(recordValue) > 0 {
|
|
|
|
|
|
query["value"] = recordValue
|
|
|
|
|
|
|
|
|
|
|
|
// CNAME兼容点(.)符号
|
|
|
|
|
|
if recordType == "CNAME" && !strings.HasSuffix(recordValue, ".") {
|
2021-01-01 23:31:30 +08:00
|
|
|
|
b, err := this.ExistDomainRecord(tx, domainId, recordName, recordType, recordRoute, recordValue+".")
|
2020-12-23 16:49:47 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if b {
|
|
|
|
|
|
return true, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
recordType = strings.ToUpper(recordType)
|
2021-01-01 23:31:30 +08:00
|
|
|
|
return this.Query(tx).
|
2020-12-23 16:49:47 +08:00
|
|
|
|
Pk(domainId).
|
|
|
|
|
|
Where("JSON_CONTAINS(records, :query)").
|
|
|
|
|
|
Param("query", query.AsJSON()).
|
|
|
|
|
|
Exist()
|
|
|
|
|
|
}
|