common.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "math"
  11. "net"
  12. "os"
  13. "reflect"
  14. "runtime"
  15. "strconv"
  16. "sync"
  17. "sync/atomic"
  18. "unicode"
  19. googleuuid "github.com/gofiber/fiber/v2/internal/uuid"
  20. )
  21. const (
  22. 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"
  23. 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"
  24. )
  25. // Copyright © 2014, Roger Peppe
  26. // github.com/rogpeppe/fastuuid
  27. // All rights reserved.
  28. var (
  29. uuidSeed [24]byte
  30. uuidCounter uint64
  31. uuidSetup sync.Once
  32. unitsSlice = []byte("kmgtp")
  33. )
  34. // UUID generates an universally unique identifier (UUID)
  35. func UUID() string {
  36. // Setup seed & counter once
  37. uuidSetup.Do(func() {
  38. if _, err := rand.Read(uuidSeed[:]); err != nil {
  39. return
  40. }
  41. uuidCounter = binary.LittleEndian.Uint64(uuidSeed[:8])
  42. })
  43. if atomic.LoadUint64(&uuidCounter) <= 0 {
  44. return "00000000-0000-0000-0000-000000000000"
  45. }
  46. // first 8 bytes differ, taking a slice of the first 16 bytes
  47. x := atomic.AddUint64(&uuidCounter, 1)
  48. uuid := uuidSeed
  49. binary.LittleEndian.PutUint64(uuid[:8], x)
  50. uuid[6], uuid[9] = uuid[9], uuid[6]
  51. // RFC4122 v4
  52. uuid[6] = (uuid[6] & 0x0f) | 0x40
  53. uuid[8] = uuid[8]&0x3f | 0x80
  54. // create UUID representation of the first 128 bits
  55. b := make([]byte, 36)
  56. hex.Encode(b[0:8], uuid[0:4])
  57. b[8] = '-'
  58. hex.Encode(b[9:13], uuid[4:6])
  59. b[13] = '-'
  60. hex.Encode(b[14:18], uuid[6:8])
  61. b[18] = '-'
  62. hex.Encode(b[19:23], uuid[8:10])
  63. b[23] = '-'
  64. hex.Encode(b[24:], uuid[10:16])
  65. return UnsafeString(b)
  66. }
  67. // UUIDv4 returns a Random (Version 4) UUID.
  68. // The strength of the UUIDs is based on the strength of the crypto/rand package.
  69. func UUIDv4() string {
  70. token, err := googleuuid.NewRandom()
  71. if err != nil {
  72. return UUID()
  73. }
  74. return token.String()
  75. }
  76. // FunctionName returns function name
  77. func FunctionName(fn interface{}) string {
  78. t := reflect.ValueOf(fn).Type()
  79. if t.Kind() == reflect.Func {
  80. return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
  81. }
  82. return t.String()
  83. }
  84. // GetArgument check if key is in arguments
  85. func GetArgument(arg string) bool {
  86. for i := range os.Args[1:] {
  87. if os.Args[1:][i] == arg {
  88. return true
  89. }
  90. }
  91. return false
  92. }
  93. // IncrementIPRange Find available next IP address
  94. func IncrementIPRange(ip net.IP) {
  95. for j := len(ip) - 1; j >= 0; j-- {
  96. ip[j]++
  97. if ip[j] > 0 {
  98. break
  99. }
  100. }
  101. }
  102. // ConvertToBytes returns integer size of bytes from human-readable string, ex. 42kb, 42M
  103. // Returns 0 if string is unrecognized
  104. func ConvertToBytes(humanReadableString string) int {
  105. strLen := len(humanReadableString)
  106. if strLen == 0 {
  107. return 0
  108. }
  109. var unitPrefixPos, lastNumberPos int
  110. // loop the string
  111. for i := strLen - 1; i >= 0; i-- {
  112. // check if the char is a number
  113. if unicode.IsDigit(rune(humanReadableString[i])) {
  114. lastNumberPos = i
  115. break
  116. } else if humanReadableString[i] != ' ' {
  117. unitPrefixPos = i
  118. }
  119. }
  120. if lastNumberPos < 0 {
  121. return 0
  122. }
  123. // fetch the number part and parse it to float
  124. size, err := strconv.ParseFloat(humanReadableString[:lastNumberPos+1], 64)
  125. if err != nil {
  126. return 0
  127. }
  128. // check the multiplier from the string and use it
  129. if unitPrefixPos > 0 {
  130. // convert multiplier char to lowercase and check if exists in units slice
  131. index := bytes.IndexByte(unitsSlice, toLowerTable[humanReadableString[unitPrefixPos]])
  132. if index != -1 {
  133. size *= math.Pow(1000, float64(index+1))
  134. }
  135. }
  136. return int(size)
  137. }