relay.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package controller
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "time"
  7. "github.com/gorilla/mux"
  8. "github.com/gravitl/netmaker/database"
  9. "github.com/gravitl/netmaker/functions"
  10. "github.com/gravitl/netmaker/models"
  11. )
  12. func createRelay(w http.ResponseWriter, r *http.Request) {
  13. var relay models.RelayRequest
  14. var params = mux.Vars(r)
  15. w.Header().Set("Content-Type", "application/json")
  16. err := json.NewDecoder(r.Body).Decode(&relay)
  17. if err != nil {
  18. returnErrorResponse(w, r, formatError(err, "internal"))
  19. return
  20. }
  21. relay.NetID = params["network"]
  22. relay.NodeID = params["macaddress"]
  23. node, err := CreateRelay(relay)
  24. if err != nil {
  25. returnErrorResponse(w, r, formatError(err, "internal"))
  26. return
  27. }
  28. functions.PrintUserLog(r.Header.Get("user"), "created relay on node "+relay.NodeID+" on network "+relay.NetID, 1)
  29. w.WriteHeader(http.StatusOK)
  30. json.NewEncoder(w).Encode(node)
  31. }
  32. func CreateRelay(relay models.RelayRequest) (models.Node, error) {
  33. node, err := functions.GetNodeByMacAddress(relay.NetID, relay.NodeID)
  34. if node.OS == "windows" || node.OS == "macos" { // add in darwin later
  35. return models.Node{}, errors.New(node.OS + " is unsupported for relay")
  36. }
  37. if err != nil {
  38. return models.Node{}, err
  39. }
  40. err = ValidateRelay(relay)
  41. if err != nil {
  42. return models.Node{}, err
  43. }
  44. node.IsRelay = "yes"
  45. node.RelayAddrs = relay.RelayAddrs
  46. key, err := functions.GetRecordKey(relay.NodeID, relay.NetID)
  47. if err != nil {
  48. return node, err
  49. }
  50. node.SetLastModified()
  51. node.PullChanges = "yes"
  52. nodeData, err := json.Marshal(&node)
  53. if err != nil {
  54. return node, err
  55. }
  56. if err = database.Insert(key, string(nodeData), database.NODES_TABLE_NAME); err != nil {
  57. return models.Node{}, err
  58. }
  59. err = SetRelayedNodes("yes", node.Network, node.RelayAddrs)
  60. if err != nil {
  61. return node, err
  62. }
  63. if err = functions.NetworkNodesUpdatePullChanges(node.Network); err != nil {
  64. return models.Node{}, err
  65. }
  66. return node, nil
  67. }
  68. func deleteRelay(w http.ResponseWriter, r *http.Request) {
  69. w.Header().Set("Content-Type", "application/json")
  70. var params = mux.Vars(r)
  71. nodeMac := params["macaddress"]
  72. netid := params["network"]
  73. node, err := DeleteRelay(netid, nodeMac)
  74. if err != nil {
  75. returnErrorResponse(w, r, formatError(err, "internal"))
  76. return
  77. }
  78. functions.PrintUserLog(r.Header.Get("user"), "deleted egress gateway "+nodeMac+" on network "+netid, 1)
  79. w.WriteHeader(http.StatusOK)
  80. json.NewEncoder(w).Encode(node)
  81. }
  82. func SetRelayedNodes(yesOrno string, networkName string, addrs []string) error {
  83. collections, err := database.FetchRecords(database.NODES_TABLE_NAME)
  84. if err != nil {
  85. return err
  86. }
  87. for _, value := range collections {
  88. var node models.Node
  89. err := json.Unmarshal([]byte(value), &node)
  90. if err != nil {
  91. return err
  92. }
  93. if node.Network == networkName {
  94. for _, addr := range addrs {
  95. if addr == node.Address || addr == node.Address6 {
  96. node.IsRelayed = yesOrno
  97. data, err := json.Marshal(&node)
  98. if err != nil {
  99. return err
  100. }
  101. node.SetID()
  102. database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  103. }
  104. }
  105. }
  106. }
  107. return nil
  108. }
  109. func ValidateRelay(relay models.RelayRequest) error {
  110. var err error
  111. //isIp := functions.IsIpCIDR(gateway.RangeString)
  112. empty := len(relay.RelayAddrs) == 0
  113. if empty {
  114. err = errors.New("IP Ranges Cannot Be Empty")
  115. }
  116. return err
  117. }
  118. func UpdateRelay(network string, oldAddrs []string, newAddrs []string) {
  119. time.Sleep(time.Second / 4)
  120. err := SetRelayedNodes("no", network, oldAddrs)
  121. if err != nil {
  122. functions.PrintUserLog("netmaker", err.Error(), 1)
  123. }
  124. err = SetRelayedNodes("yes", network, newAddrs)
  125. if err != nil {
  126. functions.PrintUserLog("netmaker", err.Error(), 1)
  127. }
  128. }
  129. func DeleteRelay(network, macaddress string) (models.Node, error) {
  130. node, err := functions.GetNodeByMacAddress(network, macaddress)
  131. if err != nil {
  132. return models.Node{}, err
  133. }
  134. err = SetRelayedNodes("no", node.Network, node.RelayAddrs)
  135. if err != nil {
  136. return node, err
  137. }
  138. node.IsRelay = "no"
  139. node.RelayAddrs = []string{}
  140. node.SetLastModified()
  141. node.PullChanges = "yes"
  142. key, err := functions.GetRecordKey(node.MacAddress, node.Network)
  143. if err != nil {
  144. return models.Node{}, err
  145. }
  146. data, err := json.Marshal(&node)
  147. if err != nil {
  148. return models.Node{}, err
  149. }
  150. if err = database.Insert(key, string(data), database.NODES_TABLE_NAME); err != nil {
  151. return models.Node{}, err
  152. }
  153. if err = functions.NetworkNodesUpdatePullChanges(network); err != nil {
  154. return models.Node{}, err
  155. }
  156. return node, nil
  157. }