-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
94 lines (80 loc) · 1.9 KB
/
helper.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package helper
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)
// ConnIsClose 根据错误来判断连接是否已关闭
func ConnIsClose(err error) bool {
return err == io.EOF ||
strings.Contains(err.Error(), "use of closed network connection") ||
strings.Contains(err.Error(), "the mux has closed") ||
strings.Contains(err.Error(), "connection reset by peer") ||
strings.Contains(err.Error(), "broken pipe")
}
// 根据错误来判断监听是否已关闭
func AcceptIsClose(err error) bool {
return strings.Contains(err.Error(), "use of closed network connection") ||
strings.Contains(err.Error(), "the mux has closed")
}
// NormalizeMac 规范化 MAC,去掉 `:` 再转为小写
func NormalizeMac(mac string) string {
return strings.ToLower(strings.ReplaceAll(mac, ":", ""))
}
// MD5Hash 获取MD5值
func MD5Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}
// Min 获取两个数值最小值
func Min(x, y int) int {
if x > y {
return y
}
return x
}
// Max 获取两个数值最大值
func Max(x, y int) int {
if x > y {
return x
}
return y
}
func Ip2long(ipstr string) (ip uint32) {
r := `^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})`
reg, err := regexp.Compile(r)
if err != nil {
return
}
ips := reg.FindStringSubmatch(ipstr)
if ips == nil {
return
}
ip1, _ := strconv.Atoi(ips[1])
ip2, _ := strconv.Atoi(ips[2])
ip3, _ := strconv.Atoi(ips[3])
ip4, _ := strconv.Atoi(ips[4])
if ip1 > 255 || ip2 > 255 || ip3 > 255 || ip4 > 255 {
return
}
ip += uint32(ip1 * 0x1000000)
ip += uint32(ip2 * 0x10000)
ip += uint32(ip3 * 0x100)
ip += uint32(ip4)
return
}
func Long2ip(ip uint32) string {
return fmt.Sprintf("%d.%d.%d.%d", ip>>24, ip<<8>>24, ip<<16>>24, ip<<24>>24)
}
func InArrayString(str string, arr []string) bool {
for _, v := range arr {
if str == v {
return true
}
}
return false
}