@@ -2,6 +2,7 @@ package manet
2
2
3
3
import (
4
4
"net"
5
+ "strings"
5
6
6
7
ma "github.com/multiformats/go-multiaddr"
7
8
)
@@ -46,6 +47,35 @@ var unroutableCIDR6 = []string{
46
47
"ff00::/8" ,
47
48
}
48
49
50
+ // unResolvableDomains do not resolve to an IP address.
51
+ // Ref: https://en.wikipedia.org/wiki/Special-use_domain_name#Reserved_domain_names
52
+ var unResolvableDomains = []string {
53
+ // Reverse DNS Lookup
54
+ ".in-addr.arpa" ,
55
+ ".ip6.arpa" ,
56
+
57
+ // RFC 6761: Users MAY assume that queries for "invalid" names will always return NXDOMAIN
58
+ // responses
59
+ ".invalid" ,
60
+ }
61
+
62
+ // privateUseDomains are reserved for private use and have no central authority for consistent
63
+ // address resolution
64
+ // Ref: https://en.wikipedia.org/wiki/Special-use_domain_name#Reserved_domain_names
65
+ var privateUseDomains = []string {
66
+ // RFC 8375: Reserved for home networks
67
+ ".home.arpa" ,
68
+
69
+ // MDNS
70
+ ".local" ,
71
+
72
+ // RFC 6761: Users may assume that IPv4 and IPv6 address queries for localhost names will
73
+ // always resolve to the respective IP loopback address
74
+ ".localhost" ,
75
+ // RFC 6761: No central authority for .test names
76
+ ".test" ,
77
+ }
78
+
49
79
func init () {
50
80
Private4 = parseCIDR (privateCIDR4 )
51
81
Private6 = parseCIDR (privateCIDR6 )
@@ -65,7 +95,8 @@ func parseCIDR(cidrs []string) []*net.IPNet {
65
95
return ipnets
66
96
}
67
97
68
- // IsPublicAddr retruns true if the IP part of the multiaddr is a publicly routable address
98
+ // IsPublicAddr returns true if the IP part of the multiaddr is a publicly routable address
99
+ // or if it's a dns address without a special use domain e.g. .local.
69
100
func IsPublicAddr (a ma.Multiaddr ) bool {
70
101
isPublic := false
71
102
ma .ForEach (a , func (c ma.Component ) bool {
@@ -78,6 +109,21 @@ func IsPublicAddr(a ma.Multiaddr) bool {
78
109
case ma .P_IP6 :
79
110
ip := net .IP (c .RawValue ())
80
111
isPublic = ! inAddrRange (ip , Private6 ) && ! inAddrRange (ip , Unroutable6 )
112
+ case ma .P_DNS , ma .P_DNS4 , ma .P_DNS6 , ma .P_DNSADDR :
113
+ dnsAddr := c .Value ()
114
+ isPublic = true
115
+ for _ , ud := range unResolvableDomains {
116
+ if strings .HasSuffix (dnsAddr , ud ) {
117
+ isPublic = false
118
+ return false
119
+ }
120
+ }
121
+ for _ , pd := range privateUseDomains {
122
+ if strings .HasSuffix (dnsAddr , pd ) {
123
+ isPublic = false
124
+ break
125
+ }
126
+ }
81
127
}
82
128
return false
83
129
})
0 commit comments