mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 11:56:39 +08:00
Merge pull request #1769 from gravitl/feature_v0.17.1_local_interfaces
add all local interfaces to node
This commit is contained in:
commit
0d9b47f212
5 changed files with 56 additions and 0 deletions
|
@ -43,6 +43,13 @@ var seededRand *rand.Rand = rand.New(
|
||||||
type NodeCheckin struct {
|
type NodeCheckin struct {
|
||||||
Version string
|
Version string
|
||||||
Connected string
|
Connected string
|
||||||
|
Ifaces []Iface
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iface struct for local interfaces of a node
|
||||||
|
type Iface struct {
|
||||||
|
Name string
|
||||||
|
Address net.IPNet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Node - struct for node model
|
// Node - struct for node model
|
||||||
|
@ -51,6 +58,7 @@ type Node struct {
|
||||||
Address string `json:"address" bson:"address" yaml:"address" validate:"omitempty,ipv4"`
|
Address string `json:"address" bson:"address" yaml:"address" validate:"omitempty,ipv4"`
|
||||||
Address6 string `json:"address6" bson:"address6" yaml:"address6" validate:"omitempty,ipv6"`
|
Address6 string `json:"address6" bson:"address6" yaml:"address6" validate:"omitempty,ipv6"`
|
||||||
LocalAddress string `json:"localaddress" bson:"localaddress" yaml:"localaddress" validate:"omitempty"`
|
LocalAddress string `json:"localaddress" bson:"localaddress" yaml:"localaddress" validate:"omitempty"`
|
||||||
|
Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
|
||||||
Name string `json:"name" bson:"name" yaml:"name" validate:"omitempty,max=62,in_charset"`
|
Name string `json:"name" bson:"name" yaml:"name" validate:"omitempty,max=62,in_charset"`
|
||||||
NetworkSettings Network `json:"networksettings" bson:"networksettings" yaml:"networksettings" validate:"-"`
|
NetworkSettings Network `json:"networksettings" bson:"networksettings" yaml:"networksettings" validate:"-"`
|
||||||
ListenPort int32 `json:"listenport" bson:"listenport" yaml:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
|
ListenPort int32 `json:"listenport" bson:"listenport" yaml:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
|
||||||
|
|
|
@ -52,6 +52,7 @@ func Ping(client mqtt.Client, msg mqtt.Message) {
|
||||||
node.SetLastCheckIn()
|
node.SetLastCheckIn()
|
||||||
node.Version = checkin.Version
|
node.Version = checkin.Version
|
||||||
node.Connected = checkin.Connected
|
node.Connected = checkin.Connected
|
||||||
|
node.Interfaces = checkin.Ifaces
|
||||||
if err := logic.UpdateNode(&node, &node); err != nil {
|
if err := logic.UpdateNode(&node, &node); err != nil {
|
||||||
logger.Log(0, "error updating node", node.Name, node.ID, " on checkin", err.Error())
|
logger.Log(0, "error updating node", node.Name, node.ID, " on checkin", err.Error())
|
||||||
return
|
return
|
||||||
|
|
|
@ -127,6 +127,37 @@ func getPrivateAddrBackup() (string, error) {
|
||||||
return local, err
|
return local, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getInterfaces() (*[]models.Iface, error) {
|
||||||
|
ifaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var data []models.Iface
|
||||||
|
var link models.Iface
|
||||||
|
for _, iface := range ifaces {
|
||||||
|
if iface.Flags&net.FlagUp == 0 {
|
||||||
|
continue // interface down
|
||||||
|
}
|
||||||
|
if iface.Flags&net.FlagLoopback != 0 {
|
||||||
|
continue // loopback interface
|
||||||
|
}
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
link.Name = iface.Name
|
||||||
|
_, cidr, err := net.ParseCIDR(addr.String())
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
link.Address = *cidr
|
||||||
|
data = append(data, link)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &data, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetNode - gets node locally
|
// GetNode - gets node locally
|
||||||
func GetNode(network string) models.Node {
|
func GetNode(network string) models.Node {
|
||||||
|
|
||||||
|
|
|
@ -238,6 +238,14 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error {
|
||||||
logger.Log(1, "network:", cfg.Network, "error retrieving private address: ", err.Error())
|
logger.Log(1, "network:", cfg.Network, "error retrieving private address: ", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(cfg.Node.Interfaces) == 0 {
|
||||||
|
ip, err := getInterfaces()
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(0, "failed to retrive local interfaces", err.Error())
|
||||||
|
} else {
|
||||||
|
cfg.Node.Interfaces = *ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set endpoint if blank. set to local if local net, retrieve from function if not
|
// set endpoint if blank. set to local if local net, retrieve from function if not
|
||||||
if cfg.Node.Endpoint == "" {
|
if cfg.Node.Endpoint == "" {
|
||||||
|
|
|
@ -140,6 +140,14 @@ func Hello(nodeCfg *config.ClientConfig) {
|
||||||
var checkin models.NodeCheckin
|
var checkin models.NodeCheckin
|
||||||
checkin.Version = ncutils.Version
|
checkin.Version = ncutils.Version
|
||||||
checkin.Connected = nodeCfg.Node.Connected
|
checkin.Connected = nodeCfg.Node.Connected
|
||||||
|
ip, err := getInterfaces()
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(0, "failed to retrieve local interfaces", err.Error())
|
||||||
|
} else {
|
||||||
|
nodeCfg.Node.Interfaces = *ip
|
||||||
|
config.Write(nodeCfg, nodeCfg.Network)
|
||||||
|
}
|
||||||
|
checkin.Ifaces = nodeCfg.Node.Interfaces
|
||||||
data, err := json.Marshal(checkin)
|
data, err := json.Marshal(checkin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log(0, "unable to marshal checkin data", err.Error())
|
logger.Log(0, "unable to marshal checkin data", err.Error())
|
||||||
|
|
Loading…
Add table
Reference in a new issue