summaryrefslogtreecommitdiff
path: root/hostnameStatus.go
blob: 127bc618258aad3d6172ca92c90cebaeced3e6fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* 
	figures out if your hostname is valid
	then checks if your DNS is setup correctly
*/

package main

import (
	"os"
	"fmt"
	"time"
	"reflect"
	"strings"
	"sort"
	"errors"

	"go.wit.com/log"
	"go.wit.com/gui/gui"
	"go.wit.com/gui/gadgets"
)

type hostnameStatus struct {
	ready		bool
	hidden		bool
	changed		bool

	lastname	string // used to watch for changes in the hostname

	window		*gadgets.BasicWindow

	// Primary Directives
	status		*gadgets.OneLiner
	summary		*gadgets.OneLiner
	statusIPv4	*gadgets.OneLiner
	statusIPv6	*gadgets.OneLiner

	hostname	*gadgets.OneLiner
	domainname	*gadgets.OneLiner

	// what the current IP addresses your network has given you
	currentIPv4	*gadgets.OneLiner
	currentIPv6	*gadgets.OneLiner

	// what the DNS servers have
	NSrr		*gadgets.OneLiner
	dnsA		*gadgets.OneLiner
	dnsAAAA		*gadgets.OneLiner
	dnsAPI		*gadgets.OneLiner
	APIprovider	string

	speed		*gadgets.OneLiner
	speedActual	*gadgets.OneLiner

	// Actions
//	dnsValue	*gui.Node
//	dnsAction	*gui.Node
}

func NewHostnameStatusWindow(p *gui.Node) *hostnameStatus {
	var hs *hostnameStatus
	hs = new(hostnameStatus)

	hs.ready = false
	hs.hidden = true
	// hs.hostname = me.hostname

	hs.window = gadgets.NewBasicWindow(p, "fix hostname here" + " Status")
	hs.window.Draw()
	hs.window.Hide()

	group := hs.window.Box().NewGroup("Summary")
	grid := group.NewGrid("LookupStatus", 2, 2)

	hs.status	= gadgets.NewOneLiner(grid, "status").Set("unknown")
	hs.statusIPv4	= gadgets.NewOneLiner(grid, "IPv4").Set("unknown")
	hs.statusIPv6	= gadgets.NewOneLiner(grid, "IPv6").Set("unknown")

	group.Pad()
	grid.Pad()

	group = hs.window.Box().NewGroup("Details")
	grid = group.NewGrid("LookupDetails", 2, 2)

	hs.hostname	= gadgets.NewOneLiner(grid, "hostname")
	hs.domainname	= gadgets.NewOneLiner(grid, "domain name")
	hs.currentIPv4	= gadgets.NewOneLiner(grid, "Current IPv4")
	hs.currentIPv6	= gadgets.NewOneLiner(grid, "Current IPv6")

	hs.NSrr		= gadgets.NewOneLiner(grid, "dns NS records").Set("unknown")
	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")

	hs.speed	= gadgets.NewOneLiner(grid, "speed").Set("unknown")
	hs.speedActual	= gadgets.NewOneLiner(grid, "actual").Set("unknown")

	group.Pad()
	grid.Pad()

	hs.hidden = false
	hs.ready = true
	return hs
}

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()
}

func (hs *hostnameStatus) Update() {
	log.Info("hostnameStatus() Update() START")
	if hs == nil {
		log.Error(errors.New("hostnameStatus() Update() hs == nil"))
		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}
	hs.statusIPv4.Set(s)
}

func (hs *hostnameStatus) setIPv6(s string) {
	if ! hs.Ready() {return}
	hs.statusIPv6.Set(s)
}

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
	}
	var ol *gadgets.OneLiner
	if reflect.TypeOf(a) == reflect.TypeOf(ol) {
		ol = a.(*gadgets.OneLiner)
		if ol == nil {
			// log.Println("ol = nil", reflect.TypeOf(a), "a =", a)
			return
		}
		// log.Println("SETTING ol:", ol)
		ol.Set(s)
		return
	}
	log.Warn("unknown type TypeOf(a) =", reflect.TypeOf(a), "a =", a)
	os.Exit(0)
}

// 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
}

func (hs *hostnameStatus) GetIPv6() []string {
	if ! hs.Ready() { return nil}
	return strings.Split(hs.dnsAAAA.Get(), "\n")
}

func (hs *hostnameStatus) updateStatus() {
	if ! hs.Ready() { return }
	var s string
	var vals []string
	log.Log(STATUS, "updateStatus() START")

	// copy the OS status over
	lasthostname := hs.hostname.Get()
	hostname := me.statusOS.GetHostname()

	// hostname changed or was setup for the first time. Set the window title, etc
	if lasthostname != hostname {
		me.changed = true
		hs.hostname.Set(hostname)
		hs.window.Title(hostname + " Status")
		me.statusDNSbutton.Set(hostname + " status")
	}
	hs.domainname.Set(me.statusOS.GetDomainName())

	var tmp []string
	tmp = me.statusOS.GetIPv4()
	sort.Strings(tmp)
	s = strings.Join(tmp, "\n")
	if s != hs.currentIPv4.Get() {
		log.Log(CHANGE, "DNS IPv4 Addresses changed", tmp)
		hs.currentIPv4.Set(s)
		hs.changed = true
	}

	tmp = me.statusOS.GetIPv6()
	sort.Strings(tmp)
	s = strings.Join(tmp, "\n")
	if s != hs.currentIPv6.Get() {
		log.Log(CHANGE, "DNS IPv6 Addresses changed", tmp)
		hs.currentIPv6.Set(s)
		hs.changed = true
	}

	if me.statusOS.ValidHostname() {
		vals = lookupDoH(me.statusOS.GetHostname(), "AAAA")

		log.Log(STATUS, "DNS IPv6 Addresses for ", me.statusOS.GetHostname(), "=", vals)
		hs.dnsAAAA.Set(strings.Join(vals, "\n"))

		vals = lookupDoH(me.statusOS.GetHostname(), "A")
		log.Log(STATUS, "IPv4 Addresses for ", me.statusOS.GetHostname(), "=", vals)
		s = strings.Join(vals, "\n")
		if (s == "") {
			s = "(none)"
			hs.setIPv4("NEEDS CNAME")
		}
		hs.set(hs.dnsA, s)

		vals = lookupDoH(me.statusOS.GetHostname(), "CNAME")
		s = strings.Join(vals, "\n")
		if (s != "") {
			hs.set(hs.dnsA, "CNAME " + s)
			hs.setIPv4("GOOD")
		}
		if hs.changed {
			log.Log(CHANGE, "stuff changed. trying fixIPv6dns()")
			fixIPv6dns()
			hs.changed = false
		}
	}

	if hs.IPv4() && hs.IPv6() {
		hs.status.Set("GOOD")
	} else {
		hs.status.Set("BROKEN")
	}
	
	last := hs.statusIPv6.Get()
	if hs.verifyIPv6() {
		if last != "WORKING" {
			log.Log(CHANGE, "Your DNS IPv6 has started working.", me.statusOS.GetHostname(), "should now work")
			hs.changed = true
			hs.statusIPv6.Set("WORKING")
			me.DnsStatus.SetText("WORKING")
		}
	} else {
		if last != "BROKEN" {
			log.Log(CHANGE, "Your DNS entries for IPv6 have BROKEN")
			hs.changed = true
			hs.statusIPv6.Set("BROKEN")
			me.DnsStatus.SetText("BROKEN")
		}
	}
}

func (hs *hostnameStatus) verifyIPv6() bool {
	var working bool = true
	osAAAA := make(map[string]string)
	dnsAAAA := make(map[string]string)

	log.Log(INFO, "What are the AAAA resource records in the OS?")
	tmp := me.statusOS.GetIPv6()
	if len(tmp) == 0 {
		// you don't have any IPv6 addresses in your OS right now
		return false
	}
	for _, aaaa := range me.statusOS.GetIPv6() {
		log.Log(INFO, "FOUND OS  AAAA ip", aaaa)
		osAAAA[aaaa] = "os"
	}

	log.Log(INFO, "What are the AAAA resource records in DNS?")
	for _, aaaa := range me.statusDNS.GetIPv6() {
		log.Log(INFO, "FOUND DNS AAAA ip", aaaa)
		dnsAAAA[aaaa] = "dns"
	}

	for aaaa, _ := range dnsAAAA {
		if osAAAA[aaaa] == "os" {
			log.Log(INFO, "DNS AAAA is in     OS", aaaa)
		} else {
			working = false
			log.Log(INFO, "DNS AAAA is not in OS", aaaa)
			addToFixWindow("DELETE", aaaa)
		}
	}

	for aaaa, _ := range osAAAA {
		if dnsAAAA[aaaa] == "dns" {
			log.Log(INFO, "OS  AAAA is in     DNS", aaaa)
		} else {
			working = false
			log.Log(INFO, "OS  AAAA is not in DNS", aaaa)
			addToFixWindow("CREATE", aaaa)
		}
	}

	return working
}

func (hs *hostnameStatus) Show() {
	log.Log(STATUS, "hostnameStatus.Show() window")
	if hs.hidden {
		hs.window.Show()
	}
	hs.hidden = false
}

func (hs *hostnameStatus) Hide() {
	log.Log(STATUS, "hostnameStatus.Hide() window")
	if ! hs.hidden {
		hs.window.Hide()
	}
	hs.hidden = true
}

func (hs *hostnameStatus) GetDNSapi() string {
	return hs.APIprovider
}

func (hs *hostnameStatus) SetDNSapi(api string) {
	if api == hs.APIprovider {
		return
	}
	hs.APIprovider = api
	hs.dnsAPI.Set(api)
	hs.changed = true
}