2023-03-01 11:21:47 -06:00
|
|
|
// GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
|
|
|
|
// Copyright (c) 2023 WIT.COM, Inc.
|
2023-02-18 23:37:11 -06:00
|
|
|
// This is a control panel for DNS
|
2023-03-09 14:21:34 -06:00
|
|
|
|
2023-02-08 11:04:04 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-20 03:13:43 -06:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sort"
|
2023-03-01 11:21:47 -06:00
|
|
|
"strconv"
|
2023-02-18 23:37:11 -06:00
|
|
|
"runtime"
|
2023-03-26 16:17:32 -05:00
|
|
|
"time"
|
2023-12-16 09:02:59 -06:00
|
|
|
"embed"
|
2023-12-20 03:13:43 -06:00
|
|
|
|
2023-12-28 15:36:05 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-03 12:40:31 -06:00
|
|
|
"go.wit.com/gui/gui"
|
|
|
|
"go.wit.com/gui/debugger"
|
|
|
|
|
2023-12-20 03:13:43 -06:00
|
|
|
"github.com/miekg/dns"
|
2023-02-08 11:04:04 -06:00
|
|
|
)
|
|
|
|
|
2023-12-16 09:02:59 -06:00
|
|
|
//go:embed plugins/*.so
|
|
|
|
var resToolkit embed.FS
|
|
|
|
|
2023-02-08 11:04:04 -06:00
|
|
|
func main() {
|
2023-12-16 09:02:59 -06:00
|
|
|
// parsedown()
|
2023-02-09 19:47:52 -06:00
|
|
|
|
2023-02-18 23:37:11 -06:00
|
|
|
// initialize the maps to track IP addresses and network interfaces
|
|
|
|
me.ipmap = make(map[string]*IPtype)
|
2023-03-01 11:21:47 -06:00
|
|
|
me.dnsmap = make(map[string]*IPtype)
|
2023-02-18 23:37:11 -06:00
|
|
|
me.ifmap = make(map[int]*IFtype)
|
2023-12-20 03:13:43 -06:00
|
|
|
me.nsmap = make(map[string]string)
|
|
|
|
|
|
|
|
// initialize maps for the returned DNS records
|
|
|
|
me.ipv4s = make(map[string]dns.RR)
|
|
|
|
me.ipv6s = make(map[string]dns.RR)
|
2023-02-09 09:07:00 -06:00
|
|
|
|
2023-12-28 15:36:05 -06:00
|
|
|
// send all log() output to a file in /tmp
|
|
|
|
log.SetTmp()
|
2023-04-11 15:24:43 -05:00
|
|
|
|
2024-01-05 00:07:13 -06:00
|
|
|
me.myGui = gui.New().Default()
|
2023-12-20 03:13:43 -06:00
|
|
|
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Sleep(me.artificialSleep)
|
2023-04-07 11:23:47 -05:00
|
|
|
setupControlPanelWindow()
|
2023-12-20 03:13:43 -06:00
|
|
|
|
2024-01-03 13:25:07 -06:00
|
|
|
if debugger.ArgDebug() {
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Sleep(2)
|
2024-01-05 00:07:13 -06:00
|
|
|
debugger.DebugWindow(me.myGui)
|
2024-01-03 12:40:31 -06:00
|
|
|
}
|
|
|
|
|
2023-04-11 15:24:43 -05:00
|
|
|
// forever monitor for network and dns changes
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Sleep(me.artificialSleep)
|
2023-03-25 19:02:49 -05:00
|
|
|
checkNetworkChanges()
|
2023-02-09 09:07:00 -06:00
|
|
|
}
|
|
|
|
|
2023-02-18 23:37:11 -06:00
|
|
|
/*
|
|
|
|
Poll for changes to the networking settings
|
|
|
|
*/
|
2023-12-28 09:43:45 -06:00
|
|
|
|
|
|
|
/* https://github.com/robfig/cron/blob/master/cron.go
|
|
|
|
|
|
|
|
// Run the cron scheduler, or no-op if already running.
|
|
|
|
func (c *Cron) Run() {
|
|
|
|
c.runningMu.Lock()
|
|
|
|
if c.running {
|
|
|
|
c.runningMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.running = true
|
|
|
|
c.runningMu.Unlock()
|
|
|
|
c.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// run the scheduler.. this is private just due to the need to synchronize
|
|
|
|
// access to the 'running' state variable.
|
|
|
|
func (c *Cron) run() {
|
|
|
|
c.logger.Info("start")
|
|
|
|
*/
|
|
|
|
|
2023-02-18 23:37:11 -06:00
|
|
|
func checkNetworkChanges() {
|
2023-12-20 03:13:43 -06:00
|
|
|
var lastLocal time.Time = time.Now()
|
|
|
|
var lastDNS time.Time = time.Now()
|
2023-12-28 15:36:05 -06:00
|
|
|
|
2023-12-21 17:56:56 -06:00
|
|
|
timer2 := time.NewTimer(time.Second)
|
|
|
|
go func() {
|
|
|
|
<-timer2.C
|
|
|
|
fmt.Println("Timer 2 fired")
|
|
|
|
}()
|
|
|
|
|
2023-02-09 09:07:00 -06:00
|
|
|
for {
|
2023-12-21 17:56:56 -06:00
|
|
|
time.Sleep(me.ttl.Duration)
|
2023-12-20 03:13:43 -06:00
|
|
|
if (time.Since(lastLocal) > me.localSleep) {
|
2023-03-01 11:21:47 -06:00
|
|
|
if (runtime.GOOS == "linux") {
|
2023-12-20 03:13:43 -06:00
|
|
|
duration := timeFunction(linuxLoop)
|
|
|
|
s := fmt.Sprint(duration)
|
|
|
|
me.LocalSpeedActual.SetText(s)
|
2023-03-01 11:21:47 -06:00
|
|
|
} else {
|
2023-12-20 03:13:43 -06:00
|
|
|
// TODO: make windows and macos diagnostics
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Warn("Windows and MacOS don't work yet")
|
2023-03-01 11:21:47 -06:00
|
|
|
}
|
2023-12-20 03:13:43 -06:00
|
|
|
lastLocal = time.Now()
|
|
|
|
}
|
2023-12-21 17:56:56 -06:00
|
|
|
if (time.Since(lastDNS) > me.dnsTtl.Duration) {
|
2023-12-20 03:13:43 -06:00
|
|
|
DNSloop()
|
|
|
|
lastDNS = time.Now()
|
|
|
|
}
|
2023-12-21 17:56:56 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
stop2 := timer2.Stop()
|
|
|
|
if stop2 {
|
|
|
|
fmt.Println("Timer 2 stopped")
|
|
|
|
}
|
|
|
|
*/
|
2023-12-20 03:13:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run this on each timeout
|
|
|
|
func DNSloop() {
|
|
|
|
duration := timeFunction(dnsTTL)
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Info("dnsTTL() execution Time: ", duration)
|
2023-12-20 03:13:43 -06:00
|
|
|
var s, newSpeed string
|
|
|
|
if (duration > 5000 * time.Millisecond ) {
|
|
|
|
newSpeed = "VERY BAD"
|
|
|
|
suggestProcDebugging()
|
|
|
|
} else if (duration > 2000 * time.Millisecond ) {
|
|
|
|
newSpeed = "BAD"
|
|
|
|
suggestProcDebugging()
|
|
|
|
} else if (duration > 500 * time.Millisecond ) {
|
|
|
|
suggestProcDebugging()
|
|
|
|
newSpeed = "SLOW"
|
|
|
|
} else if (duration > 100 * time.Millisecond ) {
|
|
|
|
newSpeed = "OK"
|
|
|
|
if (me.fixProc != nil) {
|
2023-12-28 09:43:45 -06:00
|
|
|
// me.fixProc.Disable()
|
2023-12-20 03:13:43 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newSpeed = "FAST"
|
|
|
|
if (me.fixProc != nil) {
|
2023-12-28 09:43:45 -06:00
|
|
|
// me.fixProc.Disable()
|
2023-02-09 09:07:00 -06:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 03:13:43 -06:00
|
|
|
if (newSpeed != me.DnsSpeedLast) {
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(CHANGE, "dns lookup speed changed =", newSpeed)
|
|
|
|
log.Log(CHANGE, "dnsTTL() execution Time: ", duration)
|
2023-12-20 03:13:43 -06:00
|
|
|
me.DnsSpeed.SetText(newSpeed)
|
|
|
|
me.DnsSpeedLast = newSpeed
|
|
|
|
}
|
|
|
|
s = fmt.Sprint(duration)
|
|
|
|
me.DnsSpeedActual.SetText(s)
|
2023-02-09 09:07:00 -06:00
|
|
|
}
|
2023-03-01 11:21:47 -06:00
|
|
|
|
2023-04-11 15:24:43 -05:00
|
|
|
// This checks for changes to the network settings
|
|
|
|
// and verifies that DNS is working or not working
|
2023-03-01 11:21:47 -06:00
|
|
|
func dnsTTL() {
|
2023-12-20 03:13:43 -06:00
|
|
|
updateDNS()
|
|
|
|
}
|
|
|
|
|
|
|
|
func linuxLoop() {
|
2023-03-26 16:17:32 -05:00
|
|
|
me.changed = false
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(NET, "FQDN =", me.fqdn.GetText())
|
2023-12-20 03:13:43 -06:00
|
|
|
duration := timeFunction(getHostname)
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Info("getHostname() execution Time: ", duration, "me.changed =", me.changed)
|
2023-12-20 03:13:43 -06:00
|
|
|
|
|
|
|
duration = timeFunction(scanInterfaces)
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(NET, "scanInterfaces() execution Time: ", duration)
|
2023-03-01 11:21:47 -06:00
|
|
|
for i, t := range me.ifmap {
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(NET, strconv.Itoa(i) + " iface = " + t.iface.Name)
|
2023-03-01 11:21:47 -06:00
|
|
|
}
|
2023-03-26 16:17:32 -05:00
|
|
|
|
2023-03-01 11:21:47 -06:00
|
|
|
var aaaa []string
|
2023-12-28 09:43:45 -06:00
|
|
|
aaaa = dhcpAAAA()
|
2023-03-26 16:17:32 -05:00
|
|
|
var all string
|
2023-03-01 11:21:47 -06:00
|
|
|
for _, s := range aaaa {
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(NET, "my actual AAAA = ",s)
|
2023-03-26 16:17:32 -05:00
|
|
|
all += s + "\n"
|
|
|
|
}
|
2023-03-26 16:49:56 -05:00
|
|
|
// me.IPv6.SetText(all)
|
2023-03-26 16:17:32 -05:00
|
|
|
|
|
|
|
if (me.changed) {
|
|
|
|
stamp := time.Now().Format("2006/01/02 15:04:05")
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(CHANGE, "Network things changed on", stamp)
|
2023-12-20 03:13:43 -06:00
|
|
|
duration := timeFunction(updateDNS)
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(CHANGE, "updateDNS() execution Time: ", duration)
|
2023-12-20 03:13:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
2023-03-01 11:21:47 -06:00
|
|
|
}
|
2023-12-20 03:13:43 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortLines takes a string, splits it on newlines, sorts the lines,
|
|
|
|
// and rejoins them with newlines.
|
|
|
|
func sortLines(input string) string {
|
|
|
|
lines := strings.Split(input, "\n")
|
|
|
|
|
|
|
|
// Trim leading and trailing whitespace from each line
|
|
|
|
for i, line := range lines {
|
|
|
|
lines[i] = strings.TrimSpace(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(lines)
|
|
|
|
tmp := strings.Join(lines, "\n")
|
|
|
|
tmp = strings.TrimLeft(tmp, "\n")
|
|
|
|
tmp = strings.TrimRight(tmp, "\n")
|
|
|
|
return tmp
|
2023-03-01 11:21:47 -06:00
|
|
|
}
|