Browse Source

Merge pull request #1769 from gravitl/feature_v0.17.1_local_interfaces

add all local interfaces to node
dcarns 2 years ago
parent
commit
0d9b47f212
5 changed files with 56 additions and 0 deletions
  1. 8 0
      models/node.go
  2. 1 0
      mq/handlers.go
  3. 31 0
      netclient/functions/common.go
  4. 8 0
      netclient/functions/join.go
  5. 8 0
      netclient/functions/mqpublish.go

+ 8 - 0
models/node.go

@@ -43,6 +43,13 @@ var seededRand *rand.Rand = rand.New(
 type NodeCheckin struct {
 	Version   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
@@ -51,6 +58,7 @@ type Node struct {
 	Address                 string               `json:"address" bson:"address" yaml:"address" validate:"omitempty,ipv4"`
 	Address6                string               `json:"address6" bson:"address6" yaml:"address6" validate:"omitempty,ipv6"`
 	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"`
 	NetworkSettings         Network              `json:"networksettings" bson:"networksettings" yaml:"networksettings" validate:"-"`
 	ListenPort              int32                `json:"listenport" bson:"listenport" yaml:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`

+ 1 - 0
mq/handlers.go

@@ -52,6 +52,7 @@ func Ping(client mqtt.Client, msg mqtt.Message) {
 		node.SetLastCheckIn()
 		node.Version = checkin.Version
 		node.Connected = checkin.Connected
+		node.Interfaces = checkin.Ifaces
 		if err := logic.UpdateNode(&node, &node); err != nil {
 			logger.Log(0, "error updating node", node.Name, node.ID, " on checkin", err.Error())
 			return

+ 31 - 0
netclient/functions/common.go

@@ -127,6 +127,37 @@ func getPrivateAddrBackup() (string, error) {
 	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
 func GetNode(network string) models.Node {
 

+ 8 - 0
netclient/functions/join.go

@@ -238,6 +238,14 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) 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
 	if cfg.Node.Endpoint == "" {

+ 8 - 0
netclient/functions/mqpublish.go

@@ -140,6 +140,14 @@ func Hello(nodeCfg *config.ClientConfig) {
 	var checkin models.NodeCheckin
 	checkin.Version = ncutils.Version
 	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)
 	if err != nil {
 		logger.Log(0, "unable to marshal checkin data", err.Error())