github.com/xxf098/lite-proxy@v0.15.1-0.20230422081941-12c69f323218/common/net/private_net.go (about)

     1  package net
     2  
     3  import "net"
     4  
     5  var privateNetworks []*net.IPNet
     6  
     7  func init() {
     8  	for _, cidr := range []string{
     9  		// RFC 1918: private IPv4 networks
    10  		"10.0.0.0/8",
    11  		"172.16.0.0/12",
    12  		"192.168.0.0/16",
    13  		// RFC 4193: IPv6 ULAs
    14  		"fc00::/7",
    15  		// RFC 6598: reserved prefix for CGNAT
    16  		"100.64.0.0/10",
    17  	} {
    18  		_, subnet, _ := net.ParseCIDR(cidr)
    19  		privateNetworks = append(privateNetworks, subnet)
    20  	}
    21  }
    22  
    23  func IsPrivateAddress(ip net.IP) bool {
    24  	for _, network := range privateNetworks {
    25  		if network.Contains(ip) {
    26  			return true
    27  		}
    28  	}
    29  	return false
    30  }