Last active
August 5, 2019 09:33
-
-
Save magisterquis/d48921bcd2d9736929a7e5afea6edb87 to your computer and use it in GitHub Desktop.
Quick and Dirty DNS-over-HTTPS in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/base64" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"golang.org/x/net/dns/dnsmessage" | |
) | |
func main() { | |
/* Roll a DNS query */ | |
qm, err := (&dnsmessage.Message{ | |
Header: dnsmessage.Header{ | |
RecursionDesired: true, | |
}, | |
Questions: []dnsmessage.Question{{ | |
Name: dnsmessage.MustNewName("yahoo.com."), | |
Type: dnsmessage.TypeA, | |
Class: dnsmessage.ClassINET, | |
}}, | |
}).Pack() | |
if nil != err { | |
log.Fatalf("Unable to marshal DNS request: %v", err) | |
} | |
/* Encode it and send it to the server */ | |
res, err := http.Get( | |
"https://dns.quad9.net/dns-query?dns=" + | |
base64.RawURLEncoding.EncodeToString(qm), | |
) | |
if nil != err { | |
log.Fatalf("HTTP Error: %v", err) | |
} | |
if http.StatusOK != res.StatusCode { | |
log.Fatalf("Non-OK HTTP response: %v", res.Status) | |
os.Exit(1) | |
} | |
/* Unroll DNS answer */ | |
b, err := ioutil.ReadAll(res.Body) | |
if nil != err { | |
log.Fatalf("Erorr reading HTTP response: %v", err) | |
} | |
var am dnsmessage.Message | |
if err := (&am).Unpack(b); nil != err { | |
log.Fatalf("Error unpacking DNS response %q: %v", b, err) | |
} | |
/* Ugly-Print DNS response header and answer records */ | |
fmt.Printf("Response header:\n\t%+v\n", am.Header) | |
if 0 == len(am.Answers) { | |
fmt.Printf("No answers\n") | |
return | |
} | |
fmt.Printf("Answer Records:\n") | |
for _, a := range am.Answers { | |
fmt.Printf("\t%+v\t%T:%v\n", a.Header, a.Body, a.Body) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment