util.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. err = database.Insert(networkName, string(data), database.NETWORKS_TABLE_NAME)
  56. if err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. // RandomString - returns a random string in a charset
  62. func RandomString(length int) string {
  63. randombytes := make([]byte, length)
  64. _, err := rand.Read(randombytes)
  65. if err != nil {
  66. logger.Log(0, "random string", err.Error())
  67. return ""
  68. }
  69. return base32.StdEncoding.EncodeToString(randombytes)[:length]
  70. }
  71. // StringSliceContains - sees if a string slice contains a string element
  72. func StringSliceContains(slice []string, item string) bool {
  73. for _, s := range slice {
  74. if s == item {
  75. return true
  76. }
  77. }
  78. return false
  79. }
  80. // NormalCIDR - returns the first address of CIDR
  81. func NormalizeCIDR(address string) (string, error) {
  82. ip, IPNet, err := net.ParseCIDR(address)
  83. if err != nil {
  84. return "", err
  85. }
  86. if ip.To4() == nil {
  87. net6 := iplib.Net6FromStr(IPNet.String())
  88. IPNet.IP = net6.FirstAddress()
  89. } else {
  90. net4 := iplib.Net4FromStr(IPNet.String())
  91. IPNet.IP = net4.NetworkAddress()
  92. }
  93. return IPNet.String(), nil
  94. }
  95. // StringDifference - returns the elements in `a` that aren't in `b`.
  96. func StringDifference(a, b []string) []string {
  97. mb := make(map[string]struct{}, len(b))
  98. for _, x := range b {
  99. mb[x] = struct{}{}
  100. }
  101. var diff []string
  102. for _, x := range a {
  103. if _, found := mb[x]; !found {
  104. diff = append(diff, x)
  105. }
  106. }
  107. return diff
  108. }
  109. // CheckIfFileExists - checks if file exists or not in the given path
  110. func CheckIfFileExists(filePath string) bool {
  111. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  112. return false
  113. }
  114. return true
  115. }
  116. // RemoveStringSlice - removes an element at given index i
  117. // from a given string slice
  118. func RemoveStringSlice(slice []string, i int) []string {
  119. return append(slice[:i], slice[i+1:]...)
  120. }
  121. // == private ==