102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
// This creates a simple hello world window
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"strings"
|
|
"github.com/fsnotify/fsnotify"
|
|
// "git.wit.org/wit/gui"
|
|
"github.com/davecgh/go-spew/spew"
|
|
)
|
|
|
|
func watchNetworkInterfaces() {
|
|
// Get list of network interfaces
|
|
interfaces, _ := net.Interfaces()
|
|
|
|
// Set up a notification channel
|
|
notification := make(chan net.Interface)
|
|
|
|
// Start goroutine to watch for changes
|
|
go func() {
|
|
for {
|
|
// Check for changes in each interface
|
|
for _, i := range interfaces {
|
|
if status := i.Flags & net.FlagUp; status != 0 {
|
|
notification <- i
|
|
log.Println("something on i =", i)
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func inotifyNetworkInterfaceChanges() error {
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer watcher.Close()
|
|
|
|
// Watch for network interface changes
|
|
err = watcher.Add("/sys/class/net")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for {
|
|
select {
|
|
case event := <-watcher.Events:
|
|
log.Println("inotifyNetworkInterfaceChanges() event =", event)
|
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
|
// Do something on network interface creation
|
|
}
|
|
case err := <-watcher.Errors:
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
func IsIPv6(address string) bool {
|
|
return strings.Count(address, ":") >= 2
|
|
}
|
|
|
|
func scanInterfaces() {
|
|
ifaces, _ := net.Interfaces()
|
|
spew.Dump(ifaces)
|
|
for _, i := range ifaces {
|
|
addrs, _ := i.Addrs()
|
|
log.Println("addrs = ", i)
|
|
spew.Dump(addrs)
|
|
for _, addr := range addrs {
|
|
log.Println("\taddr =", addr)
|
|
spew.Dump(addr)
|
|
ips, _ := net.LookupIP(addr.String())
|
|
log.Println("\tLookupIP(addr) =", ips)
|
|
switch v := addr.(type) {
|
|
case *net.IPNet:
|
|
log.Println("\t\taddr.(type) = *net.IPNet")
|
|
log.Println("\t\taddr.(type) =", v)
|
|
ip := v.IP
|
|
// spew.Dump(ip)
|
|
log.Println("\t\taddr.IP =", ip)
|
|
if (IsIPv6(ip.String())) {
|
|
log.Println("\t\tIP is IPv6")
|
|
} else {
|
|
log.Println("\t\tIP is IPv4")
|
|
}
|
|
log.Println("\t\tIP is IsPrivate() =", ip.IsPrivate())
|
|
log.Println("\t\tIP is IsLoopback() =", ip.IsLoopback())
|
|
log.Println("\t\tIP is IsLinkLocalUnicast() =", ip.IsLinkLocalUnicast())
|
|
if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) {
|
|
log.Println("\t\tIP is Real = false")
|
|
} else {
|
|
log.Println("\t\tIP is Real = true")
|
|
}
|
|
// log.Println("\t\tIP is () =", ip.())
|
|
default:
|
|
log.Println("\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v)
|
|
}
|
|
}
|
|
}
|
|
}
|