2021-09-14 08:25:27 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2021-09-17 09:49:09 +08:00
|
|
|
"time"
|
2021-09-18 22:33:14 +08:00
|
|
|
|
2021-09-14 08:25:27 +08:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
"github.com/gravitl/netmaker/functions"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createRelay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var relay models.RelayRequest
|
|
|
|
var params = mux.Vars(r)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&relay)
|
|
|
|
if err != nil {
|
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
relay.NetID = params["network"]
|
|
|
|
relay.NodeID = params["macaddress"]
|
|
|
|
node, err := CreateRelay(relay)
|
|
|
|
if err != nil {
|
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
functions.PrintUserLog(r.Header.Get("user"), "created relay on node "+relay.NodeID+" on network "+relay.NetID, 1)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateRelay(relay models.RelayRequest) (models.Node, error) {
|
|
|
|
node, err := functions.GetNodeByMacAddress(relay.NetID, relay.NodeID)
|
2021-09-18 23:01:34 +08:00
|
|
|
if node.OS == "windows" || node.OS == "macos" { // add in darwin later
|
2021-09-14 08:25:27 +08:00
|
|
|
return models.Node{}, errors.New(node.OS + " is unsupported for relay")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
err = ValidateRelay(relay)
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
node.IsRelay = "yes"
|
2021-09-18 22:33:14 +08:00
|
|
|
node.RelayAddrs = relay.RelayAddrs
|
2021-09-14 08:25:27 +08:00
|
|
|
|
|
|
|
key, err := functions.GetRecordKey(relay.NodeID, relay.NetID)
|
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
node.SetLastModified()
|
|
|
|
node.PullChanges = "yes"
|
|
|
|
nodeData, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
if err = database.Insert(key, string(nodeData), database.NODES_TABLE_NAME); err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
2021-09-18 22:33:14 +08:00
|
|
|
err = SetRelayedNodes("yes", node.Network, node.RelayAddrs)
|
2021-09-14 08:25:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = functions.NetworkNodesUpdatePullChanges(node.Network); err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
func deleteRelay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var params = mux.Vars(r)
|
|
|
|
nodeMac := params["macaddress"]
|
|
|
|
netid := params["network"]
|
|
|
|
node, err := DeleteRelay(netid, nodeMac)
|
|
|
|
if err != nil {
|
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
functions.PrintUserLog(r.Header.Get("user"), "deleted egress gateway "+nodeMac+" on network "+netid, 1)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(node)
|
|
|
|
}
|
|
|
|
|
2021-09-18 22:33:14 +08:00
|
|
|
func SetRelayedNodes(yesOrno string, networkName string, addrs []string) error {
|
2021-09-14 08:25:27 +08:00
|
|
|
|
|
|
|
collections, err := database.FetchRecords(database.NODES_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range collections {
|
|
|
|
|
|
|
|
var node models.Node
|
|
|
|
err := json.Unmarshal([]byte(value), &node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if node.Network == networkName {
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr == node.Address || addr == node.Address6 {
|
2021-09-18 22:33:14 +08:00
|
|
|
node.IsRelayed = yesOrno
|
2021-09-14 08:25:27 +08:00
|
|
|
data, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.SetID()
|
|
|
|
database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateRelay(relay models.RelayRequest) error {
|
|
|
|
var err error
|
|
|
|
//isIp := functions.IsIpCIDR(gateway.RangeString)
|
2021-09-18 22:33:14 +08:00
|
|
|
empty := len(relay.RelayAddrs) == 0
|
2021-09-14 08:25:27 +08:00
|
|
|
if empty {
|
|
|
|
err = errors.New("IP Ranges Cannot Be Empty")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:00:40 +08:00
|
|
|
func UpdateRelay(network string, oldAddrs []string, newAddrs []string) {
|
2021-09-18 22:33:14 +08:00
|
|
|
time.Sleep(time.Second / 4)
|
|
|
|
err := SetRelayedNodes("no", network, oldAddrs)
|
2021-09-17 08:00:40 +08:00
|
|
|
if err != nil {
|
2021-09-18 22:33:14 +08:00
|
|
|
functions.PrintUserLog("netmaker", err.Error(), 1)
|
|
|
|
}
|
|
|
|
err = SetRelayedNodes("yes", network, newAddrs)
|
2021-09-17 08:00:40 +08:00
|
|
|
if err != nil {
|
2021-09-18 22:33:14 +08:00
|
|
|
functions.PrintUserLog("netmaker", err.Error(), 1)
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 08:00:40 +08:00
|
|
|
|
2021-09-14 08:25:27 +08:00
|
|
|
func DeleteRelay(network, macaddress string) (models.Node, error) {
|
|
|
|
|
|
|
|
node, err := functions.GetNodeByMacAddress(network, macaddress)
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
2021-09-18 22:33:14 +08:00
|
|
|
err = SetRelayedNodes("no", node.Network, node.RelayAddrs)
|
2021-09-14 08:25:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node.IsRelay = "no"
|
|
|
|
node.RelayAddrs = []string{}
|
|
|
|
node.SetLastModified()
|
|
|
|
node.PullChanges = "yes"
|
|
|
|
key, err := functions.GetRecordKey(node.MacAddress, node.Network)
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
if err = database.Insert(key, string(data), database.NODES_TABLE_NAME); err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
if err = functions.NetworkNodesUpdatePullChanges(network); err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
return node, nil
|
2021-09-17 08:00:40 +08:00
|
|
|
}
|