diff options
Diffstat (limited to 'net.go')
| -rw-r--r-- | net.go | 140 |
1 files changed, 85 insertions, 55 deletions
@@ -2,13 +2,11 @@ package main import ( - "log" + // "log" "net" "strings" ) -var DEBUGNET bool = false - // this doesn't work /* func watchNetworkInterfaces() { @@ -18,21 +16,21 @@ func watchNetworkInterfaces() { // Set up a notification channel notification := make(chan net.Interface) - log.Println(DEBUGNET, "watchNet()") + debug(LogNet, "watchNet()") // Start goroutine to watch for changes go func() { - log.Println(DEBUGNET, "watchNet() func") + debug(LogNet, "watchNet() func") for { - log.Println(DEBUGNET, "forever loop start") + debug(LogNet, "forever loop start") // Check for changes in each interface for _, i := range interfaces { - log.Println(DEBUGNET, "something on i =", i) + debug(LogNet, "something on i =", i) if status := i.Flags & net.FlagUp; status != 0 { notification <- i - log.Println(DEBUGNET, "something on i =", i) + debug(LogNet, "something on i =", i) } } - log.Println(DEBUGNET, "forever loop end") + debug(LogNet, "forever loop end") } }() } @@ -44,20 +42,20 @@ func IsIPv6(address string) bool { func (t *IPtype) IsReal() bool { if (t.ip.IsPrivate() || t.ip.IsLoopback() || t.ip.IsLinkLocalUnicast()) { - log.Println(DEBUGNET, "\t\tIP is Real = false") + debug(LogNet, "\t\tIP is Real = false") return false } else { - log.Println(DEBUGNET, "\t\tIP is Real = true") + debug(LogNet, "\t\tIP is Real = true") return true } } func IsReal(ip *net.IP) bool { if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) { - log.Println(DEBUGNET, "\t\tIP is Real = false") + debug(LogNet, "\t\tIP is Real = false") return false } else { - log.Println(DEBUGNET, "\t\tIP is Real = true") + debug(LogNet, "\t\tIP is Real = true") return true } } @@ -74,7 +72,7 @@ func renameInterface(i *net.Interface) { func checkInterface(i net.Interface) { val, ok := me.ifmap[i.Index] if ! ok { - log.Println(i.Name, "is a new network interface. The linux kernel index =", i.Index) + debug(i.Name, "is a new network interface. The linux kernel index =", i.Index) me.ifmap[i.Index] = new(IFtype) me.ifmap[i.Index].gone = false me.ifmap[i.Index].iface = &i @@ -86,9 +84,9 @@ func checkInterface(i net.Interface) { return } me.ifmap[i.Index].gone = false - log.Println(args.VerboseNet, "me.ifmap[i] does exist. Need to compare everything.", i.Index, i.Name, val.iface.Index, val.iface.Name) + debug(LogNet, "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.Println(val.iface.Name, "has changed to it's name to", i.Name) + debug(val.iface.Name, "has changed to it's name to", i.Name) me.ifmap[i.Index].iface = &i me.changed = true if (me.Interfaces != nil) { @@ -112,6 +110,19 @@ func realAAAA() []string { return aaaa } +func realA() []string { + var a []string + + for s, t := range me.ipmap { + if (t.IsReal()) { + if (t.ipv4) { + a = append(a, s) + } + } + } + return a +} + func checkDNS() (map[string]*IPtype, map[string]*IPtype) { var ipv4s map[string]*IPtype var ipv6s map[string]*IPtype @@ -126,14 +137,14 @@ func checkDNS() (map[string]*IPtype, map[string]*IPtype) { ipt = "IPv6" } if (t.IsReal()) { - log.Println("\tIP is Real ", ipt, i.Index, i.Name, s) + debug("\tIP is Real ", ipt, i.Index, i.Name, s) if (t.ipv6) { ipv6s[s] = t } else { ipv4s[s] = t } } else { - log.Println("\tIP is not Real", ipt, i.Index, i.Name, s) + debug("\tIP is not Real", ipt, i.Index, i.Name, s) } } return ipv6s, ipv4s @@ -141,14 +152,14 @@ func checkDNS() (map[string]*IPtype, map[string]*IPtype) { // Will figure out if an IP address is new func checkIP(ip *net.IPNet, i net.Interface) bool { - log.Println(args.VerboseNet, "\t\taddr.(type) = *net.IPNet") - log.Println(args.VerboseNet, "\t\taddr.(type) =", ip) + debug(LogNet, "\t\taddr.(type) = *net.IPNet") + debug(LogNet, "\t\taddr.(type) =", ip) var realip string realip = ip.IP.String() val, ok := me.ipmap[realip] if ok { - log.Println(args.VerboseNet, val.ipnet.IP.String(), "is already a defined IP address") + debug(LogNet, val.ipnet.IP.String(), "is already a defined IP address") me.ipmap[realip].gone = false return false } @@ -175,82 +186,101 @@ func checkIP(ip *net.IPNet, i net.Interface) bool { } } if (IsReal(&ip.IP)) { - log.Println("\tIP is Real ", t, i.Index, i.Name, realip) + debug("\tIP is Real ", t, i.Index, i.Name, realip) } else { - log.Println("\tIP is not Real", t, i.Index, i.Name, realip) + debug("\tIP is not Real", t, i.Index, i.Name, realip) } - log.Println(args.VerboseNet, "\t\tIP is IsPrivate() =", ip.IP.IsPrivate()) - log.Println(args.VerboseNet, "\t\tIP is IsLoopback() =", ip.IP.IsLoopback()) - log.Println(args.VerboseNet, "\t\tIP is IsLinkLocalUnicast() =", ip.IP.IsLinkLocalUnicast()) - // log.Println("HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip]) + debug(LogNet, "\t\tIP is IsPrivate() =", ip.IP.IsPrivate()) + debug(LogNet, "\t\tIP is IsLoopback() =", ip.IP.IsLoopback()) + debug(LogNet, "\t\tIP is IsLinkLocalUnicast() =", ip.IP.IsLinkLocalUnicast()) + // debug("HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip]) return true } func scanInterfaces() { - me.changed = false + debug(LogNet, "scanInterfaces() START") ifaces, _ := net.Interfaces() // me.ifnew = ifaces - log.Println(DEBUGNET, SPEW, ifaces) + debug(LogNet, SPEW, ifaces) for _, i := range ifaces { addrs, _ := i.Addrs() - // log.Println("range ifaces = ", i) + // debug("range ifaces = ", i) checkInterface(i) - log.Println(args.VerboseNet, "*net.Interface.Name = ", i.Name, i.Index) - log.Println(args.VerboseNet, SPEW, i) - log.Println(DEBUGNET, SPEW, addrs) + debug(LogNet, "*net.Interface.Name = ", i.Name, i.Index) + debug(LogNet, SPEW, i) + debug(LogNet, SPEW, addrs) for _, addr := range addrs { - log.Println(DEBUGNET, "\taddr =", addr) - log.Println(DEBUGNET, SPEW, addrs) + debug(LogNet, "\taddr =", addr) + debug(LogNet, SPEW, addrs) ips, _ := net.LookupIP(addr.String()) - log.Println(DEBUGNET, "\tLookupIP(addr) =", ips) + debug(LogNet, "\tLookupIP(addr) =", ips) switch v := addr.(type) { case *net.IPNet: - checkIP(v, i) - // log.Println("\t\tIP is () =", ip.()) + if checkIP(v, i) { + debug(true, "scanInterfaces() IP is new () i =", v.IP.String()) + } default: - log.Println(DEBUGNET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v) + debug(LogNet, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v) } } } - deleteChanges() + if deleteChanges() { + me.changed = true + debug(LogNow, "deleteChanges() detected network changes") + } + updateRealAAAA() + debug(LogNet, "scanInterfaces() END") +} + +// displays the IP address found on your network interfaces +func updateRealAAAA() { var all4 string var all6 string for s, t := range me.ipmap { if (t.ipv4) { all4 += s + "\n" - log.Println("IPv4 =", s) + debug(LogNet, "IPv4 =", s) } else if (t.ipv6) { all6 += s + "\n" - log.Println("IPv6 =", s) + debug(LogNet, "IPv6 =", s) } else { - log.Println("???? =", s) + debug(LogNet, "???? =", s) } } - all4 = strings.TrimSpace(all4) - all6 = strings.TrimSpace(all6) - me.IPv4.SetText(all4) - me.IPv6.SetText(all6) + all4 = sortLines(all4) + all6 = sortLines(all6) + if (me.IPv4.S != all4) { + debug(LogNow, "IPv4 addresses have changed", all4) + me.IPv4.SetText(all4) + } + if (me.IPv6.S != all6) { + debug(LogNow, "IPv6 addresses have changed", all6) + me.IPv6.SetText(all6) + } } // delete network interfaces and ip addresses from the gui -func deleteChanges() { +func deleteChanges() bool { + var changed bool = false for i, t := range me.ifmap { if (t.gone) { - log.Println("DELETE int =", i, "name =", t.name, t.iface) + debug(LogChange, "DELETE int =", i, "name =", t.name, t.iface) delete(me.ifmap, i) - me.changed = true + changed = true } t.gone = true } for s, t := range me.ipmap { if (t.gone) { - log.Println("DELETE name =", s, "IPv4 =", t.ipv4) - log.Println("DELETE name =", s, "IPv6 =", t.ipv6) - log.Println("DELETE name =", s, "iface =", t.iface) - log.Println("DELETE name =", s, "ip =", t.ip) + debug(LogChange, "DELETE name =", s, "IPv4 =", t.ipv4) + debug(LogChange, "DELETE name =", s, "IPv6 =", t.ipv6) + debug(LogChange, "DELETE name =", s, "iface =", t.iface) + debug(LogChange, "DELETE name =", s, "ip =", t.ip) delete(me.ipmap, s) - me.changed = true + changed = true } t.gone = true } + + return changed } |
