123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // Copyright 2017 fatedier, fatedier@gmail.com
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package util
- import (
- "crypto/md5"
- "crypto/rand"
- "crypto/subtle"
- "encoding/hex"
- "fmt"
- mathrand "math/rand"
- "net"
- "strconv"
- "strings"
- "time"
- )
- // RandID return a rand string used in frp.
- func RandID() (id string, err error) {
- return RandIDWithLen(16)
- }
- // RandIDWithLen return a rand string with idLen length.
- func RandIDWithLen(idLen int) (id string, err error) {
- if idLen <= 0 {
- return "", nil
- }
- b := make([]byte, idLen/2+1)
- _, err = rand.Read(b)
- if err != nil {
- return
- }
- id = fmt.Sprintf("%x", b)
- return id[:idLen], nil
- }
- func GetAuthKey(token string, timestamp int64) (key string) {
- md5Ctx := md5.New()
- md5Ctx.Write([]byte(token))
- md5Ctx.Write([]byte(strconv.FormatInt(timestamp, 10)))
- data := md5Ctx.Sum(nil)
- return hex.EncodeToString(data)
- }
- func CanonicalAddr(host string, port int) (addr string) {
- if port == 80 || port == 443 {
- addr = host
- } else {
- addr = net.JoinHostPort(host, strconv.Itoa(port))
- }
- return
- }
- func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) {
- rangeStr = strings.TrimSpace(rangeStr)
- numbers = make([]int64, 0)
- // e.g. 1000-2000,2001,2002,3000-4000
- numRanges := strings.Split(rangeStr, ",")
- for _, numRangeStr := range numRanges {
- // 1000-2000 or 2001
- numArray := strings.Split(numRangeStr, "-")
- // length: only 1 or 2 is correct
- rangeType := len(numArray)
- switch rangeType {
- case 1:
- // single number
- singleNum, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
- if errRet != nil {
- err = fmt.Errorf("range number is invalid, %v", errRet)
- return
- }
- numbers = append(numbers, singleNum)
- case 2:
- // range numbers
- min, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
- if errRet != nil {
- err = fmt.Errorf("range number is invalid, %v", errRet)
- return
- }
- max, errRet := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64)
- if errRet != nil {
- err = fmt.Errorf("range number is invalid, %v", errRet)
- return
- }
- if max < min {
- err = fmt.Errorf("range number is invalid")
- return
- }
- for i := min; i <= max; i++ {
- numbers = append(numbers, i)
- }
- default:
- err = fmt.Errorf("range number is invalid")
- return
- }
- }
- return
- }
- func GenerateResponseErrorString(summary string, err error, detailed bool) string {
- if detailed {
- return err.Error()
- }
- return summary
- }
- func RandomSleep(duration time.Duration, minRatio, maxRatio float64) time.Duration {
- min := int64(minRatio * 1000.0)
- max := int64(maxRatio * 1000.0)
- var n int64
- if max <= min {
- n = min
- } else {
- n = mathrand.Int63n(max-min) + min
- }
- d := duration * time.Duration(n) / time.Duration(1000)
- time.Sleep(d)
- return d
- }
- func ConstantTimeEqString(a, b string) bool {
- return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
- }
|