2021-10-06 03:02:09 +08:00
|
|
|
// package for logicing client and server code
|
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-05-03 01:28:00 +08:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base32"
|
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
|
|
|
"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
|
|
|
|
2022-09-03 20:55:49 +08:00
|
|
|
"github.com/c-robinson/iplib"
|
2021-10-06 03:02:09 +08:00
|
|
|
"github.com/gravitl/netmaker/database"
|
2023-05-03 01:28:00 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
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
|
2023-01-25 18:31:46 +08:00
|
|
|
func IsAddressInCIDR(address net.IP, cidr string) bool {
|
2022-01-19 23:44:00 +08:00
|
|
|
var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
|
|
|
|
if cidrErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-01-25 18:31:46 +08:00
|
|
|
return currentCIDR.Contains(address)
|
2022-01-19 23:44:00 +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-22 03:28:58 +08:00
|
|
|
// RandomString - returns a random string in a charset
|
|
|
|
func RandomString(length int) string {
|
2023-05-03 01:28:00 +08:00
|
|
|
randombytes := make([]byte, length)
|
|
|
|
_, err := rand.Read(randombytes)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, "random string", err.Error())
|
|
|
|
return ""
|
2021-10-22 03:28:58 +08:00
|
|
|
}
|
2023-05-03 01:28:00 +08:00
|
|
|
return base32.StdEncoding.EncodeToString(randombytes)[:length]
|
2021-10-22 03:28:58 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-04-11 23:34:24 +08:00
|
|
|
// NormalizeCIDR - returns the first address of CIDR
|
2022-09-03 20:55:49 +08:00
|
|
|
func NormalizeCIDR(address string) (string, error) {
|
|
|
|
ip, IPNet, err := net.ParseCIDR(address)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if ip.To4() == nil {
|
|
|
|
net6 := iplib.Net6FromStr(IPNet.String())
|
|
|
|
IPNet.IP = net6.FirstAddress()
|
|
|
|
} else {
|
|
|
|
net4 := iplib.Net4FromStr(IPNet.String())
|
2022-09-06 21:17:13 +08:00
|
|
|
IPNet.IP = net4.NetworkAddress()
|
2022-09-03 20:55:49 +08:00
|
|
|
}
|
|
|
|
return IPNet.String(), nil
|
2022-09-08 02:11:56 +08:00
|
|
|
}
|
2022-09-07 18:57:14 +08:00
|
|
|
|
2022-09-14 03:25:56 +08:00
|
|
|
// StringDifference - returns the elements in `a` that aren't in `b`.
|
|
|
|
func StringDifference(a, b []string) []string {
|
|
|
|
mb := make(map[string]struct{}, len(b))
|
|
|
|
for _, x := range b {
|
|
|
|
mb[x] = struct{}{}
|
|
|
|
}
|
|
|
|
var diff []string
|
|
|
|
for _, x := range a {
|
|
|
|
if _, found := mb[x]; !found {
|
|
|
|
diff = append(diff, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
2022-09-30 21:01:57 +08:00
|
|
|
|
2022-10-01 08:57:30 +08:00
|
|
|
// CheckIfFileExists - checks if file exists or not in the given path
|
2022-09-30 21:01:57 +08:00
|
|
|
func CheckIfFileExists(filePath string) bool {
|
|
|
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-12-24 01:38:04 +08:00
|
|
|
|
|
|
|
// RemoveStringSlice - removes an element at given index i
|
|
|
|
// from a given string slice
|
|
|
|
func RemoveStringSlice(slice []string, i int) []string {
|
|
|
|
return append(slice[:i], slice[i+1:]...)
|
|
|
|
}
|
2023-01-25 00:20:06 +08:00
|
|
|
|
2023-11-27 14:03:21 +08:00
|
|
|
// IsSlicesEqual tells whether a and b contain the same elements.
|
|
|
|
// A nil argument is equivalent to an empty slice.
|
|
|
|
func IsSlicesEqual(a, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range a {
|
|
|
|
if v != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-25 00:20:06 +08:00
|
|
|
// == private ==
|