From 1dfd89507428551828792673717771d3d38e3648 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 17 Feb 2023 23:41:36 -0600 Subject: [PATCH] starting a checkDNS() function Signed-off-by: Jeff Carr --- Makefile | 9 +++-- gui.go | 16 +++++++++ main.go | 9 +++-- net.go | 100 ++++++++++++++++++++++++++++++++++++++++++--------- rtnetlink.go | 24 +++++++++++++ structs.go | 23 +++++++----- 6 files changed, 150 insertions(+), 31 deletions(-) create mode 100644 rtnetlink.go diff --git a/Makefile b/Makefile index 80cd008..cc0c867 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,21 @@ run: build - reset ./control-panel-dns verbose: build - reset ./control-panel-dns --verbose --verbose-net --gui-debug --toolkit-debug build-release: + reset go get -v -u -x . go build build: + reset GO111MODULE="off" go get -v -x . - GO111MODULE="off" go build + GO111MODULE="off" go build -v + +test: + GO111MODULE="off" go test -v update: GO111MODULE="off" go get -v -u -x . diff --git a/gui.go b/gui.go index 4829077..5313100 100644 --- a/gui.go +++ b/gui.go @@ -66,6 +66,22 @@ func addDNSTab(window *gui.Node, title string) { g2.NewButton("scanInterfaces()", func () { scanInterfaces() }) + g2.NewButton("dump Host.ifmap", func () { + for i, t := range me.ifmap { + log("int =", i, "name =", t.name, t.iface) + } + }) + g2.NewButton("dump Host.ipmap", func () { + for s, t := range me.ipmap { + log("name =", s, "ipv4 =", t.ipv4) + log("name =", s, "ipv6 =", t.ipv6) + log("name =", s, "iface =", t.iface) + log("name =", s, "ip =", t.ip) + } + }) + g2.NewButton("checkDNS()", func () { + checkDNS() + }) g2.NewButton("os.Hostname()", func () { name, err = os.Hostname() log("name =", name, err) diff --git a/main.go b/main.go index 6048025..7e172ad 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "runtime" - "net" + // "net" "git.wit.org/wit/gui" arg "github.com/alexflint/go-arg" ) @@ -12,8 +12,8 @@ func main() { arg.MustParse(&args) // initialize the maps to track IP addresses and network interfaces - me.ip = make(map[string]*IPtype) - me.ifmap = make(map[int]*net.Interface) + me.ipmap = make(map[string]*IPtype) + me.ifmap = make(map[int]*IFtype) go checkNetworkChanges() @@ -26,6 +26,9 @@ func main() { sleep("done scanning net") // exit("done scanning net") + // Example_listLink() + // exit() + log("Toolkit = ", args.Toolkit) // gui.InitPlugins([]string{"andlabs"}) gui.Main(initGUI) diff --git a/net.go b/net.go index 2809ec0..70ac155 100644 --- a/net.go +++ b/net.go @@ -42,6 +42,16 @@ func IsIPv6(address string) bool { return strings.Count(address, ":") >= 2 } +func (t *IPtype) IsReal() bool { + if (t.ip.IsPrivate() || t.ip.IsLoopback() || t.ip.IsLinkLocalUnicast()) { + log(DEBUGNET, "\t\tIP is Real = false") + return false + } else { + log(DEBUGNET, "\t\tIP is Real = true") + return true + } +} + func IsReal(ip *net.IP) bool { if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) { log(DEBUGNET, "\t\tIP is Real = false") @@ -52,45 +62,78 @@ func IsReal(ip *net.IP) bool { } } -func checkInterface(i *net.Interface) { +func renameInterface(i *net.Interface) { + /* + /sbin/ip link set eth1 down + /sbin/ip link set eth1 name eth123 + /sbin/ip link set eth123 up + */ +} + +// Will figure out if an interface was just added +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.ifmap[i.Index] = new(IFtype) + me.ifmap[i.Index].gone = false + me.ifmap[i.Index].iface = &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.ifmap[i.Index].gone = false + log(args.VerboseNet, "me.ifmap[i] does exist. Need to compare everything.", i.Index, i.Name, val.iface.Index, val.iface.Name) + if (val.iface.Name != i.Name) { + log(val.iface.Name, "has changed to it's name to", i.Name) + me.ifmap[i.Index].iface = &i me.ipchange = true return } } -func isNewIPNet(ip *net.IPNet, i *net.Interface) bool { +func checkDNS() { + for s, t := range me.ipmap { + i := t.iface + ipt := "IPv4" + if (t.ipv6) { + ipt = "IPv6" + } + if (t.IsReal()) { + log("\tIP is Real ", ipt, i.Index, i.Name, s) + } else { + log("\tIP is not Real", ipt, i.Index, i.Name, s) + } + } +} + +// Will figure out if an IP address is new +func checkIP(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] + val, ok := me.ipmap[realip] if ok { - log(args.VerboseNet, val.IPNet.IP, "is already a defined IP address") + log(args.VerboseNet, val.ipnet.IP.String(), "is already a defined IP address") + me.ipmap[realip].gone = false return false } - me.ip[realip] = new(IPtype) - me.ip[realip].IPNet = ip + me.ipmap[realip] = new(IPtype) + me.ipmap[realip].gone = false + me.ipmap[realip].ipv4 = true + me.ipmap[realip].ipnet = ip + me.ipmap[realip].ip = ip.IP + me.ipmap[realip].iface = &i t := "IPv4" if (IsIPv6(ip.String())) { - me.ip[realip].IPv6 = true - me.ip[realip].IPv4 = false + me.ipmap[realip].ipv6 = true + me.ipmap[realip].ipv4 = false t = "IPv6" } else { - me.ip[realip].IPv6 = false - me.ip[realip].IPv4 = true + me.ipmap[realip].ipv6 = false + me.ipmap[realip].ipv4 = true } if (IsReal(&ip.IP)) { log("\tIP is Real ", t, i.Index, i.Name, realip) @@ -100,6 +143,7 @@ func isNewIPNet(ip *net.IPNet, i *net.Interface) bool { 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()) + // log("HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip]) return true } @@ -111,7 +155,7 @@ func scanInterfaces() { for _, i := range ifaces { addrs, _ := i.Addrs() // log("range ifaces = ", i) - checkInterface(&i) + checkInterface(i) log(args.VerboseNet, "*net.Interface.Name = ", i.Name, i.Index) log(args.VerboseNet, SPEW, i) log(DEBUGNET, SPEW, addrs) @@ -122,11 +166,33 @@ func scanInterfaces() { log(DEBUGNET, "\tLookupIP(addr) =", ips) switch v := addr.(type) { case *net.IPNet: - isNewIPNet(v, &i) + checkIP(v, i) // log("\t\tIP is () =", ip.()) default: log(DEBUGNET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v) } } } + deleteChanges() +} + +// delete network interfaces and ip addresses from the gui +func deleteChanges() { + for i, t := range me.ifmap { + if (t.gone) { + log("DELETE int =", i, "name =", t.name, t.iface) + delete(me.ifmap, i) + } + t.gone = true + } + for s, t := range me.ipmap { + if (t.gone) { + log("DELETE name =", s, "IPv4 =", t.ipv4) + log("DELETE name =", s, "IPv6 =", t.ipv6) + log("DELETE name =", s, "iface =", t.iface) + log("DELETE name =", s, "ip =", t.ip) + delete(me.ipmap, s) + } + t.gone = true + } } diff --git a/rtnetlink.go b/rtnetlink.go new file mode 100644 index 0000000..83d2c34 --- /dev/null +++ b/rtnetlink.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/jsimonetti/rtnetlink" +) + +// List all interfaces +func Example_listLink() { + // Dial a connection to the rtnetlink socket + conn, err := rtnetlink.Dial(nil) + if err != nil { + exit(err) + } + defer conn.Close() + + // Request a list of interfaces + msg, err := conn.Link.List() + if err != nil { + log(err) + } + + log("%#v", msg) + log(SPEW, msg) +} diff --git a/structs.go b/structs.go index 2298a6c..4300674 100644 --- a/structs.go +++ b/structs.go @@ -12,17 +12,24 @@ type Host struct { hostname string // mirrors domainname string // kernel.org fqdn string // mirrors.kernel.org - ip map[string]*IPtype - ifmap map[int]*net.Interface // the current network settings - // ifnew []net.Interface // used to look for changes + ipmap map[string]*IPtype // the current ip addresses + ifmap map[int]*IFtype // the current interfaces ipchange bool // set to true if things change } type IPtype struct { - // IP string - IPv4 bool - IPv6 bool + gone bool // used to track if the ip exists + ipv6 bool // the future + ipv4 bool // the past LinkLocal bool - Interface *net.Interface - IPNet *net.IPNet + iface *net.Interface + ip net.IP + ipnet *net.IPNet +} + +type IFtype struct { + gone bool // used to track if the interface exists + name string // just a shortcut to the name. maybe this is dumb + // up bool // could be used to track ifup/ifdown + iface *net.Interface }