package publicip import ( "net/netip" "strings" ) func IPv4(raw string) (string, bool) { ip, err := netip.ParseAddr(strings.TrimSpace(raw)) if err != nil { return "", false } ip = ip.Unmap() if !ip.Is4() || !ip.IsGlobalUnicast() || ip.IsPrivate() { return "", false } for _, prefix := range nonPublicPrefixes { if prefix.Contains(ip) { return "", false } } return ip.String(), true } var nonPublicPrefixes = []netip.Prefix{ netip.MustParsePrefix("0.0.0.0/8"), netip.MustParsePrefix("100.64.0.0/10"), netip.MustParsePrefix("192.0.0.0/24"), netip.MustParsePrefix("192.0.2.0/24"), netip.MustParsePrefix("198.18.0.0/15"), netip.MustParsePrefix("198.51.100.0/24"), netip.MustParsePrefix("203.0.113.0/24"), netip.MustParsePrefix("240.0.0.0/4"), }