summaryrefslogtreecommitdiff
path: root/digStatusWindow.go
blob: a0917f5e3ab797c3e57f44c89f75a280bebe2c6a (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
/*
	'dig'

	This is essentially doing what the command 'dig' does
	It performing DNS queries on TCP and UDP
	against localhost, cloudflare & google

	IPv4() and IPv6() return true if they are working

	with the 'gui' package, it can also display the results
*/

package main

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

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

type digStatus struct {
	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
}

func NewDigStatusWindow() *digStatus {
	var ds *digStatus
	ds = new(digStatus)

	ds.ready = false
	ds.hidden = true

	ds.window = gadgets.RawBasicWindow("DNS Resolver Status")
	ds.window.Make()
	// ds.window.Draw()
	// ds.window.Hide()

	// summary of the current state of things
	ds.summary = ds.window.Box().NewGroup("Summary")
	g := ds.summary.NewGrid("LookupStatus", 2, 2)
	g.Pad()

	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")

	// make the area to store the raw details
	ds.detailsGroup = ds.window.Box().NewGroup("Details")
	ds.details = ds.detailsGroup.NewBox("bw vbox", false)
	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")
	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
	}
	duration := timeFunction(func() {
		ds.updateDnsStatus()
	})
	s := fmt.Sprint(duration)
	// ds.speedActual.SetText(s)
	me.digStatus.set(ds.speedActual, s)

	if duration > 500*time.Millisecond {
		me.digStatus.set(ds.speed, "SLOW")
	} else if duration > 100*time.Millisecond {
		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 {
	if ds == nil {
		return false
	}
	return ds.ready
}

// Returns true if IPv4 is working
func (ds *digStatus) IPv4() bool {
	if !ds.Ready() {
		return false
	}
	if ds.statusIPv4 == "OK" {
		return true
	}
	if ds.statusIPv4 == "GOOD" {
		return true
	}
	return false
}

// Returns true if IPv6 is working
func (ds *digStatus) IPv6() bool {
	if !ds.Ready() {
		return false
	}
	if ds.statusIPv6 == "GOOD" {
		return true
	}
	return false
}

func (ds *digStatus) setIPv4status(s string) {
	ds.statusIPv4 = s
	if !ds.Ready() {
		return
	}
	me.digStatus.set(ds.status, s)
}

func (ds *digStatus) setIPv6status(s string) {
	ds.statusIPv6 = s
	if !ds.Ready() {
		return
	}
	me.digStatus.set(ds.statusAAAA, s)
}

/*
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)
	me.DnsAAAA.SetText(s)
	// me.digStatus.set(ds.httpGoWitCom, addr)
}
*/

func (ds *digStatus) set(a any, s string) {
	if !ds.Ready() {
		return
	}
	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)
		ol.SetText(s)
		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")
	if ds == nil {
		log.Error(errors.New("updateDnsStatus() not initialized yet. ds == nil"))
		return
	}

	if !ds.ready {
		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()

	if me.statusOS.ValidHostname() {
		if ds.checkLookupDoH(me.statusOS.GetHostname()) {
			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")
	}

	if ipv4 {
		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?")
	}
	if ipv6 {
		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"
	out = shell.RunCapture(cmd)
	log.Log(DNS, "makeDnsStatusGrid() dig", out)
	me.digStatus.set(ds.DnsDigUDP, out)

	cmd = "dig +noall +answer www.wit.com AAAA"
	out = shell.RunCapture(cmd)
	log.Log(DNS, "makeDnsStatusGrid() dig", out)
	me.digStatus.set(ds.DnsDigTCP, out)

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

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("?")
	out = shell.RunCapture(cmd)
	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("?")
	out = shell.RunCapture(cmd)
	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
	}
	// me.digStatus.SetIPv6(strings.Join(s, "\n"))
	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")
	if !me.digStatus.hidden {
		me.digStatus.window.Hide()
	}
	me.digStatus.hidden = true
}

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