forked from hacklcx/HFish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.go
80 lines (65 loc) · 1.77 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
package ip
import (
"net/http"
"io/ioutil"
"github.com/axgle/mahonia"
"regexp"
"strings"
"net"
"fmt"
"HFish/utils/try"
"HFish/utils/log"
"github.com/ipipdotnet/ipdb-go"
)
var ipipDB *ipdb.City
func init() {
ipipDB, _ = ipdb.NewCity("./db/ipip.ipdb")
}
// 爬虫 ip138 获取 ip 地理信息
// ~~~~~~ 暂时废弃,采用 IPIP
func GetIp138(ip string) string {
result := ""
try.Try(func() {
resp, _ := http.Get("http://ip138.com/ips138.asp?ip=" + ip)
defer resp.Body.Close()
input, _ := ioutil.ReadAll(resp.Body)
out := mahonia.NewDecoder("gbk").ConvertString(string(input))
reg := regexp.MustCompile(`<ul class="ul1"><li>\W*`)
arr := reg.FindAllString(string(out), -1)
str1 := strings.Replace(arr[0], `<ul class="ul1"><li>本站数据:`, "", -1)
str2 := strings.Replace(str1, `</`, "", -1)
str3 := strings.Replace(str2, ` `, "", -1)
str4 := strings.Replace(str3, " ", "", -1)
result = strings.Replace(str4, "\n", "", -1)
if result == "保留地址" {
result = "本地IP"
}
}).Catch(func() {
log.Pr("IP138", "127.0.0.1", "读取 ip138 内容异常")
})
return result
}
func GetLocalIp() string {
netInterfaces, err := net.Interfaces()
if err != nil {
fmt.Println("net.Interfaces failed, err:", err.Error())
}
for i := 0; i < len(netInterfaces); i++ {
if (netInterfaces[i].Flags & net.FlagUp) != 0 {
addrs, _ := netInterfaces[i].Addrs()
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
}
}
return ""
}
// 采用 IPIP 本地库
func GetIp(ip string) (string, string, string) {
ipInfo, _ := ipipDB.FindMap(ip, "CN")
return ipInfo["country_name"], ipInfo["region_name"], ipInfo["city_name"]
}