diff --git a/Makefile b/Makefile index c2ff15e..902686b 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,9 @@ bin_path=./bin wireformat: go build -o $(bin_path)/wireformat cmd/wireformat/wireformat.go +http-resolver: + go build -o $(bin_path)/http-resolver cmd/dns/http-resolver.go + fmt: go fmt ./... diff --git a/cmd/dns/http-resolver.go b/cmd/dns/http-resolver.go new file mode 100644 index 0000000..4860222 --- /dev/null +++ b/cmd/dns/http-resolver.go @@ -0,0 +1,44 @@ +package main + +import ( + "bytes" + "encoding/binary" + "io/ioutil" + "log" + "net/http" + "os" + + check "antoine-roux.ml/projects/go/dns-tools/internal" +) + +const ( + DnsResolverUrl = "https://cloudflare-dns.com/dns-query" + DnsMethod = "POST" +) + +func main() { + + // Remove timestamp prefix from log out + log.SetFlags(0) + + // Read wireformat from stdin + data, err := ioutil.ReadFile(os.Stdin.Name()) + check.Check(err) + + // Create dns-message post request + client := &http.Client{} + req, err := http.NewRequest(DnsMethod, DnsResolverUrl, bytes.NewReader(data)) + req.Header.Add("Content-type", "application/dns-message") + check.Check(err) + + // Do post request + resp, err := client.Do(req) + check.Check(err) + defer resp.Body.Close() + + // Read http response and Print wireformat to stdout + body, err := ioutil.ReadAll(resp.Body) + check.Check(err) + + binary.Write(os.Stdout, binary.LittleEndian, body) +} diff --git a/readme.md b/readme.md index 0331db6..bd181db 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,10 @@ dns relative tools like lookup or wireformat converter ## build -`make wireformat` +``` +make wireformat +make http-resolver +``` ## run @@ -21,3 +24,9 @@ Wireformat encode and decode `./bin/wireformat -s 'www.example.com' | ./bin/wireformat -f -` `echo 'www.example.com' | ./bin/wireformat | ./bin/wireformat -f -` + + +Dns query and wireformat encode/decode + +`echo 'www.google.com' | ./bin/wireformat -s - | ./bin/http-resolver | ./bin/wireformat -f -` +