mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-01 21:30:27 +08:00
优化代码
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
*_plus.go
|
||||
*-plus.sh
|
||||
*_plus_test.go
|
||||
@@ -1,290 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NSDomainStateEnabled = 1 // 已启用
|
||||
NSDomainStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSDomainDAO dbs.DAO
|
||||
|
||||
func NewNSDomainDAO() *NSDomainDAO {
|
||||
return dbs.NewDAO(&NSDomainDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSDomains",
|
||||
Model: new(NSDomain),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSDomainDAO)
|
||||
}
|
||||
|
||||
var SharedNSDomainDAO *NSDomainDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSDomainDAO = NewNSDomainDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNSDomain 启用条目
|
||||
func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, domainId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(domainId).
|
||||
Set("state", NSDomainStateEnabled).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, domainId)
|
||||
}
|
||||
|
||||
// DisableNSDomain 禁用条目
|
||||
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, domainId int64) error {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Pk(domainId).
|
||||
Set("state", NSDomainStateDisabled).
|
||||
Set("version", version).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, domainId)
|
||||
}
|
||||
|
||||
// FindEnabledNSDomain 查找启用中的条目
|
||||
func (this *NSDomainDAO) FindEnabledNSDomain(tx *dbs.Tx, id int64) (*NSDomain, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", NSDomainStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NSDomain), err
|
||||
}
|
||||
|
||||
// FindNSDomainName 根据主键查找名称
|
||||
func (this *NSDomainDAO) FindNSDomainName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateDomain 创建域名
|
||||
func (this *NSDomainDAO) CreateDomain(tx *dbs.Tx, clusterId int64, userId int64, name string) (int64, error) {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var op = NewNSDomainOperator()
|
||||
op.ClusterId = clusterId
|
||||
op.UserId = userId
|
||||
op.Name = name
|
||||
op.Version = version
|
||||
op.IsOn = true
|
||||
op.State = NSDomainStateEnabled
|
||||
domainId, err := this.SaveInt64(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = this.NotifyUpdate(tx, domainId)
|
||||
if err != nil {
|
||||
return domainId, err
|
||||
}
|
||||
return domainId, nil
|
||||
}
|
||||
|
||||
// UpdateDomain 修改域名
|
||||
func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int64, userId int64, isOn bool) error {
|
||||
if domainId <= 0 {
|
||||
return errors.New("invalid domainId")
|
||||
}
|
||||
|
||||
oldClusterId, err := this.Query(tx).
|
||||
Pk(domainId).
|
||||
Result("clusterId").
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var op = NewNSDomainOperator()
|
||||
op.Id = domainId
|
||||
op.ClusterId = clusterId
|
||||
op.UserId = userId
|
||||
op.IsOn = isOn
|
||||
op.Version = version
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 通知更新
|
||||
if oldClusterId > 0 && oldClusterId != clusterId {
|
||||
err = models.SharedNSClusterDAO.NotifyUpdate(tx, oldClusterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, domainId)
|
||||
}
|
||||
|
||||
// CountAllEnabledDomains 计算域名数量
|
||||
func (this *NSDomainDAO) CountAllEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string) (int64, error) {
|
||||
query := this.Query(tx)
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
} else {
|
||||
query.Where("clusterId IN (SELECT id FROM " + models.SharedNSClusterDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
} else {
|
||||
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword)").
|
||||
Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
|
||||
return query.
|
||||
State(NSDomainStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// ListEnabledDomains 列出单页域名
|
||||
func (this *NSDomainDAO) ListEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string, offset int64, size int64) (result []*NSDomain, err error) {
|
||||
query := this.Query(tx)
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
} else {
|
||||
query.Where("clusterId IN (SELECT id FROM " + models.SharedNSClusterDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
} else {
|
||||
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword)").
|
||||
Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
_, err = query.
|
||||
State(NSDomainStateEnabled).
|
||||
DescPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// IncreaseVersion 增加版本
|
||||
func (this *NSDomainDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
||||
return models.SharedSysLockerDAO.Increase(tx, "NS_DOMAIN_VERSION", 1)
|
||||
}
|
||||
|
||||
// ListDomainsAfterVersion 列出某个版本后的域名
|
||||
func (this *NSDomainDAO) ListDomainsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSDomain, err error) {
|
||||
if size <= 0 {
|
||||
size = 10000
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Gte("version", version).
|
||||
Limit(size).
|
||||
Asc("version").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindDomainIdWithName 根据名称查找域名
|
||||
func (this *NSDomainDAO) FindDomainIdWithName(tx *dbs.Tx, clusterId int64, name string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("clusterId", clusterId).
|
||||
Attr("name", name).
|
||||
State(NSDomainStateEnabled).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindEnabledDomainTSIG 获取TSIG配置
|
||||
func (this *NSDomainDAO) FindEnabledDomainTSIG(tx *dbs.Tx, domainId int64) ([]byte, error) {
|
||||
tsig, err := this.Query(tx).
|
||||
Pk(domainId).
|
||||
Result("tsig").
|
||||
FindStringCol("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(tsig), nil
|
||||
}
|
||||
|
||||
// UpdateDomainTSIG 修改TSIG配置
|
||||
func (this *NSDomainDAO) UpdateDomainTSIG(tx *dbs.Tx, domainId int64, tsigJSON []byte) error {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = this.Query(tx).
|
||||
Pk(domainId).
|
||||
Set("tsig", tsigJSON).
|
||||
Set("version", version).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, domainId)
|
||||
}
|
||||
|
||||
// FindEnabledDomainClusterId 获取域名的集群ID
|
||||
func (this *NSDomainDAO) FindEnabledDomainClusterId(tx *dbs.Tx, domainId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Pk(domainId).
|
||||
State(NSDomainStateEnabled).
|
||||
Result("clusterId").
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更改
|
||||
func (this *NSDomainDAO) NotifyUpdate(tx *dbs.Tx, domainId int64) error {
|
||||
clusterId, err := this.Query(tx).
|
||||
Result("clusterId").
|
||||
Pk(domainId).
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if clusterId > 0 {
|
||||
return models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeDomainChanged)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
24
internal/db/models/nameservers/ns_domain_group_model.go
Normal file
24
internal/db/models/nameservers/ns_domain_group_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package nameservers
|
||||
|
||||
// NSDomainGroup 域名分组
|
||||
type NSDomainGroup struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
Name string `field:"name"` // 分组名称
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Order uint32 `field:"order"` // 排序
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSDomainGroupOperator struct {
|
||||
Id interface{} // ID
|
||||
UserId interface{} // 用户ID
|
||||
Name interface{} // 分组名称
|
||||
IsOn interface{} // 是否启用
|
||||
Order interface{} // 排序
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewNSDomainGroupOperator() *NSDomainGroupOperator {
|
||||
return &NSDomainGroupOperator{}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
@@ -4,15 +4,17 @@ import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// NSDomain DNS域名
|
||||
type NSDomain struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Id uint64 `field:"id"` // ID
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Name string `field:"name"` // 域名
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Version uint64 `field:"version"` // 版本
|
||||
State uint8 `field:"state"` // 状态
|
||||
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
|
||||
Tsig dbs.JSON `field:"tsig"` // TSIG配置
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Version uint64 `field:"version"` // 版本号
|
||||
Status string `field:"status"` // 状态:none|verified
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSDomainOperator struct {
|
||||
@@ -21,10 +23,12 @@ type NSDomainOperator struct {
|
||||
UserId interface{} // 用户ID
|
||||
IsOn interface{} // 是否启用
|
||||
Name interface{} // 域名
|
||||
CreatedAt interface{} // 创建时间
|
||||
Version interface{} // 版本
|
||||
State interface{} // 状态
|
||||
GroupIds interface{} // 分组ID
|
||||
Tsig interface{} // TSIG配置
|
||||
CreatedAt interface{} // 创建时间
|
||||
Version interface{} // 版本号
|
||||
Status interface{} // 状态:none|verified
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewNSDomainOperator() *NSDomainOperator {
|
||||
|
||||
@@ -1 +1,20 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
)
|
||||
|
||||
func (this *NSDomain) DecodeGroupIds() []int64 {
|
||||
if models.IsNull(this.GroupIds) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var result = []int64{}
|
||||
err := json.Unmarshal(this.GroupIds, &result)
|
||||
if err != nil {
|
||||
remotelogs.Error("NSDomain", "DecodeGroupIds:"+err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1,209 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NSKeyStateEnabled = 1 // 已启用
|
||||
NSKeyStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSKeyDAO dbs.DAO
|
||||
|
||||
func NewNSKeyDAO() *NSKeyDAO {
|
||||
return dbs.NewDAO(&NSKeyDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSKeys",
|
||||
Model: new(NSKey),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSKeyDAO)
|
||||
}
|
||||
|
||||
var SharedNSKeyDAO *NSKeyDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSKeyDAO = NewNSKeyDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNSKey 启用条目
|
||||
func (this *NSKeyDAO) EnableNSKey(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSKeyStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableNSKey 禁用条目
|
||||
func (this *NSKeyDAO) DisableNSKey(tx *dbs.Tx, keyId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(keyId).
|
||||
Set("state", NSKeyStateDisabled).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, keyId)
|
||||
}
|
||||
|
||||
// FindEnabledNSKey 查找启用中的条目
|
||||
func (this *NSKeyDAO) FindEnabledNSKey(tx *dbs.Tx, id int64) (*NSKey, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", NSKeyStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NSKey), err
|
||||
}
|
||||
|
||||
// FindNSKeyName 根据主键查找名称
|
||||
func (this *NSKeyDAO) FindNSKeyName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateKey 创建Key
|
||||
func (this *NSKeyDAO) CreateKey(tx *dbs.Tx, domainId int64, zoneId int64, name string, algo dnsconfigs.KeyAlgorithmType, secret string, secretType string) (int64, error) {
|
||||
var op = NewNSKeyOperator()
|
||||
op.DomainId = domainId
|
||||
op.ZoneId = zoneId
|
||||
op.Name = name
|
||||
op.Algo = algo
|
||||
op.Secret = secret
|
||||
op.SecretType = secretType
|
||||
op.State = NSKeyStateEnabled
|
||||
keyId, err := this.SaveInt64(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = this.NotifyUpdate(tx, keyId)
|
||||
if err != nil {
|
||||
return keyId, err
|
||||
}
|
||||
|
||||
return keyId, nil
|
||||
}
|
||||
|
||||
// UpdateKey 修改Key
|
||||
func (this *NSKeyDAO) UpdateKey(tx *dbs.Tx, keyId int64, name string, algo dnsconfigs.KeyAlgorithmType, secret string, secretType string, isOn bool) error {
|
||||
if keyId <= 0 {
|
||||
return errors.New("invalid keyId")
|
||||
}
|
||||
var op = NewNSKeyOperator()
|
||||
op.Id = keyId
|
||||
op.Name = name
|
||||
op.Algo = algo
|
||||
op.Secret = secret
|
||||
op.SecretType = secretType
|
||||
op.IsOn = isOn
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, keyId)
|
||||
}
|
||||
|
||||
// CountEnabledKeys 计算Key的数量
|
||||
func (this *NSKeyDAO) CountEnabledKeys(tx *dbs.Tx, domainId int64, zoneId int64) (int64, error) {
|
||||
var query = this.Query(tx).
|
||||
State(NSKeyStateEnabled)
|
||||
if domainId > 0 {
|
||||
query.Attr("domainId", domainId)
|
||||
}
|
||||
if zoneId > 0 {
|
||||
query.Attr("zoneId", zoneId)
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// ListEnabledKeys 列出单页Key
|
||||
func (this *NSKeyDAO) ListEnabledKeys(tx *dbs.Tx, domainId int64, zoneId int64, offset int64, size int64) (result []*NSKey, err error) {
|
||||
var query = this.Query(tx).
|
||||
State(NSKeyStateEnabled)
|
||||
if domainId > 0 {
|
||||
query.Attr("domainId", domainId)
|
||||
}
|
||||
if zoneId > 0 {
|
||||
query.Attr("zoneId", zoneId)
|
||||
}
|
||||
_, err = query.
|
||||
DescPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// IncreaseVersion 增加版本
|
||||
func (this *NSKeyDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
||||
return models.SharedSysLockerDAO.Increase(tx, "NS_KEY_VERSION", 1)
|
||||
}
|
||||
|
||||
// ListKeysAfterVersion 列出某个版本后的密钥
|
||||
func (this *NSKeyDAO) ListKeysAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSKey, err error) {
|
||||
if size <= 0 {
|
||||
size = 10000
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Gte("version", version).
|
||||
Limit(size).
|
||||
Asc("version").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *NSKeyDAO) NotifyUpdate(tx *dbs.Tx, keyId int64) error {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(keyId).
|
||||
Set("version", version).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 通知集群
|
||||
domainId, err := this.Query(tx).
|
||||
Pk(keyId).
|
||||
Result("domainId").
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if domainId > 0 {
|
||||
clusterId, err := SharedNSDomainDAO.FindEnabledDomainClusterId(tx, domainId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if clusterId > 0 {
|
||||
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeKeyChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -1,67 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type NSQuestionOptionDAO dbs.DAO
|
||||
|
||||
func NewNSQuestionOptionDAO() *NSQuestionOptionDAO {
|
||||
return dbs.NewDAO(&NSQuestionOptionDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSQuestionOptions",
|
||||
Model: new(NSQuestionOption),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSQuestionOptionDAO)
|
||||
}
|
||||
|
||||
var SharedNSQuestionOptionDAO *NSQuestionOptionDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSQuestionOptionDAO = NewNSQuestionOptionDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// FindNSQuestionOptionName 根据主键查找名称
|
||||
func (this *NSQuestionOptionDAO) FindNSQuestionOptionName(tx *dbs.Tx, id uint64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateOption 创建选项
|
||||
func (this *NSQuestionOptionDAO) CreateOption(tx *dbs.Tx, name string, values maps.Map) (int64, error) {
|
||||
if values == nil {
|
||||
values = maps.Map{}
|
||||
}
|
||||
var op = NewNSQuestionOptionOperator()
|
||||
op.Name = name
|
||||
op.Values = values.AsJSON()
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// FindOption 读取选项
|
||||
func (this *NSQuestionOptionDAO) FindOption(tx *dbs.Tx, optionId int64) (*NSQuestionOption, error) {
|
||||
one, err := this.Query(tx).
|
||||
Pk(optionId).
|
||||
Find()
|
||||
if one == nil {
|
||||
return nil, err
|
||||
}
|
||||
return one.(*NSQuestionOption), nil
|
||||
}
|
||||
|
||||
// DeleteOption 删除选项
|
||||
func (this *NSQuestionOptionDAO) DeleteOption(tx *dbs.Tx, optionId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(optionId).
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -1,289 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NSRecordStateEnabled = 1 // 已启用
|
||||
NSRecordStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSRecordDAO dbs.DAO
|
||||
|
||||
func NewNSRecordDAO() *NSRecordDAO {
|
||||
return dbs.NewDAO(&NSRecordDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSRecords",
|
||||
Model: new(NSRecord),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSRecordDAO)
|
||||
}
|
||||
|
||||
var SharedNSRecordDAO *NSRecordDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSRecordDAO = NewNSRecordDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNSRecord 启用条目
|
||||
func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, recordId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(recordId).
|
||||
Set("state", NSRecordStateEnabled).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, recordId)
|
||||
}
|
||||
|
||||
// DisableNSRecord 禁用条目
|
||||
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, recordId int64) error {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Pk(recordId).
|
||||
Set("state", NSRecordStateDisabled).
|
||||
Set("version", version).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, recordId)
|
||||
}
|
||||
|
||||
// FindEnabledNSRecord 查找启用中的条目
|
||||
func (this *NSRecordDAO) FindEnabledNSRecord(tx *dbs.Tx, id int64) (*NSRecord, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", NSRecordStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NSRecord), err
|
||||
}
|
||||
|
||||
// FindNSRecordName 根据主键查找名称
|
||||
func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateRecord 创建记录
|
||||
func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []string) (int64, error) {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var op = NewNSRecordOperator()
|
||||
op.DomainId = domainId
|
||||
op.Description = description
|
||||
op.Name = name
|
||||
op.Type = dnsType
|
||||
op.Value = value
|
||||
op.Ttl = ttl
|
||||
|
||||
if len(routeIds) == 0 {
|
||||
op.RouteIds = `["default"]`
|
||||
} else {
|
||||
routeIds, err := json.Marshal(routeIds)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.RouteIds = routeIds
|
||||
}
|
||||
|
||||
op.IsOn = true
|
||||
op.State = NSRecordStateEnabled
|
||||
op.Version = version
|
||||
recordId, err := this.SaveInt64(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = this.NotifyUpdate(tx, recordId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return recordId, nil
|
||||
}
|
||||
|
||||
// UpdateRecord 修改记录
|
||||
func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []string, isOn bool) error {
|
||||
if recordId <= 0 {
|
||||
return errors.New("invalid recordId")
|
||||
}
|
||||
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var op = NewNSRecordOperator()
|
||||
op.Id = recordId
|
||||
op.Description = description
|
||||
op.Name = name
|
||||
op.Type = dnsType
|
||||
op.Value = value
|
||||
op.Ttl = ttl
|
||||
op.IsOn = isOn
|
||||
|
||||
if len(routeIds) == 0 {
|
||||
op.RouteIds = `["default"]`
|
||||
} else {
|
||||
routeIds, err := json.Marshal(routeIds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.RouteIds = routeIds
|
||||
}
|
||||
|
||||
op.Version = version
|
||||
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, recordId)
|
||||
}
|
||||
|
||||
// CountAllEnabledDomainRecords 计算域名中记录数量
|
||||
func (this *NSRecordDAO) CountAllEnabledDomainRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeCode string) (int64, error) {
|
||||
query := this.Query(tx).
|
||||
Attr("domainId", domainId).
|
||||
State(NSRecordStateEnabled)
|
||||
if len(dnsType) > 0 {
|
||||
query.Attr("type", dnsType)
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
if len(routeCode) > 0 {
|
||||
routeCodeJSON, err := json.Marshal(routeCode)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
query.JSONContains("routeIds", string(routeCodeJSON))
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// CountAllEnabledRecords 计算所有记录数量
|
||||
func (this *NSRecordDAO) CountAllEnabledRecords(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Where("domainId IN (SELECT id FROM " + SharedNSDomainDAO.Table + " WHERE state=1)").
|
||||
State(NSRecordStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// ListEnabledRecords 列出单页记录
|
||||
func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeCode string, offset int64, size int64) (result []*NSRecord, err error) {
|
||||
query := this.Query(tx).
|
||||
Attr("domainId", domainId).
|
||||
State(NSRecordStateEnabled)
|
||||
if len(dnsType) > 0 {
|
||||
query.Attr("type", dnsType)
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
if len(routeCode) > 0 {
|
||||
routeCodeJSON, err := json.Marshal(routeCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.JSONContains("routeIds", string(routeCodeJSON))
|
||||
}
|
||||
_, err = query.
|
||||
DescPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// IncreaseVersion 增加版本
|
||||
func (this *NSRecordDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
||||
return models.SharedSysLockerDAO.Increase(tx, "NS_RECORD_VERSION", 1)
|
||||
}
|
||||
|
||||
// ListRecordsAfterVersion 列出某个版本后的记录
|
||||
func (this *NSRecordDAO) ListRecordsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRecord, err error) {
|
||||
if size <= 0 {
|
||||
size = 10000
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Gte("version", version).
|
||||
Limit(size).
|
||||
Asc("version").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindEnabledRecordWithName 查询单条记录
|
||||
func (this *NSRecordDAO) FindEnabledRecordWithName(tx *dbs.Tx, domainId int64, recordName string, recordType dnsconfigs.RecordType) (*NSRecord, error) {
|
||||
record, err := this.Query(tx).
|
||||
State(NSRecordStateEnabled).
|
||||
Attr("domainId", domainId).
|
||||
Attr("name", recordName).
|
||||
Attr("type", recordType).
|
||||
Find()
|
||||
if record == nil {
|
||||
return nil, err
|
||||
}
|
||||
return record.(*NSRecord), nil
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *NSRecordDAO) NotifyUpdate(tx *dbs.Tx, recordId int64) error {
|
||||
domainId, err := this.Query(tx).
|
||||
Pk(recordId).
|
||||
Result("domainId").
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if domainId == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
clusterId, err := SharedNSDomainDAO.FindEnabledDomainClusterId(tx, domainId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if clusterId > 0 {
|
||||
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeRecordChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNSRecord_DecodeRouteIds(t *testing.T) {
|
||||
{
|
||||
record := &NSRecord{}
|
||||
t.Log(record.DecodeRouteIds())
|
||||
}
|
||||
|
||||
{
|
||||
record := &NSRecord{RouteIds: []byte("[]")}
|
||||
t.Log(record.DecodeRouteIds())
|
||||
}
|
||||
|
||||
{
|
||||
record := &NSRecord{RouteIds: []byte("[1, 2, 3]")}
|
||||
t.Log(record.DecodeRouteIds())
|
||||
}
|
||||
|
||||
{
|
||||
record := &NSRecord{RouteIds: []byte(`["id:1", "id:2", "isp:liantong"]`)}
|
||||
t.Log(record.DecodeRouteIds())
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NSRecordHourlyStatDAO dbs.DAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
// 清理数据任务
|
||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
err := SharedNSRecordHourlyStatDAO.Clean(nil, 30) // 只保留N天
|
||||
if err != nil {
|
||||
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func NewNSRecordHourlyStatDAO() *NSRecordHourlyStatDAO {
|
||||
return dbs.NewDAO(&NSRecordHourlyStatDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSRecordHourlyStats",
|
||||
Model: new(NSRecordHourlyStat),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSRecordHourlyStatDAO)
|
||||
}
|
||||
|
||||
var SharedNSRecordHourlyStatDAO *NSRecordHourlyStatDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSRecordHourlyStatDAO = NewNSRecordHourlyStatDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// IncreaseHourlyStat 增加统计数据
|
||||
func (this *NSRecordHourlyStatDAO) IncreaseHourlyStat(tx *dbs.Tx, clusterId int64, nodeId int64, hour string, domainId int64, recordId int64, countRequests int64, bytes int64) error {
|
||||
if len(hour) != 10 {
|
||||
return errors.New("invalid hour '" + hour + "'")
|
||||
}
|
||||
return this.Query(tx).
|
||||
Param("countRequests", countRequests).
|
||||
Param("bytes", bytes).
|
||||
InsertOrUpdateQuickly(maps.Map{
|
||||
"clusterId": clusterId,
|
||||
"nodeId": nodeId,
|
||||
"domainId": domainId,
|
||||
"recordId": recordId,
|
||||
"day": hour[:8],
|
||||
"hour": hour,
|
||||
"countRequests": countRequests,
|
||||
"bytes": bytes,
|
||||
}, maps.Map{
|
||||
"countRequests": dbs.SQL("countRequests+:countRequests"),
|
||||
"bytes": dbs.SQL("bytes+:bytes"),
|
||||
})
|
||||
}
|
||||
|
||||
// FindHourlyStats 按小时统计
|
||||
func (this *NSRecordHourlyStatDAO) FindHourlyStats(tx *dbs.Tx, hourFrom string, hourTo string) (result []*NSRecordHourlyStat, err error) {
|
||||
ones, err := this.Query(tx).
|
||||
Result("hour", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
||||
Between("hour", hourFrom, hourTo).
|
||||
Group("hour").
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var m = map[string]*NSRecordHourlyStat{} // hour => *NSRecordHourlyStat
|
||||
for _, one := range ones {
|
||||
m[one.(*NSRecordHourlyStat).Hour] = one.(*NSRecordHourlyStat)
|
||||
}
|
||||
hours, err := utils.RangeHours(hourFrom, hourTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, hour := range hours {
|
||||
stat, ok := m[hour]
|
||||
if ok {
|
||||
result = append(result, stat)
|
||||
} else {
|
||||
result = append(result, &NSRecordHourlyStat{
|
||||
Hour: hour,
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// FindDailyStats 按天统计
|
||||
func (this *NSRecordHourlyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*NSRecordHourlyStat, err error) {
|
||||
ones, err := this.Query(tx).
|
||||
Result("day", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
||||
Between("day", dayFrom, dayTo).
|
||||
Group("day").
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var m = map[string]*NSRecordHourlyStat{} // day => *NSRecordHourlyStat
|
||||
for _, one := range ones {
|
||||
m[one.(*NSRecordHourlyStat).Day] = one.(*NSRecordHourlyStat)
|
||||
}
|
||||
days, err := utils.RangeDays(dayFrom, dayTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, day := range days {
|
||||
stat, ok := m[day]
|
||||
if ok {
|
||||
result = append(result, stat)
|
||||
} else {
|
||||
result = append(result, &NSRecordHourlyStat{
|
||||
Day: day,
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ListTopNodes 节点排行
|
||||
func (this *NSRecordHourlyStatDAO) ListTopNodes(tx *dbs.Tx, hourFrom string, hourTo string, size int64) (result []*NSRecordHourlyStat, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Result("MIN(clusterId) AS clusterId", "nodeId", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
||||
Between("hour", hourFrom, hourTo).
|
||||
Group("nodeId").
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// ListTopDomains 域名排行
|
||||
func (this *NSRecordHourlyStatDAO) ListTopDomains(tx *dbs.Tx, hourFrom string, hourTo string, size int64) (result []*NSRecordHourlyStat, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Result("domainId", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
||||
Between("hour", hourFrom, hourTo).
|
||||
Group("domainId").
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// Clean 清理历史数据
|
||||
func (this *NSRecordHourlyStatDAO) Clean(tx *dbs.Tx, days int) error {
|
||||
var hour = timeutil.Format("Ymd00", time.Now().AddDate(0, 0, -days))
|
||||
_, err := this.Query(tx).
|
||||
Lt("hour", hour).
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -1,268 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
NSRouteStateEnabled = 1 // 已启用
|
||||
NSRouteStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSRouteDAO dbs.DAO
|
||||
|
||||
func NewNSRouteDAO() *NSRouteDAO {
|
||||
return dbs.NewDAO(&NSRouteDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSRoutes",
|
||||
Model: new(NSRoute),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSRouteDAO)
|
||||
}
|
||||
|
||||
var SharedNSRouteDAO *NSRouteDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSRouteDAO = NewNSRouteDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNSRoute 启用条目
|
||||
func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, routeId int64) error {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Pk(routeId).
|
||||
Set("state", NSRouteStateEnabled).
|
||||
Set("version", version).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx)
|
||||
}
|
||||
|
||||
// DisableNSRoute 禁用条目
|
||||
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, routeId int64) error {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Pk(routeId).
|
||||
Set("state", NSRouteStateDisabled).
|
||||
Set("version", version).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx)
|
||||
}
|
||||
|
||||
// FindEnabledNSRoute 查找启用中的条目
|
||||
func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id int64) (*NSRoute, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", NSRouteStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NSRoute), err
|
||||
}
|
||||
|
||||
// FindEnabledRouteWithCode 根据代号获取线路信息
|
||||
func (this *NSRouteDAO) FindEnabledRouteWithCode(tx *dbs.Tx, code string) (*NSRoute, error) {
|
||||
if regexp.MustCompile(`^id:\d+$`).MatchString(code) {
|
||||
var routeId = types.Int64(code[strings.Index(code, ":")+1:])
|
||||
route, err := this.FindEnabledNSRoute(tx, routeId)
|
||||
if route == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
route.Code = "id:" + types.String(routeId)
|
||||
return route, nil
|
||||
}
|
||||
|
||||
route := dnsconfigs.FindDefaultRoute(code)
|
||||
if route == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &NSRoute{
|
||||
Id: 0,
|
||||
IsOn: true,
|
||||
Name: route.Name,
|
||||
Code: route.Code,
|
||||
State: NSRouteStateEnabled,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FindNSRouteName 根据主键查找名称
|
||||
func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateRoute 创建线路
|
||||
func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64, userId int64, name string, rangesJSON []byte) (int64, error) {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var op = NewNSRouteOperator()
|
||||
op.ClusterId = clusterId
|
||||
op.DomainId = domainId
|
||||
op.UserId = userId
|
||||
op.Name = name
|
||||
if len(rangesJSON) > 0 {
|
||||
op.Ranges = rangesJSON
|
||||
} else {
|
||||
op.Ranges = "[]"
|
||||
}
|
||||
op.IsOn = true
|
||||
op.State = NSRouteStateEnabled
|
||||
op.Version = version
|
||||
routeId, err := this.SaveInt64(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = this.NotifyUpdate(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return routeId, nil
|
||||
}
|
||||
|
||||
// UpdateRoute 修改线路
|
||||
func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rangesJSON []byte) error {
|
||||
if routeId <= 0 {
|
||||
return errors.New("invalid routeId")
|
||||
}
|
||||
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var op = NewNSRouteOperator()
|
||||
op.Id = routeId
|
||||
op.Name = name
|
||||
if len(rangesJSON) > 0 {
|
||||
op.Ranges = rangesJSON
|
||||
} else {
|
||||
op.Ranges = "[]"
|
||||
}
|
||||
|
||||
op.Version = version
|
||||
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx)
|
||||
}
|
||||
|
||||
// UpdateRouteOrders 修改线路排序
|
||||
func (this *NSRouteDAO) UpdateRouteOrders(tx *dbs.Tx, routeIds []int64) error {
|
||||
version, err := this.IncreaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
order := len(routeIds)
|
||||
for _, routeId := range routeIds {
|
||||
_, err = this.Query(tx).
|
||||
Pk(routeId).
|
||||
Set("order", order).
|
||||
Set("version", version).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
order--
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx)
|
||||
}
|
||||
|
||||
// FindAllEnabledRoutes 列出所有线路
|
||||
func (this *NSRouteDAO) FindAllEnabledRoutes(tx *dbs.Tx, clusterId int64, domainId int64, userId int64) (result []*NSRoute, err error) {
|
||||
query := this.Query(tx).
|
||||
State(NSRouteStateEnabled).
|
||||
Slice(&result).
|
||||
Desc("order").
|
||||
AscPk()
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
} else {
|
||||
// 不查询所有集群的线路
|
||||
query.Attr("clusterId", 0)
|
||||
}
|
||||
if domainId > 0 {
|
||||
query.Attr("domainId", domainId)
|
||||
}
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
_, err = query.FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// IncreaseVersion 增加版本
|
||||
func (this *NSRouteDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
||||
return models.SharedSysLockerDAO.Increase(tx, "NS_ROUTE_VERSION", 1)
|
||||
}
|
||||
|
||||
// ListRoutesAfterVersion 列出某个版本后的域名
|
||||
func (this *NSRouteDAO) ListRoutesAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRoute, err error) {
|
||||
if size <= 0 {
|
||||
size = 10000
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Gte("version", version).
|
||||
Limit(size).
|
||||
Asc("version").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *NSRouteDAO) NotifyUpdate(tx *dbs.Tx) error {
|
||||
// 线路变更时所有集群都要更新
|
||||
clusterIds, err := models.SharedNSClusterDAO.FindAllEnabledClusterIds(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, clusterId := range clusterIds {
|
||||
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeRouteChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -1,63 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NSZoneStateEnabled = 1 // 已启用
|
||||
NSZoneStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSZoneDAO dbs.DAO
|
||||
|
||||
func NewNSZoneDAO() *NSZoneDAO {
|
||||
return dbs.NewDAO(&NSZoneDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSZones",
|
||||
Model: new(NSZone),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSZoneDAO)
|
||||
}
|
||||
|
||||
var SharedNSZoneDAO *NSZoneDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSZoneDAO = NewNSZoneDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNSZone 启用条目
|
||||
func (this *NSZoneDAO) EnableNSZone(tx *dbs.Tx, id uint64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSZoneStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableNSZone 禁用条目
|
||||
func (this *NSZoneDAO) DisableNSZone(tx *dbs.Tx, id uint64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSZoneStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledNSZone 查找启用中的条目
|
||||
func (this *NSZoneDAO) FindEnabledNSZone(tx *dbs.Tx, id uint64) (*NSZone, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", NSZoneStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NSZone), err
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build plus
|
||||
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dnsclients
|
||||
@@ -39,7 +41,7 @@ func (this *LocalEdgeDNSProvider) Auth(params maps.Map) error {
|
||||
// GetDomains 获取所有域名列表
|
||||
func (this *LocalEdgeDNSProvider) GetDomains() (domains []string, err error) {
|
||||
var tx *dbs.Tx
|
||||
domainOnes, err := nameservers.SharedNSDomainDAO.ListEnabledDomains(tx, this.clusterId, 0, "", 0, 1000)
|
||||
domainOnes, err := nameservers.SharedNSDomainDAO.ListEnabledDomains(tx, this.clusterId, 0, 0, "", 0, 10000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -52,28 +52,6 @@ func FindAllProviderTypes() []maps.Map {
|
||||
return typeMaps
|
||||
}
|
||||
|
||||
// FindProvider 查找服务商实例
|
||||
func FindProvider(providerType ProviderType) ProviderInterface {
|
||||
switch providerType {
|
||||
case ProviderTypeDNSPod:
|
||||
return &DNSPodProvider{}
|
||||
case ProviderTypeAliDNS:
|
||||
return &AliDNSProvider{}
|
||||
case ProviderTypeHuaweiDNS:
|
||||
return &HuaweiDNSProvider{}
|
||||
case ProviderTypeCloudFlare:
|
||||
return &CloudFlareProvider{}
|
||||
case ProviderTypeLocalEdgeDNS:
|
||||
return &LocalEdgeDNSProvider{}
|
||||
case ProviderTypeUserEdgeDNS:
|
||||
return &UserEdgeDNSProvider{}
|
||||
case ProviderTypeCustomHTTP:
|
||||
return &CustomHTTPProvider{}
|
||||
}
|
||||
|
||||
return filterProvider(providerType)
|
||||
}
|
||||
|
||||
// FindProviderTypeName 查找服务商名称
|
||||
func FindProviderTypeName(providerType ProviderType) string {
|
||||
for _, t := range FindAllProviderTypes() {
|
||||
|
||||
@@ -6,10 +6,24 @@ package dnsclients
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
// FindProvider 查找服务商实例
|
||||
func FindProvider(providerType ProviderType) ProviderInterface {
|
||||
switch providerType {
|
||||
case ProviderTypeDNSPod:
|
||||
return &DNSPodProvider{}
|
||||
case ProviderTypeAliDNS:
|
||||
return &AliDNSProvider{}
|
||||
case ProviderTypeHuaweiDNS:
|
||||
return &HuaweiDNSProvider{}
|
||||
case ProviderTypeCloudFlare:
|
||||
return &CloudFlareProvider{}
|
||||
case ProviderTypeCustomHTTP:
|
||||
return &CustomHTTPProvider{}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func filterTypeMaps(typeMaps []maps.Map) []maps.Map {
|
||||
return typeMaps
|
||||
}
|
||||
|
||||
func filterProvider(providerType string) ProviderInterface {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user