This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |