summaryrefslogtreecommitdiff
path: root/hostname.go
blob: 32897d55c05ddf33f8bf09d4f475574cacac46df (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
// inspired from:
// https://github.com/mactsouk/opensource.com.git
// and
// https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go

package main

import (
	"log"
//	"net"
	"strings"

	"go.wit.com/shell"

	"go.wit.com/control-panel-dns/cloudflare"

	"github.com/miekg/dns"
)

// will try to get this hosts FQDN
import "github.com/Showmax/go-fqdn"

// this is the king of dns libraries
// import "github.com/miekg/dns"


func getHostname() {
	var err error
	var s string = "gui.Label == nil"
	s, err = fqdn.FqdnHostname()
	if (err != nil) {
		debug(LogError, "FQDN hostname error =", err)
		return
	}
	if (me.fqdn != nil) {
		if (me.hostname != s) {
			me.fqdn.SetText(s)
			me.hostname = s
			me.changed = true
		}
	}
	debug(LogNet, "FQDN =", s)

	dn := run("domainname")
	if (me.domainname.S != dn) {
		debug(LogChange, "domainname has changed from", me.domainname.S, "to", dn)
		me.domainname.SetText(dn)
		me.changed = true
	}

	hshort := run("hostname -s")
	if (me.hostshort.S != hshort) {
		debug(LogChange, "hostname -s has changed from", me.hostshort.S, "to", hshort)
		me.hostshort.SetText(hshort)
		me.changed = true
	}

	var test string
	test = hshort + "." + dn
	if (me.hostname != test) {
		debug(LogInfo, "me.hostname", me.hostname, "does not equal", test)
		if (me.hostnameStatus.S != "BROKEN") {
			debug(LogChange, "me.hostname", me.hostname, "does not equal", test)
			me.changed = true
			me.hostnameStatus.SetText("BROKEN")
		}
	} else {
		if (me.hostnameStatus.S != "VALID") {
			debug(LogChange, "me.hostname", me.hostname, "is valid")
			me.hostnameStatus.SetText("VALID")
			me.changed = true
		}
		// enable the cloudflare button if the provider is cloudflare
		if (me.cloudflareB == nil) {
			debug(LogChange, "me.cloudflare == nil; me.DnsAPI.S =", me.DnsAPI.S)
			if (me.DnsAPI.S == "cloudflare") {
				me.cloudflareB = me.mainStatus.NewButton("cloudflare wit.com", func () {
					cloudflare.CreateRR(myGui, "wit.com", "3777302ac4a78cd7fa4f6d3f72086d06")
				})
			}
		}
	}
}

// returns true if the hostname is good
// check that all the OS settings are correct here
// On Linux, /etc/hosts, /etc/hostname
//      and domainname and hostname
func goodHostname(h string) bool {
	hostname := shell.Chomp(shell.Cat("/etc/hostname"))
	debug(true, "hostname =", hostname)

	hs := run("hostname -s")
	dn := run("domainname")
	debug(true, "hostname short =", hs, "domainname =", dn)

	tmp := hs + "." + dn
	if (hostname == tmp) {
		debug(true, "hostname seems to be good", hostname)
		return true
	}

	return false
}

/*
func digAAAA(s string) []string {
	var aaaa []string
	// lookup the IP address from DNS
	rrset := dnssecsocket.Dnstrace(s, "AAAA")
	// debug(true, args.VerboseDNS, SPEW, rrset)
	for i, rr := range rrset {
		ipaddr := dns.Field(rr, 1)
		// how the hell do you detect a RRSIG	AAAA record here?
		if (ipaddr == "28") {
			continue
		}
		debug(LogNow, "r.Answer =", i, "rr =", rr, "ipaddr =", ipaddr)
		aaaa = append(aaaa, ipaddr)
		me.ipv6s[ipaddr] = rr
	}
	debug(true, args.VerboseDNS, "aaaa =", aaaa)
	log.Println("digAAAA() returned =", aaaa)
	log.Println("digAAAA() me.ipv6s =", me.ipv6s)
	os.Exit(0)
	return aaaa
}
*/

func digAAAA(hostname string) []string {
	var blah, ipv6Addresses []string
	// domain := hostname
	recordType := dns.TypeAAAA // dns.TypeTXT

	// Cloudflare's DNS server
	blah, _ = dnsUdpLookup("1.1.1.1:53", hostname, recordType)
	log.Println("digAAAA() has BLAH =", blah)

	if (len(blah) == 0) {
		log.Println("digAAAA() RUNNING dnsAAAAlookupDoH(domain)")
		ipv6Addresses, _ = dnsAAAAlookupDoH(hostname)
		log.Println("digAAAA() has ipv6Addresses =", strings.Join(ipv6Addresses, " "))
		log.Printf("digAAAA() IPv6 Addresses for %s:\n", hostname)
		for _, addr := range ipv6Addresses {
			log.Println(addr)
		}
		return ipv6Addresses
	}

	// TODO: check digDoH vs blah, if so, then port 53 TCP and/or UDP is broken or blocked
	log.Println("digAAAA() has BLAH =", blah)

	return blah
}

/*
func dnsHttpsLookup(domain string, recordType uint16) ([]string, error) {
	domain := "google.com"
	dnsLookupDoH(domain string) ([]string, error) {
	ipv6Addresses, err := dnsLookupDoH(domain)
	if err != nil {
		log.Println("Error:", err)
		return
	}

	log.Printf("IPv6 Addresses for %s:\n", domain)
	for _, addr := range ipv6Addresses {
		log.Println(addr)
	}
}
*/