-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (63 loc) · 1.79 KB
/
main.go
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
// DoH-reference-client is a learning project to get comfortable
// with DNS-over-HTTP.
//
// It's written after the 14th edition of the IETF draft ``DNS Queries over HTTPS (DoH)``,
// https://tools.ietf.org/html/draft-ietf-doh-dns-over-https-14.
//
// Each implementation detail is documented with a comment from the
// corresponding section in the draft.
//
// This project is not meant to be used for anything else accept learning!
//
package main
import (
"flag"
"fmt"
"log"
"strings"
"github.com/pascaldierich/doh-reference-client/lib"
)
var server = flag.String("server", "https://mozilla.cloudflare-dns.com/dns-query", "DoH server address")
var method = flag.String("method", "GET", "http method to use. Select \"GET\" or \"POST\"")
var address = flag.String("address", "", "host address to resolve")
func main() {
flag.Parse()
if *address == "" {
log.Fatal("Set address to resolve")
}
switch *method {
case "GET", "get":
rrs, err := sendGETRequest(*server, *address)
if err != nil {
log.Fatal(err)
}
printIP(rrs)
case "POST", "post":
rrs, err := sendPOSTRequest(*server, *address)
if err != nil {
log.Fatal(err)
}
printIP(rrs)
default:
log.Fatal("Select http method. Use \"GET\" or \"POST\"")
}
}
func printIP(rrs []*lib.RR) {
// NOTE: At the moment we can only unmarshal A RDATA formats
// as described in RFC 1035. Thats why we print out the last
// RDATA section which most likely is an A RDATA format,
// as the previous would be a CNAME.
var ip string
for _, rr := range rrs {
ip = UnmarshalRDATA(rr.RDATA)
}
fmt.Printf("-> %v\n", ip)
}
// Unmarshals RDATA format as A RDATA format as described in RFC 1035.
func UnmarshalRDATA(data []byte) (ip string) {
for _, b := range data {
ip += fmt.Sprintf("%d.", int(b))
}
ip = strings.TrimSuffix(ip, ".")
return
}