network.go 668 B

1234567891011121314151617181920212223242526272829303132333435
  1. package util
  2. import (
  3. "net"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. )
  6. func DetectedHostAddress() string {
  7. netInterfaces, err := net.Interfaces()
  8. if err != nil {
  9. glog.V(0).Infof("failed to detect net interfaces: %v", err)
  10. return ""
  11. }
  12. for _, netInterface := range netInterfaces {
  13. if (netInterface.Flags & net.FlagUp) == 0 {
  14. continue
  15. }
  16. addrs, err := netInterface.Addrs()
  17. if err != nil {
  18. glog.V(0).Infof("get interface addresses: %v", err)
  19. }
  20. for _, a := range addrs {
  21. if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  22. if ipNet.IP.To4() != nil {
  23. return ipNet.IP.String()
  24. }
  25. }
  26. }
  27. }
  28. return "localhost"
  29. }