From 6c5413a71f9c3bbe92108b828b65fc1e18a2ab25 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 17 Feb 2023 00:43:24 -0600 Subject: [PATCH] fuck this shit about logging. I hate log.whatthefuck*(){} Do you know what I fucking don't care about? log() You don't fucking care either. Fucking ignore it until you need it that is what fucking logging is for. building something that works. So, here you go. a fucking log() function. Also, because I'm annoyed today sleep() and exit() Because, when I want you to fucking sleep or fucking exit, I don't want to go to the top of a file and declare stupid shit related to nanoseconds or add "import os.Exit" or whatever the fuck. fucking exit or fucking sleep. god damn it, you know what I fucking wanted. stop wasting my time. life is short. Signed-off-by: Jeff Carr --- dns.go | 20 ++--------- fsnotify.go | 76 ++++++++++++++++++++++++++++++++++++++++ gui.go | 19 +++++----- he-ipv6-tunnel.sh | 19 ++++++++-- log.go | 86 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 +--- net.go | 88 +++++++++++++++++++---------------------------- netlink.go | 16 +++++++++ structs.go | 26 ++++++++++++++ unix.go | 9 +++-- 10 files changed, 273 insertions(+), 92 deletions(-) create mode 100644 fsnotify.go create mode 100644 log.go create mode 100644 structs.go diff --git a/dns.go b/dns.go index 017ffce..93ffc8a 100644 --- a/dns.go +++ b/dns.go @@ -7,27 +7,11 @@ package main import ( // "os" // "os/exec" - "log" "net" // "git.wit.org/wit/gui" // "github.com/davecgh/go-spew/spew" ) -type IPtype struct { - // IP string - IPv4 bool - IPv6 bool - LinkLocal bool -} - -type Host struct { - Name string - domainname string - hostname string - fqdn string - ips map[string]*IPtype -} - /* Check a bunch of things. If they don't work right, then things are not correctly configured They are things like: @@ -43,9 +27,9 @@ func (h *Host) verifyETC() bool { func (h *Host) updateIPs(host string) { ips, err := net.LookupIP(host) if err != nil { - log.Fatal(err) + exit(err) } for _, ip := range ips { - log.Println(host, ip) + log(host, ip) } } diff --git a/fsnotify.go b/fsnotify.go new file mode 100644 index 0000000..814c9c7 --- /dev/null +++ b/fsnotify.go @@ -0,0 +1,76 @@ +package main + +// Watches for changes to a directory. Works cross-platform + +import ( + "github.com/fsnotify/fsnotify" +) + +// This would be a really dumb way to watch for new network interfaces +// since it then watches a linux only directory /sys/class/net for changes + +func watchSysClassNet() { + // Create new watcher. + watcher, err := fsnotify.NewWatcher() + if err != nil { + exit(err) + } + defer watcher.Close() + + // Start listening for events. + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + log("event:", event) + if event.Has(fsnotify.Write) { + log("modified file:", event.Name) + } + case err, ok := <-watcher.Errors: + if !ok { + return + } + log("error:", err) + } + } + }() + + // Add a path. + err = watcher.Add("/tmp") + if err != nil { + exit(err) + } + + // Block main goroutine forever. + <-make(chan struct{}) +} + +func fsnotifyNetworkInterfaceChanges() error { + watcher, err := fsnotify.NewWatcher() + if err != nil { + return err + } + defer watcher.Close() + + // Watch for network interface changes + err = watcher.Add("/sys/class/net") + if err != nil { + return err + } + for { + select { + case event := <-watcher.Events: + log("fsnotifyNetworkInterfaceChanges() event =", event) + if event.Op&fsnotify.Create == fsnotify.Create { + // Do something on network interface creation + } + case err := <-watcher.Errors: + log("fsnotifyNetworkInterfaceChanges() event err =", err) + return err + } + } +} + diff --git a/gui.go b/gui.go index 71d7b7f..4829077 100644 --- a/gui.go +++ b/gui.go @@ -4,7 +4,6 @@ package main import ( "os" "os/user" - "log" "net" "git.wit.org/wit/gui" "github.com/davecgh/go-spew/spew" @@ -38,7 +37,7 @@ func addDNSTab(window *gui.Node, title string) { var name string newNode = window.NewTab(title) - log.Println("addDemoTab() newNode.Dump") + log("addDemoTab() newNode.Dump") newNode.Dump() g = newNode.NewGroup("group 1") @@ -49,32 +48,32 @@ func addDNSTab(window *gui.Node, title string) { dd.OnChanged = func(*gui.Node) { s := dd.GetText() tb.SetText("hello world " + args.User + "\n" + s) - log.Println("text =", s) + log("text =", s) } g.NewLabel("UID =") g2 = newNode.NewGroup("group 2") tb = g2.NewTextbox("tb") - log.Println("tb =", tb.GetText()) + log("tb =", tb.GetText()) tb.OnChanged = func(*gui.Node) { s := tb.GetText() - log.Println("text =", s) + log("text =", s) } g2.NewButton("hello", func () { - log.Println("world") + log("world") }) g2.NewButton("scanInterfaces()", func () { scanInterfaces() }) g2.NewButton("os.Hostname()", func () { name, err = os.Hostname() - log.Println("name =", name, err) + log("name =", name, err) }) g2.NewButton("os.User()", func () { user, _ := user.Current() spew.Dump(user) - log.Println("os.Getuid =", os.Getuid()) + log("os.Getuid =", os.Getuid()) }) g2.NewButton("Escalate()", func () { Escalate() @@ -84,7 +83,7 @@ func addDNSTab(window *gui.Node, title string) { if err != nil { return } - log.Println("host =", host) + log("host =", host) }) g2.NewButton("DumpPublicDNSZone(apple.com)", func () { DumpPublicDNSZone("apple.com") @@ -93,6 +92,6 @@ func addDNSTab(window *gui.Node, title string) { } func myDefaultExit(n *gui.Node) { - log.Println("You can Do exit() things here") + log("You can Do exit() things here") os.Exit(0) } diff --git a/he-ipv6-tunnel.sh b/he-ipv6-tunnel.sh index 23ddee6..bc95429 100755 --- a/he-ipv6-tunnel.sh +++ b/he-ipv6-tunnel.sh @@ -34,9 +34,24 @@ if [ "$1" = "down" ]; then exit fi +if [ "$1" = "ping" ]; then + ping -c 3 2001:470:1f10:13d::1 + exit +fi + modprobe ipv6 -ip tunnel add he-ipv6 mode sit remote 184.105.253.14 local 74.87.91.117 ttl 255 +ip tunnel add he-ipv6 mode sit remote 184.105.253.14 local 40.132.180.131 ttl 255 ip link set he-ipv6 up -ip addr add 2001:470:1f10:2a::2/64 dev he-ipv6 +ip addr add 2001:470:1f10:13d::2/64 dev he-ipv6 ip route add ::/0 dev he-ipv6 ip -f inet6 addr +ifconfig he-ipv6 mtu 1460 + + +# old attempt from the something or pabtz hotel +# modprobe ipv6 +# ip tunnel add he-ipv6 mode sit remote 184.105.253.14 local 74.87.91.117 ttl 255 +# ip link set he-ipv6 up +# ip addr add 2001:470:1f10:2a::2/64 dev he-ipv6 +# ip route add ::/0 dev he-ipv6 +# ip -f inet6 addr diff --git a/log.go b/log.go new file mode 100644 index 0000000..18401fd --- /dev/null +++ b/log.go @@ -0,0 +1,86 @@ +// I like things to be easy. Why can't the standard language be like this? + +package main + +import ( + "os" + golog "log" + "reflect" + "github.com/davecgh/go-spew/spew" + // "net" +) + +/* + sleep() # you know what this does? sleeps for 1 second. yep. dump. easy. + sleep(.1) # you know what this does? yes, it sleeps for 1/10th of a second +*/ +func sleep(a ...any) { + log("sleep", a) +} + +/* + exit() # yep. exits. I guess everything must be fine + exit(3) # I guess 3 it is then + exit("dont like apples") # ok. I'll make a note of that +*/ +func exit(a ...any) { + log("exit", a) + //if (a) { + // os.Exit(a) + //} + os.Exit(0) +} + +/* + I've spent, am spending, too much time thinking about 'logging'. 'log', 'logrus', 'zap', whatever. + I'm not twitter. i don't give a fuck about how many nanoseconds it takes to log. Anyway, this + implementation is probably faster than all of those because you just set one bool to FALSE + and it all stops. + Sometimes I need to capture to stdout, sometimes stdout can't + work because it doesn't exist for the user. This whole thing is a PITA. Then it's spread over 8 million references in every .go file. I'm tapping out and putting + it in one place. here it is. Also, this makes having debug levels really fucking easy. You can define whatever level of logging you want from anywhere (command line) + etc. + + log() # doesn't do anything + log(stuff) # sends it to whatever log you define in a single place. here is the place +*/ + +var LOGOFF bool // turn this off, all logging stops +var WARN bool +var INFO bool + +type spewt struct { + a bool +} + +var SPEW spewt + +func log(a ...any) { + if (LOGOFF) { + return + } + + if (a == nil) { + return + } + var blah bool + if (reflect.TypeOf(a[0]) == reflect.TypeOf(blah)) { + // golog.Println("\t a[0] = bool") + if (a[0] == false) { + return + } + a = a[1:] + } + + if (reflect.TypeOf(a[0]) == reflect.TypeOf(SPEW)) { + a = a[1:] + spew.Dump(a) + return + } + + golog.Println(a...) + // golog.Println("\t a[0] =", a[0]) + // for argNum, arg := range a { + // golog.Println("\t", argNum, arg) + // } +} diff --git a/main.go b/main.go index 48bdd56..d555ac6 100644 --- a/main.go +++ b/main.go @@ -2,19 +2,16 @@ package main import ( - "log" // "net" // "github.com/fsnotify/fsnotify" "git.wit.org/wit/gui" arg "github.com/alexflint/go-arg" ) -var me Host - func main() { arg.MustParse(&args) // fmt.Println(args.Foo, args.Bar, args.User) - log.Println("Toolkit = ", args.Toolkit) + log("Toolkit = ", args.Toolkit) me.ips = make(map[string]*IPtype) @@ -22,6 +19,5 @@ func main() { scanInterfaces() watchNetworkInterfaces() - go inotifyNetworkInterfaceChanges() gui.Main(initGUI) } diff --git a/net.go b/net.go index 65e9222..88bba59 100644 --- a/net.go +++ b/net.go @@ -2,14 +2,14 @@ package main import ( - "log" "net" "strings" - "github.com/fsnotify/fsnotify" // "git.wit.org/wit/gui" - "github.com/davecgh/go-spew/spew" ) +var DEBUGNET bool = false + +// this doesn't work func watchNetworkInterfaces() { // Get list of network interfaces interfaces, _ := net.Interfaces() @@ -17,45 +17,28 @@ func watchNetworkInterfaces() { // Set up a notification channel notification := make(chan net.Interface) + log(DEBUGNET, "watchNet()") // Start goroutine to watch for changes go func() { + log(DEBUGNET, "watchNet() func") for { + log(DEBUGNET, "forever loop start") // Check for changes in each interface for _, i := range interfaces { + log(DEBUGNET, "something on i =", i) if status := i.Flags & net.FlagUp; status != 0 { notification <- i - log.Println("something on i =", i) + log(DEBUGNET, "something on i =", i) } } - log.Println("forever loop") + log(DEBUGNET, "forever loop end") } }() -} - -func inotifyNetworkInterfaceChanges() error { - watcher, err := fsnotify.NewWatcher() - if err != nil { - return err - } - defer watcher.Close() - - // Watch for network interface changes - err = watcher.Add("/sys/class/net") - if err != nil { - return err - } - for { - select { - case event := <-watcher.Events: - log.Println("inotifyNetworkInterfaceChanges() event =", event) - if event.Op&fsnotify.Create == fsnotify.Create { - // Do something on network interface creation - } - case err := <-watcher.Errors: - log.Println("inotifyNetworkInterfaceChanges() event err =", err) - return err - } - } + log() + log(true, "this is true") + log(false, "this is false") + sleep(10.3) + exit(0) } func IsIPv6(address string) bool { @@ -64,49 +47,50 @@ func IsIPv6(address string) bool { func IsReal(ip *net.IP) bool { if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) { - log.Println("\t\tIP is Real = false") + log(DEBUGNET, "\t\tIP is Real = false") return false } else { - log.Println("\t\tIP is Real = true") + log(DEBUGNET, "\t\tIP is Real = true") return true } } func scanInterfaces() { ifaces, _ := net.Interfaces() - spew.Dump(ifaces) + log(DEBUGNET, SPEW, ifaces) for _, i := range ifaces { addrs, _ := i.Addrs() - log.Println("addrs = ", i) - spew.Dump(addrs) + // log("range ifaces = ", i) + log("*net.Interface.Name = ", i.Name) + log(SPEW, i) + log(DEBUGNET, SPEW, addrs) for _, addr := range addrs { - log.Println("\taddr =", addr) - spew.Dump(addr) + log(DEBUGNET, "\taddr =", addr) + log(DEBUGNET, SPEW, addrs) ips, _ := net.LookupIP(addr.String()) - log.Println("\tLookupIP(addr) =", ips) + log(DEBUGNET, "\tLookupIP(addr) =", ips) switch v := addr.(type) { case *net.IPNet: - log.Println("\t\taddr.(type) = *net.IPNet") - log.Println("\t\taddr.(type) =", v) + log(DEBUGNET, "\t\taddr.(type) = *net.IPNet") + log(DEBUGNET, "\t\taddr.(type) =", v) ip := v.IP - // spew.Dump(ip) - log.Println("\t\taddr.IP =", ip) + log(DEBUGNET, "\t\taddr.IP =", ip) if (IsIPv6(ip.String())) { - log.Println("\t\tIP is IPv6") + log(DEBUGNET, "\t\tIP is IPv6") } else { - log.Println("\t\tIP is IPv4") + log(DEBUGNET, "\t\tIP is IPv4") } if (IsReal(&ip)) { - log.Println("\t\tIP is Real = true") + log("\tIP is Real ", addr) } else { - log.Println("\t\tIP is Real = false") + log("\tIP is not Real", addr) } - log.Println("\t\tIP is IsPrivate() =", ip.IsPrivate()) - log.Println("\t\tIP is IsLoopback() =", ip.IsLoopback()) - log.Println("\t\tIP is IsLinkLocalUnicast() =", ip.IsLinkLocalUnicast()) - // log.Println("\t\tIP is () =", 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.()) default: - log.Println("\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v) + log(DEBUGNET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v) } } } diff --git a/netlink.go b/netlink.go index ff32b96..8b2e14f 100644 --- a/netlink.go +++ b/netlink.go @@ -1,5 +1,21 @@ package main +// examples of what ifconfig does +// example of AF_NETLINK change: +// https://stackoverflow.com/questions/579783/how-to-detect-ip-address-change-programmatically-in-linux/2353441#2353441 +// from that page, a link to watch for any ip event: +// https://github.com/angt/ipevent/blob/master/ipevent.c + +/* + c example from ipevent.c : + int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + + struct sockaddr_nl snl = { + .nl_family = AF_NETLINK, + .nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR, + }; +*/ + /* import ( // "os" diff --git a/structs.go b/structs.go new file mode 100644 index 0000000..e77d034 --- /dev/null +++ b/structs.go @@ -0,0 +1,26 @@ +// This creates a simple hello world window +package main + +import ( + // "net" + // "github.com/fsnotify/fsnotify" + // "git.wit.org/wit/gui" + // arg "github.com/alexflint/go-arg" +) + +var me Host + +type IPtype struct { + // IP string + IPv4 bool + IPv6 bool + LinkLocal bool +} + +type Host struct { + Name string + domainname string + hostname string + fqdn string + ips map[string]*IPtype +} diff --git a/unix.go b/unix.go index 6a78e72..82dc1d4 100644 --- a/unix.go +++ b/unix.go @@ -7,7 +7,6 @@ package main import ( "os" "os/exec" - "log" "net" // "git.wit.org/wit/gui" // "github.com/davecgh/go-spew/spew" @@ -25,7 +24,7 @@ func Escalate() { cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { - log.Fatal(err) + exit(err) } } } @@ -39,16 +38,16 @@ func DumpPublicDNSZone(zone string) { panic(err) } for _, entry := range entries { - log.Println(entry) + log(entry) } } func dumpIPs(host string) { ips, err := net.LookupIP(host) if err != nil { - log.Fatal(err) + exit(err) } for _, ip := range ips { - log.Println(host, ip) + log(host, ip) } }