util.go 3.6 KB

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