mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-31 18:46:35 +08:00
DNS服务支持密钥管理
This commit is contained in:
@@ -10,6 +10,7 @@ type NSDomain struct {
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Version uint64 `field:"version"` // 版本
|
||||
State uint8 `field:"state"` // 状态
|
||||
Tsig string `field:"tsig"` // TSIG配置
|
||||
}
|
||||
|
||||
type NSDomainOperator struct {
|
||||
@@ -21,6 +22,7 @@ type NSDomainOperator struct {
|
||||
CreatedAt interface{} // 创建时间
|
||||
Version interface{} // 版本
|
||||
State interface{} // 状态
|
||||
Tsig interface{} // TSIG配置
|
||||
}
|
||||
|
||||
func NewNSDomainOperator() *NSDomainOperator {
|
||||
|
||||
137
internal/db/models/nameservers/ns_key_dao.go
Normal file
137
internal/db/models/nameservers/ns_key_dao.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
_ "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, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSKeyStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 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) {
|
||||
op := NewNSKeyOperator()
|
||||
op.DomainId = domainId
|
||||
op.ZoneId = zoneId
|
||||
op.Name = name
|
||||
op.Algo = algo
|
||||
op.Secret = secret
|
||||
op.SecretType = secretType
|
||||
op.State = NSKeyStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
op := NewNSKeyOperator()
|
||||
op.Id = keyId
|
||||
op.Name = name
|
||||
op.Algo = algo
|
||||
op.Secret = secret
|
||||
op.SecretType = secretType
|
||||
op.IsOn = isOn
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// 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().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *NSKeyDAO) NotifyUpdate(tx *dbs.Tx, keyId int64) error {
|
||||
// TODO 需要实现
|
||||
return nil
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_key_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_key_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
30
internal/db/models/nameservers/ns_key_model.go
Normal file
30
internal/db/models/nameservers/ns_key_model.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package nameservers
|
||||
|
||||
// NSKey 密钥管理
|
||||
type NSKey struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
IsOn uint8 `field:"isOn"` // 状态
|
||||
Name string `field:"name"` // 名称
|
||||
DomainId uint64 `field:"domainId"` // 域名ID
|
||||
ZoneId uint64 `field:"zoneId"` // 子域ID
|
||||
Algo string `field:"algo"` // 算法
|
||||
Secret string `field:"secret"` // 密码
|
||||
SecretType string `field:"secretType"` // 密码类型
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSKeyOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 状态
|
||||
Name interface{} // 名称
|
||||
DomainId interface{} // 域名ID
|
||||
ZoneId interface{} // 子域ID
|
||||
Algo interface{} // 算法
|
||||
Secret interface{} // 密码
|
||||
SecretType interface{} // 密码类型
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewNSKeyOperator() *NSKeyOperator {
|
||||
return &NSKeyOperator{}
|
||||
}
|
||||
1
internal/db/models/nameservers/ns_key_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_key_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
63
internal/db/models/nameservers/ns_zone_dao.go
Normal file
63
internal/db/models/nameservers/ns_zone_dao.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_zone_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_zone_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
26
internal/db/models/nameservers/ns_zone_model.go
Normal file
26
internal/db/models/nameservers/ns_zone_model.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package nameservers
|
||||
|
||||
// NSZone 域名子域
|
||||
type NSZone struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
DomainId uint64 `field:"domainId"` // 域名ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
Order uint32 `field:"order"` // 排序
|
||||
Version uint64 `field:"version"` // 版本
|
||||
Tsig string `field:"tsig"` // TSIG配置
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSZoneOperator struct {
|
||||
Id interface{} // ID
|
||||
DomainId interface{} // 域名ID
|
||||
IsOn interface{} // 是否启用
|
||||
Order interface{} // 排序
|
||||
Version interface{} // 版本
|
||||
Tsig interface{} // TSIG配置
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewNSZoneOperator() *NSZoneOperator {
|
||||
return &NSZoneOperator{}
|
||||
}
|
||||
1
internal/db/models/nameservers/ns_zone_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_zone_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
Reference in New Issue
Block a user