-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.go
174 lines (157 loc) · 3.39 KB
/
ip.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package vege
import (
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
)
var SkipHttpsClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}}
// GetLocalIpv4List
// 获取所有非lo的网卡ip
func GetLocalIpv4List() ([]string, error) {
//获取所有网卡
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
res := make([]string, 0)
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
res = append(res, ipnet.IP.String())
}
}
}
if len(res) == 0 {
err = errors.New("RcpWHuHB not find ip address~")
return nil, err
}
return res, nil
}
// GetLocalIpv4ByUdp
// 获取 localIp by net.Dial("udp", "8.8.8.8:53")
func GetLocalIpv4ByUdp() (ip string, err error) {
conn, err := net.Dial("udp", "8.8.8.8:53")
if err != nil {
return "", err
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
ip = strings.Split(localAddr.String(), ":")[0]
return
}
// GetMacAddr
// 获取本机的MAC地址
func GetMacAddr() (map[string]string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("ePbWfcoN net.Interfaces() err:%w", err)
}
res := make(map[string]string, len(interfaces))
for _, inter := range interfaces {
m := inter.HardwareAddr.String() //获取本机MAC地址
if m != "" {
res[inter.Name] = m
}
}
return res, nil
}
type IpTyp uint8
const (
IpV4Typ IpTyp = iota
IpV6Typ
)
const (
IPSB4URL = `http://api-ipv4.ip.sb/ip`
IPSB6URL = `http://api-ipv6.ip.sb/ip`
)
// GetPubIpVipsb 通过ip.sb获取公网ip
func GetPubIpVipsb(typ IpTyp) (ip string, err error) {
//http实现
url := ""
switch typ {
case IpV4Typ:
url = IPSB4URL
break
case IpV6Typ:
url = IPSB6URL
break
default:
return "", fmt.Errorf("NXmZuhNz no support type:%d", typ)
}
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
ip = strings.TrimSpace(string(b))
return ip, nil
}
const (
IPIP4URL = `http://myip.ipip.net/s`
)
// GetPubIpVipip 通过ip.sb获取公网ip 仅支持ipv4
func GetPubIpVipip(typ IpTyp) (ip string, err error) {
//http实现
url := ""
switch typ {
case IpV4Typ:
url = IPIP4URL
break
case IpV6Typ:
return "", fmt.Errorf("wZWdFhsH no support type:%d", typ)
default:
return "", fmt.Errorf("WMaPBIjm no support type:%d", typ)
}
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
ip = strings.TrimSpace(string(b))
return ip, nil
}
const (
XForwardedFor = "X-Forwarded-For"
XRealIP = "X-Real-IP"
)
// RemoteIp 返回远程客户端的 IP,如 192.168.1.1
func RemoteIp(req *http.Request) string {
remoteAddr := req.RemoteAddr
if ip := req.Header.Get(XRealIP); ip != "" {
remoteAddr = ip
} else if ip = req.Header.Get(XForwardedFor); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
if remoteAddr == "::1" {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
// Ip2Long 将 IPv4 字符串形式转为 uint32
func Ip2Long(str string) uint32 {
ip := net.ParseIP(str)
if ip == nil {
return 0
}
ip = ip.To4()
return binary.BigEndian.Uint32(ip)
}