mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-07 07:55:48 +08:00
EdgeDNS支持内置线路
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -89,7 +88,7 @@ func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id int64) (string, error)
|
||||
}
|
||||
|
||||
// CreateRecord 创建记录
|
||||
func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64) (int64, error) {
|
||||
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
|
||||
@@ -104,7 +103,7 @@ func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description st
|
||||
op.Ttl = ttl
|
||||
|
||||
if len(routeIds) == 0 {
|
||||
op.RouteIds = "[]"
|
||||
op.RouteIds = `["default"]`
|
||||
} else {
|
||||
routeIds, err := json.Marshal(routeIds)
|
||||
if err != nil {
|
||||
@@ -129,7 +128,7 @@ func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description st
|
||||
}
|
||||
|
||||
// UpdateRecord 修改记录
|
||||
func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64, isOn bool) error {
|
||||
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")
|
||||
}
|
||||
@@ -149,7 +148,7 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
|
||||
op.IsOn = isOn
|
||||
|
||||
if len(routeIds) == 0 {
|
||||
op.RouteIds = "[]"
|
||||
op.RouteIds = `["default"]`
|
||||
} else {
|
||||
routeIds, err := json.Marshal(routeIds)
|
||||
if err != nil {
|
||||
@@ -169,7 +168,7 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
|
||||
}
|
||||
|
||||
// CountAllEnabledDomainRecords 计算域名中记录数量
|
||||
func (this *NSRecordDAO) CountAllEnabledDomainRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64) (int64, error) {
|
||||
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)
|
||||
@@ -180,8 +179,12 @@ func (this *NSRecordDAO) CountAllEnabledDomainRecords(tx *dbs.Tx, domainId int64
|
||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
if routeId > 0 {
|
||||
query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
|
||||
if len(routeCode) > 0 {
|
||||
routeCodeJSON, err := json.Marshal(routeCode)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
query.JSONContains("routeIds", string(routeCodeJSON))
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
@@ -195,7 +198,7 @@ func (this *NSRecordDAO) CountAllEnabledRecords(tx *dbs.Tx) (int64, error) {
|
||||
}
|
||||
|
||||
// ListEnabledRecords 列出单页记录
|
||||
func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64, offset int64, size int64) (result []*NSRecord, err error) {
|
||||
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)
|
||||
@@ -206,8 +209,12 @@ func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType
|
||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
if routeId > 0 {
|
||||
query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
|
||||
if len(routeCode) > 0 {
|
||||
routeCodeJSON, err := json.Marshal(routeCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.JSONContains("routeIds", string(routeCodeJSON))
|
||||
}
|
||||
_, err = query.
|
||||
DescPk().
|
||||
|
||||
@@ -3,4 +3,27 @@ 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: "[]"}
|
||||
t.Log(record.DecodeRouteIds())
|
||||
}
|
||||
|
||||
{
|
||||
record := &NSRecord{RouteIds: "[1, 2, 3]"}
|
||||
t.Log(record.DecodeRouteIds())
|
||||
}
|
||||
|
||||
{
|
||||
record := &NSRecord{RouteIds: `["id:1", "id:2", "isp:liantong"]`}
|
||||
t.Log(record.DecodeRouteIds())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,26 @@
|
||||
package nameservers
|
||||
|
||||
import "encoding/json"
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
func (this *NSRecord) DecodeRouteIds() []int64 {
|
||||
routeIds := []int64{}
|
||||
func (this *NSRecord) DecodeRouteIds() []string {
|
||||
var routeIds = []string{}
|
||||
if len(this.RouteIds) > 0 {
|
||||
_ = json.Unmarshal([]byte(this.RouteIds), &routeIds)
|
||||
err := json.Unmarshal([]byte(this.RouteIds), &routeIds)
|
||||
if err != nil {
|
||||
// 检查是否有旧的数据
|
||||
var oldRouteIds = []int64{}
|
||||
err = json.Unmarshal([]byte(this.RouteIds), &oldRouteIds)
|
||||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
routeIds = []string{}
|
||||
for _, routeId := range oldRouteIds {
|
||||
routeIds = append(routeIds, "id:"+types.String(routeId))
|
||||
}
|
||||
}
|
||||
}
|
||||
return routeIds
|
||||
}
|
||||
|
||||
@@ -3,10 +3,14 @@ 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 (
|
||||
@@ -83,6 +87,33 @@ func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id int64) (*NSRoute, erro
|
||||
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: 1,
|
||||
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).
|
||||
|
||||
@@ -11,6 +11,7 @@ type NSRoute struct {
|
||||
Ranges string `field:"ranges"` // 范围
|
||||
Order uint32 `field:"order"` // 排序
|
||||
Version uint64 `field:"version"` // 版本号
|
||||
Code string `field:"code"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
@@ -24,6 +25,7 @@ type NSRouteOperator struct {
|
||||
Ranges interface{} // 范围
|
||||
Order interface{} // 排序
|
||||
Version interface{} // 版本号
|
||||
Code interface{} // 代号
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user