2023-02-18 23:37:11 -06:00
|
|
|
// This creates a simple hello world window
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-20 03:13:43 -06:00
|
|
|
// "log"
|
2023-02-18 23:37:11 -06:00
|
|
|
"net"
|
|
|
|
"strings"
|
2024-01-03 12:40:31 -06:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
2023-02-18 23:37:11 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// this doesn't work
|
|
|
|
/*
|
|
|
|
func watchNetworkInterfaces() {
|
|
|
|
// Get list of network interfaces
|
|
|
|
interfaces, _ := net.Interfaces()
|
|
|
|
|
|
|
|
// Set up a notification channel
|
|
|
|
notification := make(chan net.Interface)
|
|
|
|
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "watchNet()")
|
2023-02-18 23:37:11 -06:00
|
|
|
// Start goroutine to watch for changes
|
|
|
|
go func() {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "watchNet() func")
|
2023-02-18 23:37:11 -06:00
|
|
|
for {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "forever loop start")
|
2023-02-18 23:37:11 -06:00
|
|
|
// Check for changes in each interface
|
|
|
|
for _, i := range interfaces {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "something on i =", i)
|
2023-02-18 23:37:11 -06:00
|
|
|
if status := i.Flags & net.FlagUp; status != 0 {
|
|
|
|
notification <- i
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "something on i =", i)
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "forever loop end")
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
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()) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\t\tIP is Real = false")
|
2023-02-18 23:37:11 -06:00
|
|
|
return false
|
|
|
|
} else {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\t\tIP is Real = true")
|
2023-02-18 23:37:11 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsReal(ip *net.IP) bool {
|
|
|
|
if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\t\tIP is Real = false")
|
2023-02-18 23:37:11 -06:00
|
|
|
return false
|
|
|
|
} else {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\t\tIP is Real = true")
|
2023-02-18 23:37:11 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Info(i.Name, "is a new network interface. The linux kernel index =", i.Index)
|
2023-02-18 23:37:11 -06:00
|
|
|
me.ifmap[i.Index] = new(IFtype)
|
|
|
|
me.ifmap[i.Index].gone = false
|
|
|
|
me.ifmap[i.Index].iface = &i
|
2023-03-26 16:17:32 -05:00
|
|
|
me.changed = true
|
2023-03-25 08:09:34 -05:00
|
|
|
if (me.Interfaces != nil) {
|
|
|
|
me.Interfaces.AddText(i.Name)
|
2023-03-25 08:40:48 -05:00
|
|
|
me.Interfaces.SetText(i.Name)
|
2023-03-25 08:09:34 -05:00
|
|
|
}
|
2023-02-18 23:37:11 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
me.ifmap[i.Index].gone = false
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "me.ifmap[i] does exist. Need to compare everything.", i.Index, i.Name, val.iface.Index, val.iface.Name)
|
2023-02-18 23:37:11 -06:00
|
|
|
if (val.iface.Name != i.Name) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Info(val.iface.Name, "has changed to it's name to", i.Name)
|
2023-02-18 23:37:11 -06:00
|
|
|
me.ifmap[i.Index].iface = &i
|
2023-03-26 16:17:32 -05:00
|
|
|
me.changed = true
|
2023-03-25 08:09:34 -05:00
|
|
|
if (me.Interfaces != nil) {
|
|
|
|
me.Interfaces.AddText(i.Name)
|
2023-03-25 08:40:48 -05:00
|
|
|
me.Interfaces.SetText(i.Name)
|
2023-03-25 08:09:34 -05:00
|
|
|
}
|
2023-02-18 23:37:11 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 09:43:45 -06:00
|
|
|
/*
|
|
|
|
These are the real IP address you have been
|
|
|
|
given from DHCP
|
|
|
|
*/
|
|
|
|
func dhcpAAAA() []string {
|
2023-02-18 23:37:11 -06:00
|
|
|
var aaaa []string
|
|
|
|
|
|
|
|
for s, t := range me.ipmap {
|
|
|
|
if (t.IsReal()) {
|
|
|
|
if (t.ipv6) {
|
|
|
|
aaaa = append(aaaa, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aaaa
|
|
|
|
}
|
|
|
|
|
2023-12-20 03:13:43 -06:00
|
|
|
func realA() []string {
|
|
|
|
var a []string
|
|
|
|
|
|
|
|
for s, t := range me.ipmap {
|
|
|
|
if (t.IsReal()) {
|
|
|
|
if (t.ipv4) {
|
|
|
|
a = append(a, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-02-18 23:37:11 -06:00
|
|
|
func checkDNS() (map[string]*IPtype, map[string]*IPtype) {
|
|
|
|
var ipv4s map[string]*IPtype
|
|
|
|
var ipv6s map[string]*IPtype
|
|
|
|
|
|
|
|
ipv4s = make(map[string]*IPtype)
|
|
|
|
ipv6s = make(map[string]*IPtype)
|
|
|
|
|
|
|
|
for s, t := range me.ipmap {
|
|
|
|
i := t.iface
|
|
|
|
ipt := "IPv4"
|
|
|
|
if (t.ipv6) {
|
|
|
|
ipt = "IPv6"
|
|
|
|
}
|
|
|
|
if (t.IsReal()) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Info("\tIP is Real ", ipt, i.Index, i.Name, s)
|
2023-02-18 23:37:11 -06:00
|
|
|
if (t.ipv6) {
|
|
|
|
ipv6s[s] = t
|
|
|
|
} else {
|
|
|
|
ipv4s[s] = t
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Info("\tIP is not Real", ipt, i.Index, i.Name, s)
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipv6s, ipv4s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Will figure out if an IP address is new
|
|
|
|
func checkIP(ip *net.IPNet, i net.Interface) bool {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\t\taddr.(type) = *net.IPNet")
|
|
|
|
log.Log(NET, "\t\taddr.(type) =", ip)
|
2023-02-18 23:37:11 -06:00
|
|
|
var realip string
|
|
|
|
realip = ip.IP.String()
|
|
|
|
|
|
|
|
val, ok := me.ipmap[realip]
|
|
|
|
if ok {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, val.ipnet.IP.String(), "is already a defined IP address")
|
2023-02-18 23:37:11 -06:00
|
|
|
me.ipmap[realip].gone = false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ipmap[realip].ipv6 = true
|
|
|
|
me.ipmap[realip].ipv4 = false
|
|
|
|
t = "IPv6"
|
2023-03-25 08:40:48 -05:00
|
|
|
if (me.IPv6 != nil) {
|
|
|
|
me.IPv6.SetText(realip)
|
|
|
|
}
|
2023-02-18 23:37:11 -06:00
|
|
|
} else {
|
|
|
|
me.ipmap[realip].ipv6 = false
|
|
|
|
me.ipmap[realip].ipv4 = true
|
2023-03-25 08:40:48 -05:00
|
|
|
if (me.IPv4 != nil) {
|
|
|
|
me.IPv4.SetText(realip)
|
|
|
|
}
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
if (IsReal(&ip.IP)) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Info("\tIP is Real ", t, i.Index, i.Name, realip)
|
2023-02-18 23:37:11 -06:00
|
|
|
} else {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Info("\tIP is not Real", t, i.Index, i.Name, realip)
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\t\tIP is IsPrivate() =", ip.IP.IsPrivate())
|
|
|
|
log.Log(NET, "\t\tIP is IsLoopback() =", ip.IP.IsLoopback())
|
|
|
|
log.Log(NET, "\t\tIP is IsLinkLocalUnicast() =", ip.IP.IsLinkLocalUnicast())
|
|
|
|
// log.Info("HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip])
|
2023-02-18 23:37:11 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanInterfaces() {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "scanInterfaces() START")
|
2023-02-18 23:37:11 -06:00
|
|
|
ifaces, _ := net.Interfaces()
|
|
|
|
// me.ifnew = ifaces
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, SPEW, ifaces)
|
2023-02-18 23:37:11 -06:00
|
|
|
for _, i := range ifaces {
|
|
|
|
addrs, _ := i.Addrs()
|
2024-01-03 12:40:31 -06:00
|
|
|
// log.Info("range ifaces = ", i)
|
2023-02-18 23:37:11 -06:00
|
|
|
checkInterface(i)
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "*net.Interface.Name = ", i.Name, i.Index)
|
|
|
|
log.Log(NET, SPEW, i)
|
|
|
|
log.Log(NET, SPEW, addrs)
|
2023-02-18 23:37:11 -06:00
|
|
|
for _, addr := range addrs {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\taddr =", addr)
|
|
|
|
log.Log(NET, SPEW, addrs)
|
2023-02-18 23:37:11 -06:00
|
|
|
ips, _ := net.LookupIP(addr.String())
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\tLookupIP(addr) =", ips)
|
2023-02-18 23:37:11 -06:00
|
|
|
switch v := addr.(type) {
|
|
|
|
case *net.IPNet:
|
2023-12-20 03:13:43 -06:00
|
|
|
if checkIP(v, i) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(true, "scanInterfaces() IP is new () i =", v.IP.String())
|
2023-12-20 03:13:43 -06:00
|
|
|
}
|
2023-02-18 23:37:11 -06:00
|
|
|
default:
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v)
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 03:13:43 -06:00
|
|
|
if deleteChanges() {
|
|
|
|
me.changed = true
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "deleteChanges() detected network changes")
|
2023-12-20 03:13:43 -06:00
|
|
|
}
|
|
|
|
updateRealAAAA()
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "scanInterfaces() END")
|
2023-12-20 03:13:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// displays the IP address found on your network interfaces
|
|
|
|
func updateRealAAAA() {
|
2023-03-26 16:17:32 -05:00
|
|
|
var all4 string
|
|
|
|
var all6 string
|
|
|
|
for s, t := range me.ipmap {
|
|
|
|
if (t.ipv4) {
|
|
|
|
all4 += s + "\n"
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "IPv4 =", s)
|
2023-03-26 21:29:38 -05:00
|
|
|
} else if (t.ipv6) {
|
2023-03-26 16:17:32 -05:00
|
|
|
all6 += s + "\n"
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "IPv6 =", s)
|
2023-03-26 21:29:38 -05:00
|
|
|
} else {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "???? =", s)
|
2023-03-26 16:17:32 -05:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 03:13:43 -06:00
|
|
|
all4 = sortLines(all4)
|
|
|
|
all6 = sortLines(all6)
|
|
|
|
if (me.IPv4.S != all4) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "IPv4 addresses have changed", all4)
|
2023-12-20 03:13:43 -06:00
|
|
|
me.IPv4.SetText(all4)
|
|
|
|
}
|
|
|
|
if (me.IPv6.S != all6) {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Log(NET, "IPv6 addresses have changed", all6)
|
2023-12-20 03:13:43 -06:00
|
|
|
me.IPv6.SetText(all6)
|
|
|
|
}
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete network interfaces and ip addresses from the gui
|
2023-12-20 03:13:43 -06:00
|
|
|
func deleteChanges() bool {
|
|
|
|
var changed bool = false
|
2023-02-18 23:37:11 -06:00
|
|
|
for i, t := range me.ifmap {
|
|
|
|
if (t.gone) {
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(CHANGE, "DELETE int =", i, "name =", t.name, t.iface)
|
2023-02-18 23:37:11 -06:00
|
|
|
delete(me.ifmap, i)
|
2023-12-20 03:13:43 -06:00
|
|
|
changed = true
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
t.gone = true
|
|
|
|
}
|
|
|
|
for s, t := range me.ipmap {
|
|
|
|
if (t.gone) {
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(CHANGE, "DELETE name =", s, "IPv4 =", t.ipv4)
|
|
|
|
log.Log(CHANGE, "DELETE name =", s, "IPv6 =", t.ipv6)
|
|
|
|
log.Log(CHANGE, "DELETE name =", s, "iface =", t.iface)
|
|
|
|
log.Log(CHANGE, "DELETE name =", s, "ip =", t.ip)
|
2023-02-18 23:37:11 -06:00
|
|
|
delete(me.ipmap, s)
|
2023-12-20 03:13:43 -06:00
|
|
|
changed = true
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
t.gone = true
|
|
|
|
}
|
2023-12-20 03:13:43 -06:00
|
|
|
|
|
|
|
return changed
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|