1
0

util.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "encoding/hex"
  19. "fmt"
  20. mathrand "math/rand"
  21. "net"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. // RandID return a rand string used in frp.
  27. func RandID() (id string, err error) {
  28. return RandIDWithLen(8)
  29. }
  30. // RandIDWithLen return a rand string with idLen length.
  31. func RandIDWithLen(idLen int) (id string, err error) {
  32. b := make([]byte, idLen)
  33. _, err = rand.Read(b)
  34. if err != nil {
  35. return
  36. }
  37. id = fmt.Sprintf("%x", b)
  38. return
  39. }
  40. func GetAuthKey(token string, timestamp int64) (key string) {
  41. token = token + fmt.Sprintf("%d", timestamp)
  42. md5Ctx := md5.New()
  43. md5Ctx.Write([]byte(token))
  44. data := md5Ctx.Sum(nil)
  45. return hex.EncodeToString(data)
  46. }
  47. func CanonicalAddr(host string, port int) (addr string) {
  48. if port == 80 || port == 443 {
  49. addr = host
  50. } else {
  51. addr = net.JoinHostPort(host, strconv.Itoa(port))
  52. }
  53. return
  54. }
  55. func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) {
  56. rangeStr = strings.TrimSpace(rangeStr)
  57. numbers = make([]int64, 0)
  58. // e.g. 1000-2000,2001,2002,3000-4000
  59. numRanges := strings.Split(rangeStr, ",")
  60. for _, numRangeStr := range numRanges {
  61. // 1000-2000 or 2001
  62. numArray := strings.Split(numRangeStr, "-")
  63. // length: only 1 or 2 is correct
  64. rangeType := len(numArray)
  65. if rangeType == 1 {
  66. // single number
  67. singleNum, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  68. if errRet != nil {
  69. err = fmt.Errorf("range number is invalid, %v", errRet)
  70. return
  71. }
  72. numbers = append(numbers, singleNum)
  73. } else if rangeType == 2 {
  74. // range numbers
  75. min, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  76. if errRet != nil {
  77. err = fmt.Errorf("range number is invalid, %v", errRet)
  78. return
  79. }
  80. max, errRet := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64)
  81. if errRet != nil {
  82. err = fmt.Errorf("range number is invalid, %v", errRet)
  83. return
  84. }
  85. if max < min {
  86. err = fmt.Errorf("range number is invalid")
  87. return
  88. }
  89. for i := min; i <= max; i++ {
  90. numbers = append(numbers, i)
  91. }
  92. } else {
  93. err = fmt.Errorf("range number is invalid")
  94. return
  95. }
  96. }
  97. return
  98. }
  99. func GenerateResponseErrorString(summary string, err error, detailed bool) string {
  100. if detailed {
  101. return err.Error()
  102. }
  103. return summary
  104. }
  105. func RandomSleep(duration time.Duration, minRatio, maxRatio float64) time.Duration {
  106. min := int64(minRatio * 1000.0)
  107. max := int64(maxRatio * 1000.0)
  108. var n int64
  109. if max <= min {
  110. n = min
  111. } else {
  112. n = mathrand.Int63n(max-min) + min
  113. }
  114. d := duration * time.Duration(n) / time.Duration(1000)
  115. time.Sleep(d)
  116. return d
  117. }