package enforce import ( "net" "net/netip" ) // SkipInfra is the skip reason of the fixed infrastructure allowlist. const SkipInfra = "infrastructure" func mustPrefixes(l ...string) []netip.Prefix { out := make([]netip.Prefix, 0, len(l)) for _, s := range l { out = append(out, netip.MustParsePrefix(s)) } return out } // cloudflare are the published Cloudflare ranges (https://www.cloudflare.com/ips/). // They are never banned: behind the CDN the "client" of a request is an edge // address shared by thousands of innocent visitors. var cloudflare = mustPrefixes( "173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", "141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", "197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/13", "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22", "2400:cb00::/32", "2606:4700::/32", "2803:f800::/32", "2405:b500::/32", "2405:8100::/32", "2a06:98c0::/29", "2c0f:f248::/32") // publicResolvers are the well known public DNS resolvers. var publicResolvers = func() map[netip.Addr]bool { m := map[netip.Addr]bool{} for _, s := range []string{"1.1.1.1", "1.0.0.1", "8.8.8.8", "8.8.4.4", "9.9.9.9", "149.112.112.112", "208.67.222.222", "208.67.220.220", "2606:4700:4700::1111", "2606:4700:4700::1001", "2001:4860:4860::8888", "2001:4860:4860::8844", "2620:fe::fe", "2620:fe::9", "2620:119:35::35", "2620:119:53::53"} { m[netip.MustParseAddr(s)] = true } return m }() // IsCloudflare reports whether a belongs to a Cloudflare range. func IsCloudflare(a netip.Addr) bool { a = a.Unmap() for _, p := range cloudflare { if p.Contains(a) { return true } } return false } // IsInfrastructure reports whether a is in the fixed allowlist (Cloudflare, public resolvers). func IsInfrastructure(a netip.Addr) bool { a = a.Unmap() return publicResolvers[a] || IsCloudflare(a) } // interfaceAddrs lists the addresses of the local interfaces. func interfaceAddrs() []netip.Addr { var out []netip.Addr addrs, err := net.InterfaceAddrs() if err != nil { return nil } for _, x := range addrs { var ip net.IP switch v := x.(type) { case *net.IPNet: ip = v.IP case *net.IPAddr: ip = v.IP } if a, ok := netip.AddrFromSlice(ip); ok { out = append(out, a.Unmap()) } } return out }