13fdf464ae
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
33 lines
687 B
Go
33 lines
687 B
Go
package netutil
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
func IsLocalOrPrivateHost(host string) bool {
|
|
host = strings.ToLower(strings.TrimSpace(host))
|
|
if host == "" {
|
|
return false
|
|
}
|
|
if host == "localhost" || strings.HasSuffix(host, ".localhost") || host == "host.docker.internal" {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(host)
|
|
return ip != nil && IsLocalOrPrivateIP(ip)
|
|
}
|
|
|
|
func IsLocalOrPrivateIP(ip net.IP) bool {
|
|
if ip == nil {
|
|
return false
|
|
}
|
|
if ip.IsLoopback() || ip.IsPrivate() || ip.IsUnspecified() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
|
|
return true
|
|
}
|
|
ip4 := ip.To4()
|
|
if ip4 == nil {
|
|
return false
|
|
}
|
|
return ip4[0] == 100 && ip4[1] >= 64 && ip4[1] <= 127
|
|
}
|