2021-12-08 01:46:55 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-02-08 03:04:07 +08:00
|
|
|
"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-12-21 04:29:09 +08:00
|
|
|
func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error) {
|
|
|
|
var returnnodes []models.Node
|
2022-02-08 00:30:15 +08:00
|
|
|
|
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-12-21 04:29:09 +08:00
|
|
|
return returnnodes, models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-24 02:41:17 +08:00
|
|
|
host, err := GetHost(node.HostID.String())
|
2022-12-21 04:29:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return returnnodes, models.Node{}, err
|
|
|
|
}
|
|
|
|
if host.OS != "linux" {
|
|
|
|
return returnnodes, models.Node{}, fmt.Errorf("only linux machines can be relay nodes")
|
2022-02-08 03:04:07 +08:00
|
|
|
}
|
2021-12-08 01:46:55 +08:00
|
|
|
err = ValidateRelay(relay)
|
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return returnnodes, models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
node.IsRelay = true
|
2021-12-08 01:46:55 +08:00
|
|
|
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-12-21 04:29:09 +08:00
|
|
|
if err = database.Insert(node.ID.String(), string(nodeData), database.NODES_TABLE_NAME); err != nil {
|
|
|
|
return returnnodes, models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-06-08 03:17:04 +08:00
|
|
|
returnnodes, err = SetRelayedNodes(true, 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
|
|
|
}
|
2022-02-08 00:30:15 +08:00
|
|
|
return returnnodes, node, nil
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
|
|
|
|
2023-01-10 21:24:52 +08:00
|
|
|
// CreateHostRelay - creates a host relay
|
2023-01-09 14:12:57 +08:00
|
|
|
func CreateHostRelay(relay models.HostRelayRequest) (relayHost *models.Host, relayedHosts []models.Host, err error) {
|
|
|
|
|
|
|
|
relayHost, err = GetHost(relay.HostID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-01-10 22:49:25 +08:00
|
|
|
err = validateHostRelay(relay)
|
2023-01-09 14:12:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
relayHost.IsRelay = true
|
2023-01-10 01:33:23 +08:00
|
|
|
relayHost.ProxyEnabled = true
|
2023-01-09 14:12:57 +08:00
|
|
|
relayHost.RelayedHosts = relay.RelayedHosts
|
|
|
|
err = UpsertHost(relayHost)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
relayedHosts = SetRelayedHosts(true, relay.HostID, relay.RelayedHosts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-10 21:24:52 +08:00
|
|
|
// SetRelayedHosts - updates the relayed hosts status
|
2023-01-09 14:12:57 +08:00
|
|
|
func SetRelayedHosts(setRelayed bool, relayHostID string, relayedHostIDs []string) []models.Host {
|
|
|
|
var relayedHosts []models.Host
|
|
|
|
for _, relayedHostID := range relayedHostIDs {
|
|
|
|
host, err := GetHost(relayedHostID)
|
|
|
|
if err == nil {
|
|
|
|
if setRelayed {
|
|
|
|
host.IsRelayed = true
|
|
|
|
host.RelayedBy = relayHostID
|
2023-01-10 01:33:23 +08:00
|
|
|
host.ProxyEnabled = true
|
2023-01-09 14:12:57 +08:00
|
|
|
} else {
|
|
|
|
host.IsRelayed = false
|
|
|
|
host.RelayedBy = ""
|
|
|
|
}
|
|
|
|
err = UpsertHost(host)
|
|
|
|
if err == nil {
|
|
|
|
relayedHosts = append(relayedHosts, *host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relayedHosts
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:46:55 +08:00
|
|
|
// SetRelayedNodes- set relayed nodes
|
2022-12-21 04:29:09 +08:00
|
|
|
func SetRelayedNodes(setRelayed bool, networkName string, addrs []string) ([]models.Node, error) {
|
|
|
|
var returnnodes []models.Node
|
2022-06-11 20:52:40 +08:00
|
|
|
networkNodes, err := GetNetworkNodes(networkName)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2022-02-08 00:30:15 +08:00
|
|
|
return returnnodes, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-06-11 20:52:40 +08:00
|
|
|
for _, node := range networkNodes {
|
2022-12-21 04:29:09 +08:00
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr == node.Address.IP.String() || addr == node.Address6.IP.String() {
|
|
|
|
if setRelayed {
|
|
|
|
node.IsRelayed = true
|
|
|
|
} else {
|
|
|
|
node.IsRelayed = false
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
data, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
|
|
|
return returnnodes, err
|
|
|
|
}
|
|
|
|
database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME)
|
|
|
|
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-12-21 04:29:09 +08:00
|
|
|
func GetRelayedNodes(relayNode *models.Node) ([]models.Node, error) {
|
|
|
|
var returnnodes []models.Node
|
2022-11-04 03:54:48 +08:00
|
|
|
networkNodes, err := GetNetworkNodes(relayNode.Network)
|
|
|
|
if err != nil {
|
|
|
|
return returnnodes, err
|
|
|
|
}
|
|
|
|
for _, node := range networkNodes {
|
2022-12-21 04:29:09 +08:00
|
|
|
for _, addr := range relayNode.RelayAddrs {
|
|
|
|
if addr == node.Address.IP.String() || addr == node.Address6.IP.String() {
|
|
|
|
returnnodes = append(returnnodes, node)
|
2022-11-04 03:54:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnnodes, nil
|
|
|
|
}
|
2021-12-08 01:46:55 +08:00
|
|
|
|
2023-01-10 21:24:52 +08:00
|
|
|
// GetRelayedHosts - gets the relayed hosts of a relay host
|
2023-01-09 14:12:57 +08:00
|
|
|
func GetRelayedHosts(relayHost *models.Host) []models.Host {
|
|
|
|
relayedHosts := []models.Host{}
|
|
|
|
|
|
|
|
for _, hostID := range relayHost.RelayedHosts {
|
|
|
|
relayedHost, err := GetHost(hostID)
|
|
|
|
if err == nil {
|
|
|
|
relayedHosts = append(relayedHosts, *relayedHost)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relayedHosts
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-10 22:49:25 +08:00
|
|
|
func validateHostRelay(relay models.HostRelayRequest) error {
|
2023-01-10 01:33:23 +08:00
|
|
|
if len(relay.RelayedHosts) == 0 {
|
|
|
|
return errors.New("relayed hosts are empty")
|
|
|
|
}
|
2023-01-09 14:12:57 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:46:55 +08:00
|
|
|
// UpdateRelay - updates a relay
|
2022-12-21 04:29:09 +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-06-08 03:17:04 +08:00
|
|
|
_, err := SetRelayedNodes(false, network, oldAddrs)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Log(1, err.Error())
|
|
|
|
}
|
2022-06-08 03:17:04 +08:00
|
|
|
returnnodes, err = SetRelayedNodes(true, 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-12-21 04:29:09 +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-12-21 04:29:09 +08:00
|
|
|
return returnnodes, models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-06-16 00:05:55 +08:00
|
|
|
returnnodes, err = SetRelayedNodes(false, 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
|
|
|
}
|
|
|
|
|
2022-12-21 04:29:09 +08:00
|
|
|
node.IsRelay = false
|
2021-12-08 01:46:55 +08:00
|
|
|
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-12-21 04:29:09 +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-12-21 04:29:09 +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
|
|
|
}
|
2023-01-09 14:12:57 +08:00
|
|
|
|
2023-01-10 22:22:54 +08:00
|
|
|
// DeleteHostRelay - removes host as relay
|
2023-01-09 14:12:57 +08:00
|
|
|
func DeleteHostRelay(relayHostID string) (relayHost *models.Host, relayedHosts []models.Host, err error) {
|
|
|
|
relayHost, err = GetHost(relayHostID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
relayedHosts = SetRelayedHosts(false, relayHostID, relayHost.RelayedHosts)
|
|
|
|
relayHost.IsRelay = false
|
|
|
|
relayHost.RelayedHosts = []string{}
|
|
|
|
err = UpsertHost(relayHost)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-01-11 01:18:09 +08:00
|
|
|
|
|
|
|
// UpdateHostRelay - updates the relay host with new relayed hosts
|
|
|
|
func UpdateHostRelay(relayHostID string, oldRelayedHosts, newRelayedHosts []string) {
|
|
|
|
_ = SetRelayedHosts(false, relayHostID, oldRelayedHosts)
|
|
|
|
_ = SetRelayedHosts(true, relayHostID, newRelayedHosts)
|
|
|
|
}
|