relay.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "github.com/google/uuid"
  7. proLogic "github.com/gravitl/netmaker/pro/logic"
  8. "github.com/gorilla/mux"
  9. controller "github.com/gravitl/netmaker/controllers"
  10. "github.com/gravitl/netmaker/logger"
  11. "github.com/gravitl/netmaker/logic"
  12. "github.com/gravitl/netmaker/models"
  13. "github.com/gravitl/netmaker/mq"
  14. )
  15. // RelayHandlers - handle Pro Relays
  16. func RelayHandlers(r *mux.Router) {
  17. r.HandleFunc("/api/nodes/{network}/{nodeid}/createrelay", controller.Authorize(false, true, "user", http.HandlerFunc(createRelay))).Methods(http.MethodPost)
  18. r.HandleFunc("/api/nodes/{network}/{nodeid}/deleterelay", controller.Authorize(false, true, "user", http.HandlerFunc(deleteRelay))).Methods(http.MethodDelete)
  19. r.HandleFunc("/api/v1/host/{hostid}/failoverme", controller.Authorize(true, false, "host", http.HandlerFunc(failOverME))).Methods(http.MethodPost)
  20. }
  21. // swagger:route POST /api/nodes/{network}/{nodeid}/createrelay nodes createRelay
  22. //
  23. // Create a relay.
  24. //
  25. // Schemes: https
  26. //
  27. // Security:
  28. // oauth
  29. //
  30. // Responses:
  31. // 200: nodeResponse
  32. func createRelay(w http.ResponseWriter, r *http.Request) {
  33. var relayRequest models.RelayRequest
  34. var params = mux.Vars(r)
  35. w.Header().Set("Content-Type", "application/json")
  36. err := json.NewDecoder(r.Body).Decode(&relayRequest)
  37. if err != nil {
  38. logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
  39. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  40. return
  41. }
  42. relayRequest.NetID = params["network"]
  43. relayRequest.NodeID = params["nodeid"]
  44. _, relayNode, err := proLogic.CreateRelay(relayRequest)
  45. if err != nil {
  46. logger.Log(0, r.Header.Get("user"),
  47. fmt.Sprintf("failed to create relay on node [%s] on network [%s]: %v", relayRequest.NodeID, relayRequest.NetID, err))
  48. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
  49. return
  50. }
  51. for _, relayedNodeID := range relayNode.RelayedNodes {
  52. relayedNode, err := logic.GetNodeByID(relayedNodeID)
  53. if err == nil {
  54. if relayedNode.FailedOverBy != uuid.Nil {
  55. go logic.ResetFailedOverPeer(&relayedNode)
  56. }
  57. }
  58. }
  59. go mq.PublishPeerUpdate()
  60. logger.Log(1, r.Header.Get("user"), "created relay on node", relayRequest.NodeID, "on network", relayRequest.NetID)
  61. apiNode := relayNode.ConvertToAPINode()
  62. w.WriteHeader(http.StatusOK)
  63. json.NewEncoder(w).Encode(apiNode)
  64. }
  65. // swagger:route DELETE /api/nodes/{network}/{nodeid}/deleterelay nodes deleteRelay
  66. //
  67. // Remove a relay.
  68. //
  69. // Schemes: https
  70. //
  71. // Security:
  72. // oauth
  73. //
  74. // Responses:
  75. // 200: nodeResponse
  76. func deleteRelay(w http.ResponseWriter, r *http.Request) {
  77. w.Header().Set("Content-Type", "application/json")
  78. var params = mux.Vars(r)
  79. nodeid := params["nodeid"]
  80. netid := params["network"]
  81. updateNodes, node, err := proLogic.DeleteRelay(netid, nodeid)
  82. if err != nil {
  83. logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
  84. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  85. return
  86. }
  87. logger.Log(1, r.Header.Get("user"), "deleted relay server", nodeid, "on network", netid)
  88. go func() {
  89. for _, relayedNode := range updateNodes {
  90. err = mq.NodeUpdate(&relayedNode)
  91. if err != nil {
  92. logger.Log(1, "relayed node update ", relayedNode.ID.String(), "on network", relayedNode.Network, ": ", err.Error())
  93. }
  94. h, err := logic.GetHost(relayedNode.HostID.String())
  95. if err == nil {
  96. if h.OS == models.OS_Types.IoT {
  97. nodes, err := logic.GetAllNodes()
  98. if err != nil {
  99. return
  100. }
  101. node.IsRelay = true // for iot update to recognise that it has to delete relay peer
  102. if err = mq.PublishSingleHostPeerUpdate(h, nodes, &node, nil); err != nil {
  103. logger.Log(1, "failed to publish peer update to host", h.ID.String(), ": ", err.Error())
  104. }
  105. }
  106. }
  107. }
  108. mq.PublishPeerUpdate()
  109. }()
  110. logger.Log(1, r.Header.Get("user"), "deleted relay on node", node.ID.String(), "on network", node.Network)
  111. apiNode := node.ConvertToAPINode()
  112. w.WriteHeader(http.StatusOK)
  113. json.NewEncoder(w).Encode(apiNode)
  114. }