2024-01-21 00:15:20 -06:00
|
|
|
/*
|
2024-01-06 01:41:33 -06:00
|
|
|
'dig'
|
|
|
|
|
|
|
|
This is essentially doing what the command 'dig' does
|
|
|
|
It performing DNS queries on TCP and UDP
|
|
|
|
against localhost, cloudflare & google
|
2024-01-21 00:15:20 -06:00
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
IPv4() and IPv6() return true if they are working
|
2024-01-21 00:15:20 -06:00
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
with the 'gui' package, it can also display the results
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-21 00:15:20 -06:00
|
|
|
"errors"
|
2024-01-06 01:41:33 -06:00
|
|
|
"fmt"
|
2024-01-21 00:15:20 -06:00
|
|
|
"os"
|
2024-01-06 01:41:33 -06:00
|
|
|
"reflect"
|
2024-01-21 00:15:20 -06:00
|
|
|
"time"
|
2024-01-06 01:41:33 -06:00
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
"go.wit.com/gui"
|
2024-01-31 13:34:02 -06:00
|
|
|
"go.wit.com/lib/gadgets"
|
2024-01-21 00:15:20 -06:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-01-31 13:34:02 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-06 01:41:33 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type digStatus struct {
|
2024-01-21 00:15:20 -06:00
|
|
|
ready bool
|
|
|
|
hidden bool
|
|
|
|
statusIPv4 string
|
|
|
|
statusIPv6 string
|
|
|
|
|
|
|
|
parent *gui.Node
|
|
|
|
window *gadgets.BasicWindow
|
|
|
|
group *gui.Node
|
|
|
|
grid *gui.Node
|
|
|
|
|
|
|
|
summary *gui.Node
|
|
|
|
status *gadgets.OneLiner
|
|
|
|
statusAAAA *gadgets.OneLiner
|
|
|
|
speed *gadgets.OneLiner
|
|
|
|
speedActual *gadgets.OneLiner
|
|
|
|
|
|
|
|
detailsGroup *gui.Node
|
|
|
|
details *gui.Node
|
|
|
|
dsLocalhost *resolverStatus
|
|
|
|
dsLocalNetwork *resolverStatus
|
|
|
|
dsCloudflare *resolverStatus
|
|
|
|
dsGoogle *resolverStatus
|
|
|
|
DnsDigUDP *gui.Node
|
|
|
|
DnsDigTCP *gui.Node
|
|
|
|
|
|
|
|
httpGoWitCom *gadgets.OneLiner
|
|
|
|
statusHTTP *gadgets.OneLiner
|
2024-01-06 01:41:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDigStatusWindow(p *gui.Node) *digStatus {
|
|
|
|
var ds *digStatus
|
|
|
|
ds = new(digStatus)
|
|
|
|
|
|
|
|
ds.ready = false
|
|
|
|
ds.hidden = true
|
|
|
|
|
|
|
|
ds.window = gadgets.NewBasicWindow(p, "DNS Resolver Status")
|
2024-01-15 17:22:51 -06:00
|
|
|
ds.window.Make()
|
|
|
|
// ds.window.Draw()
|
|
|
|
// ds.window.Hide()
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
// summary of the current state of things
|
|
|
|
ds.summary = ds.window.Box().NewGroup("Summary")
|
|
|
|
g := ds.summary.NewGrid("LookupStatus", 2, 2)
|
|
|
|
g.Pad()
|
|
|
|
|
2024-01-31 12:36:52 -06:00
|
|
|
ds.status = gadgets.NewOneLiner(g, "status").SetText("unknown")
|
|
|
|
ds.statusAAAA = gadgets.NewOneLiner(g, "IPv6 status").SetText("unknown")
|
|
|
|
ds.statusHTTP = gadgets.NewOneLiner(g, "IPv6 via HTTP").SetText("unknown")
|
|
|
|
ds.speed = gadgets.NewOneLiner(g, "speed").SetText("unknown")
|
|
|
|
ds.speedActual = gadgets.NewOneLiner(g, "actual").SetText("unknown")
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
// make the area to store the raw details
|
2024-01-10 20:22:25 -06:00
|
|
|
ds.detailsGroup = ds.window.Box().NewGroup("Details")
|
|
|
|
ds.details = ds.detailsGroup.NewBox("bw vbox", false)
|
2024-01-21 00:15:20 -06:00
|
|
|
ds.dsLocalhost = NewResolverStatus(ds.details, "(localhost)", "127.0.0.1:53", "go.wit.com")
|
|
|
|
ds.dsLocalNetwork = NewResolverStatus(ds.details, "(Local Network)", "192.168.86.1:53", "go.wit.com")
|
|
|
|
ds.dsCloudflare = NewResolverStatus(ds.details, "(cloudflare)", "1.1.1.1:53", "go.wit.com")
|
|
|
|
ds.dsGoogle = NewResolverStatus(ds.details, "(google)", "8.8.8.8:53", "go.wit.com")
|
2024-01-06 01:41:33 -06:00
|
|
|
ds.makeDnsStatusGrid()
|
|
|
|
ds.makeHttpStatusGrid()
|
|
|
|
|
|
|
|
ds.hidden = false
|
|
|
|
ds.ready = true
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) Update() {
|
|
|
|
log.Info("digStatus() Update() START")
|
|
|
|
if ds == nil {
|
|
|
|
log.Error(errors.New("digStatus() Update() ds == nil"))
|
|
|
|
return
|
|
|
|
}
|
2024-01-21 00:15:20 -06:00
|
|
|
duration := timeFunction(func() {
|
2024-01-06 01:41:33 -06:00
|
|
|
ds.updateDnsStatus()
|
|
|
|
})
|
|
|
|
s := fmt.Sprint(duration)
|
2024-01-31 12:36:52 -06:00
|
|
|
// ds.speedActual.SetText(s)
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.set(ds.speedActual, s)
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if duration > 500*time.Millisecond {
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.set(ds.speed, "SLOW")
|
2024-01-21 00:15:20 -06:00
|
|
|
} else if duration > 100*time.Millisecond {
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.set(ds.speed, "OK")
|
|
|
|
} else {
|
|
|
|
me.digStatus.set(ds.speed, "FAST")
|
|
|
|
}
|
|
|
|
log.Info("digStatus() Update() END")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the status is valid
|
|
|
|
func (ds *digStatus) Ready() bool {
|
2024-01-21 00:15:20 -06:00
|
|
|
if ds == nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-06 01:41:33 -06:00
|
|
|
return ds.ready
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if IPv4 is working
|
|
|
|
func (ds *digStatus) IPv4() bool {
|
2024-01-21 00:15:20 -06:00
|
|
|
if !ds.Ready() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ds.statusIPv4 == "OK" {
|
2024-01-06 01:41:33 -06:00
|
|
|
return true
|
|
|
|
}
|
2024-01-21 00:15:20 -06:00
|
|
|
if ds.statusIPv4 == "GOOD" {
|
2024-01-06 01:41:33 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if IPv6 is working
|
|
|
|
func (ds *digStatus) IPv6() bool {
|
2024-01-21 00:15:20 -06:00
|
|
|
if !ds.Ready() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ds.statusIPv6 == "GOOD" {
|
2024-01-06 01:41:33 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) setIPv4status(s string) {
|
|
|
|
ds.statusIPv4 = s
|
2024-01-21 00:15:20 -06:00
|
|
|
if !ds.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.set(ds.status, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) setIPv6status(s string) {
|
|
|
|
ds.statusIPv6 = s
|
2024-01-21 00:15:20 -06:00
|
|
|
if !ds.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.set(ds.statusAAAA, s)
|
|
|
|
}
|
|
|
|
|
2024-01-07 10:38:17 -06:00
|
|
|
/*
|
2024-01-06 01:41:33 -06:00
|
|
|
func (ds *digStatus) SetIPv6(s string) {
|
|
|
|
if ! ds.Ready() {return}
|
|
|
|
log.Warn("Should SetIPv6() here to", s)
|
|
|
|
log.Warn("Should SetIPv6() here to", s)
|
|
|
|
log.Warn("Should SetIPv6() here to", s)
|
|
|
|
log.Warn("Should SetIPv6() here to", s)
|
2024-01-31 12:36:52 -06:00
|
|
|
me.DnsAAAA.SetText(s)
|
2024-01-06 01:41:33 -06:00
|
|
|
// me.digStatus.set(ds.httpGoWitCom, addr)
|
|
|
|
}
|
2024-01-07 10:38:17 -06:00
|
|
|
*/
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
func (ds *digStatus) set(a any, s string) {
|
2024-01-21 00:15:20 -06:00
|
|
|
if !ds.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-06 01:41:33 -06:00
|
|
|
if ds.hidden {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if a == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var n *gui.Node
|
|
|
|
if reflect.TypeOf(a) == reflect.TypeOf(n) {
|
|
|
|
n = a.(*gui.Node)
|
|
|
|
n.SetText(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var ol *gadgets.OneLiner
|
|
|
|
if reflect.TypeOf(a) == reflect.TypeOf(ol) {
|
|
|
|
ol = a.(*gadgets.OneLiner)
|
2024-01-31 12:36:52 -06:00
|
|
|
ol.SetText(s)
|
2024-01-06 01:41:33 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warn("unknown type TypeOf(a) =", reflect.TypeOf(a), "a =", a)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) updateDnsStatus() {
|
|
|
|
var cmd, out string
|
|
|
|
var ipv4, ipv6 bool
|
|
|
|
|
|
|
|
log.Info("updateDnsStatus() START")
|
2024-01-21 00:15:20 -06:00
|
|
|
if ds == nil {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Error(errors.New("updateDnsStatus() not initialized yet. ds == nil"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if !ds.ready {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Error(errors.New("updateDnsStatus() not ready yet"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv4, ipv6 = ds.dsLocalhost.update()
|
|
|
|
ipv4, ipv6 = ds.dsLocalNetwork.update()
|
|
|
|
ipv4, ipv6 = ds.dsCloudflare.update()
|
|
|
|
ipv4, ipv6 = ds.dsGoogle.update()
|
|
|
|
|
2024-01-06 16:59:33 -06:00
|
|
|
if me.statusOS.ValidHostname() {
|
|
|
|
if ds.checkLookupDoH(me.statusOS.GetHostname()) {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "updateDnsStatus() HTTP DNS lookups working")
|
|
|
|
me.digStatus.set(ds.statusHTTP, "WORKING")
|
|
|
|
} else {
|
|
|
|
log.Log(DNS, "updateDnsStatus() HTTP DNS lookups not working")
|
|
|
|
log.Log(DNS, "updateDnsStatus() It's really unlikely you are on the internet")
|
|
|
|
me.digStatus.set(ds.statusHTTP, "BROKEN")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
me.digStatus.set(ds.statusHTTP, "INVALID HOSTNAME")
|
|
|
|
}
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if ipv4 {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "updateDnsStatus() IPv4 A lookups working")
|
|
|
|
ds.setIPv4status("OK")
|
|
|
|
} else {
|
|
|
|
log.Log(DNS, "updateDnsStatus() IPv4 A lookups not working. No internet?")
|
|
|
|
ds.setIPv4status("No Internet?")
|
|
|
|
}
|
2024-01-21 00:15:20 -06:00
|
|
|
if ipv6 {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "updateDnsStatus() IPv6 AAAA lookups working")
|
|
|
|
ds.setIPv4status("GOOD")
|
|
|
|
ds.setIPv6status("GOOD")
|
|
|
|
} else {
|
|
|
|
log.Log(DNS, "updateDnsStatus() IPv6 AAAA lookups are not working")
|
|
|
|
ds.setIPv6status("Need VPN")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = "dig +noall +answer www.wit.com A"
|
2024-02-11 04:10:36 -06:00
|
|
|
out = shell.RunCapture(cmd)
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "makeDnsStatusGrid() dig", out)
|
|
|
|
me.digStatus.set(ds.DnsDigUDP, out)
|
|
|
|
|
|
|
|
cmd = "dig +noall +answer www.wit.com AAAA"
|
2024-02-11 04:10:36 -06:00
|
|
|
out = shell.RunCapture(cmd)
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "makeDnsStatusGrid() dig", out)
|
|
|
|
me.digStatus.set(ds.DnsDigTCP, out)
|
2024-01-07 07:09:59 -06:00
|
|
|
|
|
|
|
/*
|
2024-01-21 00:15:20 -06:00
|
|
|
g2.NewButton("dig +trace", func () {
|
|
|
|
log.Log(NOW, "TODO: redo this")
|
|
|
|
// o := shell.Run("dig +trace +noadditional DS " + me.hostname + " @8.8.8.8")
|
|
|
|
// log.Println(o)
|
|
|
|
})
|
2024-01-07 07:09:59 -06:00
|
|
|
*/
|
2024-01-06 01:41:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) makeHttpStatusGrid() {
|
|
|
|
group := ds.details.NewGroup("dns.google.com via HTTPS")
|
|
|
|
grid := group.NewGrid("LookupStatus", 2, 2)
|
|
|
|
|
|
|
|
ds.httpGoWitCom = gadgets.NewOneLiner(grid, "go.wit.com")
|
|
|
|
me.digStatus.set(ds.httpGoWitCom, "unknown")
|
|
|
|
|
|
|
|
group.Pad()
|
|
|
|
grid.Pad()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) makeDnsStatusGrid() {
|
|
|
|
var cmd, out string
|
|
|
|
group := ds.details.NewGroup("dig results")
|
|
|
|
grid := group.NewGrid("LookupStatus", 2, 2)
|
|
|
|
|
|
|
|
cmd = "dig +noall +answer go.wit.com A"
|
|
|
|
grid.NewLabel(cmd)
|
|
|
|
ds.DnsDigUDP = grid.NewLabel("?")
|
2024-02-11 04:10:36 -06:00
|
|
|
out = shell.RunCapture(cmd)
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "makeDnsStatusGrid() dig", out)
|
|
|
|
me.digStatus.set(ds.DnsDigUDP, out)
|
|
|
|
|
|
|
|
cmd = "dig +noall +answer go.wit.com AAAA"
|
|
|
|
grid.NewLabel(cmd)
|
|
|
|
ds.DnsDigTCP = grid.NewLabel("?")
|
2024-02-11 04:10:36 -06:00
|
|
|
out = shell.RunCapture(cmd)
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "makeDnsStatusGrid() dig", out)
|
|
|
|
me.digStatus.set(ds.DnsDigTCP, out)
|
|
|
|
|
|
|
|
group.Pad()
|
|
|
|
grid.Pad()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) checkLookupDoH(hostname string) bool {
|
|
|
|
var status bool = false
|
|
|
|
|
|
|
|
ipv6Addresses := lookupDoH(hostname, "AAAA")
|
|
|
|
|
|
|
|
log.Log(DNS, "IPv6 Addresses for ", hostname)
|
|
|
|
var s []string
|
|
|
|
for _, addr := range ipv6Addresses {
|
|
|
|
log.Log(DNS, addr)
|
|
|
|
s = append(s, addr)
|
|
|
|
status = true
|
|
|
|
}
|
2024-01-07 10:38:17 -06:00
|
|
|
// me.digStatus.SetIPv6(strings.Join(s, "\n"))
|
2024-01-06 01:41:33 -06:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) Show() {
|
|
|
|
log.Info("digStatus.Show() window")
|
|
|
|
if me.digStatus.hidden {
|
|
|
|
me.digStatus.window.Show()
|
|
|
|
}
|
|
|
|
me.digStatus.hidden = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *digStatus) Hide() {
|
|
|
|
log.Info("digStatus.Hide() window")
|
2024-01-21 00:15:20 -06:00
|
|
|
if !me.digStatus.hidden {
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.window.Hide()
|
|
|
|
}
|
|
|
|
me.digStatus.hidden = true
|
|
|
|
}
|
2024-02-01 08:49:16 -06:00
|
|
|
|
|
|
|
func digLoop() {
|
|
|
|
me.digStatus.Update()
|
|
|
|
|
|
|
|
if me.digStatus.Ready() {
|
|
|
|
current := me.statusIPv6.String()
|
|
|
|
if me.digStatus.IPv6() {
|
|
|
|
if current != "WORKING" {
|
|
|
|
log.Log(CHANGE, "IPv6 resolution is WORKING")
|
|
|
|
me.statusIPv6.SetText("WORKING")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if current != "Need VPN" {
|
|
|
|
log.Log(CHANGE, "IPv6 resolution seems to have broken")
|
|
|
|
me.statusIPv6.SetText("Need VPN")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|