This repository has been archived by the owner. It is now read-only.
forked from name5566/leaf
/
ip.go
163 lines (146 loc) · 3.1 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
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
package netutil
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"math/big"
"net"
"net/http"
)
func NetAddr2IPPort(addr net.Addr) (ip string, port int) {
switch addr := addr.(type) {
case *net.UDPAddr:
ip = addr.IP.String()
port = addr.Port
case *net.TCPAddr:
ip = addr.IP.String()
port = addr.Port
}
return
}
func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
func IsInternetOK() (ok bool) {
_, err := http.Get("http://www.google.cn/generate_204")
if err != nil {
return false
}
return true
}
func GetAllIP() []net.IP {
ifaces, err := net.Interfaces()
if err != nil {
return nil
}
result := make([]net.IP, 0, 1)
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
ip, ok := addr.(*net.IPNet)
if ok && !ip.IP.IsLoopback() && ip.IP.To4() != nil {
result = append(result, ip.IP)
}
}
}
return result
}
func IsPublicIP(IP net.IP) bool {
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return false
}
if ip4 := IP.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return false
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return false
case ip4[0] == 192 && ip4[1] == 168:
return false
default:
return true
}
}
return false
}
func InetAton(ip net.IP) int64 {
ipv4Int := big.NewInt(0)
ipv4Int.SetBytes(ip.To4())
return ipv4Int.Int64()
}
func InetNtoa(ipnr int64) net.IP {
var bs [4]byte
bs[0] = byte(ipnr & 0xFF)
bs[1] = byte((ipnr >> 8) & 0xFF)
bs[2] = byte((ipnr >> 16) & 0xFF)
bs[3] = byte((ipnr >> 24) & 0xFF)
return net.IPv4(bs[3], bs[2], bs[1], bs[0])
}
func IpBetween(from net.IP, to net.IP, test net.IP) bool {
if from == nil || to == nil || test == nil {
return false
}
from16 := from.To16()
to16 := to.To16()
test16 := test.To16()
if from16 == nil || to16 == nil || test16 == nil {
return false
}
if bytes.Compare(test16, from16) >= 0 && bytes.Compare(test16, to16) <= 0 {
return true
}
return false
}
type IPInfo struct {
Code int `json:"code"`
Data IP `json:"data"`
}
type IP struct {
Country string `json:"country"`
CountryId string `json:"country_id"`
Area string `json:"area"`
AreaId string `json:"area_id"`
Region string `json:"region"`
RegionId string `json:"region_id"`
City string `json:"city"`
CityId string `json:"city_id"`
Isp string `json:"isp"`
}
//获取公网IP
func GetExternalIP() string {
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return ""
}
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
return string(content)
}
func GetIPInfo(ip string) *IPInfo {
url := "http://ip.taobao.com/service/getIpInfo.php?ip="
url += ip
resp, err := http.Get(url)
if err != nil {
return nil
}
defer resp.Body.Close()
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
var result IPInfo
if err := json.Unmarshal(out, &result); err != nil {
return nil
}
return &result
}