netmaker/controllers/nodeGrpcController.go

206 lines
5.8 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package controller
import (
2021-05-06 23:57:32 +08:00
"context"
2021-08-03 06:06:26 +08:00
"encoding/json"
"errors"
2021-08-06 02:01:16 +08:00
"strings"
2021-09-18 22:33:14 +08:00
2021-05-06 23:57:32 +08:00
"github.com/gravitl/netmaker/functions"
2021-03-26 00:17:52 +08:00
nodepb "github.com/gravitl/netmaker/grpc"
"github.com/gravitl/netmaker/logic"
2021-03-26 00:17:52 +08:00
"github.com/gravitl/netmaker/models"
)
2021-10-09 03:07:12 +08:00
// NodeServiceServer - represents the service server for gRPC
2021-03-26 00:17:52 +08:00
type NodeServiceServer struct {
nodepb.UnimplementedNodeServiceServer
}
2021-05-06 23:57:32 +08:00
2021-10-09 03:07:12 +08:00
// NodeServiceServer.ReadNode - reads node and responds with gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) ReadNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2021-03-26 00:17:52 +08:00
// convert string id (from proto) to mongoDB ObjectId
2021-08-06 02:15:32 +08:00
macAndNetwork := strings.Split(req.Data, "###")
2021-03-26 00:17:52 +08:00
2021-08-06 02:15:32 +08:00
if len(macAndNetwork) != 2 {
return nil, errors.New("could not read node, invalid node id given")
}
node, err := GetNode(macAndNetwork[0], macAndNetwork[1])
2021-03-26 00:17:52 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2021-08-06 02:19:19 +08:00
node.SetLastCheckIn()
2021-03-26 00:17:52 +08:00
// Cast to ReadNodeRes type
2021-08-03 06:06:26 +08:00
nodeData, err := json.Marshal(&node)
if err != nil {
return nil, err
}
2021-10-27 00:27:29 +08:00
logic.UpdateNode(&node, &node)
2021-08-03 06:06:26 +08:00
response := &nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
2021-03-26 00:17:52 +08:00
}
return response, nil
}
2021-07-22 06:55:19 +08:00
2021-10-09 03:07:12 +08:00
// NodeServiceServer.CreateNode - creates a node and responds over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2021-03-26 00:17:52 +08:00
// Get the protobuf node type from the protobuf request type
// Essentially doing req.Node to access the struct with a nil check
2021-08-03 06:06:26 +08:00
var node models.Node
data := req.GetData()
if err := json.Unmarshal([]byte(data), &node); err != nil {
return nil, err
2021-03-26 00:17:52 +08:00
}
2021-05-06 23:57:32 +08:00
//Check to see if key is valid
//TODO: Triple inefficient!!! This is the third call to the DB we make for networks
2021-10-27 00:27:29 +08:00
validKey := logic.IsKeyValid(node.Network, node.AccessKey)
network, err := logic.GetParentNetwork(node.Network)
2021-05-06 23:57:32 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
}
2021-05-06 23:57:32 +08:00
if !validKey {
//Check to see if network will allow manual sign up
//may want to switch this up with the valid key check and avoid a DB call that way.
2021-07-24 06:24:34 +08:00
if network.AllowManualSignUp == "yes" {
2021-07-27 00:24:36 +08:00
node.IsPending = "yes"
2021-05-06 23:57:32 +08:00
} else {
2021-08-03 06:06:26 +08:00
return nil, errors.New("invalid key, and network does not allow no-key signups")
2021-05-06 23:57:32 +08:00
}
}
2021-03-26 00:17:52 +08:00
node, err = logic.CreateNode(node, node.Network)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2021-08-03 06:06:26 +08:00
nodeData, err := json.Marshal(&node)
2021-03-26 00:17:52 +08:00
// return the node in a CreateNodeRes type
2021-08-03 06:06:26 +08:00
response := &nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
2021-03-26 00:17:52 +08:00
}
err = logic.SetNetworkNodesLastModified(node.Network)
2021-05-06 23:57:32 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-05-06 23:57:32 +08:00
}
2021-03-26 00:17:52 +08:00
2021-05-06 23:57:32 +08:00
return response, nil
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// NodeServiceServer.UpdateNode updates a node and responds over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2021-03-26 00:17:52 +08:00
// Get the node data from the request
2021-08-03 06:06:26 +08:00
var newnode models.Node
if err := json.Unmarshal([]byte(req.GetData()), &newnode); err != nil {
return nil, err
2021-05-06 23:57:32 +08:00
}
2021-07-25 04:13:24 +08:00
macaddress := newnode.MacAddress
networkName := newnode.Network
2021-03-26 00:17:52 +08:00
2021-10-27 00:27:29 +08:00
node, err := logic.GetNodeByMacAddress(networkName, macaddress)
2021-05-06 23:57:32 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-05-06 23:57:32 +08:00
}
2021-10-27 00:27:29 +08:00
err = logic.UpdateNode(&node, &newnode)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2021-08-06 11:38:34 +08:00
nodeData, err := json.Marshal(&newnode)
if err != nil {
return nil, err
}
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
2021-03-26 00:17:52 +08:00
}, nil
}
2021-10-09 03:07:12 +08:00
// NodeServiceServer.DeleteNode - deletes a node and responds over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) DeleteNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
nodeID := req.GetData()
2021-03-26 00:17:52 +08:00
2021-08-03 06:06:26 +08:00
err := DeleteNode(nodeID, true)
2021-07-22 06:55:19 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-03-26 00:17:52 +08:00
}
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
Data: "success",
Type: nodepb.STRING_TYPE,
2021-03-26 00:17:52 +08:00
}, nil
}
2021-10-09 03:07:12 +08:00
// NodeServiceServer.GetPeers - fetches peers over gRPC
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) GetPeers(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2021-08-06 02:01:16 +08:00
macAndNetwork := strings.Split(req.Data, "###")
if len(macAndNetwork) == 2 {
2021-08-06 11:08:59 +08:00
// TODO: Make constant and new variable for isServer
node, err := GetNode(macAndNetwork[0], macAndNetwork[1])
if err != nil {
return nil, err
}
2021-10-09 03:07:12 +08:00
if node.IsServer == "yes" && logic.IsLeader(&node) {
logic.SetNetworkServerPeers(&node)
2021-08-06 11:08:59 +08:00
}
2021-09-18 22:33:14 +08:00
excludeIsRelayed := node.IsRelay != "yes"
var relayedNode string
if node.IsRelayed == "yes" {
2021-09-17 08:00:40 +08:00
relayedNode = node.Address
}
peers, err := logic.GetPeersList(macAndNetwork[1], excludeIsRelayed, relayedNode)
2021-08-06 02:01:16 +08:00
if err != nil {
return nil, err
}
2021-03-26 00:17:52 +08:00
2021-08-06 02:01:16 +08:00
peersData, err := json.Marshal(&peers)
functions.PrintUserLog(node.Address, "checked in successfully", 3)
2021-08-06 02:01:16 +08:00
return &nodepb.Object{
Data: string(peersData),
Type: nodepb.NODE_TYPE,
}, err
2021-05-06 23:57:32 +08:00
}
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
2021-08-06 02:01:16 +08:00
Data: "",
2021-08-03 06:06:26 +08:00
Type: nodepb.NODE_TYPE,
2021-08-06 02:01:16 +08:00
}, errors.New("could not fetch peers, invalid node id")
2021-03-26 00:17:52 +08:00
}
2021-05-20 01:59:10 +08:00
2021-10-09 03:07:12 +08:00
// NodeServiceServer.GetExtPeers - returns ext peers for a gateway node
2021-08-03 06:06:26 +08:00
func (s *NodeServiceServer) GetExtPeers(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
2021-07-22 06:55:19 +08:00
// Initiate a NodeItem type to write decoded data to
//data := &models.PeersResponse{}
// collection.Find returns a cursor for our (empty) query
2021-08-06 02:10:35 +08:00
macAndNetwork := strings.Split(req.Data, "###")
if len(macAndNetwork) != 2 {
return nil, errors.New("did not receive valid node id when fetching ext peers")
2021-08-03 06:06:26 +08:00
}
peers, err := logic.GetExtPeersList(macAndNetwork[0], macAndNetwork[1])
2021-07-22 06:55:19 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-07-22 06:55:19 +08:00
}
2021-08-03 06:06:26 +08:00
var extPeers []models.Node
2021-07-22 06:55:19 +08:00
for i := 0; i < len(peers); i++ {
2021-08-03 06:06:26 +08:00
extPeers = append(extPeers, models.Node{
Address: peers[i].Address,
Address6: peers[i].Address6,
Endpoint: peers[i].Endpoint,
PublicKey: peers[i].PublicKey,
PersistentKeepalive: peers[i].KeepAlive,
ListenPort: peers[i].ListenPort,
LocalAddress: peers[i].LocalAddress,
2021-07-22 06:55:19 +08:00
})
}
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
extData, err := json.Marshal(&extPeers)
2021-07-22 06:55:19 +08:00
if err != nil {
2021-08-03 06:06:26 +08:00
return nil, err
2021-07-22 06:55:19 +08:00
}
2021-08-03 06:06:26 +08:00
return &nodepb.Object{
Data: string(extData),
Type: nodepb.EXT_PEER,
}, nil
2021-05-20 01:59:10 +08:00
}