mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	[域名服务]实现基本的线路配置
This commit is contained in:
		
							
								
								
									
										6
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								go.sum
									
									
									
									
									
								
							@@ -175,12 +175,6 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
 | 
				
			|||||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
 | 
					github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
 | 
				
			||||||
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
 | 
					github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
					github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210125103732-4d79fc3c3d0b h1:niQL2t//041GUaqDPM5D+ldyr0Ng2WKwZJHPRLQhQtk=
 | 
					 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210125103732-4d79fc3c3d0b/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
					 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210302120856-7588e79bdbe3 h1:k9K3HHMmkF7HYyIHz21AtmYH4Zdk/8OI98a8P0O8o1I=
 | 
					 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210302120856-7588e79bdbe3/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
					 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210325033016-3279bdaa087d h1:FQTYJmZeCMdwM0Bz+C4h31SDBt04ap6A4JOjm+FfYwk=
 | 
					 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210325033016-3279bdaa087d/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
					 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f h1:r2O8PONj/KiuZjJHVHn7KlCePUIjNtgAmvLfgRafQ8o=
 | 
					github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f h1:r2O8PONj/KiuZjJHVHn7KlCePUIjNtgAmvLfgRafQ8o=
 | 
				
			||||||
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
					github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
				
			||||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
 | 
					github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ import (
 | 
				
			|||||||
	_ "github.com/go-sql-driver/mysql"
 | 
						_ "github.com/go-sql-driver/mysql"
 | 
				
			||||||
	"github.com/iwind/TeaGo/Tea"
 | 
						"github.com/iwind/TeaGo/Tea"
 | 
				
			||||||
	"github.com/iwind/TeaGo/dbs"
 | 
						"github.com/iwind/TeaGo/dbs"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@@ -132,11 +133,11 @@ func (this *NSRecordDAO) CountAllEnabledRecords(tx *dbs.Tx, domainId int64, dnsT
 | 
				
			|||||||
		query.Attr("type", dnsType)
 | 
							query.Attr("type", dnsType)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if len(keyword) > 0 {
 | 
						if len(keyword) > 0 {
 | 
				
			||||||
		query.Where("(name LIKE :keyword OR description LIKE :keyword)").
 | 
							query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
 | 
				
			||||||
			Param("keyword", "%"+keyword+"%")
 | 
								Param("keyword", "%"+keyword+"%")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if routeId > 0 {
 | 
						if routeId > 0 {
 | 
				
			||||||
		query.JSONContains("routeIds", routeId)
 | 
							query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return query.Count()
 | 
						return query.Count()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -149,11 +150,11 @@ func (this *NSRecordDAO) ListAllEnabledRecords(tx *dbs.Tx, domainId int64, dnsTy
 | 
				
			|||||||
		query.Attr("type", dnsType)
 | 
							query.Attr("type", dnsType)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if len(keyword) > 0 {
 | 
						if len(keyword) > 0 {
 | 
				
			||||||
		query.Where("(name LIKE :keyword OR description LIKE :keyword)").
 | 
							query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
 | 
				
			||||||
			Param("keyword", "%"+keyword+"%")
 | 
								Param("keyword", "%"+keyword+"%")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if routeId > 0 {
 | 
						if routeId > 0 {
 | 
				
			||||||
		query.JSONContains("routeIds", routeId)
 | 
							query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	_, err = query.
 | 
						_, err = query.
 | 
				
			||||||
		DescPk().
 | 
							DescPk().
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package nameservers
 | 
					package nameservers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
				
			||||||
	_ "github.com/go-sql-driver/mysql"
 | 
						_ "github.com/go-sql-driver/mysql"
 | 
				
			||||||
	"github.com/iwind/TeaGo/Tea"
 | 
						"github.com/iwind/TeaGo/Tea"
 | 
				
			||||||
	"github.com/iwind/TeaGo/dbs"
 | 
						"github.com/iwind/TeaGo/dbs"
 | 
				
			||||||
@@ -33,7 +34,7 @@ func init() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// EnableNSRoute 启用条目
 | 
					// EnableNSRoute 启用条目
 | 
				
			||||||
func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id uint32) error {
 | 
					func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id int64) error {
 | 
				
			||||||
	_, err := this.Query(tx).
 | 
						_, err := this.Query(tx).
 | 
				
			||||||
		Pk(id).
 | 
							Pk(id).
 | 
				
			||||||
		Set("state", NSRouteStateEnabled).
 | 
							Set("state", NSRouteStateEnabled).
 | 
				
			||||||
@@ -42,7 +43,7 @@ func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id uint32) error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DisableNSRoute 禁用条目
 | 
					// DisableNSRoute 禁用条目
 | 
				
			||||||
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, id uint32) error {
 | 
					func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, id int64) error {
 | 
				
			||||||
	_, err := this.Query(tx).
 | 
						_, err := this.Query(tx).
 | 
				
			||||||
		Pk(id).
 | 
							Pk(id).
 | 
				
			||||||
		Set("state", NSRouteStateDisabled).
 | 
							Set("state", NSRouteStateDisabled).
 | 
				
			||||||
@@ -63,9 +64,81 @@ func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id int64) (*NSRoute, erro
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FindNSRouteName 根据主键查找名称
 | 
					// FindNSRouteName 根据主键查找名称
 | 
				
			||||||
func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id uint32) (string, error) {
 | 
					func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
 | 
				
			||||||
	return this.Query(tx).
 | 
						return this.Query(tx).
 | 
				
			||||||
		Pk(id).
 | 
							Pk(id).
 | 
				
			||||||
		Result("name").
 | 
							Result("name").
 | 
				
			||||||
		FindStringCol("")
 | 
							FindStringCol("")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// CreateRoute 创建线路
 | 
				
			||||||
 | 
					func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64, userId int64, name string, rangesJSON []byte) (int64, error) {
 | 
				
			||||||
 | 
						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
 | 
				
			||||||
 | 
						return this.SaveInt64(tx, op)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateRoute 修改线路
 | 
				
			||||||
 | 
					func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rangesJSON []byte) error {
 | 
				
			||||||
 | 
						if routeId <= 0 {
 | 
				
			||||||
 | 
							return errors.New("invalid routeId")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						op := NewNSRouteOperator()
 | 
				
			||||||
 | 
						op.Id = routeId
 | 
				
			||||||
 | 
						op.Name = name
 | 
				
			||||||
 | 
						if len(rangesJSON) > 0 {
 | 
				
			||||||
 | 
							op.Ranges = rangesJSON
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							op.Ranges = "[]"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return this.Save(tx, op)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateRouteOrders 修改线路排序
 | 
				
			||||||
 | 
					func (this *NSRouteDAO) UpdateRouteOrders(tx *dbs.Tx, routeIds []int64) error {
 | 
				
			||||||
 | 
						order := len(routeIds)
 | 
				
			||||||
 | 
						for _, routeId := range routeIds {
 | 
				
			||||||
 | 
							_, err := this.Query(tx).
 | 
				
			||||||
 | 
								Pk(routeId).
 | 
				
			||||||
 | 
								Set("order", order).
 | 
				
			||||||
 | 
								Update()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							order--
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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").
 | 
				
			||||||
 | 
							DescPk()
 | 
				
			||||||
 | 
						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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,23 +3,25 @@ package nameservers
 | 
				
			|||||||
// NSRoute DNS线路
 | 
					// NSRoute DNS线路
 | 
				
			||||||
type NSRoute struct {
 | 
					type NSRoute struct {
 | 
				
			||||||
	Id        uint32 `field:"id"`        // ID
 | 
						Id        uint32 `field:"id"`        // ID
 | 
				
			||||||
 | 
						IsOn      uint8  `field:"isOn"`      // 是否启用
 | 
				
			||||||
	ClusterId uint32 `field:"clusterId"` // 集群ID
 | 
						ClusterId uint32 `field:"clusterId"` // 集群ID
 | 
				
			||||||
	DomainId  uint32 `field:"domainId"`  // 域名ID
 | 
						DomainId  uint32 `field:"domainId"`  // 域名ID
 | 
				
			||||||
	UserId    uint32 `field:"userId"`    // 用户ID
 | 
						UserId    uint32 `field:"userId"`    // 用户ID
 | 
				
			||||||
	Name      string `field:"name"`      // 名称
 | 
						Name      string `field:"name"`      // 名称
 | 
				
			||||||
	Conds     string `field:"conds"`     // 条件定义
 | 
						Ranges    string `field:"ranges"`    // 范围
 | 
				
			||||||
	IsOn      uint8  `field:"isOn"`      // 是否启用
 | 
						Order     uint32 `field:"order"`     // 排序
 | 
				
			||||||
	State     uint8  `field:"state"`     // 状态
 | 
						State     uint8  `field:"state"`     // 状态
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type NSRouteOperator struct {
 | 
					type NSRouteOperator struct {
 | 
				
			||||||
	Id        interface{} // ID
 | 
						Id        interface{} // ID
 | 
				
			||||||
 | 
						IsOn      interface{} // 是否启用
 | 
				
			||||||
	ClusterId interface{} // 集群ID
 | 
						ClusterId interface{} // 集群ID
 | 
				
			||||||
	DomainId  interface{} // 域名ID
 | 
						DomainId  interface{} // 域名ID
 | 
				
			||||||
	UserId    interface{} // 用户ID
 | 
						UserId    interface{} // 用户ID
 | 
				
			||||||
	Name      interface{} // 名称
 | 
						Name      interface{} // 名称
 | 
				
			||||||
	Conds     interface{} // 条件定义
 | 
						Ranges    interface{} // 范围
 | 
				
			||||||
	IsOn      interface{} // 是否启用
 | 
						Order     interface{} // 排序
 | 
				
			||||||
	State     interface{} // 状态
 | 
						State     interface{} // 状态
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -94,4 +94,5 @@ func (this *APINode) registerServices(server *grpc.Server) {
 | 
				
			|||||||
	pb.RegisterNSNodeServiceServer(server, &nameservers.NSNodeService{})
 | 
						pb.RegisterNSNodeServiceServer(server, &nameservers.NSNodeService{})
 | 
				
			||||||
	pb.RegisterNSDomainServiceServer(server, &nameservers.NSDomainService{})
 | 
						pb.RegisterNSDomainServiceServer(server, &nameservers.NSDomainService{})
 | 
				
			||||||
	pb.RegisterNSRecordServiceServer(server, &nameservers.NSRecordService{})
 | 
						pb.RegisterNSRecordServiceServer(server, &nameservers.NSRecordService{})
 | 
				
			||||||
 | 
						pb.RegisterNSRouteServiceServer(server, &nameservers.NSRouteService{})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										185
									
								
								internal/rpc/services/nameservers/service_ns_route.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										185
									
								
								internal/rpc/services/nameservers/service_ns_route.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,185 @@
 | 
				
			|||||||
 | 
					// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package nameservers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// NSRouteService 线路相关服务
 | 
				
			||||||
 | 
					type NSRouteService struct {
 | 
				
			||||||
 | 
						services.BaseService
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// CreateNSRoute 创建线路
 | 
				
			||||||
 | 
					func (this *NSRouteService) CreateNSRoute(ctx context.Context, req *pb.CreateNSRouteRequest) (*pb.CreateNSRouteResponse, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx, 0)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						routeId, err := nameservers.SharedNSRouteDAO.CreateRoute(tx, req.NsClusterId, req.NsDomainId, req.UserId, req.Name, req.RangesJSON)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return &pb.CreateNSRouteResponse{NsRouteId: routeId}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateNSRoute 修改线路
 | 
				
			||||||
 | 
					func (this *NSRouteService) UpdateNSRoute(ctx context.Context, req *pb.UpdateNSRouteRequest) (*pb.RPCSuccess, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx, 0)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						err = nameservers.SharedNSRouteDAO.UpdateRoute(tx, req.NsRouteId, req.Name, req.RangesJSON)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return this.Success()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// DeleteNSRoute 删除线路
 | 
				
			||||||
 | 
					func (this *NSRouteService) DeleteNSRoute(ctx context.Context, req *pb.DeleteNSRouteRequest) (*pb.RPCSuccess, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx, 0)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						err = nameservers.SharedNSRouteDAO.DisableNSRoute(tx, req.NsRouteId)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return this.Success()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FindEnabledNSRoute 获取单个路线信息
 | 
				
			||||||
 | 
					func (this *NSRouteService) FindEnabledNSRoute(ctx context.Context, req *pb.FindEnabledNSRouteRequest) (*pb.FindEnabledNSRouteResponse, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx, 0)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						route, err := nameservers.SharedNSRouteDAO.FindEnabledNSRoute(tx, req.NsRouteId)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if route == nil {
 | 
				
			||||||
 | 
							return &pb.FindEnabledNSRouteResponse{NsRoute: nil}, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 集群
 | 
				
			||||||
 | 
						var pbCluster *pb.NSCluster
 | 
				
			||||||
 | 
						if route.ClusterId > 0 {
 | 
				
			||||||
 | 
							cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(route.ClusterId))
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if cluster != nil {
 | 
				
			||||||
 | 
								pbCluster = &pb.NSCluster{
 | 
				
			||||||
 | 
									Id:   int64(cluster.Id),
 | 
				
			||||||
 | 
									IsOn: cluster.IsOn == 1,
 | 
				
			||||||
 | 
									Name: cluster.Name,
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 域名
 | 
				
			||||||
 | 
						var pbDomain *pb.NSDomain
 | 
				
			||||||
 | 
						if route.DomainId > 0 {
 | 
				
			||||||
 | 
							domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, int64(route.DomainId))
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if domain != nil {
 | 
				
			||||||
 | 
								pbDomain = &pb.NSDomain{
 | 
				
			||||||
 | 
									Id:   int64(domain.Id),
 | 
				
			||||||
 | 
									Name: domain.Name,
 | 
				
			||||||
 | 
									IsOn: domain.IsOn == 1,
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &pb.FindEnabledNSRouteResponse{NsRoute: &pb.NSRoute{
 | 
				
			||||||
 | 
							Id:         int64(route.Id),
 | 
				
			||||||
 | 
							IsOn:       route.IsOn == 1,
 | 
				
			||||||
 | 
							Name:       route.Name,
 | 
				
			||||||
 | 
							RangesJSON: []byte(route.Ranges),
 | 
				
			||||||
 | 
							NsCluster:  pbCluster,
 | 
				
			||||||
 | 
							NsDomain:   pbDomain,
 | 
				
			||||||
 | 
						}}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FindAllEnabledNSRoutes 读取所有线路
 | 
				
			||||||
 | 
					func (this *NSRouteService) FindAllEnabledNSRoutes(ctx context.Context, req *pb.FindAllEnabledNSRoutesRequest) (*pb.FindAllEnabledNSRoutesResponse, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx, 0)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						routes, err := nameservers.SharedNSRouteDAO.FindAllEnabledRoutes(tx, req.NsClusterId, req.NsDomainId, req.UserId)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var pbRoutes = []*pb.NSRoute{}
 | 
				
			||||||
 | 
						for _, route := range routes {
 | 
				
			||||||
 | 
							// 集群
 | 
				
			||||||
 | 
							var pbCluster *pb.NSCluster
 | 
				
			||||||
 | 
							if route.ClusterId > 0 {
 | 
				
			||||||
 | 
								cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(route.ClusterId))
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if cluster != nil {
 | 
				
			||||||
 | 
									pbCluster = &pb.NSCluster{
 | 
				
			||||||
 | 
										Id:   int64(cluster.Id),
 | 
				
			||||||
 | 
										IsOn: cluster.IsOn == 1,
 | 
				
			||||||
 | 
										Name: cluster.Name,
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 域名
 | 
				
			||||||
 | 
							var pbDomain *pb.NSDomain
 | 
				
			||||||
 | 
							if route.DomainId > 0 {
 | 
				
			||||||
 | 
								domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, int64(route.DomainId))
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if domain != nil {
 | 
				
			||||||
 | 
									pbDomain = &pb.NSDomain{
 | 
				
			||||||
 | 
										Id:   int64(domain.Id),
 | 
				
			||||||
 | 
										Name: domain.Name,
 | 
				
			||||||
 | 
										IsOn: domain.IsOn == 1,
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							pbRoutes = append(pbRoutes, &pb.NSRoute{
 | 
				
			||||||
 | 
								Id:         int64(route.Id),
 | 
				
			||||||
 | 
								IsOn:       route.IsOn == 1,
 | 
				
			||||||
 | 
								Name:       route.Name,
 | 
				
			||||||
 | 
								RangesJSON: []byte(route.Ranges),
 | 
				
			||||||
 | 
								NsCluster:  pbCluster,
 | 
				
			||||||
 | 
								NsDomain:   pbDomain,
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return &pb.FindAllEnabledNSRoutesResponse{NsRoutes: pbRoutes}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateNSRouteOrders 设置线路排序
 | 
				
			||||||
 | 
					func (this *NSRouteService) UpdateNSRouteOrders(ctx context.Context, req *pb.UpdateNSRouteOrdersRequest) (*pb.RPCSuccess, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx, 0)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						err = nameservers.SharedNSRouteDAO.UpdateRouteOrders(tx, req.NsRouteIds)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return this.Success()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user