add all local interfaces to node

This commit is contained in:
Matthew R. Kasun 2022-11-28 11:16:19 -05:00
parent 2a69df0979
commit 6b18ff57c9
5 changed files with 55 additions and 0 deletions

View file

@ -43,6 +43,12 @@ var seededRand *rand.Rand = rand.New(
type NodeCheckin struct {
Version string
Connected string
Ifaces []Iface
}
type Iface struct {
Name string
Address net.IPNet
}
// Node - struct for node model
@ -51,6 +57,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"`

View file

@ -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

View file

@ -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 {

View file

@ -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 == "" {

View file

@ -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())