2024-01-21 00:15:20 -06:00
|
|
|
/*
|
2024-01-06 01:41:33 -06:00
|
|
|
Performs DNS queries on TCP and UDP
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2024-01-21 00:15:20 -06:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2024-01-06 01:41:33 -06:00
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
"go.wit.com/lib/gadgets"
|
|
|
|
"go.wit.com/gui"
|
2024-01-06 01:41:33 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type resolverStatus struct {
|
2024-01-21 00:15:20 -06:00
|
|
|
title string
|
|
|
|
server string // The DNS server. Example: "127.0.0.1:53" or "1.1.1.1:53"
|
|
|
|
hostname string // the hostname to lookup. Example: "www.google.com" or "go.wit.com"
|
2024-01-06 01:41:33 -06:00
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
parent *gui.Node
|
|
|
|
group *gui.Node
|
|
|
|
grid *gui.Node
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
// DNS setup options
|
2024-01-21 00:15:20 -06:00
|
|
|
udpA *gui.Node
|
|
|
|
tcpA *gui.Node
|
|
|
|
udpAAAA *gui.Node
|
|
|
|
tcpAAAA *gui.Node
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
// show the display
|
2024-01-21 00:15:20 -06:00
|
|
|
aFail *gui.Node
|
|
|
|
aSuccess *gui.Node
|
|
|
|
aaaaFail *gui.Node
|
|
|
|
aaaaSuccess *gui.Node
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
// interger counters
|
2024-01-21 00:15:20 -06:00
|
|
|
aFailc int
|
|
|
|
aSuccessc int
|
|
|
|
aaaaFailc int
|
|
|
|
aaaaSuccessc int
|
2024-01-06 01:41:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *resolverStatus) set(a any, s string) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Makes a DNS Status Grid
|
|
|
|
func NewResolverStatus(p *gui.Node, title string, server string, hostname string) *resolverStatus {
|
|
|
|
var rs *resolverStatus
|
|
|
|
rs = new(resolverStatus)
|
|
|
|
rs.parent = p
|
|
|
|
rs.group = p.NewGroup(server + " " + title + " lookup")
|
2024-01-10 20:22:25 -06:00
|
|
|
rs.group = rs.group.NewBox("bw vbox", false)
|
2024-01-06 01:41:33 -06:00
|
|
|
rs.grid = rs.group.NewGrid("LookupStatus", 5, 2)
|
|
|
|
|
|
|
|
rs.server = server
|
|
|
|
rs.hostname = hostname
|
|
|
|
|
|
|
|
rs.grid.NewLabel("")
|
|
|
|
rs.grid.NewLabel("UDP")
|
|
|
|
rs.grid.NewLabel("TCP")
|
|
|
|
rs.grid.NewLabel("Success")
|
|
|
|
rs.grid.NewLabel("Fail")
|
|
|
|
|
|
|
|
rs.grid.NewLabel("A")
|
|
|
|
rs.udpA = rs.grid.NewLabel("?")
|
|
|
|
rs.tcpA = rs.grid.NewLabel("?")
|
|
|
|
rs.aSuccess = rs.grid.NewLabel("?")
|
|
|
|
rs.aFail = rs.grid.NewLabel("?")
|
|
|
|
|
|
|
|
rs.grid.NewLabel("AAAA")
|
|
|
|
rs.udpAAAA = rs.grid.NewLabel("?")
|
|
|
|
rs.tcpAAAA = rs.grid.NewLabel("?")
|
|
|
|
rs.aaaaSuccess = rs.grid.NewLabel("?")
|
|
|
|
rs.aaaaFail = rs.grid.NewLabel("?")
|
|
|
|
|
|
|
|
rs.group.Margin()
|
|
|
|
rs.grid.Margin()
|
|
|
|
rs.group.Pad()
|
|
|
|
rs.grid.Pad()
|
|
|
|
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
// special thanks to the Element Hotel wifi in Philidelphia that allowed me to
|
|
|
|
// easily debug this code since the internet connection here blocks port 53 traffic
|
|
|
|
func (rs *resolverStatus) update() (bool, bool) {
|
|
|
|
var results []string
|
|
|
|
var a bool = false
|
|
|
|
var aaaa bool = false
|
|
|
|
|
|
|
|
log.Log(DNS, "resolverStatus.update() For server", rs.server, "on", rs.hostname)
|
|
|
|
results, _ = dnsUdpLookup(rs.server, rs.hostname, dns.TypeA)
|
|
|
|
log.Log(DNS, "resolverStatus.update() UDP type A =", results)
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if len(results) == 0 {
|
2024-01-06 01:41:33 -06:00
|
|
|
rs.set(rs.udpA, "BROKEN")
|
|
|
|
rs.aFailc += 1
|
|
|
|
} else {
|
|
|
|
rs.set(rs.udpA, "WORKING")
|
|
|
|
rs.aSuccessc += 1
|
|
|
|
a = true
|
|
|
|
}
|
|
|
|
|
|
|
|
results, _ = dnsTcpLookup(rs.server, rs.hostname, dns.TypeA)
|
|
|
|
log.Log(DNS, "resolverStatus.update() TCP type A =", results)
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if len(results) == 0 {
|
2024-01-06 01:41:33 -06:00
|
|
|
rs.set(rs.tcpA, "BROKEN")
|
|
|
|
rs.aFailc += 1
|
|
|
|
} else {
|
|
|
|
me.digStatus.set(rs.tcpA, "WORKING")
|
|
|
|
rs.aSuccessc += 1
|
|
|
|
a = true
|
|
|
|
}
|
|
|
|
|
|
|
|
me.digStatus.set(rs.aFail, strconv.Itoa(rs.aFailc))
|
2024-01-21 00:15:20 -06:00
|
|
|
me.digStatus.set(rs.aSuccess, strconv.Itoa(rs.aSuccessc))
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
results, _ = dnsUdpLookup(rs.server, rs.hostname, dns.TypeAAAA)
|
|
|
|
log.Log(DNS, "resolverStatus.update() UDP type AAAA =", results)
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if len(results) == 0 {
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.set(rs.udpAAAA, "BROKEN")
|
|
|
|
rs.aaaaFailc += 1
|
|
|
|
me.digStatus.set(rs.aaaaFail, strconv.Itoa(rs.aaaaFailc))
|
|
|
|
} else {
|
|
|
|
me.digStatus.set(rs.udpAAAA, "WORKING")
|
|
|
|
rs.aaaaSuccessc += 1
|
|
|
|
aaaa = true
|
|
|
|
}
|
|
|
|
|
|
|
|
results, _ = dnsTcpLookup(rs.server, rs.hostname, dns.TypeAAAA)
|
|
|
|
log.Log(DNS, "resolverStatus.update() UDP type AAAA =", results)
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if len(results) == 0 {
|
2024-01-06 01:41:33 -06:00
|
|
|
me.digStatus.set(rs.tcpAAAA, "BROKEN")
|
|
|
|
rs.aaaaFailc += 1
|
|
|
|
me.digStatus.set(rs.aaaaFail, strconv.Itoa(rs.aaaaFailc))
|
|
|
|
} else {
|
|
|
|
me.digStatus.set(rs.tcpAAAA, "WORKING")
|
|
|
|
rs.aaaaSuccessc += 1
|
|
|
|
aaaa = true
|
|
|
|
}
|
|
|
|
|
|
|
|
me.digStatus.set(rs.aaaaFail, strconv.Itoa(rs.aaaaFailc))
|
2024-01-21 00:15:20 -06:00
|
|
|
me.digStatus.set(rs.aaaaSuccess, strconv.Itoa(rs.aaaaSuccessc))
|
2024-01-06 01:41:33 -06:00
|
|
|
|
|
|
|
return a, aaaa
|
|
|
|
}
|
|
|
|
|
|
|
|
// dnsLookup performs a DNS lookup for the specified record type (e.g., "TXT", "AAAA") for a given domain.
|
|
|
|
func dnsUdpLookup(server string, domain string, recordType uint16) ([]string, error) {
|
|
|
|
var records []string
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(dns.Fqdn(domain), recordType)
|
|
|
|
r, _, err := c.Exchange(m, server) // If server = "1.1.1.1:53" then use Cloudflare's DNS server
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ans := range r.Answer {
|
|
|
|
records = append(records, ans.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dnsTcpLookup(server string, domain string, recordType uint16) ([]string, error) {
|
|
|
|
var records []string
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
2024-01-21 00:15:20 -06:00
|
|
|
c.Net = "tcp" // Specify to use TCP for the query
|
|
|
|
c.Timeout = time.Second * 5 // Set a 5-second timeout
|
2024-01-06 01:41:33 -06:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(dns.Fqdn(domain), recordType)
|
2024-01-21 00:15:20 -06:00
|
|
|
r, _, err := c.Exchange(m, server) // If server = "1.1.1.1:53" then use Cloudflare's DNS server
|
2024-01-06 01:41:33 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ans := range r.Answer {
|
|
|
|
records = append(records, ans.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|