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
|
package main
import (
"fmt"
"go.wit.com/log"
"io/ioutil"
"encoding/json"
"net/http"
)
/*
func getAAAArecords() {
hostname := "go.wit.com"
ipv6Addresses, err := dnsLookupDoH(hostname)
if err != nil {
log.Error(err, "getAAAArecords")
return
}
fmt.Printf("IPv6 Addresses for %s:\n", hostname)
for _, addr := range ipv6Addresses {
log.Println(addr)
}
}
*/
// dnsLookupDoH performs a DNS lookup for AAAA records over HTTPS.
func dnsAAAAlookupDoH(domain string) ([]string, error) {
var ipv6Addresses []string
// Construct the URL for a DNS query with Google's DNS-over-HTTPS API
url := fmt.Sprintf("https://dns.google/resolve?name=%s&type=AAAA", domain)
log.Println("curl", url)
// Perform the HTTP GET request
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("error performing DNS-over-HTTPS request: %w", err)
}
defer resp.Body.Close()
// Read and unmarshal the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response: %w", err)
}
var data struct {
Answer []struct {
Data string `json:"data"`
} `json:"Answer"`
}
if err := json.Unmarshal(body, &data); err != nil {
return nil, fmt.Errorf("error unmarshaling response: %w", err)
}
// Extract the IPv6 addresses
for _, answer := range data.Answer {
ipv6Addresses = append(ipv6Addresses, answer.Data)
}
return ipv6Addresses, nil
}
// 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
url := fmt.Sprintf("https://dns.google/resolve?name=%s&type=%s", hostname, rrType)
log.Println("curl", url)
// 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
}
|