-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
ip_wan.go
155 lines (147 loc) · 4.65 KB
/
ip_wan.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
package ip2region
import (
"errors"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/nging/v5/application/library/restclient"
)
const (
IPv4Rule = `((?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`
IPv6Rule = `((?:(?:(?:[0-9A-Fa-f]{1,4}:){7}(?:[0-9A-Fa-f]{1,4}|:))|(?:(?:[0-9A-Fa-f]{1,4}:){6}(?::[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){5}(?:(?:(?::[0-9A-Fa-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){4}(?:(?:(?::[0-9A-Fa-f]{1,4}){1,3})|(?:(?::[0-9A-Fa-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9A-Fa-f]{1,4}:){3}(?:(?:(?::[0-9A-Fa-f]{1,4}){1,4})|(?:(?::[0-9A-Fa-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9A-Fa-f]{1,4}:){2}(?:(?:(?::[0-9A-Fa-f]{1,4}){1,5})|(?:(?::[0-9A-Fa-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9A-Fa-f]{1,4}:){1}(?:(?:(:[0-9A-Fa-f]{1,4}){1,6})|(?:(?::[0-9A-Fa-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9A-Fa-f]{1,4}){1,7})|(?:(?::[0-9A-Fa-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))))`
)
var ipv6Regexp = regexp.MustCompile(IPv6Rule)
var ipv4Regexp = regexp.MustCompile(IPv4Rule)
func FindIPv4(content string) string {
matches := ipv4Regexp.FindAllStringSubmatch(content, 1)
if len(matches) > 0 && len(matches[0]) > 1 {
return matches[0][1]
}
return ``
}
func FindIPv6(content string) string {
matches := ipv6Regexp.FindAllStringSubmatch(content, 1)
if len(matches) > 0 && len(matches[0]) > 1 {
return matches[0][1]
}
return ``
}
type WANIP struct {
IP string
QueryTime time.Time
}
func GetWANIP(cachedSeconds float64, ipVers ...int) (wanIP WANIP, err error) {
var (
ipv4 string
ipv6 string
ipVer = 4
)
if len(ipVers) > 0 && ipVers[0] == 6 {
ipVer = ipVers[0]
}
cacheFile := `v` + strconv.Itoa(ipVer)
if cachedSeconds > 0 {
var valid bool
if m, e := common.ModTimeCache(`ip`, cacheFile); e == nil {
wanIP.QueryTime = m
if time.Since(m).Seconds() < cachedSeconds { // 缓存1小时(3600秒)
valid = true
}
}
if valid {
if b, e := common.ReadCache(`ip`, cacheFile); e == nil {
wanIP.IP = strings.TrimSpace(string(b))
return
}
}
}
var errs []string
for _, provider := range wanIPProviders {
if provider == nil || provider.Disabled || len(provider.URL) == 0 {
continue
}
if ipVer == 4 {
if !provider.HasIPv4Rule() {
continue
}
} else {
if !provider.HasIPv6Rule() {
continue
}
}
client := restclient.Resty()
resp, err := client.Execute(provider.Method, provider.URL)
if err != nil {
errs = append(errs, `[`+provider.Name+`] `+err.Error())
continue
}
if !resp.IsSuccess() {
if resp.StatusCode() == http.StatusNotFound {
provider.Disabled = true
}
errs = append(errs, `[`+provider.Name+`] `+strconv.Itoa(resp.StatusCode())+`: `+resp.Status())
continue
}
body := resp.Body()
if len(body) == 0 {
continue
}
if provider.ip6regexp != nil {
if ipVer == 6 {
matches := provider.ip6regexp.FindAllStringSubmatch(string(body), 1)
if len(matches) > 0 && len(matches[0]) > 1 {
ipv6 = strings.TrimSpace(matches[0][1])
if len(ipv6) > 0 {
if ipv6 = FindIPv6(ipv6); len(ipv6) > 0 {
break
}
}
}
}
} else if provider.IP6Rule == `=` {
if ipVer == 6 {
ipv6 = strings.TrimSpace(string(body))
if len(ipv6) > 0 {
if ipv6 = FindIPv6(ipv6); len(ipv6) > 0 {
break
}
}
}
continue // 返回内容是IPv6,则没有必要再找IPv4了
}
if ipVer == 4 {
if provider.ip4regexp != nil {
matches := provider.ip4regexp.FindAllStringSubmatch(string(body), 1)
//com.Dump(matches)
if len(matches) > 0 && len(matches[0]) > 1 {
ipv4 = strings.TrimSpace(matches[0][1])
}
} else {
ipv4 = strings.TrimSpace(string(body))
}
if len(ipv4) > 0 {
if ipv4 = FindIPv4(ipv4); len(ipv4) > 0 {
break
}
}
}
}
wanIP.QueryTime = time.Now()
if ipVer == 4 {
wanIP.IP = ipv4
} else {
wanIP.IP = ipv6
}
if len(wanIP.IP) > 0 {
if err := common.WriteCache(`ip`, cacheFile, []byte(wanIP.IP)); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
err = errors.New(strings.Join(errs, "\n"))
}
return
}