Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
private networks and IsPublicAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Oct 16, 2018
1 parent 1cb9a0e commit cbdd2e9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package autonat

import (
"net"

ma "github.com/multiformats/go-multiaddr"
)

// Private4 and Private6 are well-known private networks
// These are exported to allow overriding for testing
var Private4, Private6 []*net.IPNet
var privateCIDR4 = []string{
// localhost
"127.0.0.0/8",
// private networks
"10.0.0.0/8",
"100.64.0.0/10",
"172.16.0.0/12",
"192.168.0.0/16",
// link local
"169.254.0.0/16",
}
var privateCIDR6 = []string{
// localhost
"::1/128",
// ULA reserved
"fc00::/7",
// link local
"fe80::/10",
}

func init() {
Private4 = parsePrivateCIDR(privateCIDR4)
Private6 = parsePrivateCIDR(privateCIDR6)
}

func parsePrivateCIDR(cidrs []string) []*net.IPNet {
ipnets := make([]*net.IPNet, len(cidrs))
for i, cidr := range cidrs {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
panic(err)
}
ipnets[i] = ipnet
}
return ipnets
}

// IsPublicAddr retruns true if the IP part of the multiaddr is not in a private network
func IsPublicAddr(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 {
if ipnet.Contains(ip) {
return true
}
}

return false
}

0 comments on commit cbdd2e9

Please sign in to comment.