mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
refactor join, fix network add; db retry connect; fix port issue; sqlite default
This commit is contained in:
62
netclient/functions/joinutils.go
Normal file
62
netclient/functions/joinutils.go
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user