common.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 Github Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package utils
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "encoding/binary"
  9. "encoding/hex"
  10. "fmt"
  11. "math"
  12. "net"
  13. "os"
  14. "reflect"
  15. "runtime"
  16. "strconv"
  17. "sync"
  18. "sync/atomic"
  19. "unicode"
  20. googleuuid "github.com/google/uuid"
  21. )
  22. const (
  23. toLowerTable = "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
  24. toUpperTable = "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\u007f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
  25. )
  26. // Copyright © 2014, Roger Peppe
  27. // github.com/rogpeppe/fastuuid
  28. // All rights reserved.
  29. const (
  30. emptyUUID = "00000000-0000-0000-0000-000000000000"
  31. )
  32. var (
  33. uuidSeed [24]byte
  34. uuidCounter uint64
  35. uuidSetup sync.Once
  36. unitsSlice = []byte("kmgtp")
  37. )
  38. // UUID generates an universally unique identifier (UUID)
  39. func UUID() string {
  40. // Setup seed & counter once
  41. uuidSetup.Do(func() {
  42. if _, err := rand.Read(uuidSeed[:]); err != nil {
  43. panic(fmt.Errorf("utils: failed to seed UUID generator: %w", err))
  44. }
  45. uuidCounter = binary.LittleEndian.Uint64(uuidSeed[:8])
  46. })
  47. // first 8 bytes differ, taking a slice of the first 16 bytes
  48. x := atomic.AddUint64(&uuidCounter, 1)
  49. uuid := uuidSeed
  50. binary.LittleEndian.PutUint64(uuid[:8], x)
  51. uuid[6], uuid[9] = uuid[9], uuid[6]
  52. // RFC4122 v4
  53. uuid[6] = (uuid[6] & 0x0f) | 0x40
  54. uuid[8] = uuid[8]&0x3f | 0x80
  55. // create UUID representation of the first 128 bits
  56. b := make([]byte, 36)
  57. hex.Encode(b[0:8], uuid[0:4])
  58. b[8] = '-'
  59. hex.Encode(b[9:13], uuid[4:6])
  60. b[13] = '-'
  61. hex.Encode(b[14:18], uuid[6:8])
  62. b[18] = '-'
  63. hex.Encode(b[19:23], uuid[8:10])
  64. b[23] = '-'
  65. hex.Encode(b[24:], uuid[10:16])
  66. return UnsafeString(b)
  67. }
  68. // UUIDv4 returns a Random (Version 4) UUID.
  69. // The strength of the UUIDs is based on the strength of the crypto/rand package.
  70. func UUIDv4() string {
  71. token, err := googleuuid.NewRandom()
  72. if err != nil {
  73. panic(fmt.Errorf("utils: failed to generate secure UUID: %w", err))
  74. }
  75. return token.String()
  76. }
  77. // FunctionName returns function name
  78. func FunctionName(fn interface{}) string {
  79. t := reflect.ValueOf(fn).Type()
  80. if t.Kind() == reflect.Func {
  81. return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
  82. }
  83. return t.String()
  84. }
  85. // GetArgument check if key is in arguments
  86. func GetArgument(arg string) bool {
  87. for i := range os.Args[1:] {
  88. if os.Args[1:][i] == arg {
  89. return true
  90. }
  91. }
  92. return false
  93. }
  94. // IncrementIPRange Find available next IP address
  95. func IncrementIPRange(ip net.IP) {
  96. for j := len(ip) - 1; j >= 0; j-- {
  97. ip[j]++
  98. if ip[j] > 0 {
  99. break
  100. }
  101. }
  102. }
  103. // ConvertToBytes returns integer size of bytes from human-readable string, ex. 42kb, 42M
  104. // Returns 0 if string is unrecognized
  105. func ConvertToBytes(humanReadableString string) int {
  106. strLen := len(humanReadableString)
  107. if strLen == 0 {
  108. return 0
  109. }
  110. var unitPrefixPos, lastNumberPos int
  111. // loop the string
  112. for i := strLen - 1; i >= 0; i-- {
  113. // check if the char is a number
  114. if unicode.IsDigit(rune(humanReadableString[i])) {
  115. lastNumberPos = i
  116. break
  117. } else if humanReadableString[i] != ' ' {
  118. unitPrefixPos = i
  119. }
  120. }
  121. if lastNumberPos < 0 {
  122. return 0
  123. }
  124. // fetch the number part and parse it to float
  125. size, err := strconv.ParseFloat(humanReadableString[:lastNumberPos+1], 64)
  126. if err != nil {
  127. return 0
  128. }
  129. // check the multiplier from the string and use it
  130. if unitPrefixPos > 0 {
  131. // convert multiplier char to lowercase and check if exists in units slice
  132. index := bytes.IndexByte(unitsSlice, toLowerTable[humanReadableString[unitPrefixPos]])
  133. if index != -1 {
  134. const bytesPerKB = 1000
  135. size *= math.Pow(bytesPerKB, float64(index+1))
  136. }
  137. }
  138. return int(size)
  139. }