2020-12-14 21:25:11 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"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/rpc/pb"
|
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UserNodeService struct {
|
|
|
|
|
BaseService
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// CreateUserNode 创建用户节点
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) CreateUserNode(ctx context.Context, req *pb.CreateUserNodeRequest) (*pb.CreateUserNodeResponse, error) {
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
nodeId, err := models.SharedUserNodeDAO.CreateUserNode(tx, req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.AccessAddrsJSON, req.IsOn)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 17:56:35 +08:00
|
|
|
return &pb.CreateUserNodeResponse{UserNodeId: nodeId}, nil
|
2020-12-14 21:25:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// UpdateUserNode 修改用户节点
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) UpdateUserNode(ctx context.Context, req *pb.UpdateUserNodeRequest) (*pb.RPCSuccess, error) {
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-05 17:56:35 +08:00
|
|
|
err = models.SharedUserNodeDAO.UpdateUserNode(tx, req.UserNodeId, req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.AccessAddrsJSON, req.IsOn)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// DeleteUserNode 删除用户节点
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) DeleteUserNode(ctx context.Context, req *pb.DeleteUserNodeRequest) (*pb.RPCSuccess, error) {
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-05 17:56:35 +08:00
|
|
|
err = models.SharedUserNodeDAO.DisableUserNode(tx, req.UserNodeId)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// FindAllEnabledUserNodes 列出所有可用用户节点
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) FindAllEnabledUserNodes(ctx context.Context, req *pb.FindAllEnabledUserNodesRequest) (*pb.FindAllEnabledUserNodesResponse, error) {
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
nodes, err := models.SharedUserNodeDAO.FindAllEnabledUserNodes(tx)
|
2020-12-14 21:25:11 +08:00
|
|
|
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 == 1,
|
|
|
|
|
UniqueId: node.UniqueId,
|
|
|
|
|
Secret: node.Secret,
|
|
|
|
|
Name: node.Name,
|
|
|
|
|
Description: node.Description,
|
2022-03-22 19:30:30 +08:00
|
|
|
HttpJSON: node.Http,
|
|
|
|
|
HttpsJSON: node.Https,
|
|
|
|
|
AccessAddrsJSON: node.AccessAddrs,
|
2020-12-14 21:25:11 +08:00
|
|
|
AccessAddrs: accessAddrs,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 17:56:35 +08:00
|
|
|
return &pb.FindAllEnabledUserNodesResponse{UserNodes: result}, nil
|
2020-12-14 21:25:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// CountAllEnabledUserNodes 计算用户节点数量
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) CountAllEnabledUserNodes(ctx context.Context, req *pb.CountAllEnabledUserNodesRequest) (*pb.RPCCountResponse, error) {
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
count, err := models.SharedUserNodeDAO.CountAllEnabledUserNodes(tx)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.SuccessCount(count)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// ListEnabledUserNodes 列出单页的用户节点
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) ListEnabledUserNodes(ctx context.Context, req *pb.ListEnabledUserNodesRequest) (*pb.ListEnabledUserNodesResponse, error) {
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
nodes, err := models.SharedUserNodeDAO.ListEnabledUserNodes(tx, req.Offset, req.Size)
|
2020-12-14 21:25:11 +08:00
|
|
|
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 == 1,
|
|
|
|
|
UniqueId: node.UniqueId,
|
|
|
|
|
Secret: node.Secret,
|
|
|
|
|
Name: node.Name,
|
|
|
|
|
Description: node.Description,
|
2022-03-22 19:30:30 +08:00
|
|
|
HttpJSON: node.Http,
|
|
|
|
|
HttpsJSON: node.Https,
|
|
|
|
|
AccessAddrsJSON: node.AccessAddrs,
|
2020-12-14 21:25:11 +08:00
|
|
|
AccessAddrs: accessAddrs,
|
2022-03-22 19:30:30 +08:00
|
|
|
StatusJSON: node.Status,
|
2020-12-14 21:25:11 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 17:56:35 +08:00
|
|
|
return &pb.ListEnabledUserNodesResponse{UserNodes: result}, nil
|
2020-12-14 21:25:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// FindEnabledUserNode 根据ID查找节点
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) FindEnabledUserNode(ctx context.Context, req *pb.FindEnabledUserNodeRequest) (*pb.FindEnabledUserNodeResponse, error) {
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-05 17:56:35 +08:00
|
|
|
node, err := models.SharedUserNodeDAO.FindEnabledUserNode(tx, req.UserNodeId)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if node == nil {
|
2021-11-05 17:56:35 +08:00
|
|
|
return &pb.FindEnabledUserNodeResponse{UserNode: nil}, nil
|
2020-12-14 21:25:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accessAddrs, err := node.DecodeAccessAddrStrings()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := &pb.UserNode{
|
|
|
|
|
Id: int64(node.Id),
|
|
|
|
|
IsOn: node.IsOn == 1,
|
|
|
|
|
UniqueId: node.UniqueId,
|
|
|
|
|
Secret: node.Secret,
|
|
|
|
|
Name: node.Name,
|
|
|
|
|
Description: node.Description,
|
2022-03-22 19:30:30 +08:00
|
|
|
HttpJSON: node.Http,
|
|
|
|
|
HttpsJSON: node.Https,
|
|
|
|
|
AccessAddrsJSON: node.AccessAddrs,
|
2020-12-14 21:25:11 +08:00
|
|
|
AccessAddrs: accessAddrs,
|
|
|
|
|
}
|
2021-11-05 17:56:35 +08:00
|
|
|
return &pb.FindEnabledUserNodeResponse{UserNode: result}, nil
|
2020-12-14 21:25:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// FindCurrentUserNode 获取当前用户节点的版本
|
2020-12-14 21:25:11 +08:00
|
|
|
func (this *UserNodeService) FindCurrentUserNode(ctx context.Context, req *pb.FindCurrentUserNodeRequest) (*pb.FindCurrentUserNodeResponse, error) {
|
2021-07-22 18:42:57 +08:00
|
|
|
_, err := this.ValidateUserNode(ctx)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2020-12-14 21:25:11 +08:00
|
|
|
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]
|
2021-01-01 23:31:30 +08:00
|
|
|
node, err := models.SharedUserNodeDAO.FindEnabledUserNodeWithUniqueId(tx, nodeId)
|
2020-12-14 21:25:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if node == nil {
|
2021-11-05 17:56:35 +08:00
|
|
|
return &pb.FindCurrentUserNodeResponse{UserNode: nil}, nil
|
2020-12-14 21:25:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accessAddrs, err := node.DecodeAccessAddrStrings()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := &pb.UserNode{
|
|
|
|
|
Id: int64(node.Id),
|
|
|
|
|
IsOn: node.IsOn == 1,
|
|
|
|
|
UniqueId: node.UniqueId,
|
|
|
|
|
Secret: node.Secret,
|
|
|
|
|
Name: node.Name,
|
|
|
|
|
Description: node.Description,
|
2022-03-22 19:30:30 +08:00
|
|
|
HttpJSON: node.Http,
|
|
|
|
|
HttpsJSON: node.Https,
|
|
|
|
|
AccessAddrsJSON: node.AccessAddrs,
|
2020-12-14 21:25:11 +08:00
|
|
|
AccessAddrs: accessAddrs,
|
|
|
|
|
}
|
2021-11-05 17:56:35 +08:00
|
|
|
return &pb.FindCurrentUserNodeResponse{UserNode: result}, nil
|
2020-12-14 21:25:11 +08:00
|
|
|
}
|
2021-01-20 10:08:06 +08:00
|
|
|
|
2021-07-11 18:05:57 +08:00
|
|
|
// UpdateUserNodeStatus 更新节点状态
|
2021-01-20 10:08:06 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 17:56:35 +08:00
|
|
|
if req.UserNodeId > 0 {
|
|
|
|
|
nodeId = req.UserNodeId
|
2021-01-20 10:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if nodeId <= 0 {
|
|
|
|
|
return nil, errors.New("'nodeId' should be greater than 0")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
err = models.SharedUserNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
2021-11-05 17:56:35 +08:00
|
|
|
|
|
|
|
|
// CountAllEnabledUserNodesWithSSLCertId 计算使用某个SSL证书的用户节点数量
|
|
|
|
|
func (this *UserNodeService) CountAllEnabledUserNodesWithSSLCertId(ctx context.Context, req *pb.CountAllEnabledUserNodesWithSSLCertIdRequest) (*pb.RPCCountResponse, error) {
|
|
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
|
|
|
|
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)
|
|
|
|
|
}
|