2021-10-06 03:02:09 +08:00
|
|
|
// package for logicing client and server code
|
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2022-02-16 10:50:47 +08:00
|
|
|
crand "crypto/rand"
|
2021-10-09 03:07:12 +08:00
|
|
|
"encoding/base64"
|
2021-10-06 03:02:09 +08:00
|
|
|
"encoding/json"
|
2022-01-19 23:44:00 +08:00
|
|
|
"fmt"
|
2022-02-16 10:50:47 +08:00
|
|
|
"math/big"
|
2021-10-22 03:28:58 +08:00
|
|
|
"math/rand"
|
2022-01-19 23:44:00 +08:00
|
|
|
"net"
|
2022-01-18 06:44:12 +08:00
|
|
|
"os"
|
2021-10-06 03:02:09 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-18 06:44:12 +08:00
|
|
|
// FileExists - checks if local file exists
|
|
|
|
func FileExists(f string) bool {
|
|
|
|
info, err := os.Stat(f)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !info.IsDir()
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:44:00 +08:00
|
|
|
// IsAddressInCIDR - util to see if an address is in a cidr or not
|
|
|
|
func IsAddressInCIDR(address, cidr string) bool {
|
|
|
|
var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
|
|
|
|
if cidrErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var addrParts = strings.Split(address, ".")
|
|
|
|
var addrPartLength = len(addrParts)
|
|
|
|
if addrPartLength != 4 {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
if addrParts[addrPartLength-1] == "0" ||
|
|
|
|
addrParts[addrPartLength-1] == "255" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ip, _, err := net.ParseCIDR(fmt.Sprintf("%s/32", address))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return currentCIDR.Contains(ip)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:50:47 +08:00
|
|
|
// GenerateCryptoString - generates random string of n length
|
|
|
|
func GenerateCryptoString(n int) (string, error) {
|
|
|
|
const chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
|
|
|
|
ret := make([]byte, n)
|
|
|
|
for i := range ret {
|
|
|
|
num, err := crand.Int(crand.Reader, big.NewInt(int64(len(chars))))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ret[i] = chars[num.Int64()]
|
|
|
|
}
|
2022-01-12 01:00:14 +08:00
|
|
|
|
2022-02-16 10:50:47 +08:00
|
|
|
return string(ret), nil
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
|
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 ==
|
|
|
|
|
2022-01-04 00:30:02 +08:00
|
|
|
func getNetworkEgressAndNodes(networkName string) ([]models.Node, []models.Node, error) {
|
|
|
|
var networkNodes, egressNetworkNodes []models.Node
|
|
|
|
collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
if database.IsEmptyRecord(err) {
|
|
|
|
return networkNodes, egressNetworkNodes, nil
|
|
|
|
}
|
|
|
|
logger.Log(2, err.Error())
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range collection {
|
|
|
|
var node = models.Node{}
|
|
|
|
err := json.Unmarshal([]byte(value), &node)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(2, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if node.Network == networkName {
|
|
|
|
networkNodes = append(networkNodes, node)
|
|
|
|
if node.IsEgressGateway == "yes" {
|
|
|
|
egressNetworkNodes = append(egressNetworkNodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return networkNodes, egressNetworkNodes, nil
|
|
|
|
}
|
|
|
|
|
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
|
2022-03-15 03:56:01 +08:00
|
|
|
peer.Network = node.Network
|
2021-10-06 03:02:09 +08:00
|
|
|
peer.LocalAddress = node.LocalAddress
|
2022-04-19 20:07:21 +08:00
|
|
|
peer.LocalListenPort = node.LocalListenPort
|
2021-10-06 03:02:09 +08:00
|
|
|
peer.ListenPort = node.ListenPort
|
|
|
|
peer.AllowedIPs = node.AllowedIPs
|
|
|
|
peer.UDPHolePunch = node.UDPHolePunch
|
|
|
|
peer.Address = node.Address
|
|
|
|
peer.Address6 = node.Address6
|
2022-02-22 00:08:45 +08:00
|
|
|
peer.IsHub = node.IsHub
|
2021-10-06 03:02:09 +08:00
|
|
|
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
|
|
|
|
}
|
2022-01-04 00:30:02 +08:00
|
|
|
|
|
|
|
// StringSliceContains - sees if a string slice contains a string element
|
|
|
|
func StringSliceContains(slice []string, item string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == item {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-01-18 23:55:02 +08:00
|
|
|
|
|
|
|
// == private ==
|
|
|
|
|
|
|
|
// sets the network server peers of a given node
|
|
|
|
func setNetworkServerPeers(serverNode *models.Node) {
|
|
|
|
if currentPeersList, err := getSystemPeers(serverNode); err == nil {
|
|
|
|
if database.SetPeers(currentPeersList, serverNode.Network) {
|
|
|
|
logger.Log(1, "set new peers on network", serverNode.Network)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Log(1, "could not set peers on network", serverNode.Network, ":", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 06:13:19 +08:00
|
|
|
|
|
|
|
// ShouldPublishPeerPorts - Gets ports from iface, sets, and returns true if they are different
|
|
|
|
func ShouldPublishPeerPorts(serverNode *models.Node) bool {
|
|
|
|
if currentPeersList, err := getSystemPeers(serverNode); err == nil {
|
|
|
|
if database.SetPeers(currentPeersList, serverNode.Network) {
|
|
|
|
logger.Log(1, "set new peers on network", serverNode.Network)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|