mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
Extend the system informations
This commit is contained in:
42
client/system/ip.go
Normal file
42
client/system/ip.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func localAddresses() (string, string) {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:53")
|
||||
if err != nil {
|
||||
log.Errorf("failed to check ip: %s", err)
|
||||
return "", ""
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
log.Errorf("failed to list interfaces: %s", err)
|
||||
return "", ""
|
||||
}
|
||||
for _, i := range ifaces {
|
||||
addrs, err := i.Addrs()
|
||||
if err != nil {
|
||||
log.Errorf("failed to list addresses: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
cidr, _, err := net.ParseCIDR(addr.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if localAddr.IP.String() == cidr.String() {
|
||||
return addr.String(), i.HardwareAddr.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
Reference in New Issue
Block a user