-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
49 lines (43 loc) · 1.07 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package qsocket
import (
"fmt"
"math/rand"
"runtime"
"strings"
)
const UserAgentTemplate = "Mozilla/5.0 (%s; %s) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.3"
func GetDeviceUserAgent() string {
return fmt.Sprintf(
UserAgentTemplate,
strings.ToUpper(runtime.GOOS),
strings.ToUpper(runtime.GOARCH),
Version,
)
}
func RandomString(charset string, length int) string {
str := ""
for i := 0; i < length; i++ {
str += string(charset[rand.Intn(len(charset))])
}
return str
}
func NewChecksumUri(sType SocketType) string {
uri := RandomString(URI_CHARSET, rand.Intn(3)+1)
for i := 0; i < 16; i++ {
if CalcChecksum([]byte(uri), CHECKSUM_BASE) == byte(sType) {
return uri
}
uri += RandomString(URI_CHARSET, 1)
}
return NewChecksumUri(sType)
}
// CalcChecksum calculates the modulus based checksum of the given data,
// modulus base is given in the base variable.
func CalcChecksum(data []byte, base byte) byte {
checksum := uint32(0)
for _, n := range data {
checksum += uint32(n)
checksum = checksum % uint32(base)
}
return byte(checksum)
}