2024-01-21 00:15:20 -06:00
|
|
|
/*
|
2024-01-07 05:46:59 -06:00
|
|
|
Show a box for a configuration error
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-02 11:51:16 -06:00
|
|
|
"os"
|
2024-01-10 20:22:25 -06:00
|
|
|
"time"
|
|
|
|
|
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-04-15 05:06:14 -05:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-01-07 05:46:59 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type errorBox struct {
|
2024-01-21 00:15:20 -06:00
|
|
|
ready bool
|
|
|
|
hidden bool
|
2024-01-07 05:46:59 -06:00
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
parent *gui.Node
|
|
|
|
group *gui.Node
|
|
|
|
grid *gui.Node
|
2024-01-07 05:46:59 -06:00
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
l *gui.Node
|
|
|
|
b *gui.Node
|
2024-01-07 05:46:59 -06:00
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
fixes map[string]*anError
|
2024-01-07 10:38:17 -06:00
|
|
|
|
2024-01-07 05:46:59 -06:00
|
|
|
something *gadgets.OneLiner
|
|
|
|
}
|
|
|
|
|
2024-01-07 10:38:17 -06:00
|
|
|
type anError struct {
|
2024-01-21 00:15:20 -06:00
|
|
|
// kind ProblemType // what kind of error is it?
|
|
|
|
// action ActionType
|
|
|
|
// aaaa string
|
|
|
|
// status string
|
2024-01-07 10:38:17 -06:00
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
kindLabel *gui.Node
|
2024-01-08 22:34:08 -06:00
|
|
|
actionLabel *gui.Node
|
2024-01-21 00:15:20 -06:00
|
|
|
ipLabel *gui.Node
|
2024-01-07 10:38:17 -06:00
|
|
|
statusLabel *gui.Node
|
2024-01-21 00:15:20 -06:00
|
|
|
button *gui.Node
|
2024-01-08 22:34:08 -06:00
|
|
|
|
|
|
|
problem *Problem
|
2024-01-07 10:38:17 -06:00
|
|
|
}
|
|
|
|
|
2024-02-25 18:37:44 -06:00
|
|
|
func InitErrorBox(p *gui.Node, name string, ip string) *errorBox {
|
2024-01-07 05:46:59 -06:00
|
|
|
var eb *errorBox
|
|
|
|
eb = new(errorBox)
|
|
|
|
eb.parent = p
|
2024-01-07 10:38:17 -06:00
|
|
|
eb.group = p.NewGroup(name)
|
2024-01-08 22:34:08 -06:00
|
|
|
eb.grid = eb.group.NewGrid("stuff", 5, 1)
|
2024-01-07 05:46:59 -06:00
|
|
|
|
2024-01-07 10:38:17 -06:00
|
|
|
eb.grid.NewLabel("Type")
|
2024-01-08 22:34:08 -06:00
|
|
|
eb.grid.NewLabel("Action")
|
2024-01-07 10:38:17 -06:00
|
|
|
eb.grid.NewLabel("IP")
|
|
|
|
eb.grid.NewLabel("Status")
|
|
|
|
eb.grid.NewLabel("")
|
2024-01-07 05:46:59 -06:00
|
|
|
|
2024-01-07 10:38:17 -06:00
|
|
|
eb.fixes = make(map[string]*anError)
|
2024-01-08 11:05:40 -06:00
|
|
|
eb.ready = true
|
2024-01-07 05:46:59 -06:00
|
|
|
return eb
|
|
|
|
}
|
|
|
|
|
2024-01-08 11:55:56 -06:00
|
|
|
func (eb *errorBox) Show() {
|
2024-01-21 00:15:20 -06:00
|
|
|
if eb == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-08 11:55:56 -06:00
|
|
|
eb.hidden = false
|
|
|
|
eb.group.Show()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eb *errorBox) Hide() {
|
2024-01-21 00:15:20 -06:00
|
|
|
if eb == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-08 11:55:56 -06:00
|
|
|
eb.hidden = true
|
|
|
|
eb.group.Hide()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eb *errorBox) Toggle() {
|
2024-01-21 00:15:20 -06:00
|
|
|
if eb == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-08 11:55:56 -06:00
|
|
|
if eb.hidden {
|
|
|
|
eb.Show()
|
|
|
|
} else {
|
|
|
|
eb.Hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 11:05:40 -06:00
|
|
|
func (eb *errorBox) Ready() bool {
|
2024-01-21 00:15:20 -06:00
|
|
|
if eb == nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-08 11:05:40 -06:00
|
|
|
return eb.ready
|
|
|
|
}
|
2024-01-07 10:38:17 -06:00
|
|
|
|
2024-01-08 22:34:08 -06:00
|
|
|
func (eb *errorBox) addIPerror(kind ProblemType, action ActionType, ip string) bool {
|
2024-01-21 00:15:20 -06:00
|
|
|
if !eb.Ready() {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-08 22:34:08 -06:00
|
|
|
tmp := kind.String() + " " + ip
|
2024-01-07 10:38:17 -06:00
|
|
|
if eb.fixes[tmp] != nil {
|
|
|
|
log.Log(WARN, "Error is already here", kind, ip)
|
2024-01-10 20:22:25 -06:00
|
|
|
log.Log(WARN, "kind =", kind)
|
|
|
|
log.Log(WARN, "action =", action)
|
|
|
|
log.Log(WARN, "ip =", ip)
|
2024-01-07 10:38:17 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
anErr := new(anError)
|
|
|
|
|
2024-01-08 22:34:08 -06:00
|
|
|
anErr.kindLabel = eb.grid.NewLabel(kind.String())
|
|
|
|
anErr.actionLabel = eb.grid.NewLabel(action.String())
|
2024-01-07 10:38:17 -06:00
|
|
|
anErr.ipLabel = eb.grid.NewLabel(ip)
|
|
|
|
anErr.statusLabel = eb.grid.NewLabel("")
|
2024-01-08 22:34:08 -06:00
|
|
|
anErr.button = eb.grid.NewButton("Try to Fix", func() {
|
2024-01-07 10:38:17 -06:00
|
|
|
log.Log(WARN, "got", kind, "here. IP =", ip)
|
2024-01-07 11:23:51 -06:00
|
|
|
eb.fix(tmp)
|
2024-01-07 10:38:17 -06:00
|
|
|
})
|
2024-01-08 22:34:08 -06:00
|
|
|
anErr.problem = new(Problem)
|
|
|
|
anErr.problem.kind = kind
|
2024-01-10 20:22:25 -06:00
|
|
|
anErr.problem.action = action
|
2024-01-08 22:34:08 -06:00
|
|
|
anErr.problem.aaaa = ip
|
2024-01-10 20:22:25 -06:00
|
|
|
anErr.problem.born = time.Now()
|
|
|
|
anErr.problem.duration = 30 * time.Second
|
|
|
|
anErr.problem.begun = false
|
|
|
|
anErr.problem.begunResult = false
|
2024-01-07 10:38:17 -06:00
|
|
|
eb.fixes[tmp] = anErr
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-04-15 05:06:14 -05:00
|
|
|
func (e *anError) Age() string {
|
|
|
|
// Get the current time
|
|
|
|
currentTime := time.Now()
|
|
|
|
|
|
|
|
// Calculate the duration between t current time
|
|
|
|
duration := currentTime.Sub(e.problem.born)
|
|
|
|
|
|
|
|
age := shell.FormatDuration(duration)
|
|
|
|
log.Log(WARN, "Error Age() =", age)
|
|
|
|
|
|
|
|
return age
|
2024-01-08 12:55:23 -06:00
|
|
|
}
|
|
|
|
|
2024-04-15 05:06:14 -05:00
|
|
|
func (myErr *anError) Fix() bool {
|
2024-01-10 20:22:25 -06:00
|
|
|
log.Log(WARN, "should try to fix", myErr.problem.kind, "here. IP =", myErr.problem.aaaa)
|
2024-03-02 11:51:16 -06:00
|
|
|
if os.Getenv("DNS_AUTOCORRECT") != "true" {
|
|
|
|
log.Log(WARN, "not autofixing. $DNS_AUTOCORRECT != true")
|
2024-01-10 20:22:25 -06:00
|
|
|
log.Log(WARN, "problem.kind =", myErr.problem.kind)
|
|
|
|
log.Log(WARN, "problem.action =", myErr.problem.action)
|
|
|
|
log.Log(WARN, "problem.aaaa =", myErr.problem.aaaa)
|
|
|
|
log.Log(WARN, "problem.duration =", myErr.problem.duration)
|
|
|
|
log.Log(WARN, "problem.begun =", myErr.problem.begun)
|
|
|
|
log.Log(WARN, "problem.begunResult =", myErr.problem.begunResult)
|
|
|
|
// if myErr.problem.begunTime != nil {
|
2024-01-21 00:15:20 -06:00
|
|
|
log.Log(WARN, "problem.begunTime =", myErr.problem.begunTime)
|
2024-01-10 20:22:25 -06:00
|
|
|
// }
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if myErr.problem.begun {
|
|
|
|
log.Log(WARN, "problem has already begun. need to check the status of the problem here")
|
|
|
|
log.Log(WARN, "problem.begun =", myErr.problem.begun)
|
|
|
|
log.Log(WARN, "problem.begunResult =", myErr.problem.begunResult)
|
|
|
|
log.Log(WARN, "problem.duration =", myErr.problem.duration)
|
|
|
|
delay := time.Since(myErr.problem.begunTime)
|
|
|
|
log.Log(WARN, "problem duration time =", delay)
|
|
|
|
if delay >= myErr.problem.duration {
|
|
|
|
log.Log(WARN, "duration eclipsed. check the status of the error here")
|
2024-01-07 11:23:51 -06:00
|
|
|
}
|
2024-01-10 20:22:25 -06:00
|
|
|
return false
|
2024-01-07 11:23:51 -06:00
|
|
|
}
|
2024-01-10 20:22:25 -06:00
|
|
|
if myErr.problem.kind == RR {
|
|
|
|
if myErr.problem.action == DELETE {
|
|
|
|
myErr.problem.begun = true
|
|
|
|
myErr.problem.begunTime = time.Now()
|
|
|
|
if deleteFromDNS(myErr.problem.aaaa) {
|
|
|
|
log.Log(INFO, "Delete AAAA", myErr.problem.aaaa, "Worked")
|
|
|
|
myErr.problem.begunResult = true
|
|
|
|
} else {
|
|
|
|
log.Log(INFO, "Delete AAAA", myErr.problem.aaaa, "Failed")
|
|
|
|
myErr.problem.begunResult = false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if myErr.problem.action == CREATE {
|
|
|
|
myErr.problem.begun = true
|
|
|
|
myErr.problem.begunTime = time.Now()
|
|
|
|
if addToDNS(myErr.problem.aaaa) {
|
|
|
|
log.Log(INFO, "Delete AAAA", myErr.problem.aaaa, "Worked")
|
|
|
|
myErr.problem.begunResult = true
|
|
|
|
} else {
|
|
|
|
log.Log(INFO, "Delete AAAA", myErr.problem.aaaa, "Failed")
|
|
|
|
myErr.problem.begunResult = false
|
|
|
|
}
|
|
|
|
return true
|
2024-01-07 11:23:51 -06:00
|
|
|
}
|
|
|
|
}
|
2024-01-07 10:38:17 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-04-15 05:06:14 -05:00
|
|
|
// get all your problems!
|
|
|
|
func (eb *errorBox) Scan() []anError {
|
|
|
|
for s, thing := range eb.fixes {
|
|
|
|
log.Log(WARN, "Scan()", s, thing.actionLabel.String(), thing.ipLabel.String(), thing.Age())
|
|
|
|
if thing.problem.begun {
|
|
|
|
log.Log(WARN, "Scan()", "attempted to fix has happened")
|
|
|
|
} else {
|
|
|
|
log.Log(WARN, "Scan()", "attempted to fix not yet happened")
|
|
|
|
thing.Fix()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eb *errorBox) fix(key string) bool {
|
|
|
|
if eb.fixes[key] == nil {
|
|
|
|
log.Log(WARN, "Unknown error. could not find key =", key)
|
|
|
|
log.Log(WARN, "TODO: probably remove this error. key =", key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
myErr := eb.fixes[key]
|
|
|
|
return myErr.Fix()
|
|
|
|
}
|
|
|
|
|
2024-01-07 05:46:59 -06:00
|
|
|
func (eb *errorBox) update() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eb *errorBox) toggle() {
|
|
|
|
}
|