2023-12-28 09:43:45 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-21 00:15:20 -06:00
|
|
|
"encoding/json"
|
2023-12-28 09:43:45 -06:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2024-01-21 00:15:20 -06:00
|
|
|
"strings"
|
2024-01-06 16:59:33 -06:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2024-01-21 00:15:20 -06:00
|
|
|
"go.wit.com/log"
|
2023-12-28 09:43:45 -06:00
|
|
|
)
|
|
|
|
|
2023-12-29 01:36:10 -06:00
|
|
|
// dnsLookupDoH performs a DNS lookup for AAAA records over HTTPS.
|
|
|
|
func lookupDoH(hostname string, rrType string) []string {
|
|
|
|
var values []string
|
|
|
|
|
|
|
|
// Construct the URL for a DNS query with Google's DNS-over-HTTPS API
|
2024-01-06 01:41:33 -06:00
|
|
|
url := fmt.Sprintf("https://dns.google.com/resolve?name=%s&type=%s", hostname, rrType)
|
2023-12-29 01:36:10 -06:00
|
|
|
|
2024-01-06 01:41:33 -06:00
|
|
|
log.Log(DNS, "lookupDoH()", url)
|
|
|
|
if hostname == "" {
|
|
|
|
log.Warn("lookupDoH() was sent a empty hostname")
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-29 01:36:10 -06:00
|
|
|
|
|
|
|
// Perform the HTTP GET request
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "error performing DNS-over-HTTPS request")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Read and unmarshal the response body
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Errorf("error reading response: %w", err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Answer []struct {
|
|
|
|
Data string `json:"data"`
|
|
|
|
} `json:"Answer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, &data); err != nil {
|
|
|
|
log.Error(fmt.Errorf("error unmarshaling response: %w", err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the IPv6 addresses
|
|
|
|
for _, answer := range data.Answer {
|
|
|
|
values = append(values, answer.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values
|
|
|
|
}
|
2024-01-06 16:59:33 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-01-21 00:15:20 -06:00
|
|
|
if len(blah) == 0 {
|
2024-01-06 16:59:33 -06:00
|
|
|
log.Println("digAAAA() RUNNING dnsAAAAlookupDoH(domain)")
|
|
|
|
ipv6Addresses = lookupDoH(hostname, "AAAA")
|
|
|
|
log.Println("digAAAA() has ipv6Addresses =", strings.Join(ipv6Addresses, " "))
|
|
|
|
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
|
|
|
|
}
|