util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "strconv"
  21. "strings"
  22. )
  23. // RandId return a rand string used in frp.
  24. func RandId() (id string, err error) {
  25. return RandIdWithLen(8)
  26. }
  27. // RandIdWithLen return a rand string with idLen length.
  28. func RandIdWithLen(idLen int) (id string, err error) {
  29. b := make([]byte, idLen)
  30. _, err = rand.Read(b)
  31. if err != nil {
  32. return
  33. }
  34. id = fmt.Sprintf("%x", b)
  35. return
  36. }
  37. func GetAuthKey(token string, timestamp int64) (key string) {
  38. token = token + fmt.Sprintf("%d", timestamp)
  39. md5Ctx := md5.New()
  40. md5Ctx.Write([]byte(token))
  41. data := md5Ctx.Sum(nil)
  42. return hex.EncodeToString(data)
  43. }
  44. func CanonicalAddr(host string, port int) (addr string) {
  45. if port == 80 || port == 443 {
  46. addr = host
  47. } else {
  48. addr = fmt.Sprintf("%s:%d", host, port)
  49. }
  50. return
  51. }
  52. func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) {
  53. rangeStr = strings.TrimSpace(rangeStr)
  54. numbers = make([]int64, 0)
  55. // e.g. 1000-2000,2001,2002,3000-4000
  56. numRanges := strings.Split(rangeStr, ",")
  57. for _, numRangeStr := range numRanges {
  58. // 1000-2000 or 2001
  59. numArray := strings.Split(numRangeStr, "-")
  60. // length: only 1 or 2 is correct
  61. rangeType := len(numArray)
  62. if rangeType == 1 {
  63. // single number
  64. singleNum, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  65. if errRet != nil {
  66. err = fmt.Errorf("range number is invalid, %v", errRet)
  67. return
  68. }
  69. numbers = append(numbers, singleNum)
  70. } else if rangeType == 2 {
  71. // range numbers
  72. min, 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. max, errRet := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64)
  78. if errRet != nil {
  79. err = fmt.Errorf("range number is invalid, %v", errRet)
  80. return
  81. }
  82. if max < min {
  83. err = fmt.Errorf("range number is invalid")
  84. return
  85. }
  86. for i := min; i <= max; i++ {
  87. numbers = append(numbers, i)
  88. }
  89. } else {
  90. err = fmt.Errorf("range number is invalid")
  91. return
  92. }
  93. }
  94. return
  95. }