util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "crypto/md5"
  17. "crypto/rand"
  18. "crypto/subtle"
  19. "encoding/hex"
  20. "fmt"
  21. mathrand "math/rand"
  22. "net"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. // RandID return a rand string used in frp.
  28. func RandID() (id string, err error) {
  29. return RandIDWithLen(16)
  30. }
  31. // RandIDWithLen return a rand string with idLen length.
  32. func RandIDWithLen(idLen int) (id string, err error) {
  33. if idLen <= 0 {
  34. return "", nil
  35. }
  36. b := make([]byte, idLen/2+1)
  37. _, err = rand.Read(b)
  38. if err != nil {
  39. return
  40. }
  41. id = fmt.Sprintf("%x", b)
  42. return id[:idLen], nil
  43. }
  44. // RandIDWithRandLen return a rand string with length between [start, end).
  45. func RandIDWithRandLen(start, end int) (id string, err error) {
  46. if start >= end {
  47. err = fmt.Errorf("start should be less than end")
  48. return
  49. }
  50. idLen := mathrand.Intn(end-start) + start
  51. return RandIDWithLen(idLen)
  52. }
  53. func GetAuthKey(token string, timestamp int64) (key string) {
  54. md5Ctx := md5.New()
  55. md5Ctx.Write([]byte(token))
  56. md5Ctx.Write([]byte(strconv.FormatInt(timestamp, 10)))
  57. data := md5Ctx.Sum(nil)
  58. return hex.EncodeToString(data)
  59. }
  60. func CanonicalAddr(host string, port int) (addr string) {
  61. if port == 80 || port == 443 {
  62. addr = host
  63. } else {
  64. addr = net.JoinHostPort(host, strconv.Itoa(port))
  65. }
  66. return
  67. }
  68. func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) {
  69. rangeStr = strings.TrimSpace(rangeStr)
  70. numbers = make([]int64, 0)
  71. // e.g. 1000-2000,2001,2002,3000-4000
  72. numRanges := strings.Split(rangeStr, ",")
  73. for _, numRangeStr := range numRanges {
  74. // 1000-2000 or 2001
  75. numArray := strings.Split(numRangeStr, "-")
  76. // length: only 1 or 2 is correct
  77. rangeType := len(numArray)
  78. switch rangeType {
  79. case 1:
  80. // single number
  81. singleNum, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  82. if errRet != nil {
  83. err = fmt.Errorf("range number is invalid, %v", errRet)
  84. return
  85. }
  86. numbers = append(numbers, singleNum)
  87. case 2:
  88. // range numbers
  89. min, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  90. if errRet != nil {
  91. err = fmt.Errorf("range number is invalid, %v", errRet)
  92. return
  93. }
  94. max, errRet := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64)
  95. if errRet != nil {
  96. err = fmt.Errorf("range number is invalid, %v", errRet)
  97. return
  98. }
  99. if max < min {
  100. err = fmt.Errorf("range number is invalid")
  101. return
  102. }
  103. for i := min; i <= max; i++ {
  104. numbers = append(numbers, i)
  105. }
  106. default:
  107. err = fmt.Errorf("range number is invalid")
  108. return
  109. }
  110. }
  111. return
  112. }
  113. func GenerateResponseErrorString(summary string, err error, detailed bool) string {
  114. if detailed {
  115. return err.Error()
  116. }
  117. return summary
  118. }
  119. func RandomSleep(duration time.Duration, minRatio, maxRatio float64) time.Duration {
  120. min := int64(minRatio * 1000.0)
  121. max := int64(maxRatio * 1000.0)
  122. var n int64
  123. if max <= min {
  124. n = min
  125. } else {
  126. n = mathrand.Int63n(max-min) + min
  127. }
  128. d := duration * time.Duration(n) / time.Duration(1000)
  129. time.Sleep(d)
  130. return d
  131. }
  132. func ConstantTimeEqString(a, b string) bool {
  133. return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
  134. }