-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmyip.go
262 lines (234 loc) · 4.75 KB
/
myip.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package myip
import (
"io/ioutil"
"net"
"net/http"
"strings"
"time"
"github.com/fufuok/utils"
)
var externalIPAPI = map[string][]string{
"ipv4": {
"https://4.ipw.cn",
"http://ipinfo.io/ip",
"http://ifconfig.me/ip",
"http://ident.me",
"http://myexternalip.com/raw",
"https://api64.ipify.org",
"https://api.ipify.org",
},
"ipv6": {
"https://6.ipw.cn",
"http://ifconfig.me/ip",
"http://ident.me",
"http://myexternalip.com/raw",
},
}
// ExternalIPAny 获取外网地址
func ExternalIPAny(retries ...int) string {
n := 1
if len(retries) > 0 && retries[0] > 0 {
n += retries[0]
}
ip := ""
for i := 0; i < n; i++ {
ip = getExternalIP("ipv4", false)
if ip == "" {
ip = getExternalIP("ipv6", false)
}
if ip != "" {
break
}
}
return ip
}
// ExternalIP 获取外网地址 (出口公网地址)
func ExternalIP(v ...string) string {
if len(v) > 0 && v[0] == "ipv6" {
return ExternalIPv6()
}
return ExternalIPv4()
}
// ExternalIPv4 获取外网地址 (IPv4)
func ExternalIPv4() string {
return getExternalIP("ipv4", true)
}
// ExternalIPv6 获取外网地址 (IPv6)
func ExternalIPv6() string {
return getExternalIP("ipv6", true)
}
// 逐项请求外网地址
func getExternalIP(v string, strict bool) string {
if v != "ipv6" {
v = "ipv4"
}
for _, u := range externalIPAPI[v] {
if ip, ok := getAPI(u); ok {
if strict {
switch v {
case "ipv6":
if strings.Count(ip, ":") <= 1 {
continue
}
default:
if strings.Count(ip, ":") > 0 || strings.Count(ip, ".") != 3 {
continue
}
}
}
return ip
}
}
return ""
}
// 请求 API 获取公网 IP
func getAPI(u string) (string, bool) {
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Get(u)
if err != nil {
return "", false
}
defer func() {
_ = resp.Body.Close()
}()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", false
}
ip := net.ParseIP(strings.TrimSpace(string(b)))
if ip != nil {
return ip.String(), true
}
return "", false
}
// InternalIPAny 获取内网地址
func InternalIPAny() string {
ip := InternalIPv4()
if ip == "" {
ip = InternalIPv6()
}
return ip
}
// InternalIPv4 获取内网地址 (IPv4)
func InternalIPv4() string {
return InternalIP("", "udp4")
}
// InternalIPv6 获取内网地址 (临时 IPv6 地址)
func InternalIPv6() string {
return InternalIP("[2001:4860:4860::8888]:53", "udp6")
}
// InternalIP 获取内网地址 (出口本地地址)
func InternalIP(dstAddr, network string) string {
if dstAddr == "" {
dstAddr = "8.8.8.8:53"
}
if network == "" {
network = "udp"
}
conn, err := net.DialTimeout(network, dstAddr, time.Second)
if err != nil {
return ""
}
defer func() {
_ = conn.Close()
}()
addr := conn.LocalAddr().String()
ip := net.ParseIP(addr).String()
if ip == "<nil>" {
ip, _, _ = net.SplitHostPort(addr)
}
return ip
}
// LocalIP 获取本地地址 (第一个), 可指定要排除的接口, 比如: "lo", "vpp"
func LocalIP(exclude ...string) string {
ifaces, err := net.Interfaces()
if err != nil {
return ""
}
var ip net.IP
for _, i := range ifaces {
if utils.InStrings(exclude, i.Name) {
continue
}
addrs, err := i.Addrs()
if err != nil {
return ""
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
ip = net.IPv4zero
}
if !ip.IsLinkLocalUnicast() && !ip.IsLoopback() && ip.To4() != nil {
return ip.String()
}
}
}
return ""
}
// LocalIPv4s 获取所有本地地址 IPv4
func LocalIPv4s() (ips []string) {
addrs, err := net.InterfaceAddrs()
if err == nil {
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok &&
!ipnet.IP.IsLinkLocalUnicast() && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
}
return
}
// InterfaceAddrs 获取所有带 IP 的接口和对应的所有 IP
// 排除本地链路地址和环回地址
func InterfaceAddrs(v ...string) (map[string][]net.IP, error) {
ifAddrs := make(map[string][]net.IP)
ifaces, err := net.Interfaces()
if err != nil {
return ifAddrs, err
}
var (
ip net.IP
t string
)
if len(v) > 0 {
t = strings.ToLower(v[0])
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
return ifAddrs, err
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
ip = net.IPv4zero
}
if ip.IsLinkLocalUnicast() || ip.IsLoopback() {
continue
}
switch t {
case "ipv6":
if ip.To4() != nil {
continue
}
case "ipv4":
if ip.To4() == nil {
continue
}
}
ifAddrs[i.Name] = append(ifAddrs[i.Name], ip)
}
}
return ifAddrs, nil
}