summaryrefslogtreecommitdiff
path: root/main.go
blob: b9f039fedfb9dda93e0eec5824ce630294dd7a21 (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
// GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
// Copyright (c) 2023 WIT.COM, Inc.
// This is a control panel for DNS

package main

import 	(
	"fmt"
	// "runtime"
	"time"
	"embed"

	"go.wit.com/log"
	"go.wit.com/gui/gui"
	"go.wit.com/gui/debugger"

	"go.wit.com/gui/lib/linuxstatus"

	"github.com/miekg/dns"
)

//go:embed plugins/*
var resToolkit embed.FS

func main() {
	// parsedown()

	// initialize the maps to track IP addresses and network interfaces
	me.ipmap = make(map[string]*IPtype)
	me.dnsmap = make(map[string]*IPtype)
	me.ifmap = make(map[int]*IFtype)
	me.nsmap = make(map[string]string)

	// initialize maps for the returned DNS records
	me.ipv4s = make(map[string]dns.RR)
	me.ipv6s = make(map[string]dns.RR)

	if args.TmpLog {
		// send all log() output to a file in /tmp
		log.SetTmp()
	}

	me.myGui = gui.New()
	me.myGui.InitEmbed(resToolkit)
	me.myGui.Default()

	log.Sleep(me.artificialSleep)
	setupControlPanelWindow()

	me.digStatus = NewDigStatusWindow(me.myGui)
	me.statusDNS = NewHostnameStatusWindow(me.myGui)

	me.statusOS = linuxstatus.New()
	me.statusOS.SetParent(me.myGui)
	me.statusOS.InitWindow()
	me.statusOS.Make()
	me.statusOS.Draw()
	me.statusOS.Draw2()

	if debugger.ArgDebug() {
		go func() {
			log.Sleep(2)
			debugger.DebugWindow(me.myGui)
		}()
	}

	log.Sleep(me.artificialSleep)

	// TCP & UDP port 53 lookups + DNS over HTTP lookups + os.Exec(dig)
	go myTicker(60 * time.Second, "DNSloop", func() {
		me.digStatus.Update()

		if me.digStatus.Ready() {
			current := me.statusIPv6.Get()
			if me.digStatus.IPv6() {
				if current != "WORKING" {
					log.Log(CHANGE, "IPv6 resolution is WORKING")
					me.statusIPv6.Set("WORKING")
				}
			} else {
				if current != "Need VPN" {
					log.Log(CHANGE, "IPv6 resolution seems to have broken")
					me.statusIPv6.Set("Need VPN")
				}
			}
		}
	})

	// checks if your DNS records are still broken
	// if everything is working, then it just ignores
	// things until the timeout happens

	lastProvider := "unknown"
	go myTicker(10 * time.Second, "DNSloop", func() {
		log.Log(INFO, "me.statusDNS.Update() START")
		me.statusDNS.Update()

		provider := me.statusDNS.GetDNSapi()
		if provider != lastProvider {
			log.Log(CHANGE, "Your DNS API provider appears to have changed to", provider)
			lastProvider = provider
			me.apiButton.SetText(provider + " wit.com")
		}
		if provider == "cloudflare" {
			me.DnsAPIstatus.Set("WORKING")
		}
	})

	// probes the OS network settings
	go myTicker(500 * time.Millisecond, "me.statusOS,Update()", func() {
		duration := timeFunction( func() {
			me.statusOS.Update()

			if me.statusOS.ValidHostname() {
				if me.hostnameStatus.GetText() != "WORKING" {
					me.hostnameStatus.Set("WORKING")
					me.changed = true
				}
			}

			// re-check DNS API provider
			if (me.statusOS.Changed()) {
				// lookup the NS records for your domain
				// if your host is test.wit.com, find the NS resource records for wit.com
				lookupNS(me.statusOS.GetDomainName())
				log.Log(CHANGE, "lookupNS() RUNNING NOW. this will determine your DNS API provider")
			}
			if me.statusDNS.GetDNSapi() == "unknown" {
				lookupNS(me.statusOS.GetDomainName())
				log.Log(CHANGE, "lookupNS() RUNNING NOW. this will determine your DNS API provider")
			}
		})
		s := fmt.Sprint(duration)
		me.statusOS.SetSpeedActual(s)
	})

	// check the four known things to see if they are all WORKING
	myTicker(10 * time.Second, "MAIN LOOP", func() {
		var working bool = true
		if me.hostnameStatus.GetText() != "WORKING" {
			log.Log(CHANGE, "The hostname is not WORKING yet", me.hostnameStatus.GetText())
			working = false
		}
		if me.statusIPv6.Get() != "WORKING" {
			log.Log(CHANGE, "IPv6 DNS lookup has not been confirmed yet", me.statusIPv6.Get())
			working = false
		}
		if me.DnsStatus.GetText() != "WORKING" {
			log.Log(CHANGE, "Your IPv6 DNS settings have not been confirmed yet", me.DnsStatus.GetText()) 
			working = false
		}
		if me.DnsAPIstatus.GetText() != "WORKING" {
			log.Log(CHANGE, "The DNS API provider is not yet working", me.DnsAPIstatus.GetText())
			working = false
		}
		if working {
			log.Log(CHANGE, "EVERYTHING IS WORKING. YOU HAVE IPv6 BLISS. TODO: don't check so often now")
		} else {
			log.Log(CHANGE, "EVERYTHING IS NOT WORKING. scanning errors:")
			me.problems.Scan()
		}
	})
}

/*
	// Example usage
	duration := timeFunction(FunctionToTime)
	log.Println("Execution Time: ", duration)
*/

// timeFunction takes a function as an argument and returns the execution time.
func timeFunction(f func()) time.Duration {
	startTime := time.Now() // Record the start time
	f()                     // Execute the function
	return time.Since(startTime) // Calculate the elapsed time
}

func timeStamp() string {
	stamp := time.Now().Format("2006/01/02 15:04:05")
	log.Log(CHANGE, "Network things changed on", stamp)
	return stamp
}


func myTicker(t time.Duration, name string, f func()) {
	ticker := time.NewTicker(t)
	defer ticker.Stop()
	done := make(chan bool)
	/*
	go func() {
		time.Sleep(10 * time.Second)
		done <- true
	}()
	*/
	for {
		select {
		case <-done:
			fmt.Println("Done!")
			return
		case t := <-ticker.C:
			log.Log(INFO, name, "Current time: ", t)
			f()
		}
	}
}