2021-10-06 03:02:09 +08:00
|
|
|
// package for logicing client and server code
|
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2021-10-09 03:07:12 +08:00
|
|
|
"encoding/base64"
|
2021-10-06 03:02:09 +08:00
|
|
|
"encoding/json"
|
2021-10-22 03:28:58 +08:00
|
|
|
"math/rand"
|
2021-10-06 03:02:09 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-10-13 03:44:19 +08:00
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
"github.com/gravitl/netmaker/database"
|
2021-12-07 04:31:08 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2021-10-06 03:02:09 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
2021-10-13 22:02:04 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
2021-10-06 03:02:09 +08:00
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// IsBase64 - checks if a string is in base64 format
|
|
|
|
// This is used to validate public keys (make sure they're base64 encoded like all public keys should be).
|
2021-10-06 03:02:09 +08:00
|
|
|
func IsBase64(s string) bool {
|
|
|
|
_, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// CheckEndpoint - checks if an endpoint is valid
|
2021-10-06 03:02:09 +08:00
|
|
|
func CheckEndpoint(endpoint string) bool {
|
|
|
|
endpointarr := strings.Split(endpoint, ":")
|
|
|
|
return len(endpointarr) == 2
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// SetNetworkServerPeers - sets the network server peers of a given node
|
2021-10-06 03:02:09 +08:00
|
|
|
func SetNetworkServerPeers(node *models.Node) {
|
|
|
|
if currentPeersList, err := GetSystemPeers(node); err == nil {
|
|
|
|
if database.SetPeers(currentPeersList, node.Network) {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(1, "set new peers on network", node.Network)
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(1, "could not set peers on network", node.Network, ":", err.Error())
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// DeleteNode - deletes a node from database or moves into delete nodes table
|
2021-10-20 21:22:05 +08:00
|
|
|
func DeleteNode(node *models.Node, exterminate bool) error {
|
2021-10-06 03:02:09 +08:00
|
|
|
var err error
|
2021-10-20 21:22:05 +08:00
|
|
|
node.SetID()
|
|
|
|
var key = node.ID
|
2021-10-06 03:02:09 +08:00
|
|
|
if !exterminate {
|
|
|
|
args := strings.Split(key, "###")
|
|
|
|
node, err := GetNode(args[0], args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.Action = models.NODE_DELETE
|
|
|
|
nodedata, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = database.Insert(key, string(nodedata), database.DELETED_NODES_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := database.DeleteRecord(database.DELETED_NODES_TABLE_NAME, key); err != nil {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(2, err.Error())
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-20 21:22:05 +08:00
|
|
|
if err = database.DeleteRecord(database.NODES_TABLE_NAME, key); err != nil {
|
2021-10-06 03:02:09 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if servercfg.IsDNSMode() {
|
2021-12-11 04:01:10 +08:00
|
|
|
SetDNS()
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-10-20 21:22:05 +08:00
|
|
|
return removeLocalServer(node)
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// CreateNode - creates a node in database
|
2021-12-11 10:09:42 +08:00
|
|
|
func CreateNode(node *models.Node) error {
|
2021-10-06 03:02:09 +08:00
|
|
|
|
|
|
|
//encrypt that password so we never see it
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(node.Password), 5)
|
|
|
|
if err != nil {
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
//set password to encrypted password
|
|
|
|
node.Password = string(hash)
|
|
|
|
if node.Name == models.NODE_SERVER_NAME {
|
|
|
|
node.IsServer = "yes"
|
|
|
|
}
|
2021-10-20 23:06:54 +08:00
|
|
|
if node.DNSOn == "" {
|
|
|
|
if servercfg.IsDNSMode() {
|
|
|
|
node.DNSOn = "yes"
|
|
|
|
} else {
|
|
|
|
node.DNSOn = "no"
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-12-08 04:04:00 +08:00
|
|
|
SetNodeDefaults(node)
|
2021-12-11 10:09:42 +08:00
|
|
|
node.Address, err = UniqueAddress(node.Network)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-12-11 10:09:42 +08:00
|
|
|
node.Address6, err = UniqueAddress6(node.Network)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
//Create a JWT for the node
|
2021-12-11 10:09:42 +08:00
|
|
|
tokenString, _ := CreateJWT(node.MacAddress, node.Network)
|
2021-10-06 03:02:09 +08:00
|
|
|
if tokenString == "" {
|
|
|
|
//returnErrorResponse(w, r, errorResponse)
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-12-08 04:04:00 +08:00
|
|
|
err = ValidateNode(node, false)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-10-27 00:27:29 +08:00
|
|
|
key, err := GetRecordKey(node.MacAddress, node.Network)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
nodebytes, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
err = database.Insert(key, string(nodebytes), database.NODES_TABLE_NAME)
|
|
|
|
if err != nil {
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
if node.IsPending != "yes" {
|
2021-10-27 00:27:29 +08:00
|
|
|
DecrimentKey(node.Network, node.AccessKey)
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
SetNetworkNodesLastModified(node.Network)
|
|
|
|
if servercfg.IsDNSMode() {
|
2021-10-27 00:27:29 +08:00
|
|
|
err = SetDNS()
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-12-11 10:09:42 +08:00
|
|
|
return err
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// SetNetworkNodesLastModified - sets the network nodes last modified
|
2021-10-06 03:02:09 +08:00
|
|
|
func SetNetworkNodesLastModified(networkName string) error {
|
|
|
|
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
|
2021-10-27 00:27:29 +08:00
|
|
|
network, err := GetParentNetwork(networkName)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
network.NodesLastModified = timestamp
|
|
|
|
data, err := json.Marshal(&network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = database.Insert(networkName, string(data), database.NETWORKS_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// GetNode - fetches a node from database
|
2021-10-06 03:02:09 +08:00
|
|
|
func GetNode(macaddress string, network string) (models.Node, error) {
|
|
|
|
var node models.Node
|
|
|
|
|
2021-10-27 00:27:29 +08:00
|
|
|
key, err := GetRecordKey(macaddress, network)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
data, err := database.FetchRecord(database.NODES_TABLE_NAME, key)
|
|
|
|
if err != nil {
|
|
|
|
if data == "" {
|
2021-12-11 04:01:10 +08:00
|
|
|
data, _ = database.FetchRecord(database.DELETED_NODES_TABLE_NAME, key)
|
2021-10-06 03:02:09 +08:00
|
|
|
err = json.Unmarshal([]byte(data), &node)
|
|
|
|
}
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal([]byte(data), &node); err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2021-10-27 00:27:29 +08:00
|
|
|
SetNodeDefaults(&node)
|
2021-10-06 03:02:09 +08:00
|
|
|
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// GetNodePeers - fetches peers for a given node
|
2021-10-06 03:02:09 +08:00
|
|
|
func GetNodePeers(networkName string, excludeRelayed bool) ([]models.Node, error) {
|
|
|
|
var peers []models.Node
|
|
|
|
collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
if database.IsEmptyRecord(err) {
|
|
|
|
return peers, nil
|
|
|
|
}
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(2, err.Error())
|
2021-10-06 03:02:09 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
udppeers, errN := database.GetPeers(networkName)
|
|
|
|
if errN != nil {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(2, errN.Error())
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
for _, value := range collection {
|
2021-12-12 01:07:54 +08:00
|
|
|
var node = &models.Node{}
|
|
|
|
var peer = models.Node{}
|
2021-12-11 04:01:10 +08:00
|
|
|
err := json.Unmarshal([]byte(value), node)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(2, err.Error())
|
2021-10-06 03:02:09 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if node.IsEgressGateway == "yes" { // handle egress stuff
|
|
|
|
peer.EgressGatewayRanges = node.EgressGatewayRanges
|
|
|
|
peer.IsEgressGateway = node.IsEgressGateway
|
|
|
|
}
|
|
|
|
allow := node.IsRelayed != "yes" || !excludeRelayed
|
|
|
|
|
|
|
|
if node.Network == networkName && node.IsPending != "yes" && allow {
|
|
|
|
peer = setPeerInfo(node)
|
|
|
|
if node.UDPHolePunch == "yes" && errN == nil && CheckEndpoint(udppeers[node.PublicKey]) {
|
|
|
|
endpointstring := udppeers[node.PublicKey]
|
|
|
|
endpointarr := strings.Split(endpointstring, ":")
|
|
|
|
if len(endpointarr) == 2 {
|
|
|
|
port, err := strconv.Atoi(endpointarr[1])
|
|
|
|
if err == nil {
|
|
|
|
peer.Endpoint = endpointarr[0]
|
|
|
|
peer.ListenPort = int32(port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if node.IsRelay == "yes" {
|
2021-10-27 00:27:29 +08:00
|
|
|
network, err := GetNetwork(networkName)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err == nil {
|
|
|
|
peer.AllowedIPs = append(peer.AllowedIPs, network.AddressRange)
|
|
|
|
} else {
|
|
|
|
peer.AllowedIPs = append(peer.AllowedIPs, node.RelayAddrs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers, err
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// GetPeersList - gets the peers of a given network
|
2021-10-06 03:02:09 +08:00
|
|
|
func GetPeersList(networkName string, excludeRelayed bool, relayedNodeAddr string) ([]models.Node, error) {
|
|
|
|
var peers []models.Node
|
|
|
|
var err error
|
|
|
|
if relayedNodeAddr == "" {
|
|
|
|
peers, err = GetNodePeers(networkName, excludeRelayed)
|
|
|
|
} else {
|
2021-12-11 04:01:10 +08:00
|
|
|
var relayNode models.Node
|
2021-10-27 00:27:29 +08:00
|
|
|
relayNode, err = GetNodeRelay(networkName, relayedNodeAddr)
|
2021-10-06 03:02:09 +08:00
|
|
|
if relayNode.Address != "" {
|
2021-12-12 01:07:54 +08:00
|
|
|
var peerNode = setPeerInfo(&relayNode)
|
2021-10-27 00:27:29 +08:00
|
|
|
network, err := GetNetwork(networkName)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err == nil {
|
2021-12-11 04:01:10 +08:00
|
|
|
peerNode.AllowedIPs = append(peerNode.AllowedIPs, network.AddressRange)
|
2021-10-06 03:02:09 +08:00
|
|
|
} else {
|
2021-12-11 04:01:10 +08:00
|
|
|
peerNode.AllowedIPs = append(peerNode.AllowedIPs, peerNode.RelayAddrs...)
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
nodepeers, err := GetNodePeers(networkName, false)
|
2021-12-11 04:01:10 +08:00
|
|
|
if err == nil && peerNode.UDPHolePunch == "yes" {
|
2021-10-06 03:02:09 +08:00
|
|
|
for _, nodepeer := range nodepeers {
|
2021-12-11 04:01:10 +08:00
|
|
|
if nodepeer.Address == peerNode.Address {
|
|
|
|
peerNode.Endpoint = nodepeer.Endpoint
|
|
|
|
peerNode.ListenPort = nodepeer.ListenPort
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 04:01:10 +08:00
|
|
|
peers = append(peers, peerNode)
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return peers, err
|
|
|
|
}
|
|
|
|
|
2021-10-22 03:28:58 +08:00
|
|
|
// RandomString - returns a random string in a charset
|
|
|
|
func RandomString(length int) string {
|
2021-12-11 04:01:10 +08:00
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
2021-10-22 03:28:58 +08:00
|
|
|
|
|
|
|
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
b := make([]byte, length)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = charset[seededRand.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2021-12-07 04:31:08 +08:00
|
|
|
// == Private Methods ==
|
|
|
|
|
2021-12-11 04:01:10 +08:00
|
|
|
func setPeerInfo(node *models.Node) models.Node {
|
2021-10-06 03:02:09 +08:00
|
|
|
var peer models.Node
|
|
|
|
peer.RelayAddrs = node.RelayAddrs
|
|
|
|
peer.IsRelay = node.IsRelay
|
|
|
|
peer.IsServer = node.IsServer
|
|
|
|
peer.IsRelayed = node.IsRelayed
|
|
|
|
peer.PublicKey = node.PublicKey
|
|
|
|
peer.Endpoint = node.Endpoint
|
2021-10-20 03:53:01 +08:00
|
|
|
peer.Name = node.Name
|
2021-10-06 03:02:09 +08:00
|
|
|
peer.LocalAddress = node.LocalAddress
|
|
|
|
peer.ListenPort = node.ListenPort
|
|
|
|
peer.AllowedIPs = node.AllowedIPs
|
|
|
|
peer.UDPHolePunch = node.UDPHolePunch
|
|
|
|
peer.Address = node.Address
|
|
|
|
peer.Address6 = node.Address6
|
|
|
|
peer.EgressGatewayRanges = node.EgressGatewayRanges
|
|
|
|
peer.IsEgressGateway = node.IsEgressGateway
|
|
|
|
peer.IngressGatewayRange = node.IngressGatewayRange
|
|
|
|
peer.IsIngressGateway = node.IsIngressGateway
|
|
|
|
peer.IsPending = node.IsPending
|
|
|
|
return peer
|
|
|
|
}
|
2021-10-13 03:44:19 +08:00
|
|
|
|
2021-10-13 22:02:04 +08:00
|
|
|
func setIPForwardingLinux() error {
|
|
|
|
out, err := ncutils.RunCmd("sysctl net.ipv4.ip_forward", true)
|
|
|
|
if err != nil {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(0, "WARNING: Error encountered setting ip forwarding. This can break functionality.")
|
2021-10-13 22:02:04 +08:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
s := strings.Fields(string(out))
|
|
|
|
if s[2] != "1" {
|
|
|
|
_, err = ncutils.RunCmd("sysctl -w net.ipv4.ip_forward=1", true)
|
|
|
|
if err != nil {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(0, "WARNING: Error encountered setting ip forwarding. You may want to investigate this.")
|
2021-10-13 22:02:04 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|