-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
geCurIp.go
298 lines (281 loc) · 7.97 KB
/
geCurIp.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package go_utils
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/hktalent/htmlquery"
"io"
"log"
"math/big"
"net"
"net/http"
"net/url"
"regexp"
"strings"
)
var skpMac = regexp.MustCompile(`docker|lo|utun|gif|stf|awd`)
// 获取本机互联网ip
func GetActivePlublicIp() (string, string) {
var szIp = GetPublicIp()
ifc, err := net.Interfaces()
if err != nil {
fmt.Println(err)
return "", ""
}
for _, i := range ifc {
if 0 < len(skpMac.FindAllString(i.Name, -1)) {
continue
}
if i.Flags&net.FlagUp == 0 || i.Flags&net.FlagLoopback != 0 {
continue
}
addrs, _ := i.Addrs()
bHb := false
for _, addr := range addrs {
if bHb {
break
}
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
if ip.IsPrivate() || 20 < len(ip.String()) {
continue
}
if szIp == ip.String() {
return szIp, i.Name
}
}
}
}
return "", ""
}
// 获取当前 mac 地址 hex 格式,可以作为 51pwn.com 的前缀
func GetActiveMac() string {
ifc, err := net.Interfaces()
if err != nil {
fmt.Println(err)
return ""
}
var a []string
for _, i := range ifc {
if 0 < len(skpMac.FindAllString(i.Name, -1)) {
continue
}
macAddr := strings.TrimSpace(hex.EncodeToString(i.HardwareAddr))
// interface down; loopback interface
if i.Flags&net.FlagUp == 0 || i.Flags&net.FlagLoopback != 0 || macAddr == "" {
continue
}
addrs, _ := i.Addrs()
bHb := false
for _, addr := range addrs {
if bHb {
break
}
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
if !ip.IsPrivate() {
continue
}
a = append(a, macAddr)
bHb = true
fmt.Println(macAddr, ip, addr.String(), addr.Network(), i.Flags.String())
break
}
}
}
if 0 < len(a) {
return strings.Join(a, ",")
} else {
m1 := GetIp()
if nil != m1 {
szIp := fmt.Sprintf("%v", (*m1)["ip"])
return Pack32BinaryIP4(szIp)
}
return ""
}
}
func IP4toInt(IPv4Address net.IP) int64 {
IPv4Int := big.NewInt(0)
IPv4Int.SetBytes(IPv4Address.To4())
return IPv4Int.Int64()
}
// Pack32BinaryIP4("127.0.0.1") 7f000001
// Pack32BinaryIP4("192.168.0.1") c0a80001
func Pack32BinaryIP4(ip4Address string) string {
ipv4Decimal := IP4toInt(net.ParseIP(ip4Address))
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, uint32(ipv4Decimal))
if err != nil {
fmt.Println("Unable to write to buffer:", err)
}
// present in hexadecimal format
result := fmt.Sprintf("%x", buf.Bytes())
return result
}
var PubIp *map[string]interface{}
// map[ad_info:map[adcode:510105 city:成都市 district:xxx nation:中国 province:四川省] ip:117.76.248.175 location:map[lat:30.67485 lng:104.06291]]
/**
{
"ip": "117.176.248.75",
"network": "117.176.240.0/20",
"version": "IPv4",
"city": "Chengdu",
"region": "Sichuan",
"region_code": "SC",
"country": "CN",
"country_name": "China",
"country_code": "CN",
"country_code_iso3": "CHN",
"country_capital": "Beijing",
"country_tld": ".cn",
"continent_code": "AS",
"in_eu": false,
"postal": null,
"latitude": 30.6498,
"longitude": 104.0555,
"timezone": "Asia/Shanghai",
"utc_offset": "+0800",
"country_calling_code": "+86",
"currency": "CNY",
"currency_name": "Yuan Renminbi",
"languages": "zh-CN,yue,wuu,dta,ug,za",
"country_area": 9596960.0,
"country_population": 1411778724,
"asn": "AS9808",
"org": "China Mobile Communications Group Co., Ltd."
}
*/
func GetFromIpapi() *map[string]interface{} {
m1 := map[string]interface{}{}
szUrl := "https://ipapi.co/json/"
c := GetClient(szUrl)
c.DoGetWithClient4SetHd(c.GetClient4Http2(), szUrl, "GET", nil, func(resp *http.Response, err error, szU string) {
if nil == err && nil != resp {
if data, err := io.ReadAll(resp.Body); nil == err {
if nil == Json.Unmarshal(data, &m1) {
m2 := map[string]interface{}{}
m1["location"] = m2
m2["lat"] = m1["latitude"]
m2["lng"] = m1["longitude"]
delete(m1, "latitude")
delete(m1, "longitude")
m3 := map[string]interface{}{}
m1["ad_info"] = m3
m3["city"] = m1["city"]
m3["country"] = m1["country"]
delete(m1, "city")
delete(m1, "country")
}
}
}
}, func() map[string]string {
return map[string]string{
"authority": "ipapi.co",
"accept": "*/*",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"referer": "https://ipapi.co/",
"sec-ch-ua": `"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"`,
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
}
}, true)
return &m1
}
func GetFromIplocation() *map[string]interface{} {
m1 := map[string]interface{}{}
if doc, err := htmlquery.LoadURL("https://iplocation.com"); nil == err {
if node, err := htmlquery.Query(doc, "body > div.top-container > div.bottom-container > div.rubber-container.result > div > table > tbody > tr:nth-child(1) > td > b"); nil == err {
m1["ip"] = node.Data
}
m2 := map[string]interface{}{}
m1["location"] = m2
if node, err := htmlquery.Query(doc, "body > div.top-container > div.bottom-container > div.rubber-container.result > div > table > tbody > tr:nth-child(2) > td"); nil == err {
m2["lat"] = node.Data
}
if node, err := htmlquery.Query(doc, "body > div.top-container > div.bottom-container > div.rubber-container.result > div > table > tbody > tr:nth-child(3) > td"); nil == err {
m2["lng"] = node.Data
}
m3 := map[string]interface{}{}
m1["ad_info"] = m3
if node, err := htmlquery.Query(doc, "body > div.top-container > div.bottom-container > div.rubber-container.result > div > table > tbody > tr:nth-child(6) > td"); nil == err {
m3["city"] = node.Data
}
if node, err := htmlquery.Query(doc, "body > div.top-container > div.bottom-container > div.rubber-container.result > div > table > tbody > tr:nth-child(4) > td > span"); nil == err {
m3["country"] = node.Data
}
}
return &m1
}
var hd1 = map[string]string{"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0", "Accept": "*/*"}
// 当前ip,自动跳过socks proxy
// X-Limit: current_qps=1; limit_qps=50; current_pv=10197; limit_pv=1000000
func GetIp() *map[string]interface{} {
szIp := GetPublicIp()
if nil != Cache1 {
if oM, err := GetAny[map[string]interface{}](szIp); nil == err && 0 < len(oM) {
return &oM
}
}
if nil != PubIp && 0 < len(*PubIp) {
return PubIp
}
szUrl := "https://apis.map.qq.com/ws/location/v1/ip"
c := GetClient(szUrl)
c.UseHttp2 = false
var m1 map[string]interface{}
p11 := c.GetClient(nil)
c.DoGetWithClient4SetHd(p11, szUrl, "POST", strings.NewReader("key="+url.QueryEscape("IVOBZ-QNW6P-SUKDY-LFQSE-LUFCJ-3CFUE")+"&sig=afebe5ad5227ec75a1f3d8b97f888cda"), func(r *http.Response, err1 error, szU string) {
if nil == err1 && nil != r {
defer r.Body.Close()
if data, err := io.ReadAll(r.Body); nil == err {
log.Println(string(data))
if nil == Json.Unmarshal(data, &m1) {
log.Printf("%+v", m1)
}
}
}
}, func() map[string]string {
return hd1
}, false)
if m2, ok := m1["result"]; ok {
m1 = m2.(map[string]interface{})
} else { // 失败,用其他方法获取
m1 = *GetFromIpapi()
bOk := false
if x1, ok := m1["location"]; ok {
if x2, ok := x1.(map[string]interface{}); ok {
if _, ok := x2["lat"]; ok {
bOk = true
}
}
}
if !bOk {
m1 = *GetFromIplocation()
}
}
if 0 == len(m1) {
m1["location"] = GetLocation()
}
PubIp = &m1
PutAny[map[string]interface{}](szIp, m1)
return PubIp
}
// https://www.ipplus360.com/getLocation
func GetLocation() string {
s1 := ""
DoUrlCbk("https://www.ipplus360.com/getLocation", "", hd1, func(r *http.Response, szUrl string) {
defer r.Body.Close()
if data, err := io.ReadAll(r.Body); nil == err {
var m1 = map[string]interface{}{}
if nil == Json.Unmarshal(data, &m1) {
s1 = GetJQ2Str(m1, "data")
}
}
})
return s1
}