mirror of
https://github.com/warmbly/warmbly.git
synced 2026-10-18 16:03:00 +00:00
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
35 lines
774 B
Go
35 lines
774 B
Go
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"),
|
|
}
|