-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
55 lines (47 loc) · 902 Bytes
/
util.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
50
51
52
53
54
55
package sdk
import (
"math/rand"
"net"
"strings"
"time"
)
func getRand() int {
b := make([]byte, 2)
_, err := rand.Read(b)
if err != nil {
return 0
}
return (int(b[0]) << 8) + int(b[1])
}
const alphabeta string = "0123456789abcdefghijklmnopqrstuvwxyz"
func getRandString(n int) string {
rand.Seed(time.Now().UnixNano())
buf := make([]byte, n)
rand.Read(buf[:])
rc := byte(0)
for k, v := range buf {
rc = v & 0x23
buf[k] = alphabeta[rc]
}
return string(buf)
}
// is isIPV4 or not
func isIPV4(add string) bool {
ip := net.ParseIP(add)
return ip != nil && strings.Contains(add, ".")
}
func strAND(s string) string {
return "&" + s
}
func catchInterfacePanic() {
if r := recover(); r != nil {
sdklog.Error("catchInterfacePanic: %v", r)
}
}
func checkStrRes(x interface{}, defaultRes string) string {
res, ok := x.(string)
if ok {
return res
}
return defaultRes
}