start dns resolver detection and debugging

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-17 22:35:33 -06:00
parent a417cb2930
commit 9abe9f9947
10 changed files with 149 additions and 17 deletions

View File

@ -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)
}
}

2
dns.go
View File

@ -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)

View File

@ -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
}

22
gui.go
View File

@ -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) {

14
log.go
View File

@ -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
}

13
main.go
View File

@ -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
*/
}
/*

4
net.go
View File

@ -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

99
proc.go Normal file
View File

@ -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 ""
}

View File

@ -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()

View File

@ -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)