forked from MQEnergy/go-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
43 lines (40 loc) · 876 Bytes
/
node.go
File metadata and controls
43 lines (40 loc) · 876 Bytes
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
package go_websocket
import (
"errors"
"net"
"strconv"
"strings"
)
// convertToIntIP 转换ip为int
func convertToIntIP(ip string) (int, error) {
ips := strings.Split(ip, ".")
E := errors.New("Not A IP.")
if len(ips) != 4 {
return 0, E
}
var intIP int
for k, v := range ips {
i, err := strconv.Atoi(v)
if err != nil || i > 255 {
return 0, E
}
intIP = intIP | i<<uint(8*(3-k))
}
return intIP, nil
}
// GetLocalIpToInt 获取本机IP转成int
func GetLocalIpToInt() (int, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return 0, err
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return convertToIntIP(ipnet.IP.String())
}
}
}
return 0, errors.New("can not find the client ip address")
}