dampen output. actually track IPs

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-02-17 14:01:48 -06:00
parent 00c2f38011
commit bccb9e98ed
4 changed files with 68 additions and 36 deletions

2
log.go
View File

@ -32,7 +32,7 @@ func sleep(a ...any) {
return return
} }
log("sleep", a[0]) log(args.Verbose, "sleep", a[0])
switch a[0].(type) { switch a[0].(type) {
case int: case int:

14
main.go
View File

@ -1,22 +1,19 @@
// This creates a simple hello world window // This is a control panel for DNS
package main package main
import ( import (
"runtime" "runtime"
// "net" "net"
// "github.com/fsnotify/fsnotify"
"git.wit.org/wit/gui" "git.wit.org/wit/gui"
arg "github.com/alexflint/go-arg" arg "github.com/alexflint/go-arg"
) )
func main() { func main() {
arg.MustParse(&args) 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) me.ip = make(map[string]*IPtype)
me.ifmap = make(map[int]*net.Interface)
// gui.InitPlugins([]string{"andlabs"})
go checkNetworkChanges() go checkNetworkChanges()
@ -29,7 +26,8 @@ func main() {
sleep("done scanning net") sleep("done scanning net")
// exit("done scanning net") // exit("done scanning net")
// watchNetworkInterfaces() log("Toolkit = ", args.Toolkit)
// gui.InitPlugins([]string{"andlabs"})
gui.Main(initGUI) gui.Main(initGUI)
} }

78
net.go
View File

@ -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() { func scanInterfaces() {
me.ipchange = false
ifaces, _ := net.Interfaces() ifaces, _ := net.Interfaces()
me.ifnew = ifaces // me.ifnew = ifaces
log(DEBUGNET, SPEW, ifaces) log(DEBUGNET, SPEW, ifaces)
for _, i := range ifaces { for _, i := range ifaces {
addrs, _ := i.Addrs() addrs, _ := i.Addrs()
// log("range ifaces = ", i) // 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(args.VerboseNet, SPEW, i)
log(DEBUGNET, SPEW, addrs) log(DEBUGNET, SPEW, addrs)
for _, addr := range addrs { for _, addr := range addrs {
@ -69,26 +122,7 @@ func scanInterfaces() {
log(DEBUGNET, "\tLookupIP(addr) =", ips) log(DEBUGNET, "\tLookupIP(addr) =", ips)
switch v := addr.(type) { switch v := addr.(type) {
case *net.IPNet: case *net.IPNet:
log(DEBUGNET, "\t\taddr.(type) = *net.IPNet") isNewIPNet(v, &i)
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())
// log("\t\tIP is () =", ip.()) // log("\t\tIP is () =", ip.())
default: default:
log(DEBUGNET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v) log(DEBUGNET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v)

View File

@ -9,13 +9,12 @@ import (
var me Host var me Host
type Host struct { type Host struct {
domainname string // kernel.org
hostname string // mirrors hostname string // mirrors
domainname string // kernel.org
fqdn string // mirrors.kernel.org fqdn string // mirrors.kernel.org
ip map[string]*IPtype ip map[string]*IPtype
ifmap map[int]*net.Interface ifmap map[int]*net.Interface // the current network settings
ifcur []net.Interface // the current network settings // ifnew []net.Interface // used to look for changes
ifnew []net.Interface // used to look for changes
ipchange bool // set to true if things change ipchange bool // set to true if things change
} }
@ -24,5 +23,6 @@ type IPtype struct {
IPv4 bool IPv4 bool
IPv6 bool IPv6 bool
LinkLocal bool LinkLocal bool
nic *net.Interface Interface *net.Interface
IPNet *net.IPNet
} }