node_grpc.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. nodepb "github.com/gravitl/netmaker/grpc"
  8. "github.com/gravitl/netmaker/logger"
  9. "github.com/gravitl/netmaker/logic"
  10. "github.com/gravitl/netmaker/models"
  11. "github.com/gravitl/netmaker/servercfg"
  12. )
  13. // NodeServiceServer - represents the service server for gRPC
  14. type NodeServiceServer struct {
  15. nodepb.UnimplementedNodeServiceServer
  16. }
  17. // NodeServiceServer.ReadNode - reads node and responds with gRPC
  18. func (s *NodeServiceServer) ReadNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
  19. var node, err = getNewOrLegacyNode(req.Data)
  20. if err != nil {
  21. return nil, err
  22. }
  23. node.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
  24. if err != nil {
  25. return nil, err
  26. }
  27. node.SetLastCheckIn()
  28. // Cast to ReadNodeRes type
  29. nodeData, errN := json.Marshal(&node)
  30. if errN != nil {
  31. return nil, err
  32. }
  33. logic.UpdateNode(&node, &node)
  34. response := &nodepb.Object{
  35. Data: string(nodeData),
  36. Type: nodepb.NODE_TYPE,
  37. }
  38. return response, nil
  39. }
  40. // NodeServiceServer.CreateNode - creates a node and responds over gRPC
  41. func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
  42. var node = models.Node{}
  43. var err error
  44. data := req.GetData()
  45. if err := json.Unmarshal([]byte(data), &node); err != nil {
  46. return nil, err
  47. }
  48. validKey := logic.IsKeyValid(node.Network, node.AccessKey)
  49. node.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if !validKey {
  54. if node.NetworkSettings.AllowManualSignUp == "yes" {
  55. node.IsPending = "yes"
  56. } else {
  57. return nil, errors.New("invalid key, and network does not allow no-key signups")
  58. }
  59. }
  60. err = logic.CreateNode(&node)
  61. if err != nil {
  62. return nil, err
  63. }
  64. nodeData, errN := json.Marshal(&node)
  65. if errN != nil {
  66. return nil, err
  67. }
  68. response := &nodepb.Object{
  69. Data: string(nodeData),
  70. Type: nodepb.NODE_TYPE,
  71. }
  72. err = logic.SetNetworkNodesLastModified(node.Network)
  73. if err != nil {
  74. return nil, err
  75. }
  76. err = runServerPeerUpdate(node.Network, true)
  77. if err != nil {
  78. logger.Log(1, "internal error when setting peers after node,", node.ID, "was created (gRPC)")
  79. }
  80. logger.Log(0, "new node,", node.Name, ", added on network,"+node.Network)
  81. return response, nil
  82. }
  83. // NodeServiceServer.UpdateNode updates a node and responds over gRPC
  84. func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
  85. var newnode models.Node
  86. if err := json.Unmarshal([]byte(req.GetData()), &newnode); err != nil {
  87. return nil, err
  88. }
  89. node, err := logic.GetNodeByIDorMacAddress(newnode.ID, newnode.MacAddress, newnode.Network)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if !servercfg.GetRce() {
  94. newnode.PostDown = node.PostDown
  95. newnode.PostUp = node.PostUp
  96. }
  97. var shouldPeersUpdate = logic.ShouldPeersUpdate(&node, &newnode)
  98. err = logic.UpdateNode(&node, &newnode)
  99. if err != nil {
  100. return nil, err
  101. }
  102. newnode.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
  103. if err != nil {
  104. return nil, err
  105. }
  106. nodeData, errN := json.Marshal(&newnode)
  107. if errN != nil {
  108. return nil, err
  109. }
  110. err = runServerPeerUpdate(newnode.Network, shouldPeersUpdate)
  111. if err != nil {
  112. logger.Log(1, "could not update peers on gRPC after node,", newnode.ID, "updated (gRPC), \nerror:", err.Error())
  113. }
  114. return &nodepb.Object{
  115. Data: string(nodeData),
  116. Type: nodepb.NODE_TYPE,
  117. }, nil
  118. }
  119. // NodeServiceServer.DeleteNode - deletes a node and responds over gRPC
  120. func (s *NodeServiceServer) DeleteNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
  121. var node, err = getNewOrLegacyNode(req.Data)
  122. if err != nil {
  123. return nil, err
  124. }
  125. err = logic.DeleteNodeByID(&node, true)
  126. if err != nil {
  127. return nil, err
  128. }
  129. err = runServerPeerUpdate(node.Network, true)
  130. if err != nil {
  131. logger.Log(1, "internal error when setting peers after deleting node:", node.ID, "over gRPC")
  132. }
  133. return &nodepb.Object{
  134. Data: "success",
  135. Type: nodepb.STRING_TYPE,
  136. }, nil
  137. }
  138. // NodeServiceServer.GetPeers - fetches peers over gRPC
  139. func (s *NodeServiceServer) GetPeers(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
  140. var node, err = getNewOrLegacyNode(req.Data)
  141. if err != nil {
  142. return nil, err
  143. }
  144. excludeIsRelayed := node.IsRelay != "yes"
  145. var relayedNode string
  146. if node.IsRelayed == "yes" {
  147. relayedNode = node.Address
  148. }
  149. peers, err := logic.GetPeersList(node.Network, excludeIsRelayed, relayedNode)
  150. if err != nil {
  151. return nil, err
  152. }
  153. peersData, err := json.Marshal(&peers)
  154. logger.Log(3, node.Address, "checked in successfully")
  155. return &nodepb.Object{
  156. Data: string(peersData),
  157. Type: nodepb.NODE_TYPE,
  158. }, err
  159. }
  160. // NodeServiceServer.GetExtPeers - returns ext peers for a gateway node
  161. func (s *NodeServiceServer) GetExtPeers(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
  162. var node, err = getNewOrLegacyNode(req.Data)
  163. if err != nil {
  164. return nil, err
  165. }
  166. peers, err := logic.GetExtPeersList(&node)
  167. if err != nil {
  168. return nil, err
  169. }
  170. var extPeers []models.Node
  171. for i := 0; i < len(peers); i++ {
  172. extPeers = append(extPeers, models.Node{
  173. Address: peers[i].Address,
  174. Address6: peers[i].Address6,
  175. Endpoint: peers[i].Endpoint,
  176. PublicKey: peers[i].PublicKey,
  177. PersistentKeepalive: peers[i].KeepAlive,
  178. ListenPort: peers[i].ListenPort,
  179. LocalAddress: peers[i].LocalAddress,
  180. })
  181. }
  182. extData, err := json.Marshal(&extPeers)
  183. if err != nil {
  184. return nil, err
  185. }
  186. return &nodepb.Object{
  187. Data: string(extData),
  188. Type: nodepb.EXT_PEER,
  189. }, nil
  190. }
  191. // == private methods ==
  192. func getNewOrLegacyNode(data string) (models.Node, error) {
  193. var reqNode, node models.Node
  194. var err error
  195. if err = json.Unmarshal([]byte(data), &reqNode); err != nil {
  196. oldID := strings.Split(data, "###") // handle legacy client IDs
  197. if len(oldID) == 2 {
  198. if node, err = logic.GetNodeByIDorMacAddress(reqNode.ID, oldID[0], oldID[1]); err != nil {
  199. return models.Node{}, err
  200. }
  201. } else {
  202. return models.Node{}, err
  203. }
  204. } else {
  205. node, err = logic.GetNodeByIDorMacAddress(reqNode.ID, reqNode.MacAddress, reqNode.Network)
  206. if err != nil {
  207. return models.Node{}, err
  208. }
  209. }
  210. return node, nil
  211. }