util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. func GetAuthKey(token string, timestamp int64) (key string) {
  45. md5Ctx := md5.New()
  46. md5Ctx.Write([]byte(token))
  47. md5Ctx.Write([]byte(strconv.FormatInt(timestamp, 10)))
  48. data := md5Ctx.Sum(nil)
  49. return hex.EncodeToString(data)
  50. }
  51. func CanonicalAddr(host string, port int) (addr string) {
  52. if port == 80 || port == 443 {
  53. addr = host
  54. } else {
  55. addr = net.JoinHostPort(host, strconv.Itoa(port))
  56. }
  57. return
  58. }
  59. func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) {
  60. rangeStr = strings.TrimSpace(rangeStr)
  61. numbers = make([]int64, 0)
  62. // e.g. 1000-2000,2001,2002,3000-4000
  63. numRanges := strings.Split(rangeStr, ",")
  64. for _, numRangeStr := range numRanges {
  65. // 1000-2000 or 2001
  66. numArray := strings.Split(numRangeStr, "-")
  67. // length: only 1 or 2 is correct
  68. rangeType := len(numArray)
  69. switch rangeType {
  70. case 1:
  71. // single number
  72. singleNum, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  73. if errRet != nil {
  74. err = fmt.Errorf("range number is invalid, %v", errRet)
  75. return
  76. }
  77. numbers = append(numbers, singleNum)
  78. case 2:
  79. // range numbers
  80. min, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  81. if errRet != nil {
  82. err = fmt.Errorf("range number is invalid, %v", errRet)
  83. return
  84. }
  85. max, errRet := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64)
  86. if errRet != nil {
  87. err = fmt.Errorf("range number is invalid, %v", errRet)
  88. return
  89. }
  90. if max < min {
  91. err = fmt.Errorf("range number is invalid")
  92. return
  93. }
  94. for i := min; i <= max; i++ {
  95. numbers = append(numbers, i)
  96. }
  97. default:
  98. err = fmt.Errorf("range number is invalid")
  99. return
  100. }
  101. }
  102. return
  103. }
  104. func GenerateResponseErrorString(summary string, err error, detailed bool) string {
  105. if detailed {
  106. return err.Error()
  107. }
  108. return summary
  109. }
  110. func RandomSleep(duration time.Duration, minRatio, maxRatio float64) time.Duration {
  111. min := int64(minRatio * 1000.0)
  112. max := int64(maxRatio * 1000.0)
  113. var n int64
  114. if max <= min {
  115. n = min
  116. } else {
  117. n = mathrand.Int63n(max-min) + min
  118. }
  119. d := duration * time.Duration(n) / time.Duration(1000)
  120. time.Sleep(d)
  121. return d
  122. }
  123. func ConstantTimeEqString(a, b string) bool {
  124. return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
  125. }