util.go 2.8 KB

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