network.go 425 B

12345678910111213141516171819202122232425
  1. package util
  2. import (
  3. "net"
  4. "github.com/chrislusf/seaweedfs/weed/util/log"
  5. )
  6. func DetectedHostAddress() string {
  7. addrs, err := net.InterfaceAddrs()
  8. if err != nil {
  9. log.Infof("failed to detect ip address: %v", err)
  10. return ""
  11. }
  12. for _, a := range addrs {
  13. if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  14. if ipnet.IP.To4() != nil {
  15. return ipnet.IP.String()
  16. }
  17. }
  18. }
  19. return "localhost"
  20. }