Files
moteva/server/internal/infrastructure/netutil/local_network.go
T
2026-07-08 16:17:20 +08:00

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
}