|
|
@ -106,26 +106,34 @@ func isPrivateIP(ip_str string) bool {
|
|
|
|
|
|
|
|
|
|
|
|
// GetPrivateIP is used to return the first private IP address
|
|
|
|
// GetPrivateIP is used to return the first private IP address
|
|
|
|
// associated with an interface on the machine
|
|
|
|
// associated with an interface on the machine
|
|
|
|
func GetPrivateIP() (*net.IPNet, error) {
|
|
|
|
func GetPrivateIP() (net.IP, error) {
|
|
|
|
addresses, err := net.InterfaceAddrs()
|
|
|
|
addresses, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
|
|
|
|
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Find private IPv4 address
|
|
|
|
// Find private IPv4 address
|
|
|
|
for _, addr := range addresses {
|
|
|
|
for _, rawAddr := range addresses {
|
|
|
|
ip, ok := addr.(*net.IPNet)
|
|
|
|
var ip net.IP
|
|
|
|
if !ok {
|
|
|
|
switch addr := rawAddr.(type) {
|
|
|
|
|
|
|
|
case *net.IPAddr:
|
|
|
|
|
|
|
|
ip = addr.IP
|
|
|
|
|
|
|
|
case *net.IPNet:
|
|
|
|
|
|
|
|
ip = addr.IP
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ip.IP.To4() == nil {
|
|
|
|
|
|
|
|
|
|
|
|
if ip.To4() == nil {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isPrivateIP(ip.IP.String()) {
|
|
|
|
if !isPrivateIP(ip.String()) {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ip, nil
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("No private IP address found")
|
|
|
|
return nil, fmt.Errorf("No private IP address found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|