From 9abe9f9947d9c08590333d5150731bf451f2ff7c Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 17 Dec 2023 22:35:33 -0600 Subject: [PATCH] start dns resolver detection and debugging Signed-off-by: Jeff Carr --- bash.go | 2 +- dns.go | 2 +- fsnotify.go | 4 +-- gui.go | 22 ++++++++++++ log.go | 14 ++++---- main.go | 13 ++++++- net.go | 4 +-- proc.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ rtnetlink.go | 2 +- unix.go | 4 +-- 10 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 proc.go diff --git a/bash.go b/bash.go index 5e63e5f..5c49a39 100644 --- a/bash.go +++ b/bash.go @@ -54,7 +54,7 @@ func test() error { func mainBash() { if err := test(); err != nil { - log.Println(logError, "exit in mainBash()") + debug(LogError, "exit in mainBash()") exit(err) } } diff --git a/dns.go b/dns.go index 2bb4232..4559303 100644 --- a/dns.go +++ b/dns.go @@ -24,7 +24,7 @@ func (h *Host) verifyETC() bool { func (h *Host) updateIPs(host string) { ips, err := net.LookupIP(host) if err != nil { - log.Println(logError, "updateIPs failed", err) + debug(LogError, "updateIPs failed", err) } for _, ip := range ips { log.Println(host, ip) diff --git a/fsnotify.go b/fsnotify.go index 8e00527..23809e6 100644 --- a/fsnotify.go +++ b/fsnotify.go @@ -14,7 +14,7 @@ func watchSysClassNet() { // Create new watcher. watcher, err := fsnotify.NewWatcher() if err != nil { - log.Println(logError, "watchSysClassNet() failed:", err) + debug(LogError, "watchSysClassNet() failed:", err) return } defer watcher.Close() @@ -43,7 +43,7 @@ func watchSysClassNet() { // Add a path. err = watcher.Add("/tmp") if err != nil { - log.Println(logError, "watchSysClassNet() watcher.Add() failed:", err) + debug(LogError, "watchSysClassNet() watcher.Add() failed:", err) return } diff --git a/gui.go b/gui.go index f4b67fa..cbf482e 100644 --- a/gui.go +++ b/gui.go @@ -100,6 +100,25 @@ func debugTab(title string) { dumpIPs("www.apple.com") }) + g2 = tab.NewGroup("debugging options") + + // DEBUG flags + dbOn := g2.NewCheckbox("turn on debugging (will override all flags below)") + dbOn.Custom = func() { + DEBUGON = dbOn.B + } + + dbNet := g2.NewCheckbox("turn on network debugging)") + dbNet.Custom = func() { + DEBUGNET = dbNet.B + } + + dbProc := g2.NewCheckbox("turn on /proc debugging)") + dbProc.Custom = func() { + DEBUGPROC = dbProc.B + } + + // various timeout settings g2.NewLabel("control panel TTL (in tenths of seconds)") ttl := g2.NewSlider("dnsTTL", 1, 100) ttl.Set(me.dnsTTL * 10) @@ -115,6 +134,9 @@ func debugTab(title string) { me.dnsTTLsleep = float64(ttl2.I) / 10 log.Println("dnsTTLsleep =", me.dnsTTLsleep) } + + g2.Margin() + g2.Pad() } func myDefaultExit(n *gui.Node) { diff --git a/log.go b/log.go index 3af708a..4515393 100644 --- a/log.go +++ b/log.go @@ -8,12 +8,12 @@ import ( // various debugging flags var logNow bool = true // useful for active development -var logError bool = true -var logWarn bool = false -var logInfo bool = false -var logVerbose bool = false +var LogError bool = true +var LogProc bool = false // turn on /proc debugging output -var DEBUGOFF bool = true +var DEBUGON bool = false +var DEBUGPROC bool = false +var DEBUGNET bool = false var SPEW witlog.Spewt @@ -31,12 +31,12 @@ func sleep(a ...any) { } func exit(a ...any) { - log.Println(logError, "got to log() exit") + debug(LogError, "got to log() exit") witlog.Exit(a...) } func debug(a ...any) { - if (DEBUGOFF) { + if (! DEBUGON) { return } diff --git a/main.go b/main.go index 69cc1bb..a6b5e2b 100644 --- a/main.go +++ b/main.go @@ -107,9 +107,20 @@ func dnsTTL() { if (me.changed) { stamp := time.Now().Format("2006/01/02 15:04:05") - log.Println(logError, "Network things changed on", stamp) + debug(LogError, "Network things changed on", stamp) updateDNS() } + + processName := getProcessNameByPort(53) + fmt.Println("Process with port 53:", processName) + /* + commPath := filepath.Join("/proc", proc.Name(), "comm") + comm, err := ioutil.ReadFile(commPath) + if err != nil { + return "", err // Error reading the process name + } + return strings.TrimSpace(string(comm)), nil + */ } /* diff --git a/net.go b/net.go index 7bd33dd..444600e 100644 --- a/net.go +++ b/net.go @@ -7,8 +7,6 @@ import ( "strings" ) -var DEBUGNET bool = false - // this doesn't work /* func watchNetworkInterfaces() { @@ -187,6 +185,7 @@ func checkIP(ip *net.IPNet, i net.Interface) bool { } func scanInterfaces() { + debug(DEBUGNET, "scanInterfaces() START") me.changed = false ifaces, _ := net.Interfaces() // me.ifnew = ifaces @@ -230,6 +229,7 @@ func scanInterfaces() { all6 = strings.TrimSpace(all6) me.IPv4.SetText(all4) me.IPv6.SetText(all6) + debug(DEBUGNET, "scanInterfaces() END") } // delete network interfaces and ip addresses from the gui diff --git a/proc.go b/proc.go new file mode 100644 index 0000000..323f312 --- /dev/null +++ b/proc.go @@ -0,0 +1,99 @@ +package main + +import ( + "io/ioutil" + "os" + "path/filepath" + "strconv" + "strings" +) + +func getProcessNameByPort(port int) string { + // Convert port to hex string + portHex := strconv.FormatInt(int64(port), 16) + + // Function to search /proc/net/tcp or /proc/net/udp + searchProcNet := func(file string) string { + data, err := ioutil.ReadFile(file) + if err != nil { + return "" + } + // debug(DEBUGPROC, "searchProcNet() data:", string(data)) + + lines := strings.Split(string(data), "\n") + for _, line := range lines { + fields := strings.Fields(line) + debug(DEBUGPROC, "searchProcNet() portHex:", portHex) + if (len(fields) > 9) { + debug(DEBUGPROC, "searchProcNet() fields[9]", fields[9]) + } + debug(DEBUGPROC, "searchProcNet() lines:", line) + if len(fields) > 1 { + parts := strings.Split(fields[1], ":") + if len(parts) > 1 { + // Convert the hexadecimal string to an integer + value, _ := strconv.ParseInt(parts[1], 16, 64) + debug(DEBUGPROC, "searchProcNet() value, port =", value, port, "parts[1] =", parts[1]) + if (port == int(value)) { + debug(DEBUGPROC, "searchProcNet() THIS IS THE LINE:", fields) + return fields[9] + } + } + } + } + + return "" + } + + // Search TCP and then UDP + inode := searchProcNet("/proc/net/tcp") + if inode == "" { + inode = searchProcNet("/proc/net/udp") + } + debug(DEBUGPROC, "searchProcNet() inode =", inode) + + // Search for process with the inode + procs, _ := ioutil.ReadDir("/proc") + for _, proc := range procs { + if !proc.IsDir() { + continue + } + + fdPath := filepath.Join("/proc", proc.Name(), "fd") + fds, err := ioutil.ReadDir(fdPath) + if err != nil { + continue // Process might have exited; skip it + } + + for _, fd := range fds { + fdLink, _ := os.Readlink(filepath.Join(fdPath, fd.Name())) + var s string + s = "socket:["+inode+"]" + if strings.Contains(fdLink, "socket:[") { + debug(DEBUGPROC, "searchProcNet() fdLink has socket:", fdLink) + debug(DEBUGPROC, "searchProcNet() proc.Name() =", proc.Name(), "s =", s) + } + if strings.Contains(fdLink, "socket:[35452]") { + debug(DEBUGPROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + return proc.Name() + } + if strings.Contains(fdLink, "socket:[35450]") { + debug(DEBUGPROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + return proc.Name() + } + if strings.Contains(fdLink, "socket:[35440]") { + debug(DEBUGPROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + return proc.Name() + } + if strings.Contains(fdLink, "socket:[21303]") { + debug(DEBUGPROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + // return proc.Name() + } + if strings.Contains(fdLink, "socket:["+inode+"]") { + return proc.Name() + } + } + } + + return "" +} diff --git a/rtnetlink.go b/rtnetlink.go index 46898b5..d4449c0 100644 --- a/rtnetlink.go +++ b/rtnetlink.go @@ -10,7 +10,7 @@ func Example_listLink() { // Dial a connection to the rtnetlink socket conn, err := rtnetlink.Dial(nil) if err != nil { - log.Println(logError, "Example_listLink() failed", err) + debug(LogError, "Example_listLink() failed", err) return } defer conn.Close() diff --git a/unix.go b/unix.go index c9b5cc0..1708688 100644 --- a/unix.go +++ b/unix.go @@ -23,7 +23,7 @@ func Escalate() { cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { - log.Println(logError, "exit in Escalate()") + debug(LogError, "exit in Escalate()") exit(err) } } @@ -45,7 +45,7 @@ func DumpPublicDNSZone(zone string) { func dumpIPs(host string) { ips, err := net.LookupIP(host) if err != nil { - log.Println(logError, "dumpIPs() failed:", err) + debug(LogError, "dumpIPs() failed:", err) } for _, ip := range ips { log.Println(host, ip)