mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	阶段性提交
This commit is contained in:
		@@ -19,7 +19,7 @@ const (
 | 
				
			|||||||
	// 其他节点版本号,用来检测是否有需要升级的节点
 | 
						// 其他节点版本号,用来检测是否有需要升级的节点
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	NodeVersion          = "0.5.4"
 | 
						NodeVersion          = "0.5.4"
 | 
				
			||||||
	UserNodeVersion      = "0.5.4"
 | 
						UserNodeVersion      = "0.5.0"
 | 
				
			||||||
	DNSNodeVersion       = "0.2.8"
 | 
						DNSNodeVersion       = "0.2.8"
 | 
				
			||||||
	AuthorityNodeVersion = "0.0.2"
 | 
						AuthorityNodeVersion = "0.0.2"
 | 
				
			||||||
	MonitorNodeVersion   = "0.0.4"
 | 
						MonitorNodeVersion   = "0.0.4"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,22 +13,24 @@ type OrderMethod struct {
 | 
				
			|||||||
	Url         string   `field:"url"`         // URL
 | 
						Url         string   `field:"url"`         // URL
 | 
				
			||||||
	Secret      string   `field:"secret"`      // 密钥
 | 
						Secret      string   `field:"secret"`      // 密钥
 | 
				
			||||||
	Params      dbs.JSON `field:"params"`      // 参数
 | 
						Params      dbs.JSON `field:"params"`      // 参数
 | 
				
			||||||
 | 
						ClientType  string   `field:"clientType"`  // 客户端类型
 | 
				
			||||||
	Order       uint32   `field:"order"`       // 排序
 | 
						Order       uint32   `field:"order"`       // 排序
 | 
				
			||||||
	State       uint8    `field:"state"`       // 状态
 | 
						State       uint8    `field:"state"`       // 状态
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type OrderMethodOperator struct {
 | 
					type OrderMethodOperator struct {
 | 
				
			||||||
	Id          interface{} // ID
 | 
						Id          any // ID
 | 
				
			||||||
	Name        interface{} // 名称
 | 
						Name        any // 名称
 | 
				
			||||||
	IsOn        interface{} // 是否启用
 | 
						IsOn        any // 是否启用
 | 
				
			||||||
	Description interface{} // 描述
 | 
						Description any // 描述
 | 
				
			||||||
	ParentCode  interface{} // 内置的父级代号
 | 
						ParentCode  any // 内置的父级代号
 | 
				
			||||||
	Code        interface{} // 代号
 | 
						Code        any // 代号
 | 
				
			||||||
	Url         interface{} // URL
 | 
						Url         any // URL
 | 
				
			||||||
	Secret      interface{} // 密钥
 | 
						Secret      any // 密钥
 | 
				
			||||||
	Params      interface{} // 参数
 | 
						Params      any // 参数
 | 
				
			||||||
	Order       interface{} // 排序
 | 
						ClientType  any // 客户端类型
 | 
				
			||||||
	State       interface{} // 状态
 | 
						Order       any // 排序
 | 
				
			||||||
 | 
						State       any // 状态
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewOrderMethodOperator() *OrderMethodOperator {
 | 
					func NewOrderMethodOperator() *OrderMethodOperator {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -300,7 +300,7 @@ func (this *UserNodeDAO) CountAllEnabledUserNodesWithSSLPolicyIds(tx *dbs.Tx, ss
 | 
				
			|||||||
	if len(sslPolicyIds) == 0 {
 | 
						if len(sslPolicyIds) == 0 {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	policyStringIds := []string{}
 | 
						var policyStringIds = []string{}
 | 
				
			||||||
	for _, policyId := range sslPolicyIds {
 | 
						for _, policyId := range sslPolicyIds {
 | 
				
			||||||
		policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10))
 | 
							policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -310,3 +310,21 @@ func (this *UserNodeDAO) CountAllEnabledUserNodesWithSSLPolicyIds(tx *dbs.Tx, ss
 | 
				
			|||||||
		Param("policyIds", strings.Join(policyStringIds, ",")).
 | 
							Param("policyIds", strings.Join(policyStringIds, ",")).
 | 
				
			||||||
		Count()
 | 
							Count()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FindUserNodeAccessAddr 获取用户节点访问地址
 | 
				
			||||||
 | 
					func (this *UserNodeDAO) FindUserNodeAccessAddr(tx *dbs.Tx) (string, error) {
 | 
				
			||||||
 | 
						nodes, err := this.ListEnabledUserNodes(tx, 0, 100)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, node := range nodes {
 | 
				
			||||||
 | 
							addrs, err := node.DecodeAccessAddrStrings()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if len(addrs) > 0 {
 | 
				
			||||||
 | 
								return addrs[0], nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return "", nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,21 +22,21 @@ type UserNode struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type UserNodeOperator struct {
 | 
					type UserNodeOperator struct {
 | 
				
			||||||
	Id          interface{} // ID
 | 
						Id          any // ID
 | 
				
			||||||
	IsOn        interface{} // 是否启用
 | 
						IsOn        any // 是否启用
 | 
				
			||||||
	UniqueId    interface{} // 唯一ID
 | 
						UniqueId    any // 唯一ID
 | 
				
			||||||
	Secret      interface{} // 密钥
 | 
						Secret      any // 密钥
 | 
				
			||||||
	Name        interface{} // 名称
 | 
						Name        any // 名称
 | 
				
			||||||
	Description interface{} // 描述
 | 
						Description any // 描述
 | 
				
			||||||
	Http        interface{} // 监听的HTTP配置
 | 
						Http        any // 监听的HTTP配置
 | 
				
			||||||
	Https       interface{} // 监听的HTTPS配置
 | 
						Https       any // 监听的HTTPS配置
 | 
				
			||||||
	AccessAddrs interface{} // 外部访问地址
 | 
						AccessAddrs any // 外部访问地址
 | 
				
			||||||
	Order       interface{} // 排序
 | 
						Order       any // 排序
 | 
				
			||||||
	State       interface{} // 状态
 | 
						State       any // 状态
 | 
				
			||||||
	CreatedAt   interface{} // 创建时间
 | 
						CreatedAt   any // 创建时间
 | 
				
			||||||
	AdminId     interface{} // 管理员ID
 | 
						AdminId     any // 管理员ID
 | 
				
			||||||
	Weight      interface{} // 权重
 | 
						Weight      any // 权重
 | 
				
			||||||
	Status      interface{} // 运行状态
 | 
						Status      any // 运行状态
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewUserNodeOperator() *UserNodeOperator {
 | 
					func NewUserNodeOperator() *UserNodeOperator {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -367,11 +367,6 @@ func (this *APINode) registerServices(server *grpc.Server) {
 | 
				
			|||||||
		pb.RegisterServerBillServiceServer(server, instance)
 | 
							pb.RegisterServerBillServiceServer(server, instance)
 | 
				
			||||||
		this.rest(instance)
 | 
							this.rest(instance)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		var instance = this.serviceInstance(&services.UserNodeService{}).(*services.UserNodeService)
 | 
					 | 
				
			||||||
		pb.RegisterUserNodeServiceServer(server, instance)
 | 
					 | 
				
			||||||
		this.rest(instance)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		var instance = this.serviceInstance(&services.LoginService{}).(*services.LoginService)
 | 
							var instance = this.serviceInstance(&services.LoginService{}).(*services.LoginService)
 | 
				
			||||||
		pb.RegisterLoginServiceServer(server, instance)
 | 
							pb.RegisterLoginServiceServer(server, instance)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,304 +0,0 @@
 | 
				
			|||||||
package services
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"encoding/json"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
					 | 
				
			||||||
	rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
					 | 
				
			||||||
	"google.golang.org/grpc/metadata"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type UserNodeService struct {
 | 
					 | 
				
			||||||
	BaseService
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// CreateUserNode 创建用户节点
 | 
					 | 
				
			||||||
func (this *UserNodeService) CreateUserNode(ctx context.Context, req *pb.CreateUserNodeRequest) (*pb.CreateUserNodeResponse, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	nodeId, err := models.SharedUserNodeDAO.CreateUserNode(tx, req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.AccessAddrsJSON, req.IsOn)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return &pb.CreateUserNodeResponse{UserNodeId: nodeId}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// UpdateUserNode 修改用户节点
 | 
					 | 
				
			||||||
func (this *UserNodeService) UpdateUserNode(ctx context.Context, req *pb.UpdateUserNodeRequest) (*pb.RPCSuccess, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	err = models.SharedUserNodeDAO.UpdateUserNode(tx, req.UserNodeId, req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.AccessAddrsJSON, req.IsOn)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return this.Success()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// DeleteUserNode 删除用户节点
 | 
					 | 
				
			||||||
func (this *UserNodeService) DeleteUserNode(ctx context.Context, req *pb.DeleteUserNodeRequest) (*pb.RPCSuccess, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	err = models.SharedUserNodeDAO.DisableUserNode(tx, req.UserNodeId)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return this.Success()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// FindAllEnabledUserNodes 列出所有可用用户节点
 | 
					 | 
				
			||||||
func (this *UserNodeService) FindAllEnabledUserNodes(ctx context.Context, req *pb.FindAllEnabledUserNodesRequest) (*pb.FindAllEnabledUserNodesResponse, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	nodes, err := models.SharedUserNodeDAO.FindAllEnabledUserNodes(tx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	result := []*pb.UserNode{}
 | 
					 | 
				
			||||||
	for _, node := range nodes {
 | 
					 | 
				
			||||||
		accessAddrs, err := node.DecodeAccessAddrStrings()
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return nil, err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		result = append(result, &pb.UserNode{
 | 
					 | 
				
			||||||
			Id:              int64(node.Id),
 | 
					 | 
				
			||||||
			IsOn:            node.IsOn,
 | 
					 | 
				
			||||||
			UniqueId:        node.UniqueId,
 | 
					 | 
				
			||||||
			Secret:          node.Secret,
 | 
					 | 
				
			||||||
			Name:            node.Name,
 | 
					 | 
				
			||||||
			Description:     node.Description,
 | 
					 | 
				
			||||||
			HttpJSON:        node.Http,
 | 
					 | 
				
			||||||
			HttpsJSON:       node.Https,
 | 
					 | 
				
			||||||
			AccessAddrsJSON: node.AccessAddrs,
 | 
					 | 
				
			||||||
			AccessAddrs:     accessAddrs,
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return &pb.FindAllEnabledUserNodesResponse{UserNodes: result}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// CountAllEnabledUserNodes 计算用户节点数量
 | 
					 | 
				
			||||||
func (this *UserNodeService) CountAllEnabledUserNodes(ctx context.Context, req *pb.CountAllEnabledUserNodesRequest) (*pb.RPCCountResponse, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	count, err := models.SharedUserNodeDAO.CountAllEnabledUserNodes(tx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return this.SuccessCount(count)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ListEnabledUserNodes 列出单页的用户节点
 | 
					 | 
				
			||||||
func (this *UserNodeService) ListEnabledUserNodes(ctx context.Context, req *pb.ListEnabledUserNodesRequest) (*pb.ListEnabledUserNodesResponse, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	nodes, err := models.SharedUserNodeDAO.ListEnabledUserNodes(tx, req.Offset, req.Size)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	result := []*pb.UserNode{}
 | 
					 | 
				
			||||||
	for _, node := range nodes {
 | 
					 | 
				
			||||||
		accessAddrs, err := node.DecodeAccessAddrStrings()
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return nil, err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		result = append(result, &pb.UserNode{
 | 
					 | 
				
			||||||
			Id:              int64(node.Id),
 | 
					 | 
				
			||||||
			IsOn:            node.IsOn,
 | 
					 | 
				
			||||||
			UniqueId:        node.UniqueId,
 | 
					 | 
				
			||||||
			Secret:          node.Secret,
 | 
					 | 
				
			||||||
			Name:            node.Name,
 | 
					 | 
				
			||||||
			Description:     node.Description,
 | 
					 | 
				
			||||||
			HttpJSON:        node.Http,
 | 
					 | 
				
			||||||
			HttpsJSON:       node.Https,
 | 
					 | 
				
			||||||
			AccessAddrsJSON: node.AccessAddrs,
 | 
					 | 
				
			||||||
			AccessAddrs:     accessAddrs,
 | 
					 | 
				
			||||||
			StatusJSON:      node.Status,
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return &pb.ListEnabledUserNodesResponse{UserNodes: result}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// FindEnabledUserNode 根据ID查找节点
 | 
					 | 
				
			||||||
func (this *UserNodeService) FindEnabledUserNode(ctx context.Context, req *pb.FindEnabledUserNodeRequest) (*pb.FindEnabledUserNodeResponse, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	node, err := models.SharedUserNodeDAO.FindEnabledUserNode(tx, req.UserNodeId)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if node == nil {
 | 
					 | 
				
			||||||
		return &pb.FindEnabledUserNodeResponse{UserNode: nil}, nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	accessAddrs, err := node.DecodeAccessAddrStrings()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	result := &pb.UserNode{
 | 
					 | 
				
			||||||
		Id:              int64(node.Id),
 | 
					 | 
				
			||||||
		IsOn:            node.IsOn,
 | 
					 | 
				
			||||||
		UniqueId:        node.UniqueId,
 | 
					 | 
				
			||||||
		Secret:          node.Secret,
 | 
					 | 
				
			||||||
		Name:            node.Name,
 | 
					 | 
				
			||||||
		Description:     node.Description,
 | 
					 | 
				
			||||||
		HttpJSON:        node.Http,
 | 
					 | 
				
			||||||
		HttpsJSON:       node.Https,
 | 
					 | 
				
			||||||
		AccessAddrsJSON: node.AccessAddrs,
 | 
					 | 
				
			||||||
		AccessAddrs:     accessAddrs,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return &pb.FindEnabledUserNodeResponse{UserNode: result}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// FindCurrentUserNode 获取当前用户节点的版本
 | 
					 | 
				
			||||||
func (this *UserNodeService) FindCurrentUserNode(ctx context.Context, req *pb.FindCurrentUserNodeRequest) (*pb.FindCurrentUserNodeResponse, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateUserNode(ctx, false)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	md, ok := metadata.FromIncomingContext(ctx)
 | 
					 | 
				
			||||||
	if !ok {
 | 
					 | 
				
			||||||
		return nil, errors.New("context: need 'nodeId'")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	nodeIds := md.Get("nodeid")
 | 
					 | 
				
			||||||
	if len(nodeIds) == 0 {
 | 
					 | 
				
			||||||
		return nil, errors.New("invalid 'nodeId'")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	nodeId := nodeIds[0]
 | 
					 | 
				
			||||||
	node, err := models.SharedUserNodeDAO.FindEnabledUserNodeWithUniqueId(tx, nodeId)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if node == nil {
 | 
					 | 
				
			||||||
		return &pb.FindCurrentUserNodeResponse{UserNode: nil}, nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	accessAddrs, err := node.DecodeAccessAddrStrings()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	result := &pb.UserNode{
 | 
					 | 
				
			||||||
		Id:              int64(node.Id),
 | 
					 | 
				
			||||||
		IsOn:            node.IsOn,
 | 
					 | 
				
			||||||
		UniqueId:        node.UniqueId,
 | 
					 | 
				
			||||||
		Secret:          node.Secret,
 | 
					 | 
				
			||||||
		Name:            node.Name,
 | 
					 | 
				
			||||||
		Description:     node.Description,
 | 
					 | 
				
			||||||
		HttpJSON:        node.Http,
 | 
					 | 
				
			||||||
		HttpsJSON:       node.Https,
 | 
					 | 
				
			||||||
		AccessAddrsJSON: node.AccessAddrs,
 | 
					 | 
				
			||||||
		AccessAddrs:     accessAddrs,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return &pb.FindCurrentUserNodeResponse{UserNode: result}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// UpdateUserNodeStatus 更新节点状态
 | 
					 | 
				
			||||||
func (this *UserNodeService) UpdateUserNodeStatus(ctx context.Context, req *pb.UpdateUserNodeStatusRequest) (*pb.RPCSuccess, error) {
 | 
					 | 
				
			||||||
	// 校验节点
 | 
					 | 
				
			||||||
	_, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeUser)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if req.UserNodeId > 0 {
 | 
					 | 
				
			||||||
		nodeId = req.UserNodeId
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if nodeId <= 0 {
 | 
					 | 
				
			||||||
		return nil, errors.New("'nodeId' should be greater than 0")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 修改时间戳
 | 
					 | 
				
			||||||
	var nodeStatus = &nodeconfigs.NodeStatus{}
 | 
					 | 
				
			||||||
	err = json.Unmarshal(req.StatusJSON, nodeStatus)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("decode node status json failed: " + err.Error())
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	nodeStatus.UpdatedAt = time.Now().Unix()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 保存
 | 
					 | 
				
			||||||
	err = models.SharedUserNodeDAO.UpdateNodeStatus(tx, nodeId, nodeStatus)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return this.Success()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// CountAllEnabledUserNodesWithSSLCertId 计算使用某个SSL证书的用户节点数量
 | 
					 | 
				
			||||||
func (this *UserNodeService) CountAllEnabledUserNodesWithSSLCertId(ctx context.Context, req *pb.CountAllEnabledUserNodesWithSSLCertIdRequest) (*pb.RPCCountResponse, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
	policyIds, err := models.SharedSSLPolicyDAO.FindAllEnabledPolicyIdsWithCertId(tx, req.SslCertId)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if len(policyIds) == 0 {
 | 
					 | 
				
			||||||
		return this.SuccessCount(0)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	count, err := models.SharedUserNodeDAO.CountAllEnabledUserNodesWithSSLPolicyIds(tx, policyIds)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return this.SuccessCount(count)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user