summaryrefslogtreecommitdiff
path: root/dnsLookupStatus.go
blob: e623cae4f1845472c5710bbcc7c3cef7bf38273b (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
/* 
	'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 (
	"log"
	"fmt"
	"time"
	"strconv"

	"github.com/miekg/dns"
	"go.wit.com/gui"
	"go.wit.com/control-panel-dns/cloudflare"
	"go.wit.com/shell"
)

type digStatus struct {
	ready		bool
	statusIPv4	string
	statusIPv6	string

	parent	*gui.Node
	window	*gui.Node
	group	*gui.Node
	grid	*gui.Node
	box	*gui.Node

	summary		*gui.Node
	status		*cloudflare.OneLiner
	statusAAAA	*cloudflare.OneLiner
	speed		*cloudflare.OneLiner
	speedActual	*cloudflare.OneLiner

	details		*gui.Node
	dsLocalhost	*dnsStatus
	dsLocalNetwork	*dnsStatus
	dsCloudflare	*dnsStatus
	dsGoogle	*dnsStatus
	DnsDigUDP	*gui.Node
	DnsDigTCP	*gui.Node
}

type dnsStatus struct {
	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"

	parent	*gui.Node
	group	*gui.Node
	grid	*gui.Node

	// DNS setup options
	udpA	*gui.Node
	tcpA	*gui.Node
	udpAAAA	*gui.Node
	tcpAAAA	*gui.Node

	// show the display
	aFail		*gui.Node
	aSuccess	*gui.Node
	aaaaFail	*gui.Node
	aaaaSuccess	*gui.Node

	// interger counters
	aFailc		int
	aSuccessc	int
	aaaaFailc	int
	aaaaSuccessc	int
}

func NewDigStatusWindow(p *gui.Node) *digStatus {
	var ds *digStatus
	ds = new(digStatus)

	ds.ready = false

	ds.window = p.NewWindow("DNS Lookup Status")
	ds.box = ds.window.NewBox("hBox", true)

	// summary of the current state of things
	ds.summary = ds.box.NewGroup("Summary")

	b := ds.summary.NewBox("hBox", true)
	ds.status = cloudflare.NewOneLiner(b, "status")
	ds.status.Set("unknown")

	b = ds.summary.NewBox("hBox", true)
	ds.statusAAAA = cloudflare.NewOneLiner(b, "IPv6 status")
	ds.statusAAAA.Set("unknown")

	b = ds.summary.NewBox("hBox", true)
	ds.speed = cloudflare.NewOneLiner(b, "speed")
	ds.speed.Set("unknown")

	b = ds.summary.NewBox("hBox", true)
	ds.speedActual = cloudflare.NewOneLiner(b, "actual")
	ds.speedActual.Set("unknown")

	// make the area to store the raw details
	ds.details = ds.box.NewGroup("Details")
	ds.dsLocalhost = NewDnsStatus(ds.details, "(localhost)", "127.0.0.1:53", "go.wit.com")
	ds.dsLocalNetwork = NewDnsStatus(ds.details, "(Local Network)", "172.22.0.1:53", "go.wit.com")
	ds.dsCloudflare = NewDnsStatus(ds.details, "(cloudflare)", "1.1.1.1:53", "go.wit.com")
	ds.dsGoogle = NewDnsStatus(ds.details, "(google)", "8.8.8.8:53", "go.wit.com")
	ds.makeDnsStatusGrid()

	return ds
}

func (ds *digStatus) Update() {
	duration := timeFunction(func () { ds.updateDnsStatus() })
	s := fmt.Sprint(duration)
	ds.speedActual.Set(s)

	if (duration > 500 * time.Millisecond ) {
		ds.speed.Set("SLOW")
	} else if (duration > 100 * time.Millisecond ) {
		ds.speed.Set("OK")
	} else {
		ds.speed.Set("FAST")
	}
}

// Returns true if the status is valid
func (ds *digStatus) Ready() bool {
	return ds.ready
}

// Returns true if IPv4 is working
func (ds *digStatus) IPv4() bool {
	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.statusIPv6 == "GOOD") {
		return true
	}
	return false
}

func (ds *digStatus) setIPv4(s string) {
	ds.status.Set(s)
	ds.statusIPv4 = s
}

func (ds *digStatus) setIPv6(s string) {
	ds.statusAAAA.Set(s)
	ds.statusIPv6 = s
}

func (ds *digStatus) updateDnsStatus() {
	var cmd, out string
	var ipv4, ipv6 bool

	ipv4, ipv6 = ds.dsLocalhost.Update()
	ipv4, ipv6 = ds.dsLocalNetwork.Update()
	ipv4, ipv6 = ds.dsCloudflare.Update()
	ipv4, ipv6 = ds.dsGoogle.Update()

	if (ipv4) {
		log.Println("updateDnsStatus() IPv4 A lookups working")
		ds.setIPv4("OK")
	} else {
		log.Println("updateDnsStatus() IPv4 A lookups not working. No internet?")
		ds.setIPv4("No Internet?")
	}
	if (ipv6) {
		log.Println("updateDnsStatus() IPv6 AAAA lookups working")
		ds.setIPv4("GOOD")
		ds.setIPv6("GOOD")
	} else {
		log.Println("updateDnsStatus() IPv6 AAAA lookups are not working")
		ds.setIPv6("Need VPN")
	}

	cmd = "dig +noall +answer www.wit.com A"
	out = shell.Run(cmd)
	log.Println("makeDnsStatusGrid() dig", out)
	ds.DnsDigUDP.SetText(out)

	cmd = "dig +noall +answer www.wit.com AAAA"
	out = shell.Run(cmd)
	log.Println("makeDnsStatusGrid() dig", out)
	ds.DnsDigTCP.SetText(out)

	ds.ready = true
}

// Makes a DNS Status Grid
func NewDnsStatus(p *gui.Node, title string, server string, hostname string) *dnsStatus {
	var ds *dnsStatus
	ds = new(dnsStatus)
	ds.parent = p
	ds.group = p.NewGroup(server + " " + title + " lookup")
	ds.grid = ds.group.NewGrid("LookupStatus", 5, 2)

	ds.server = server
	ds.hostname = hostname

	ds.grid.NewLabel("")
	ds.grid.NewLabel("UDP")
	ds.grid.NewLabel("TCP")
	ds.grid.NewLabel("Success")
	ds.grid.NewLabel("Fail")

	ds.grid.NewLabel("A")
	ds.udpA = ds.grid.NewLabel("?")
	ds.tcpA = ds.grid.NewLabel("?")
	ds.aSuccess = ds.grid.NewLabel("?")
	ds.aFail = ds.grid.NewLabel("?")

	ds.grid.NewLabel("AAAA")
	ds.udpAAAA = ds.grid.NewLabel("?")
	ds.tcpAAAA = ds.grid.NewLabel("?")
	ds.aaaaSuccess = ds.grid.NewLabel("?")
	ds.aaaaFail = ds.grid.NewLabel("?")

	ds.group.Margin()
	ds.grid.Margin()
	ds.group.Pad()
	ds.grid.Pad()

	return ds
}

// 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 (ds *dnsStatus) Update() (bool, bool) {
	var results []string
	var a bool = false
	var aaaa bool = false

	log.Println("dnsStatus.Update() For server", ds.server, "on", ds.hostname)
	results, _ = dnsUdpLookup(ds.server, ds.hostname, dns.TypeA)
	log.Println("dnsStatus.Update() UDP type A =", results)

	if (len(results) == 0) {
		ds.udpA.SetText("BROKEN")
		ds.aFailc += 1
	} else {
		ds.udpA.SetText("WORKING")
		ds.aSuccessc += 1
		a = true
	}

	results, _ = dnsTcpLookup(ds.server, ds.hostname, dns.TypeA)
	log.Println("dnsStatus.Update() TCP type A =", results)

	if (len(results) == 0) {
		ds.tcpA.SetText("BROKEN")
		ds.aFailc += 1
	} else {
		ds.tcpA.SetText("WORKING")
		ds.aSuccessc += 1
		a = true
	}

	ds.aFail.SetText(strconv.Itoa(ds.aFailc))
	ds.aSuccess.SetText(strconv.Itoa(ds.aSuccessc))

	results, _ = dnsUdpLookup(ds.server, ds.hostname, dns.TypeAAAA)
	log.Println("dnsStatus.Update() UDP type AAAA =", results)

	if (len(results) == 0) {
		ds.udpAAAA.SetText("BROKEN")
		ds.aaaaFailc += 1
		ds.aaaaFail.SetText(strconv.Itoa(ds.aaaaFailc))
	} else {
		ds.udpAAAA.SetText("WORKING")
		ds.aaaaSuccessc += 1
		aaaa = true
	}

	results, _ = dnsTcpLookup(ds.server, ds.hostname, dns.TypeAAAA)
	log.Println("dnsStatus.Update() UDP type AAAA =", results)

	if (len(results) == 0) {
		ds.tcpAAAA.SetText("BROKEN")
		ds.aaaaFailc += 1
		ds.aaaaFail.SetText(strconv.Itoa(ds.aaaaFailc))
	} else {
		ds.tcpAAAA.SetText("WORKING")
		ds.aaaaSuccessc += 1
		aaaa = true
	}

	ds.aaaaFail.SetText(strconv.Itoa(ds.aaaaFailc))
	ds.aaaaSuccess.SetText(strconv.Itoa(ds.aaaaSuccessc))

	return a, aaaa
}

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.Run(cmd)
	log.Println("makeDnsStatusGrid() dig", out)
	ds.DnsDigUDP.SetText(out)

	cmd = "dig +noall +answer go.wit.com AAAA"
	grid.NewLabel(cmd)
	ds.DnsDigTCP = grid.NewLabel("?")
	out = shell.Run(cmd)
	log.Println("makeDnsStatusGrid() dig", out)
	ds.DnsDigTCP.SetText(out)

	group.Pad()
	grid.Pad()
}

// 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)
	c.Net = "tcp" // Specify to use TCP for the query
	c.Timeout = time.Second * 5  // Set a 5-second timeout
	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
}