From bccb9e98ed579a4c6fb1cf151a9cf8464da4faef Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 17 Feb 2023 14:01:48 -0600 Subject: [PATCH] dampen output. actually track IPs Signed-off-by: Jeff Carr --- log.go | 2 +- main.go | 14 +++++----- net.go | 78 +++++++++++++++++++++++++++++++++++++++--------------- structs.go | 10 +++---- 4 files changed, 68 insertions(+), 36 deletions(-) diff --git a/log.go b/log.go index 9756cf6..b4d1e0b 100644 --- a/log.go +++ b/log.go @@ -32,7 +32,7 @@ func sleep(a ...any) { return } - log("sleep", a[0]) + log(args.Verbose, "sleep", a[0]) switch a[0].(type) { case int: diff --git a/main.go b/main.go index 1a32467..6048025 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,19 @@ -// This creates a simple hello world window +// This is a control panel for DNS package main import ( "runtime" - // "net" - // "github.com/fsnotify/fsnotify" + "net" "git.wit.org/wit/gui" arg "github.com/alexflint/go-arg" ) func main() { arg.MustParse(&args) - // fmt.Println(args.Foo, args.Bar, args.User) - log("Toolkit = ", args.Toolkit) + // initialize the maps to track IP addresses and network interfaces me.ip = make(map[string]*IPtype) - - // gui.InitPlugins([]string{"andlabs"}) + me.ifmap = make(map[int]*net.Interface) go checkNetworkChanges() @@ -29,7 +26,8 @@ func main() { sleep("done scanning net") // exit("done scanning net") - // watchNetworkInterfaces() + log("Toolkit = ", args.Toolkit) + // gui.InitPlugins([]string{"andlabs"}) gui.Main(initGUI) } diff --git a/net.go b/net.go index 61c6df3..2809ec0 100644 --- a/net.go +++ b/net.go @@ -52,14 +52,67 @@ func IsReal(ip *net.IP) bool { } } +func checkInterface(i *net.Interface) { + val, ok := me.ifmap[i.Index] + if ! ok { + log(i.Name, "is a new network interface. The linux kernel index =", i.Index) + me.ifmap[i.Index] = i + me.ipchange = true + return + } + log(args.VerboseNet, "me.ifmap[i] does exist. Need to compare everything.", me.ifmap[i.Index].Name) + if (me.ifmap[i.Index].Name != val.Name) { + log(val.Name, "has changed to it's name to", i.Name) + me.ifmap[i.Index] = i + me.ipchange = true + return + } +} + +func isNewIPNet(ip *net.IPNet, i *net.Interface) bool { + log(args.VerboseNet, "\t\taddr.(type) = *net.IPNet") + log(args.VerboseNet, "\t\taddr.(type) =", ip) + var realip string + realip = ip.IP.String() + + val, ok := me.ip[realip] + if ok { + log(args.VerboseNet, val.IPNet.IP, "is already a defined IP address") + return false + } + + me.ip[realip] = new(IPtype) + me.ip[realip].IPNet = ip + t := "IPv4" + if (IsIPv6(ip.String())) { + me.ip[realip].IPv6 = true + me.ip[realip].IPv4 = false + t = "IPv6" + } else { + me.ip[realip].IPv6 = false + me.ip[realip].IPv4 = true + } + if (IsReal(&ip.IP)) { + log("\tIP is Real ", t, i.Index, i.Name, realip) + } else { + log("\tIP is not Real", t, i.Index, i.Name, realip) + } + log(args.VerboseNet, "\t\tIP is IsPrivate() =", ip.IP.IsPrivate()) + log(args.VerboseNet, "\t\tIP is IsLoopback() =", ip.IP.IsLoopback()) + log(args.VerboseNet, "\t\tIP is IsLinkLocalUnicast() =", ip.IP.IsLinkLocalUnicast()) + return true +} + func scanInterfaces() { + me.ipchange = false ifaces, _ := net.Interfaces() - me.ifnew = ifaces + // me.ifnew = ifaces log(DEBUGNET, SPEW, ifaces) for _, i := range ifaces { addrs, _ := i.Addrs() // log("range ifaces = ", i) - log("*net.Interface.Name = ", i.Name) + checkInterface(&i) + log(args.VerboseNet, "*net.Interface.Name = ", i.Name, i.Index) log(args.VerboseNet, SPEW, i) log(DEBUGNET, SPEW, addrs) for _, addr := range addrs { @@ -69,26 +122,7 @@ func scanInterfaces() { log(DEBUGNET, "\tLookupIP(addr) =", ips) switch v := addr.(type) { case *net.IPNet: - log(DEBUGNET, "\t\taddr.(type) = *net.IPNet") - log(DEBUGNET, "\t\taddr.(type) =", v) - ip := v.IP - log(DEBUGNET, "\t\taddr.IP =", ip) - var t string - t = "IPv4" - if (IsIPv6(ip.String())) { - log(DEBUGNET, "\t\tIP is IPv6") - t = "IPv6" - } else { - log(DEBUGNET, "\t\tIP is IPv4") - } - if (IsReal(&ip)) { - log("\tIP is Real ", t, i.Name, ip) - } else { - log("\tIP is not Real", t, i.Name, ip) - } - log(DEBUGNET, "\t\tIP is IsPrivate() =", ip.IsPrivate()) - log(DEBUGNET, "\t\tIP is IsLoopback() =", ip.IsLoopback()) - log(DEBUGNET, "\t\tIP is IsLinkLocalUnicast() =", ip.IsLinkLocalUnicast()) + isNewIPNet(v, &i) // log("\t\tIP is () =", ip.()) default: log(DEBUGNET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v) diff --git a/structs.go b/structs.go index 752a8d0..2298a6c 100644 --- a/structs.go +++ b/structs.go @@ -9,13 +9,12 @@ import ( var me Host type Host struct { - domainname string // kernel.org hostname string // mirrors + domainname string // kernel.org fqdn string // mirrors.kernel.org ip map[string]*IPtype - ifmap map[int]*net.Interface - ifcur []net.Interface // the current network settings - ifnew []net.Interface // used to look for changes + ifmap map[int]*net.Interface // the current network settings + // ifnew []net.Interface // used to look for changes ipchange bool // set to true if things change } @@ -24,5 +23,6 @@ type IPtype struct { IPv4 bool IPv6 bool LinkLocal bool - nic *net.Interface + Interface *net.Interface + IPNet *net.IPNet }