-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.net.go
101 lines (92 loc) · 1.92 KB
/
util.net.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
package util
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"regexp"
)
// 获取当前内网IP
func GetIPLocal() []string {
res := make([]string, 0, 4)
ips, _ := net.InterfaceAddrs()
for _, ip := range ips {
if ipnet, ok := ip.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && ipnet.IP.String()[:7] != "169.254" {
res = append(res, ipnet.IP.String())
}
}
}
return res
}
// 当前IP的缓存
var IP string
// 获取当前IP, 有缓存
func GetIP() string {
if IP != "" {
return IP
}
IP, _ = CheckIP()
return IP
}
// IP信息
type IPInfo struct {
IP string `json:"ip"`
Pro string `json:"pro"`
ProCode string `json:"proCode"`
City string `json:"city"`
CityCode string `json:"cityCode"`
Region string `json:"region"`
RegionCode string `json:"regionCode"`
Addr string `json:"addr"`
RegionNames string `json:"regionNames"`
}
// 获取当前IP信息
func GetIPInfo() *IPInfo {
res, err := http.Get("http://whois.pconline.com.cn/ipJson.jsp")
if err != nil {
return nil
}
bin, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil
}
str := string(bin)
v := &IPInfo{}
ex := regexp.MustCompile(`\{"ip".+?\}`)
bin = []byte(ex.FindString(str))
json.Unmarshal(bin, v)
return v
}
// 获取当前IP, 无缓存
func FlushIP() string {
IP, _ = CheckIP()
return IP
}
// 获取当前IP
func CheckIP() (ip string, err error) {
res, err := http.Get("http://checkip.dyndns.com/")
if err != nil {
panic("CheckIP: App is Offine! Dead.")
}
bin, err := ioutil.ReadAll(res.Body)
str := string(bin)
if len(str) < 92 {
panic("CheckIP: Bad Response!")
}
ip = str[76 : len(str)-14]
return
}
// 下载图片(带缓存)
func FetchImageAsStream(url string) (r *bytes.Buffer) {
res, err := http.Get(url)
if err != nil {
return nil
}
defer res.Body.Close()
buf := &bytes.Buffer{}
io.Copy(buf, res.Body)
return buf
}