util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // package for logicing client and server code
  2. package logic
  3. import (
  4. "crypto/rand"
  5. "encoding/base32"
  6. "encoding/base64"
  7. "encoding/json"
  8. "net"
  9. "os"
  10. "strings"
  11. "time"
  12. "github.com/c-robinson/iplib"
  13. "github.com/gravitl/netmaker/database"
  14. "github.com/gravitl/netmaker/logger"
  15. )
  16. // IsBase64 - checks if a string is in base64 format
  17. // This is used to validate public keys (make sure they're base64 encoded like all public keys should be).
  18. func IsBase64(s string) bool {
  19. _, err := base64.StdEncoding.DecodeString(s)
  20. return err == nil
  21. }
  22. // CheckEndpoint - checks if an endpoint is valid
  23. func CheckEndpoint(endpoint string) bool {
  24. endpointarr := strings.Split(endpoint, ":")
  25. return len(endpointarr) == 2
  26. }
  27. // FileExists - checks if local file exists
  28. func FileExists(f string) bool {
  29. info, err := os.Stat(f)
  30. if os.IsNotExist(err) {
  31. return false
  32. }
  33. return !info.IsDir()
  34. }
  35. // IsAddressInCIDR - util to see if an address is in a cidr or not
  36. func IsAddressInCIDR(address net.IP, cidr string) bool {
  37. var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
  38. if cidrErr != nil {
  39. return false
  40. }
  41. return currentCIDR.Contains(address)
  42. }
  43. // SetNetworkNodesLastModified - sets the network nodes last modified
  44. func SetNetworkNodesLastModified(networkName string) error {
  45. timestamp := time.Now().Unix()
  46. network, err := GetParentNetwork(networkName)
  47. if err != nil {
  48. return err
  49. }
  50. network.NodesLastModified = timestamp
  51. data, err := json.Marshal(&network)
  52. if err != nil {
  53. return err
  54. }
  55. // invalidate cache
  56. CacheNetworksMutex.Lock()
  57. CacheNodes = nil
  58. CacheNetworksMutex.Unlock()
  59. err = database.Insert(networkName, string(data), database.NETWORKS_TABLE_NAME)
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. // RandomString - returns a random string in a charset
  66. func RandomString(length int) string {
  67. randombytes := make([]byte, length)
  68. _, err := rand.Read(randombytes)
  69. if err != nil {
  70. logger.Log(0, "random string", err.Error())
  71. return ""
  72. }
  73. return base32.StdEncoding.EncodeToString(randombytes)[:length]
  74. }
  75. // StringSliceContains - sees if a string slice contains a string element
  76. func StringSliceContains(slice []string, item string) bool {
  77. for _, s := range slice {
  78. if s == item {
  79. return true
  80. }
  81. }
  82. return false
  83. }
  84. // NormalCIDR - returns the first address of CIDR
  85. func NormalizeCIDR(address string) (string, error) {
  86. ip, IPNet, err := net.ParseCIDR(address)
  87. if err != nil {
  88. return "", err
  89. }
  90. if ip.To4() == nil {
  91. net6 := iplib.Net6FromStr(IPNet.String())
  92. IPNet.IP = net6.FirstAddress()
  93. } else {
  94. net4 := iplib.Net4FromStr(IPNet.String())
  95. IPNet.IP = net4.NetworkAddress()
  96. }
  97. return IPNet.String(), nil
  98. }
  99. // StringDifference - returns the elements in `a` that aren't in `b`.
  100. func StringDifference(a, b []string) []string {
  101. mb := make(map[string]struct{}, len(b))
  102. for _, x := range b {
  103. mb[x] = struct{}{}
  104. }
  105. var diff []string
  106. for _, x := range a {
  107. if _, found := mb[x]; !found {
  108. diff = append(diff, x)
  109. }
  110. }
  111. return diff
  112. }
  113. // CheckIfFileExists - checks if file exists or not in the given path
  114. func CheckIfFileExists(filePath string) bool {
  115. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  116. return false
  117. }
  118. return true
  119. }
  120. // RemoveStringSlice - removes an element at given index i
  121. // from a given string slice
  122. func RemoveStringSlice(slice []string, i int) []string {
  123. return append(slice[:i], slice[i+1:]...)
  124. }
  125. // == private ==