github.com/Rookout/GoSDK@v0.1.48/pkg/information/network.go (about)

     1  package information
     2  
     3  import (
     4  	pb "github.com/Rookout/GoSDK/pkg/protobuf"
     5  	"net"
     6  )
     7  
     8  func resolveHostIp() string {
     9  
    10  	netInterfaceAddresses, err := net.InterfaceAddrs()
    11  
    12  	if err != nil {
    13  		return ""
    14  	}
    15  
    16  	for _, addr := range netInterfaceAddresses {
    17  		var ip net.IP
    18  		switch v := addr.(type) {
    19  		case *net.IPNet:
    20  			ip = v.IP
    21  		case *net.IPAddr:
    22  			ip = v.IP
    23  		default:
    24  			continue
    25  		}
    26  		if !ip.IsLoopback() && ip.To4() != nil {
    27  			return ip.String()
    28  		}
    29  	}
    30  	return ""
    31  }
    32  
    33  func collectNetwork(info *AgentInformation) error {
    34  	info.Network = &pb.NetworkInformation{
    35  		IpAddr: resolveHostIp(),
    36  	}
    37  	return nil
    38  }