netutils.go 759 B

1234567891011121314151617181920212223242526272829303132333435
  1. package utils
  2. import (
  3. "net"
  4. log "github.com/sirupsen/logrus"
  5. )
  6. // IsHostnameInternal will attempt to determine if the hostname is internal to
  7. // this server's network or is the loopback address.
  8. func IsHostnameInternal(hostname string) bool {
  9. // If this is already an IP address don't try to resolve it
  10. if ip := net.ParseIP(hostname); ip != nil {
  11. return isIPAddressInternal(ip)
  12. }
  13. ips, err := net.LookupIP(hostname)
  14. if err != nil {
  15. // Default to false if we can't resolve the hostname.
  16. log.Debugln("Unable to resolve hostname:", hostname)
  17. return false
  18. }
  19. for _, ip := range ips {
  20. if isIPAddressInternal(ip) {
  21. return true
  22. }
  23. }
  24. return false
  25. }
  26. func isIPAddressInternal(ip net.IP) bool {
  27. return ip.IsLoopback() || ip.IsPrivate()
  28. }