mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-22 19:18:14 +00:00
use the new multiaddr utilities in IsPublicAddr/IsPrivateAddr
This now means that addresses must *start* with the an IP address, we won't just pull one out of the middle.
This commit is contained in:
parent
7b43167897
commit
0425819f0e
57
private.go
57
private.go
@ -67,36 +67,45 @@ func parseCIDR(cidrs []string) []*net.IPNet {
|
||||
|
||||
// IsPublicAddr retruns true if the IP part of the multiaddr is a publicly routable address
|
||||
func IsPublicAddr(a ma.Multiaddr) bool {
|
||||
ip, err := a.ValueForProtocol(ma.P_IP4)
|
||||
if err == nil {
|
||||
return !inAddrRange(ip, Private4) && !inAddrRange(ip, Unroutable4)
|
||||
}
|
||||
|
||||
ip, err = a.ValueForProtocol(ma.P_IP6)
|
||||
if err == nil {
|
||||
return !inAddrRange(ip, Private6) && !inAddrRange(ip, Unroutable6)
|
||||
}
|
||||
|
||||
return false
|
||||
isPublic := false
|
||||
ma.ForEach(a, func(c ma.Component) bool {
|
||||
switch c.Protocol().Code {
|
||||
case ma.P_IP6ZONE:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
case ma.P_IP4:
|
||||
ip := net.IP(c.RawValue())
|
||||
isPublic = !inAddrRange(ip, Private4) && !inAddrRange(ip, Unroutable4)
|
||||
case ma.P_IP6:
|
||||
ip := net.IP(c.RawValue())
|
||||
isPublic = !inAddrRange(ip, Private6) && !inAddrRange(ip, Unroutable6)
|
||||
}
|
||||
return false
|
||||
})
|
||||
return isPublic
|
||||
}
|
||||
|
||||
// IsPrivateAddr returns true if the IP part of the mutiaddr is in a private network
|
||||
func IsPrivateAddr(a ma.Multiaddr) bool {
|
||||
ip, err := a.ValueForProtocol(ma.P_IP4)
|
||||
if err == nil {
|
||||
return inAddrRange(ip, Private4)
|
||||
}
|
||||
|
||||
ip, err = a.ValueForProtocol(ma.P_IP6)
|
||||
if err == nil {
|
||||
return inAddrRange(ip, Private6)
|
||||
}
|
||||
|
||||
return false
|
||||
isPrivate := false
|
||||
ma.ForEach(a, func(c ma.Component) bool {
|
||||
switch c.Protocol().Code {
|
||||
case ma.P_IP6ZONE:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
case ma.P_IP4:
|
||||
isPrivate = inAddrRange(net.IP(c.RawValue()), Private4)
|
||||
case ma.P_IP6:
|
||||
isPrivate = inAddrRange(net.IP(c.RawValue()), Private6)
|
||||
}
|
||||
return false
|
||||
})
|
||||
return isPrivate
|
||||
}
|
||||
|
||||
func inAddrRange(s string, ipnets []*net.IPNet) bool {
|
||||
ip := net.ParseIP(s)
|
||||
func inAddrRange(ip net.IP, ipnets []*net.IPNet) bool {
|
||||
for _, ipnet := range ipnets {
|
||||
if ipnet.Contains(ip) {
|
||||
return true
|
||||
|
Loading…
x
Reference in New Issue
Block a user