netmaker/logic/relay.go

177 lines
4.6 KiB
Go
Raw Normal View History

2021-12-08 01:46:55 +08:00
package logic
import (
"encoding/json"
"errors"
"fmt"
2021-12-08 01:46:55 +08:00
"time"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
)
// CreateRelay - creates a relay
2022-02-08 00:30:15 +08:00
func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error) {
var returnnodes []models.Node
2022-01-11 06:52:21 +08:00
node, err := GetNodeByID(relay.NodeID)
2021-12-08 01:46:55 +08:00
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
if node.OS != "linux" {
return returnnodes, models.Node{}, fmt.Errorf("only linux machines can be relay nodes")
}
2021-12-08 01:46:55 +08:00
err = ValidateRelay(relay)
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
node.IsRelay = "yes"
node.RelayAddrs = relay.RelayAddrs
node.SetLastModified()
nodeData, err := json.Marshal(&node)
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, node, err
2021-12-08 01:46:55 +08:00
}
2022-01-12 00:59:26 +08:00
if err = database.Insert(node.ID, string(nodeData), database.NODES_TABLE_NAME); err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
2022-02-08 00:30:15 +08:00
returnnodes, err = SetRelayedNodes("yes", node.Network, node.RelayAddrs)
2021-12-08 01:46:55 +08:00
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, node, err
2021-12-08 01:46:55 +08:00
}
if err = NetworkNodesUpdatePullChanges(node.Network); err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
2022-02-08 00:30:15 +08:00
return returnnodes, node, nil
2021-12-08 01:46:55 +08:00
}
// SetRelayedNodes- set relayed nodes
2022-02-08 00:30:15 +08:00
func SetRelayedNodes(yesOrno string, networkName string, addrs []string) ([]models.Node, error) {
var returnnodes []models.Node
2021-12-08 01:46:55 +08:00
collections, err := database.FetchRecords(database.NODES_TABLE_NAME)
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, err
2021-12-08 01:46:55 +08:00
}
network, err := GetNetworkSettings(networkName)
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, err
}
2021-12-08 01:46:55 +08:00
for _, value := range collections {
var node models.Node
err := json.Unmarshal([]byte(value), &node)
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, err
2021-12-08 01:46:55 +08:00
}
if node.Network == networkName && !(node.IsServer == "yes") {
2021-12-08 01:46:55 +08:00
for _, addr := range addrs {
if addr == node.Address || addr == node.Address6 {
node.IsRelayed = yesOrno
if yesOrno == "yes" {
node.UDPHolePunch = "no"
} else {
node.UDPHolePunch = network.DefaultUDPHolePunch
}
2021-12-08 01:46:55 +08:00
data, err := json.Marshal(&node)
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, err
2021-12-08 01:46:55 +08:00
}
database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
2022-02-08 00:30:15 +08:00
returnnodes = append(returnnodes, node)
2021-12-08 01:46:55 +08:00
}
}
}
}
2022-02-08 00:30:15 +08:00
return returnnodes, nil
2021-12-08 01:46:55 +08:00
}
2022-02-07 00:43:07 +08:00
// SetNodeIsRelayed - Sets IsRelayed to on or off for relay
2022-02-19 00:43:49 +08:00
func SetNodeIsRelayed(yesOrno string, id string) (models.Node, error) {
node, err := GetNodeByID(id)
if err != nil {
2022-02-19 00:43:49 +08:00
return node, err
}
network, err := GetNetworkByNode(&node)
if err != nil {
2022-02-19 00:43:49 +08:00
return node, err
}
node.IsRelayed = yesOrno
if yesOrno == "yes" {
node.UDPHolePunch = "no"
} else {
node.UDPHolePunch = network.DefaultUDPHolePunch
}
data, err := json.Marshal(&node)
if err != nil {
2022-02-19 00:43:49 +08:00
return node, err
}
2022-02-19 00:43:49 +08:00
return node, database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
}
2022-02-07 00:43:07 +08:00
// PeerListUnRelay - call this function if a relayed node fails to get its relay: unrelays node and gets new peer list
func PeerListUnRelay(id string, network string) ([]models.Node, error) {
2022-02-19 00:43:49 +08:00
node, err := SetNodeIsRelayed("no", id)
if err != nil {
return nil, err
}
2022-02-19 00:43:49 +08:00
return GetPeersList(&node)
}
2021-12-08 01:46:55 +08:00
// ValidateRelay - checks if relay is valid
func ValidateRelay(relay models.RelayRequest) error {
var err error
//isIp := functions.IsIpCIDR(gateway.RangeString)
empty := len(relay.RelayAddrs) == 0
if empty {
err = errors.New("IP Ranges Cannot Be Empty")
}
return err
}
// UpdateRelay - updates a relay
2022-02-08 00:30:15 +08:00
func UpdateRelay(network string, oldAddrs []string, newAddrs []string) []models.Node {
var returnnodes []models.Node
2021-12-08 01:46:55 +08:00
time.Sleep(time.Second / 4)
2022-02-08 00:30:15 +08:00
returnnodes, err := SetRelayedNodes("no", network, oldAddrs)
2021-12-08 01:46:55 +08:00
if err != nil {
logger.Log(1, err.Error())
}
2022-02-08 00:30:15 +08:00
returnnodes, err = SetRelayedNodes("yes", network, newAddrs)
2021-12-08 01:46:55 +08:00
if err != nil {
logger.Log(1, err.Error())
}
2022-02-08 00:30:15 +08:00
return returnnodes
2021-12-08 01:46:55 +08:00
}
// DeleteRelay - deletes a relay
2022-02-08 00:30:15 +08:00
func DeleteRelay(network, nodeid string) ([]models.Node, models.Node, error) {
var returnnodes []models.Node
2022-01-11 06:52:21 +08:00
node, err := GetNodeByID(nodeid)
2021-12-08 01:46:55 +08:00
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
2022-02-08 00:30:15 +08:00
_, err = SetRelayedNodes("no", node.Network, node.RelayAddrs)
2021-12-08 01:46:55 +08:00
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, node, err
2021-12-08 01:46:55 +08:00
}
node.IsRelay = "no"
node.RelayAddrs = []string{}
node.SetLastModified()
2022-01-11 06:52:21 +08:00
2021-12-08 01:46:55 +08:00
data, err := json.Marshal(&node)
if err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
2022-01-11 06:52:21 +08:00
if err = database.Insert(nodeid, string(data), database.NODES_TABLE_NAME); err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
if err = NetworkNodesUpdatePullChanges(network); err != nil {
2022-02-08 00:30:15 +08:00
return returnnodes, models.Node{}, err
2021-12-08 01:46:55 +08:00
}
2022-02-08 00:30:15 +08:00
return returnnodes, node, nil
2021-12-08 01:46:55 +08:00
}