mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	实现域名、记录同步等API
This commit is contained in:
		@@ -44,10 +44,16 @@ func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, id int64) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DisableNSDomain 禁用条目
 | 
			
		||||
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, id int64) error {
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
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()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
@@ -74,26 +80,38 @@ func (this *NSDomainDAO) FindNSDomainName(tx *dbs.Tx, id int64) (string, error)
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewNSDomainOperator()
 | 
			
		||||
	op.ClusterId = clusterId
 | 
			
		||||
	op.UserId = userId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
	op.Version = version
 | 
			
		||||
	op.IsOn = true
 | 
			
		||||
	op.State = NSDomainStateEnabled
 | 
			
		||||
	return this.SaveInt64(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateDomain 修改域名
 | 
			
		||||
func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int64, userId int64, name string, isOn bool) error {
 | 
			
		||||
func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int64, userId int64, isOn bool) error {
 | 
			
		||||
	if domainId <= 0 {
 | 
			
		||||
		return errors.New("invalid domainId")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	version, err := this.IncreaseVersion(tx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewNSDomainOperator()
 | 
			
		||||
	op.Id = domainId
 | 
			
		||||
	op.ClusterId = clusterId
 | 
			
		||||
	op.UserId = userId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
	op.IsOn = isOn
 | 
			
		||||
	op.Version = version
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -146,3 +164,23 @@ func (this *NSDomainDAO) ListEnabledDomains(tx *dbs.Tx, clusterId int64, userId
 | 
			
		||||
		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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ type NSDomain struct {
 | 
			
		||||
	IsOn      uint8  `field:"isOn"`      // 是否启用
 | 
			
		||||
	Name      string `field:"name"`      // 域名
 | 
			
		||||
	CreatedAt uint64 `field:"createdAt"` // 创建时间
 | 
			
		||||
	Version   uint64 `field:"version"`   // 版本
 | 
			
		||||
	State     uint8  `field:"state"`     // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -18,6 +19,7 @@ type NSDomainOperator struct {
 | 
			
		||||
	IsOn      interface{} // 是否启用
 | 
			
		||||
	Name      interface{} // 域名
 | 
			
		||||
	CreatedAt interface{} // 创建时间
 | 
			
		||||
	Version   interface{} // 版本
 | 
			
		||||
	State     interface{} // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -318,6 +318,18 @@ func (this *NSNodeDAO) UpdateNodeIsInstalled(tx *dbs.Tx, nodeId int64, isInstall
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateNodeStatus 更改节点状态
 | 
			
		||||
func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
 | 
			
		||||
	if statusJSON == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(nodeId).
 | 
			
		||||
		Set("status", string(statusJSON)).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotifyUpdate 通知更新
 | 
			
		||||
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
 | 
			
		||||
	// TODO 先什么都不做
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package nameservers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
@@ -47,9 +48,15 @@ func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, id uint64) error {
 | 
			
		||||
 | 
			
		||||
// DisableNSRecord 禁用条目
 | 
			
		||||
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id int64) error {
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
	version, err := this.IncreaseVersion(tx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
		Set("state", NSRecordStateDisabled).
 | 
			
		||||
		Set("version", version).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
@@ -76,6 +83,11 @@ 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) {
 | 
			
		||||
	version, err := this.IncreaseVersion(tx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewNSRecordOperator()
 | 
			
		||||
	op.DomainId = domainId
 | 
			
		||||
	op.Description = description
 | 
			
		||||
@@ -96,6 +108,7 @@ func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description st
 | 
			
		||||
 | 
			
		||||
	op.IsOn = true
 | 
			
		||||
	op.State = NSRecordStateEnabled
 | 
			
		||||
	op.Version = version
 | 
			
		||||
	return this.SaveInt64(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -104,6 +117,11 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
 | 
			
		||||
		return errors.New("invalid recordId")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	version, err := this.IncreaseVersion(tx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewNSRecordOperator()
 | 
			
		||||
	op.Id = recordId
 | 
			
		||||
	op.Description = description
 | 
			
		||||
@@ -122,6 +140,8 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
 | 
			
		||||
		op.RouteIds = routeIds
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.Version = version
 | 
			
		||||
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -164,3 +184,23 @@ func (this *NSRecordDAO) ListAllEnabledRecords(tx *dbs.Tx, domainId int64, dnsTy
 | 
			
		||||
		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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@ type NSRecord struct {
 | 
			
		||||
	Weight      uint32 `field:"weight"`      // 权重
 | 
			
		||||
	RouteIds    string `field:"routeIds"`    // 线路
 | 
			
		||||
	CreatedAt   uint64 `field:"createdAt"`   // 创建时间
 | 
			
		||||
	Version     uint64 `field:"version"`     //
 | 
			
		||||
	State       uint8  `field:"state"`       // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -28,6 +29,7 @@ type NSRecordOperator struct {
 | 
			
		||||
	Weight      interface{} // 权重
 | 
			
		||||
	RouteIds    interface{} // 线路
 | 
			
		||||
	CreatedAt   interface{} // 创建时间
 | 
			
		||||
	Version     interface{} //
 | 
			
		||||
	State       interface{} // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package nameservers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
@@ -43,10 +44,16 @@ func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id int64) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DisableNSRoute 禁用条目
 | 
			
		||||
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, id int64) error {
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
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()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
@@ -73,6 +80,11 @@ func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewNSRouteOperator()
 | 
			
		||||
	op.ClusterId = clusterId
 | 
			
		||||
	op.DomainId = domainId
 | 
			
		||||
@@ -85,6 +97,7 @@ func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64,
 | 
			
		||||
	}
 | 
			
		||||
	op.IsOn = true
 | 
			
		||||
	op.State = NSRouteStateEnabled
 | 
			
		||||
	op.Version = version
 | 
			
		||||
	return this.SaveInt64(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -93,6 +106,12 @@ func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rang
 | 
			
		||||
	if routeId <= 0 {
 | 
			
		||||
		return errors.New("invalid routeId")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	version, err := this.IncreaseVersion(tx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewNSRouteOperator()
 | 
			
		||||
	op.Id = routeId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
@@ -101,16 +120,25 @@ func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rang
 | 
			
		||||
	} else {
 | 
			
		||||
		op.Ranges = "[]"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.Version = version
 | 
			
		||||
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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).
 | 
			
		||||
		_, err = this.Query(tx).
 | 
			
		||||
			Pk(routeId).
 | 
			
		||||
			Set("order", order).
 | 
			
		||||
			Set("version", version).
 | 
			
		||||
			Update()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
@@ -142,3 +170,23 @@ func (this *NSRouteDAO) FindAllEnabledRoutes(tx *dbs.Tx, clusterId int64, domain
 | 
			
		||||
	_, 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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ type NSRoute struct {
 | 
			
		||||
	Name      string `field:"name"`      // 名称
 | 
			
		||||
	Ranges    string `field:"ranges"`    // 范围
 | 
			
		||||
	Order     uint32 `field:"order"`     // 排序
 | 
			
		||||
	Version   uint64 `field:"version"`   // 版本号
 | 
			
		||||
	State     uint8  `field:"state"`     // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -22,6 +23,7 @@ type NSRouteOperator struct {
 | 
			
		||||
	Name      interface{} // 名称
 | 
			
		||||
	Ranges    interface{} // 范围
 | 
			
		||||
	Order     interface{} // 排序
 | 
			
		||||
	Version   interface{} // 版本号
 | 
			
		||||
	State     interface{} // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -96,6 +96,8 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, ser
 | 
			
		||||
		switch role {
 | 
			
		||||
		case nodeconfigs.NodeRoleNode:
 | 
			
		||||
			query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
 | 
			
		||||
		case nodeconfigs.NodeRoleDNS:
 | 
			
		||||
			query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if serverId > 0 {
 | 
			
		||||
@@ -141,6 +143,8 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
 | 
			
		||||
		switch role {
 | 
			
		||||
		case nodeconfigs.NodeRoleNode:
 | 
			
		||||
			query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
 | 
			
		||||
		case nodeconfigs.NodeRoleDNS:
 | 
			
		||||
			query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if serverId > 0 {
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
 | 
			
		||||
	rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -38,7 +39,7 @@ func (this *NSDomainService) UpdateNSDomain(ctx context.Context, req *pb.UpdateN
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	err = nameservers.SharedNSDomainDAO.UpdateDomain(tx, req.NsDomainId, req.NsClusterId, req.UserId, req.Name, req.IsOn)
 | 
			
		||||
	err = nameservers.SharedNSDomainDAO.UpdateDomain(tx, req.NsDomainId, req.NsClusterId, req.UserId, req.IsOn)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -189,3 +190,32 @@ func (this *NSDomainService) ListEnabledNSDomains(ctx context.Context, req *pb.L
 | 
			
		||||
 | 
			
		||||
	return &pb.ListEnabledNSDomainsResponse{NsDomains: pbDomains}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListNSDomainsAfterVersion 根据版本列出一组域名
 | 
			
		||||
func (this *NSDomainService) ListNSDomainsAfterVersion(ctx context.Context, req *pb.ListNSDomainsAfterVersionRequest) (*pb.ListNSDomainsAfterVersionResponse, error) {
 | 
			
		||||
	_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 集群ID
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	domains, err := nameservers.SharedNSDomainDAO.ListDomainsAfterVersion(tx, req.Version, 2000)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var pbDomains []*pb.NSDomain
 | 
			
		||||
	for _, domain := range domains {
 | 
			
		||||
		pbDomains = append(pbDomains, &pb.NSDomain{
 | 
			
		||||
			Id:        int64(domain.Id),
 | 
			
		||||
			Name:      domain.Name,
 | 
			
		||||
			IsOn:      domain.IsOn == 1,
 | 
			
		||||
			IsDeleted: domain.State == nameservers.NSDomainStateDisabled,
 | 
			
		||||
			Version:   int64(domain.Version),
 | 
			
		||||
			NsCluster: &pb.NSCluster{Id: int64(domain.ClusterId)},
 | 
			
		||||
			User:      nil,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.ListNSDomainsAfterVersionResponse{NsDomains: pbDomains}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/installers"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
 | 
			
		||||
	rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
)
 | 
			
		||||
@@ -314,3 +315,85 @@ func (this *NSNodeService) UpdateNSNodeIsInstalled(ctx context.Context, req *pb.
 | 
			
		||||
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateNSNodeStatus 更新节点状态
 | 
			
		||||
func (this *NSNodeService) UpdateNSNodeStatus(ctx context.Context, req *pb.UpdateNSNodeStatusRequest) (*pb.RPCSuccess, error) {
 | 
			
		||||
	// 校验节点
 | 
			
		||||
	_, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if req.NodeId > 0 {
 | 
			
		||||
		nodeId = req.NodeId
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if nodeId <= 0 {
 | 
			
		||||
		return nil, errors.New("'nodeId' should be greater than 0")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	err = nameservers.SharedNSNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindCurrentNSNode 获取当前节点信息
 | 
			
		||||
func (this *NSNodeService) FindCurrentNSNode(ctx context.Context, req *pb.FindCurrentNSNodeRequest) (*pb.FindCurrentNSNodeResponse, error) {
 | 
			
		||||
	// 校验节点
 | 
			
		||||
	_, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	node, err := nameservers.SharedNSNodeDAO.FindEnabledNSNode(tx, nodeId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if node == nil {
 | 
			
		||||
		return &pb.FindCurrentNSNodeResponse{NsNode: nil}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 集群信息
 | 
			
		||||
	clusterName, err := nameservers.SharedNSClusterDAO.FindEnabledNSClusterName(tx, int64(node.ClusterId))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 安装信息
 | 
			
		||||
	installStatus, err := node.DecodeInstallStatus()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	installStatusResult := &pb.NodeInstallStatus{}
 | 
			
		||||
	if installStatus != nil {
 | 
			
		||||
		installStatusResult = &pb.NodeInstallStatus{
 | 
			
		||||
			IsRunning:  installStatus.IsRunning,
 | 
			
		||||
			IsFinished: installStatus.IsFinished,
 | 
			
		||||
			IsOk:       installStatus.IsOk,
 | 
			
		||||
			Error:      installStatus.Error,
 | 
			
		||||
			ErrorCode:  installStatus.ErrorCode,
 | 
			
		||||
			UpdatedAt:  installStatus.UpdatedAt,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindCurrentNSNodeResponse{NsNode: &pb.NSNode{
 | 
			
		||||
		Id:          int64(node.Id),
 | 
			
		||||
		Name:        node.Name,
 | 
			
		||||
		StatusJSON:  []byte(node.Status),
 | 
			
		||||
		UniqueId:    node.UniqueId,
 | 
			
		||||
		Secret:      node.Secret,
 | 
			
		||||
		IsInstalled: node.IsInstalled == 1,
 | 
			
		||||
		InstallDir:  node.InstallDir,
 | 
			
		||||
		NsCluster: &pb.NSCluster{
 | 
			
		||||
			Id:   int64(node.ClusterId),
 | 
			
		||||
			Name: clusterName,
 | 
			
		||||
		},
 | 
			
		||||
		InstallStatus: installStatusResult,
 | 
			
		||||
		IsOn:          node.IsOn == 1,
 | 
			
		||||
	}}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
 | 
			
		||||
	rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
)
 | 
			
		||||
@@ -114,6 +115,7 @@ func (this *NSRecordService) ListEnabledNSRecords(ctx context.Context, req *pb.L
 | 
			
		||||
			Ttl:         types.Int32(record.Ttl),
 | 
			
		||||
			Weight:      types.Int32(record.Weight),
 | 
			
		||||
			CreatedAt:   int64(record.CreatedAt),
 | 
			
		||||
			IsOn:        record.IsOn == 1,
 | 
			
		||||
			NsDomain:    nil,
 | 
			
		||||
			NsRoutes:    pbRoutes,
 | 
			
		||||
		})
 | 
			
		||||
@@ -176,7 +178,49 @@ func (this *NSRecordService) FindEnabledNSRecord(ctx context.Context, req *pb.Fi
 | 
			
		||||
		Ttl:         types.Int32(record.Ttl),
 | 
			
		||||
		Weight:      types.Int32(record.Weight),
 | 
			
		||||
		CreatedAt:   int64(record.CreatedAt),
 | 
			
		||||
		IsOn:        record.IsOn == 1,
 | 
			
		||||
		NsDomain:    pbDomain,
 | 
			
		||||
		NsRoutes:    pbRoutes,
 | 
			
		||||
	}}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListNSRecordsAfterVersion 根据版本列出一组记录
 | 
			
		||||
func (this *NSRecordService) ListNSRecordsAfterVersion(ctx context.Context, req *pb.ListNSRecordsAfterVersionRequest) (*pb.ListNSRecordsAfterVersionResponse, error) {
 | 
			
		||||
	_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 集群ID
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	records, err := nameservers.SharedNSRecordDAO.ListRecordsAfterVersion(tx, req.Version, 2000)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var pbRecords []*pb.NSRecord
 | 
			
		||||
	for _, record := range records {
 | 
			
		||||
		// 线路
 | 
			
		||||
		pbRoutes := []*pb.NSRoute{}
 | 
			
		||||
		routeIds := record.DecodeRouteIds()
 | 
			
		||||
		for _, routeId := range routeIds {
 | 
			
		||||
			pbRoutes = append(pbRoutes, &pb.NSRoute{Id: routeId})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		pbRecords = append(pbRecords, &pb.NSRecord{
 | 
			
		||||
			Id:          int64(record.Id),
 | 
			
		||||
			Description: "",
 | 
			
		||||
			Name:        record.Name,
 | 
			
		||||
			Type:        record.Type,
 | 
			
		||||
			Value:       record.Value,
 | 
			
		||||
			Ttl:         types.Int32(record.Ttl),
 | 
			
		||||
			Weight:      types.Int32(record.Weight),
 | 
			
		||||
			IsDeleted:   record.State == nameservers.NSRecordStateDisabled,
 | 
			
		||||
			IsOn:        record.IsOn == 1,
 | 
			
		||||
			Version:     int64(record.Version),
 | 
			
		||||
			NsDomain:    &pb.NSDomain{Id: int64(record.DomainId)},
 | 
			
		||||
			NsRoutes:    pbRoutes,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.ListNSRecordsAfterVersionResponse{NsRecords: pbRecords}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
 | 
			
		||||
	rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -183,3 +184,46 @@ func (this *NSRouteService) UpdateNSRouteOrders(ctx context.Context, req *pb.Upd
 | 
			
		||||
	}
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListNSRoutesAfterVersion 根据版本列出一组线路
 | 
			
		||||
func (this *NSRouteService) ListNSRoutesAfterVersion(ctx context.Context, req *pb.ListNSRoutesAfterVersionRequest) (*pb.ListNSRoutesAfterVersionResponse, error) {
 | 
			
		||||
	_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 集群ID
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	routes, err := nameservers.SharedNSRouteDAO.ListRoutesAfterVersion(tx, req.Version, 2000)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var pbRoutes []*pb.NSRoute
 | 
			
		||||
	for _, route := range routes {
 | 
			
		||||
		// 集群
 | 
			
		||||
		var pbCluster *pb.NSCluster
 | 
			
		||||
		if route.ClusterId > 0 {
 | 
			
		||||
			pbCluster = &pb.NSCluster{Id: int64(route.ClusterId)}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 域名
 | 
			
		||||
		var pbDomain *pb.NSDomain
 | 
			
		||||
		if route.DomainId > 0 {
 | 
			
		||||
			pbDomain = &pb.NSDomain{Id: int64(route.DomainId)}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		pbRoutes = append(pbRoutes, &pb.NSRoute{
 | 
			
		||||
			Id:         int64(route.Id),
 | 
			
		||||
			IsOn:       route.IsOn == 1,
 | 
			
		||||
			Name:       "",
 | 
			
		||||
			RangesJSON: []byte(route.Ranges),
 | 
			
		||||
			IsDeleted:  route.State == nameservers.NSRouteStateDisabled,
 | 
			
		||||
			Order:      int64(route.Order),
 | 
			
		||||
			Version:    int64(route.Version),
 | 
			
		||||
			NsCluster:  pbCluster,
 | 
			
		||||
			NsDomain:   pbDomain,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.ListNSRoutesAfterVersionResponse{NsRoutes: pbRoutes}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,8 @@ import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models/authority"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/encrypt"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
			
		||||
	"github.com/iwind/TeaGo/lists"
 | 
			
		||||
@@ -156,7 +158,24 @@ func ValidateRequest(ctx context.Context, userTypes ...UserType) (userType UserT
 | 
			
		||||
		nodeUserId = clusterId
 | 
			
		||||
	case UserTypeUser:
 | 
			
		||||
	case UserTypeMonitor:
 | 
			
		||||
	case UserTypeAuthority:
 | 
			
		||||
		nodeIntId, err := authority.SharedAuthorityNodeDAO.FindEnabledAuthorityNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeNode, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeNode, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		nodeUserId = nodeIntId
 | 
			
		||||
	case UserTypeDNS:
 | 
			
		||||
		nodeIntId, err := nameservers.SharedNSNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeNode, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeNode, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		nodeUserId = nodeIntId
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if nodeUserId > 0 {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user