-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnet.go
82 lines (62 loc) · 1.32 KB
/
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
package spf
import (
"fmt"
"net"
"strings"
)
func networkCIDR(ip, prefix string) (*net.IPNet, error) {
if prefix == "" {
ip := net.ParseIP(ip)
if ip.To4() != nil {
prefix = "32"
} else {
prefix = "128"
}
}
cidrStr := fmt.Sprintf("%s/%s", ip, prefix)
_, network, err := net.ParseCIDR(cidrStr)
return network, err
}
func ipInNetworks(ip net.IP, networks []*net.IPNet) bool {
for _, network := range networks {
if network.Contains(ip) {
return true
}
}
return false
}
func buildNetworks(ips []string, prefix string) []*net.IPNet {
var networks []*net.IPNet
for _, ip := range ips {
network, err := networkCIDR(ip, prefix)
if err == nil {
networks = append(networks, network)
}
}
return networks
}
func aNetworks(m *Mechanism) []*net.IPNet {
ips, _ := net.LookupHost(m.Domain)
return buildNetworks(ips, m.Prefix)
}
func mxNetworks(m *Mechanism) []*net.IPNet {
var networks []*net.IPNet
mxs, _ := net.LookupMX(m.Domain)
for _, mx := range mxs {
ips, _ := net.LookupHost(mx.Host)
networks = append(networks, buildNetworks(ips, m.Prefix)...)
}
return networks
}
func testPTR(m *Mechanism, ip string) bool {
names, err := net.LookupAddr(ip)
if err != nil {
return false
}
for _, name := range names {
if strings.HasSuffix(name, m.Domain) {
return true
}
}
return false
}