mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-01-07 15:33:08 +00:00
add IsPrivateAddr
This commit is contained in:
parent
49e7bdea20
commit
2945cfc2e6
15
private.go
15
private.go
@ -61,6 +61,21 @@ func IsPublicAddr(a ma.Multiaddr) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsPrivateAddr returns true if the IP part of the mutiadr 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
|
||||
}
|
||||
|
||||
func inAddrRange(s string, ipnets []*net.IPNet) bool {
|
||||
ip := net.ParseIP(s)
|
||||
for _, ipnet := range ipnets {
|
||||
|
||||
@ -16,6 +16,10 @@ func TestIsPublicAddr(t *testing.T) {
|
||||
t.Fatal("192.168.1.1 is not a public address!")
|
||||
}
|
||||
|
||||
if !IsPrivateAddr(a) {
|
||||
t.Fatal("192.168.1.1 is a private address!")
|
||||
}
|
||||
|
||||
a, err = ma.NewMultiaddr("/ip4/1.1.1.1/tcp/80")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -24,4 +28,8 @@ func TestIsPublicAddr(t *testing.T) {
|
||||
if !IsPublicAddr(a) {
|
||||
t.Fatal("1.1.1.1 is a public address!")
|
||||
}
|
||||
|
||||
if IsPrivateAddr(a) {
|
||||
t.Fatal("1.1.1.1 is not a private address!")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user