control-panel-dns/main.go

141 lines
3.3 KiB
Go

// GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
// Copyright (c) 2023 WIT.COM, Inc.
// This is a control panel for DNS
package main
import (
"fmt"
"log"
"strconv"
"runtime"
"time"
"embed"
"git.wit.org/wit/gui"
)
var myGui *gui.Node
//go:embed plugins/*.so
var resToolkit embed.FS
func main() {
// parsedown()
// initialize the maps to track IP addresses and network interfaces
me.ipmap = make(map[string]*IPtype)
me.dnsmap = make(map[string]*IPtype)
me.ifmap = make(map[int]*IFtype)
me.dnsTTL = 2 // how often to recheck DNS
me.dnsTTLsleep = .4 // sleep between loops
// will set all debugging flags
// gui.SetDebug(true)
// myGui = gui.New().InitEmbed(resToolkit).LoadToolkit("gocui")
myGui = gui.New().Default()
sleep(me.artificialSleep)
setupControlPanelWindow()
/*
if (args.GuiDebug) {
gui.DebugWindow()
}
gui.ShowDebugValues()
*/
// forever monitor for network and dns changes
sleep(me.artificialSleep)
checkNetworkChanges()
}
/*
Poll for changes to the networking settings
*/
func checkNetworkChanges() {
var ttl int = 0
for {
sleep(me.dnsTTLsleep)
ttl -= 1
if (ttl < 0) {
if (runtime.GOOS == "linux") {
duration := timeFunction(dnsTTL)
log.Println("dnsTTL() execution Time: ", duration)
var s string
if (duration > 5000 * time.Millisecond ) {
s = fmt.Sprint("VERY BAD\n", duration)
} else if (duration > 2000 * time.Millisecond ) {
s = fmt.Sprint("BAD\n", duration)
} else if (duration > 500 * time.Millisecond ) {
s = fmt.Sprint("SLOW\n", duration)
} else if (duration > 100 * time.Millisecond ) {
s = fmt.Sprint("OK\n", duration)
} else {
s = fmt.Sprint("FAST\n", duration)
}
log.Println(true, "getHostname()", s)
me.DnsSpeed.SetText(s)
} else {
log.Println("Windows and MacOS don't work yet")
}
ttl = me.dnsTTL
}
}
}
// This checks for changes to the network settings
// and verifies that DNS is working or not working
func dnsTTL() {
me.changed = false
log.Println("FQDN =", me.fqdn.GetText())
duration := timeFunction(getHostname)
debug(true, "getHostname() execution Time: ", duration)
duration = timeFunction(scanInterfaces)
log.Println("scanInterfaces() execution Time: ", duration)
for i, t := range me.ifmap {
log.Println(strconv.Itoa(i) + " iface = " + t.iface.Name)
}
var aaaa []string
aaaa = realAAAA()
var all string
for _, s := range aaaa {
log.Println("my actual AAAA = ",s)
all += s + "\n"
}
// me.IPv6.SetText(all)
if (me.changed) {
stamp := time.Now().Format("2006/01/02 15:04:05")
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
*/
}
/*
// Example usage
duration := timeFunction(FunctionToTime)
log.Println("Execution Time: ", duration)
*/
// timeFunction takes a function as an argument and returns the execution time.
func timeFunction(f func()) time.Duration {
startTime := time.Now() // Record the start time
f() // Execute the function
return time.Since(startTime) // Calculate the elapsed time
}