forked from jehiah/pulse
/
misc.go
56 lines (51 loc) · 1.11 KB
/
misc.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
package pulse
import (
"errors"
"net"
"github.com/turbobytes/geoipdb/iputils"
)
var (
securityerr = errors.New("Security error: Not allowed to connect to local IP")
tlsHandshakeTimeoutError = errors.New("net/http: TLS handshake timeout")
)
var (
// If not nil, these override CIDR tables of islocalip.
localipv4 []string
localipv6 []string
)
// islocalip is a wrapper around geoipdb/iputils.IsLocalIP
// that allows tuning CIDR tables in order to mock results.
func islocalip(ip net.IP) bool {
if localipv4 == nil && localipv6 == nil {
return iputils.IsLocalIP(ip)
}
ipv4 := ip.To4()
if ipv4 != nil {
if localipv4 != nil {
for _, cidr := range localipv4 {
_, inet, _ := net.ParseCIDR(cidr)
if inet.Contains(ipv4) {
return true
}
}
return false
} else {
return iputils.IsLocalIP(ipv4)
}
}
ipv6 := ip.To16()
if ipv6 != nil {
if localipv6 != nil {
for _, cidr := range localipv6 {
_, inet, _ := net.ParseCIDR(cidr)
if inet.Contains(ipv6) {
return true
}
}
return false
} else {
return iputils.IsLocalIP(ipv6)
}
}
return false
}