| 1 | package websource |
| 2 | |
| 3 | import "net/netip" |
| 4 | |
| 5 | // blocked are the ranges no public egress address, and no fetch target, |
| 6 | // is ever in: this host, private, shared (CGNAT and the tailnet, |
| 7 | // 100.64.0.0/10), link-local and metadata, reserved, documentation, |
| 8 | // benchmark and transition ranges. The one list for the egress relay, the |
| 9 | // Research fetcher and the site inventory's addresses. |
| 10 | var blocked = func() []netip.Prefix { |
| 11 | var out []netip.Prefix |
| 12 | for _, s := range []string{ |
| 13 | "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", |
| 14 | "192.0.0.0/24", "192.0.2.0/24", "192.88.99.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "224.0.0.0/4", "240.0.0.0/4", |
| 15 | "2001::/23", "2001:db8::/32", "2002::/16", "3fff::/20", |
| 16 | } { |
| 17 | out = append(out, netip.MustParsePrefix(s)) |
| 18 | } |
| 19 | return out |
| 20 | }() |
| 21 | |
| 22 | var publicV6 = netip.MustParsePrefix("2000::/3") |
| 23 | |
| 24 | // PublicAddress reports a globally routed unicast address, outside every |
| 25 | // private, shared, reserved, documentation and transition range. An |
| 26 | // IPv4-mapped address is judged as the IPv4 address it maps. |
| 27 | func PublicAddress(a netip.Addr) bool { |
| 28 | a = a.Unmap() |
| 29 | if !a.IsValid() || a.Zone() != "" || !a.IsGlobalUnicast() || a.IsPrivate() || a.IsLoopback() || a.IsLinkLocalUnicast() { |
| 30 | return false |
| 31 | } |
| 32 | if a.Is6() && !publicV6.Contains(a) { |
| 33 | return false |
| 34 | } |
| 35 | for _, p := range blocked { |
| 36 | if p.Contains(a) { |
| 37 | return false |
| 38 | } |
| 39 | } |
| 40 | return true |
| 41 | } |
| 42 | |