mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-07 02:20:24 +08:00
提供用户节点上传状态接口
This commit is contained in:
@@ -136,7 +136,7 @@ func (this *UserNodeDAO) FindEnabledUserNodeIdWithAddr(tx *dbs.Tx, protocol stri
|
|||||||
|
|
||||||
// 创建用户节点
|
// 创建用户节点
|
||||||
func (this *UserNodeDAO) CreateUserNode(tx *dbs.Tx, name string, description string, httpJSON []byte, httpsJSON []byte, accessAddrsJSON []byte, isOn bool) (nodeId int64, err error) {
|
func (this *UserNodeDAO) CreateUserNode(tx *dbs.Tx, name string, description string, httpJSON []byte, httpsJSON []byte, accessAddrsJSON []byte, isOn bool) (nodeId int64, err error) {
|
||||||
uniqueId, err := this.genUniqueId(tx)
|
uniqueId, err := this.GenUniqueId(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -216,8 +216,17 @@ func (this *UserNodeDAO) FindEnabledUserNodeWithUniqueId(tx *dbs.Tx, uniqueId st
|
|||||||
return result.(*UserNode), err
|
return result.(*UserNode), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据唯一ID获取节点ID
|
||||||
|
func (this *UserNodeDAO) FindEnabledUserNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("uniqueId", uniqueId).
|
||||||
|
Attr("state", UserNodeStateEnabled).
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
// 生成唯一ID
|
// 生成唯一ID
|
||||||
func (this *UserNodeDAO) genUniqueId(tx *dbs.Tx) (string, error) {
|
func (this *UserNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
||||||
for {
|
for {
|
||||||
uniqueId := rands.HexString(32)
|
uniqueId := rands.HexString(32)
|
||||||
ok, err := this.Query(tx).
|
ok, err := this.Query(tx).
|
||||||
@@ -232,3 +241,12 @@ func (this *UserNodeDAO) genUniqueId(tx *dbs.Tx) (string, error) {
|
|||||||
return uniqueId, nil
|
return uniqueId, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更改节点状态
|
||||||
|
func (this *UserNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Set("status", string(statusJSON)).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,10 +2,20 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/encrypt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseService struct {
|
type BaseService struct {
|
||||||
@@ -72,6 +82,101 @@ func (this *BaseService) ValidateUser(ctx context.Context) (userId int64, err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取节点ID
|
||||||
|
func (this *BaseService) ValidateNodeId(ctx context.Context, roles ...rpcutils.UserType) (role rpcutils.UserType, nodeIntId int64, err error) {
|
||||||
|
if ctx == nil {
|
||||||
|
err = errors.New("context should not be nil")
|
||||||
|
role = rpcutils.UserTypeNone
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
md, ok := metadata.FromIncomingContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("context: need 'nodeId'")
|
||||||
|
}
|
||||||
|
nodeIds := md.Get("nodeid")
|
||||||
|
if len(nodeIds) == 0 || len(nodeIds[0]) == 0 {
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("context: need 'nodeId'")
|
||||||
|
}
|
||||||
|
nodeId := nodeIds[0]
|
||||||
|
|
||||||
|
// 获取角色Node信息
|
||||||
|
// TODO 缓存节点ID相关信息
|
||||||
|
apiToken, err := models.SharedApiTokenDAO.FindEnabledTokenWithNode(nil, nodeId)
|
||||||
|
if err != nil {
|
||||||
|
return rpcutils.UserTypeNone, 0, err
|
||||||
|
}
|
||||||
|
if apiToken == nil {
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("context: can not find api token for node '" + nodeId + "'")
|
||||||
|
}
|
||||||
|
if !lists.ContainsString(roles, apiToken.Role) {
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("context: unsupported role '" + apiToken.Role + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens := md.Get("token")
|
||||||
|
if len(tokens) == 0 || len(tokens[0]) == 0 {
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("context: need 'token'")
|
||||||
|
}
|
||||||
|
token := tokens[0]
|
||||||
|
|
||||||
|
data, err := base64.StdEncoding.DecodeString(token)
|
||||||
|
if err != nil {
|
||||||
|
return rpcutils.UserTypeNone, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
method, err := encrypt.NewMethodInstance(teaconst.EncryptMethod, apiToken.Secret, nodeId)
|
||||||
|
if err != nil {
|
||||||
|
utils.PrintError(err)
|
||||||
|
return rpcutils.UserTypeNone, 0, err
|
||||||
|
}
|
||||||
|
data, err = method.Decrypt(data)
|
||||||
|
if err != nil {
|
||||||
|
return rpcutils.UserTypeNone, 0, err
|
||||||
|
}
|
||||||
|
if len(data) == 0 {
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("invalid token")
|
||||||
|
}
|
||||||
|
|
||||||
|
m := maps.Map{}
|
||||||
|
err = json.Unmarshal(data, &m)
|
||||||
|
if err != nil {
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("decode token error: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
timestamp := m.GetInt64("timestamp")
|
||||||
|
if time.Now().Unix()-timestamp > 600 {
|
||||||
|
// 请求超过10分钟认为超时
|
||||||
|
return rpcutils.UserTypeNone, 0, errors.New("authenticate timeout")
|
||||||
|
}
|
||||||
|
|
||||||
|
switch apiToken.Role {
|
||||||
|
case rpcutils.UserTypeNode:
|
||||||
|
nodeIntId, err = models.SharedNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
|
||||||
|
if err != nil {
|
||||||
|
return rpcutils.UserTypeNode, 0, errors.New("context: " + err.Error())
|
||||||
|
}
|
||||||
|
if nodeIntId <= 0 {
|
||||||
|
return rpcutils.UserTypeNode, 0, errors.New("context: not found node with id '" + nodeId + "'")
|
||||||
|
}
|
||||||
|
case rpcutils.UserTypeCluster:
|
||||||
|
nodeIntId, err = models.SharedNodeClusterDAO.FindEnabledClusterIdWithUniqueId(nil, nodeId)
|
||||||
|
if err != nil {
|
||||||
|
return rpcutils.UserTypeCluster, 0, errors.New("context: " + err.Error())
|
||||||
|
}
|
||||||
|
if nodeIntId <= 0 {
|
||||||
|
return rpcutils.UserTypeCluster, 0, errors.New("context: not found cluster with id '" + nodeId + "'")
|
||||||
|
}
|
||||||
|
case rpcutils.UserTypeUser:
|
||||||
|
nodeIntId, err = models.SharedUserNodeDAO.FindEnabledUserNodeIdWithUniqueId(nil, nodeId)
|
||||||
|
case rpcutils.UserTypeAdmin:
|
||||||
|
nodeIntId = 0
|
||||||
|
default:
|
||||||
|
err = errors.New("unsupported user role '" + apiToken.Role + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 返回成功
|
// 返回成功
|
||||||
func (this *BaseService) Success() (*pb.RPCSuccess, error) {
|
func (this *BaseService) Success() (*pb.RPCSuccess, error) {
|
||||||
return &pb.RPCSuccess{}, nil
|
return &pb.RPCSuccess{}, nil
|
||||||
|
|||||||
@@ -151,6 +151,7 @@ func (this *UserNodeService) ListEnabledUserNodes(ctx context.Context, req *pb.L
|
|||||||
HttpsJSON: []byte(node.Https),
|
HttpsJSON: []byte(node.Https),
|
||||||
AccessAddrsJSON: []byte(node.AccessAddrs),
|
AccessAddrsJSON: []byte(node.AccessAddrs),
|
||||||
AccessAddrs: accessAddrs,
|
AccessAddrs: accessAddrs,
|
||||||
|
StatusJSON: []byte(node.Status),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,3 +242,28 @@ func (this *UserNodeService) FindCurrentUserNode(ctx context.Context, req *pb.Fi
|
|||||||
}
|
}
|
||||||
return &pb.FindCurrentUserNodeResponse{Node: result}, nil
|
return &pb.FindCurrentUserNodeResponse{Node: result}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新节点状态
|
||||||
|
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.NodeId > 0 {
|
||||||
|
nodeId = req.NodeId
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user