util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // for example: rangeStr is "1000-2000,2001,2002,3000-4000", return an array as port ranges.
  45. func GetPortRanges(rangeStr string) (portRanges [][2]int64, err error) {
  46. // for example: 1000-2000,2001,2002,3000-4000
  47. rangeArray := strings.Split(rangeStr, ",")
  48. for _, portRangeStr := range rangeArray {
  49. // 1000-2000 or 2001
  50. portArray := strings.Split(portRangeStr, "-")
  51. // length: only 1 or 2 is correct
  52. rangeType := len(portArray)
  53. if rangeType == 1 {
  54. singlePort, err := strconv.ParseInt(portArray[0], 10, 64)
  55. if err != nil {
  56. return [][2]int64{}, err
  57. }
  58. portRanges = append(portRanges, [2]int64{singlePort, singlePort})
  59. } else if rangeType == 2 {
  60. min, err := strconv.ParseInt(portArray[0], 10, 64)
  61. if err != nil {
  62. return [][2]int64{}, err
  63. }
  64. max, err := strconv.ParseInt(portArray[1], 10, 64)
  65. if err != nil {
  66. return [][2]int64{}, err
  67. }
  68. if max < min {
  69. return [][2]int64{}, fmt.Errorf("range incorrect")
  70. }
  71. portRanges = append(portRanges, [2]int64{min, max})
  72. } else {
  73. return [][2]int64{}, fmt.Errorf("format error")
  74. }
  75. }
  76. return portRanges, nil
  77. }
  78. func ContainsPort(portRanges [][2]int64, port int64) bool {
  79. for _, pr := range portRanges {
  80. if port >= pr[0] && port <= pr[1] {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. func PortRangesCut(portRanges [][2]int64, port int64) [][2]int64 {
  87. var tmpRanges [][2]int64
  88. for _, pr := range portRanges {
  89. if port >= pr[0] && port <= pr[1] {
  90. leftRange := [2]int64{pr[0], port - 1}
  91. rightRange := [2]int64{port + 1, pr[1]}
  92. if leftRange[0] <= leftRange[1] {
  93. tmpRanges = append(tmpRanges, leftRange)
  94. }
  95. if rightRange[0] <= rightRange[1] {
  96. tmpRanges = append(tmpRanges, rightRange)
  97. }
  98. } else {
  99. tmpRanges = append(tmpRanges, pr)
  100. }
  101. }
  102. return tmpRanges
  103. }