netmaker/netclient/functions/joinutils.go

63 lines
1 KiB
Go
Raw Normal View History

package functions
import (
"github.com/gravitl/netmaker/models"
"net"
)
func getLocalIP(node models.Node) string{
var local string
ifaces, err := net.Interfaces()
if err != nil {
return local
}
_, localrange, err := net.ParseCIDR(node.LocalRange)
if err != nil {
return local
}
found := false
for _, i := range ifaces {
if i.Flags&net.FlagUp == 0 {
continue // interface down
}
if i.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := i.Addrs()
if err != nil {
return local
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
if !found {
ip = v.IP
local = ip.String()
if node.IsLocal == "yes" {
found = localrange.Contains(ip)
} else {
found = true
}
}
case *net.IPAddr:
if !found {
ip = v.IP
local = ip.String()
if node.IsLocal == "yes" {
found = localrange.Contains(ip)
} else {
found = true
}
}
}
}
}
return local
}