relay.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "github.com/gravitl/netmaker/database"
  8. "github.com/gravitl/netmaker/logger"
  9. "github.com/gravitl/netmaker/models"
  10. )
  11. // CreateRelay - creates a relay
  12. func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error) {
  13. var returnnodes []models.Node
  14. node, err := GetNodeByID(relay.NodeID)
  15. if err != nil {
  16. return returnnodes, models.Node{}, err
  17. }
  18. if node.OS != "linux" {
  19. return returnnodes, models.Node{}, fmt.Errorf("only linux machines can be relay nodes")
  20. }
  21. err = ValidateRelay(relay)
  22. if err != nil {
  23. return returnnodes, models.Node{}, err
  24. }
  25. node.IsRelay = "yes"
  26. node.RelayAddrs = relay.RelayAddrs
  27. node.SetLastModified()
  28. nodeData, err := json.Marshal(&node)
  29. if err != nil {
  30. return returnnodes, node, err
  31. }
  32. if err = database.Insert(node.ID, string(nodeData), database.NODES_TABLE_NAME); err != nil {
  33. return returnnodes, models.Node{}, err
  34. }
  35. returnnodes, err = SetRelayedNodes(true, node.Network, node.RelayAddrs)
  36. if err != nil {
  37. return returnnodes, node, err
  38. }
  39. if err = NetworkNodesUpdatePullChanges(node.Network); err != nil {
  40. return returnnodes, models.Node{}, err
  41. }
  42. return returnnodes, node, nil
  43. }
  44. // SetRelayedNodes- set relayed nodes
  45. func SetRelayedNodes(setRelayed bool, networkName string, addrs []string) ([]models.Node, error) {
  46. var returnnodes []models.Node
  47. networkNodes, err := GetNetworkNodes(networkName)
  48. if err != nil {
  49. return returnnodes, err
  50. }
  51. network, err := GetNetworkSettings(networkName)
  52. if err != nil {
  53. return returnnodes, err
  54. }
  55. for _, node := range networkNodes {
  56. if node.IsServer != "yes" {
  57. for _, addr := range addrs {
  58. if addr == node.Address || addr == node.Address6 {
  59. if setRelayed {
  60. node.UDPHolePunch = "no"
  61. node.IsRelayed = "yes"
  62. } else {
  63. node.UDPHolePunch = network.DefaultUDPHolePunch
  64. node.IsRelayed = "no"
  65. }
  66. data, err := json.Marshal(&node)
  67. if err != nil {
  68. return returnnodes, err
  69. }
  70. database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  71. returnnodes = append(returnnodes, node)
  72. }
  73. }
  74. }
  75. }
  76. return returnnodes, nil
  77. }
  78. // SetNodeIsRelayed - Sets IsRelayed to on or off for relay
  79. func SetNodeIsRelayed(yesOrno string, id string) (models.Node, error) {
  80. node, err := GetNodeByID(id)
  81. if err != nil {
  82. return node, err
  83. }
  84. network, err := GetNetworkByNode(&node)
  85. if err != nil {
  86. return node, err
  87. }
  88. node.IsRelayed = yesOrno
  89. if yesOrno == "yes" {
  90. node.UDPHolePunch = "no"
  91. } else {
  92. node.UDPHolePunch = network.DefaultUDPHolePunch
  93. }
  94. data, err := json.Marshal(&node)
  95. if err != nil {
  96. return node, err
  97. }
  98. return node, database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  99. }
  100. // ValidateRelay - checks if relay is valid
  101. func ValidateRelay(relay models.RelayRequest) error {
  102. var err error
  103. //isIp := functions.IsIpCIDR(gateway.RangeString)
  104. empty := len(relay.RelayAddrs) == 0
  105. if empty {
  106. err = errors.New("IP Ranges Cannot Be Empty")
  107. }
  108. return err
  109. }
  110. // UpdateRelay - updates a relay
  111. func UpdateRelay(network string, oldAddrs []string, newAddrs []string) []models.Node {
  112. var returnnodes []models.Node
  113. time.Sleep(time.Second / 4)
  114. _, err := SetRelayedNodes(false, network, oldAddrs)
  115. if err != nil {
  116. logger.Log(1, err.Error())
  117. }
  118. returnnodes, err = SetRelayedNodes(true, network, newAddrs)
  119. if err != nil {
  120. logger.Log(1, err.Error())
  121. }
  122. return returnnodes
  123. }
  124. // DeleteRelay - deletes a relay
  125. func DeleteRelay(network, nodeid string) ([]models.Node, models.Node, error) {
  126. var returnnodes []models.Node
  127. node, err := GetNodeByID(nodeid)
  128. if err != nil {
  129. return returnnodes, models.Node{}, err
  130. }
  131. _, err = SetRelayedNodes(false, node.Network, node.RelayAddrs)
  132. if err != nil {
  133. return returnnodes, node, err
  134. }
  135. node.IsRelay = "no"
  136. node.RelayAddrs = []string{}
  137. node.SetLastModified()
  138. data, err := json.Marshal(&node)
  139. if err != nil {
  140. return returnnodes, models.Node{}, err
  141. }
  142. if err = database.Insert(nodeid, string(data), database.NODES_TABLE_NAME); err != nil {
  143. return returnnodes, models.Node{}, err
  144. }
  145. if err = NetworkNodesUpdatePullChanges(network); err != nil {
  146. return returnnodes, models.Node{}, err
  147. }
  148. return returnnodes, node, nil
  149. }