relay.go 3.6 KB

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