2023-12-29 01:36:10 -06:00
|
|
|
/*
|
|
|
|
figures out if your hostname is valid
|
|
|
|
then checks if your DNS is setup correctly
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2024-01-03 12:40:31 -06:00
|
|
|
"errors"
|
2023-12-29 01:36:10 -06:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
2024-01-03 12:40:31 -06:00
|
|
|
"go.wit.com/gui/gui"
|
2023-12-29 02:10:25 -06:00
|
|
|
"go.wit.com/gui/gadgets"
|
2023-12-29 01:36:10 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type hostnameStatus struct {
|
|
|
|
ready bool
|
|
|
|
hidden bool
|
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
// hostname string // my hostname. Example: "test.wit.com"
|
|
|
|
lastname string // used to watch for changes in the hostname
|
2023-12-29 01:36:10 -06:00
|
|
|
|
2024-01-05 00:42:15 -06:00
|
|
|
window *gadgets.BasicWindow
|
2023-12-29 01:36:10 -06:00
|
|
|
|
2023-12-29 21:14:36 -06:00
|
|
|
// Primary Directives
|
2023-12-29 02:10:25 -06:00
|
|
|
status *gadgets.OneLiner
|
|
|
|
summary *gadgets.OneLiner
|
|
|
|
statusIPv4 *gadgets.OneLiner
|
|
|
|
statusIPv6 *gadgets.OneLiner
|
2023-12-29 01:36:10 -06:00
|
|
|
|
|
|
|
// Details Group
|
2023-12-29 21:14:36 -06:00
|
|
|
hostShort *gadgets.OneLiner
|
|
|
|
domainname *gadgets.OneLiner
|
|
|
|
|
|
|
|
// what the current IP address your network has given you
|
2023-12-29 02:10:25 -06:00
|
|
|
currentIPv4 *gadgets.OneLiner
|
|
|
|
currentIPv6 *gadgets.OneLiner
|
2023-12-29 21:14:36 -06:00
|
|
|
|
|
|
|
// what the DNS servers have
|
|
|
|
dnsA *gadgets.OneLiner
|
|
|
|
dnsAAAA *gadgets.OneLiner
|
|
|
|
dnsAPI *gadgets.OneLiner
|
|
|
|
|
|
|
|
speed *gadgets.OneLiner
|
|
|
|
speedActual *gadgets.OneLiner
|
|
|
|
|
|
|
|
// Actions
|
2024-01-06 02:48:46 -06:00
|
|
|
// dnsValue *gui.Node
|
|
|
|
// dnsAction *gui.Node
|
2023-12-29 01:36:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHostnameStatusWindow(p *gui.Node) *hostnameStatus {
|
|
|
|
var hs *hostnameStatus
|
|
|
|
hs = new(hostnameStatus)
|
|
|
|
|
|
|
|
hs.ready = false
|
|
|
|
hs.hidden = true
|
2024-01-06 01:41:33 -06:00
|
|
|
// hs.hostname = me.hostname
|
2023-12-29 01:36:10 -06:00
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
hs.window = gadgets.NewBasicWindow(p, "fix hostname here" + " Status")
|
2024-01-05 14:49:42 -06:00
|
|
|
hs.window.Draw()
|
2024-01-05 00:42:15 -06:00
|
|
|
hs.window.Hide()
|
|
|
|
|
|
|
|
group := hs.window.Box().NewGroup("Summary")
|
2023-12-29 01:36:10 -06:00
|
|
|
grid := group.NewGrid("LookupStatus", 2, 2)
|
|
|
|
|
2023-12-29 02:10:25 -06:00
|
|
|
hs.status = gadgets.NewOneLiner(grid, "status").Set("unknown")
|
|
|
|
hs.statusIPv4 = gadgets.NewOneLiner(grid, "IPv4").Set("unknown")
|
|
|
|
hs.statusIPv6 = gadgets.NewOneLiner(grid, "IPv6").Set("unknown")
|
2023-12-29 01:36:10 -06:00
|
|
|
|
|
|
|
group.Pad()
|
|
|
|
grid.Pad()
|
|
|
|
|
2024-01-05 00:42:15 -06:00
|
|
|
group = hs.window.Box().NewGroup("Details")
|
2023-12-29 01:36:10 -06:00
|
|
|
grid = group.NewGrid("LookupDetails", 2, 2)
|
|
|
|
|
2023-12-29 21:14:36 -06:00
|
|
|
hs.hostShort = gadgets.NewOneLiner(grid, "hostname -s")
|
|
|
|
hs.domainname = gadgets.NewOneLiner(grid, "domain name")
|
2023-12-29 02:10:25 -06:00
|
|
|
hs.currentIPv4 = gadgets.NewOneLiner(grid, "Current IPv4")
|
|
|
|
hs.currentIPv6 = gadgets.NewOneLiner(grid, "Current IPv6")
|
2023-12-29 01:36:10 -06:00
|
|
|
|
2023-12-29 02:10:25 -06:00
|
|
|
hs.dnsAPI = gadgets.NewOneLiner(grid, "dns API provider").Set("unknown")
|
|
|
|
hs.dnsA = gadgets.NewOneLiner(grid, "dns IPv4 resource records").Set("unknown")
|
|
|
|
hs.dnsAAAA = gadgets.NewOneLiner(grid, "dns IPv6 resource records").Set("unknown")
|
2023-12-29 21:14:36 -06:00
|
|
|
|
2023-12-29 02:10:25 -06:00
|
|
|
hs.speed = gadgets.NewOneLiner(grid, "speed").Set("unknown")
|
|
|
|
hs.speedActual = gadgets.NewOneLiner(grid, "actual").Set("unknown")
|
2023-12-29 01:36:10 -06:00
|
|
|
|
|
|
|
group.Pad()
|
|
|
|
grid.Pad()
|
|
|
|
|
2024-01-06 02:48:46 -06:00
|
|
|
/*
|
2024-01-05 00:42:15 -06:00
|
|
|
group = hs.window.Box().NewGroup("Actions")
|
2023-12-29 21:14:36 -06:00
|
|
|
grid = group.NewGrid("LookupDetails", 2, 2)
|
|
|
|
|
|
|
|
hs.dnsValue = grid.NewLabel("3.4.5.6")
|
|
|
|
hs.dnsAction = grid.NewButton("CHECK", func () {
|
2024-01-05 01:07:34 -06:00
|
|
|
log.Warn("should", hs.dnsAction.S, "here for", hs.dnsValue.S)
|
2023-12-29 21:14:36 -06:00
|
|
|
if (hs.dnsAction.S == "DELETE") {
|
|
|
|
hs.deleteDNSrecord(hs.dnsValue.S)
|
|
|
|
}
|
|
|
|
if (hs.dnsAction.S == "CREATE") {
|
|
|
|
hs.createDNSrecord(hs.dnsValue.S)
|
|
|
|
}
|
|
|
|
})
|
2024-01-06 02:48:46 -06:00
|
|
|
*/
|
2023-12-29 21:14:36 -06:00
|
|
|
|
|
|
|
group.Pad()
|
|
|
|
grid.Pad()
|
|
|
|
|
2023-12-29 01:36:10 -06:00
|
|
|
hs.hidden = false
|
|
|
|
hs.ready = true
|
|
|
|
return hs
|
|
|
|
}
|
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
func (hs *hostnameStatus) ValidHostname() bool {
|
|
|
|
return goodHostname()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) GetHostname() string {
|
|
|
|
return hs.lastname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) SetHostname(hostname string) {
|
|
|
|
if hostname == hs.lastname {return}
|
|
|
|
log.Log(CHANGE, "the hostname is changing from", hs.lastname, "to", hostname)
|
|
|
|
hs.lastname = hostname
|
|
|
|
me.changed = true
|
|
|
|
|
|
|
|
if (me.fqdn != nil) {
|
|
|
|
me.fqdn.SetText(hostname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 21:14:36 -06:00
|
|
|
func (hs *hostnameStatus) Domain() string {
|
|
|
|
if ! hs.Ready() {return ""}
|
|
|
|
return hs.domainname.Get()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) API() string {
|
|
|
|
if ! hs.Ready() {return ""}
|
|
|
|
return hs.dnsAPI.Get()
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:48:46 -06:00
|
|
|
/*
|
2023-12-29 21:14:36 -06:00
|
|
|
func (hs *hostnameStatus) deleteDNSrecord(value string) bool {
|
|
|
|
log.Info("deleteDNSrecord() START for", value)
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Info("deleteDNSrecord() hostname =", me.status.GetHostname())
|
2023-12-29 21:14:36 -06:00
|
|
|
log.Info("deleteDNSrecord() domain =", hs.Domain())
|
|
|
|
log.Info("deleteDNSrecord() DNS API Provider =", hs.API())
|
|
|
|
|
|
|
|
if (hs.API() == "cloudflare") {
|
|
|
|
log.Info("deleteDNSrecord() Try to delete via cloudflare")
|
2024-01-06 01:41:33 -06:00
|
|
|
return cloudflare.Delete(hs.Domain(), me.status.GetHostname(), value)
|
2023-12-29 21:14:36 -06:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) createDNSrecord(value string) bool {
|
|
|
|
log.Info("createDNSrecord() START for", value)
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Info("createDNSrecord() hostname =", me.status.GetHostname())
|
2023-12-29 21:14:36 -06:00
|
|
|
log.Info("createDNSrecord() domain =", hs.Domain())
|
|
|
|
log.Info("createDNSrecord() DNS API Provider =", hs.API())
|
|
|
|
|
|
|
|
if (hs.API() == "cloudflare") {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Warn("createDNSrecord() Try to create via cloudflare:", me.status.GetHostname(), value)
|
|
|
|
return cloudflare.Create(hs.Domain(), me.status.GetHostname(), value)
|
2023-12-29 21:14:36 -06:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2024-01-06 02:48:46 -06:00
|
|
|
*/
|
2023-12-29 21:14:36 -06:00
|
|
|
|
2023-12-29 01:36:10 -06:00
|
|
|
func (hs *hostnameStatus) Update() {
|
|
|
|
log.Info("hostnameStatus() Update() START")
|
|
|
|
if hs == nil {
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Error(errors.New("hostnameStatus() Update() hs == nil"))
|
2023-12-29 01:36:10 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
duration := timeFunction(func () {
|
|
|
|
hs.updateStatus()
|
|
|
|
})
|
|
|
|
s := fmt.Sprint(duration)
|
|
|
|
hs.set(hs.speedActual, s)
|
|
|
|
|
|
|
|
if (duration > 500 * time.Millisecond ) {
|
|
|
|
hs.set(hs.speed, "SLOW")
|
|
|
|
} else if (duration > 100 * time.Millisecond ) {
|
|
|
|
hs.set(hs.speed, "OK")
|
|
|
|
} else {
|
|
|
|
hs.set(hs.speed, "FAST")
|
|
|
|
}
|
|
|
|
log.Info("hostnameStatus() Update() END")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the status is valid
|
|
|
|
func (hs *hostnameStatus) Ready() bool {
|
|
|
|
if hs == nil {return false}
|
|
|
|
return hs.ready
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if IPv4 is working
|
|
|
|
func (hs *hostnameStatus) IPv4() bool {
|
|
|
|
if ! hs.Ready() {return false}
|
|
|
|
if (hs.statusIPv4.Get() == "OK") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (hs.statusIPv4.Get() == "GOOD") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if IPv6 is working
|
|
|
|
func (hs *hostnameStatus) IPv6() bool {
|
|
|
|
if ! hs.Ready() {return false}
|
|
|
|
if (hs.statusIPv6.Get() == "GOOD") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) setIPv4(s string) {
|
|
|
|
if ! hs.Ready() {return}
|
2024-01-05 15:42:46 -06:00
|
|
|
hs.statusIPv4.Set(s)
|
2023-12-29 01:36:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) setIPv6(s string) {
|
|
|
|
if ! hs.Ready() {return}
|
2024-01-05 15:42:46 -06:00
|
|
|
hs.statusIPv6.Set(s)
|
2023-12-29 01:36:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) set(a any, s string) {
|
|
|
|
if ! hs.Ready() {return}
|
|
|
|
if hs.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
|
|
|
|
}
|
2023-12-29 02:10:25 -06:00
|
|
|
var ol *gadgets.OneLiner
|
2023-12-29 01:36:10 -06:00
|
|
|
if reflect.TypeOf(a) == reflect.TypeOf(ol) {
|
2023-12-29 02:10:25 -06:00
|
|
|
ol = a.(*gadgets.OneLiner)
|
2023-12-29 01:36:10 -06:00
|
|
|
if ol == nil {
|
2024-01-06 01:41:33 -06:00
|
|
|
// log.Println("ol = nil", reflect.TypeOf(a), "a =", a)
|
2023-12-29 01:36:10 -06:00
|
|
|
return
|
|
|
|
}
|
2024-01-06 01:41:33 -06:00
|
|
|
// log.Println("SETTING ol:", ol)
|
2023-12-29 01:36:10 -06:00
|
|
|
ol.Set(s)
|
|
|
|
return
|
|
|
|
}
|
2024-01-03 12:40:31 -06:00
|
|
|
log.Warn("unknown type TypeOf(a) =", reflect.TypeOf(a), "a =", a)
|
2023-12-29 01:36:10 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
// returns true if AAAA record already exists in DNS
|
|
|
|
func (hs *hostnameStatus) existsAAAA(s string) bool {
|
|
|
|
log.Log(NOW, "existsAAAA() try to see if AAAA is already set", s)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:48:46 -06:00
|
|
|
/*
|
2023-12-29 21:14:36 -06:00
|
|
|
// figure out if I'm missing any IPv6 address in DNS
|
|
|
|
func (hs *hostnameStatus) missingAAAA() bool {
|
|
|
|
var aaaa []string
|
|
|
|
aaaa = dhcpAAAA()
|
|
|
|
for _, s := range aaaa {
|
2024-01-03 19:33:13 -06:00
|
|
|
log.Log(NET, "my actual AAAA = ",s)
|
2024-01-06 01:41:33 -06:00
|
|
|
if hs.existsAAAA(s) {
|
|
|
|
log.Log(NOW, "my actual AAAA already exists in DNS =",s)
|
|
|
|
} else {
|
|
|
|
log.Log(NOW, "my actual AAAA is missing from DNS",s)
|
|
|
|
hs.dnsValue.SetText(s)
|
|
|
|
hs.dnsAction.SetText("CREATE")
|
|
|
|
return true
|
|
|
|
}
|
2023-12-29 21:14:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2024-01-06 02:48:46 -06:00
|
|
|
*/
|
2023-12-29 21:14:36 -06:00
|
|
|
|
2023-12-29 01:36:10 -06:00
|
|
|
func (hs *hostnameStatus) updateStatus() {
|
2024-01-06 01:41:33 -06:00
|
|
|
if ! hs.Ready() { return }
|
2023-12-29 01:36:10 -06:00
|
|
|
var s string
|
|
|
|
var vals []string
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(STATUS, "updateStatus() START")
|
2023-12-29 01:36:10 -06:00
|
|
|
|
2023-12-29 21:14:36 -06:00
|
|
|
hs.hostShort.Set(me.hostshort.S)
|
2024-01-06 05:24:11 -06:00
|
|
|
hs.domainname.Set(me.statusOS.GetDomainName())
|
2023-12-29 21:14:36 -06:00
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
if hs.ValidHostname() {
|
|
|
|
vals = lookupDoH(hs.GetHostname(), "AAAA")
|
|
|
|
|
|
|
|
log.Log(STATUS, "DNS IPv6 Addresses for ", hs.GetHostname(), "=", vals)
|
|
|
|
if len(vals) == 0 {
|
|
|
|
s = "(none)"
|
|
|
|
} else {
|
|
|
|
hs.setIPv6("Check for real IPv6 addresses here")
|
2024-01-06 02:48:46 -06:00
|
|
|
/*
|
2024-01-06 01:41:33 -06:00
|
|
|
if hs.missingAAAA() {
|
|
|
|
hs.setIPv6("Add the missing IPv6 address")
|
|
|
|
}
|
2024-01-06 02:48:46 -06:00
|
|
|
*/
|
2024-01-06 01:41:33 -06:00
|
|
|
for _, addr := range vals {
|
|
|
|
log.Log(STATUS, addr)
|
|
|
|
s += addr + " (DELETE)" + "\n"
|
|
|
|
hs.setIPv6("NEEDS DELETE")
|
2024-01-06 02:48:46 -06:00
|
|
|
// hs.dnsValue.SetText(addr)
|
|
|
|
// hs.dnsAction.SetText("DELETE")
|
2024-01-06 01:41:33 -06:00
|
|
|
}
|
2023-12-29 21:14:36 -06:00
|
|
|
}
|
2024-01-06 01:41:33 -06:00
|
|
|
hs.set(hs.dnsAAAA, s)
|
|
|
|
|
|
|
|
vals = lookupDoH(hs.GetHostname(), "A")
|
|
|
|
log.Log(STATUS, "IPv4 Addresses for ", hs.GetHostname(), "=", vals)
|
|
|
|
s = strings.Join(vals, "\n")
|
|
|
|
if (s == "") {
|
|
|
|
s = "(none)"
|
|
|
|
hs.setIPv4("NEEDS CNAME")
|
2023-12-29 01:36:10 -06:00
|
|
|
}
|
2024-01-06 01:41:33 -06:00
|
|
|
hs.set(hs.dnsA, s)
|
2023-12-29 01:36:10 -06:00
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
vals = lookupDoH(hs.GetHostname(), "CNAME")
|
|
|
|
s = strings.Join(vals, "\n")
|
|
|
|
if (s != "") {
|
|
|
|
hs.set(hs.dnsA, "CNAME " + s)
|
|
|
|
hs.setIPv4("GOOD")
|
|
|
|
}
|
2023-12-29 01:36:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
hs.currentIPv4.Set(me.IPv4.S)
|
|
|
|
hs.currentIPv6.Set(me.IPv6.S)
|
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
if hs.IPv4() && hs.IPv6() {
|
2023-12-29 01:36:10 -06:00
|
|
|
hs.status.Set("GOOD")
|
|
|
|
} else {
|
|
|
|
hs.status.Set("BROKEN")
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.dnsAPI.Set(me.DnsAPI.S)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) Show() {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(STATUS, "hostnameStatus.Show() window")
|
2023-12-29 01:36:10 -06:00
|
|
|
if hs.hidden {
|
|
|
|
hs.window.Show()
|
|
|
|
}
|
|
|
|
hs.hidden = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hostnameStatus) Hide() {
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(STATUS, "hostnameStatus.Hide() window")
|
2023-12-29 01:36:10 -06:00
|
|
|
if ! hs.hidden {
|
|
|
|
hs.window.Hide()
|
|
|
|
}
|
|
|
|
hs.hidden = true
|
|
|
|
}
|