From 74bdbb38ac1d2e57418adec51c06909d95174d8c Mon Sep 17 00:00:00 2001 From: Antoine Date: Mon, 9 Nov 2020 02:18:51 +0100 Subject: [PATCH] add wireformat question creation from string or stdin --- cmd/wireformat/wireformat.go | 29 +++++++++++++++++++++++++++++ readme.md | 5 +++++ 2 files changed, 34 insertions(+) diff --git a/cmd/wireformat/wireformat.go b/cmd/wireformat/wireformat.go index d908948..d5e6d67 100644 --- a/cmd/wireformat/wireformat.go +++ b/cmd/wireformat/wireformat.go @@ -1,18 +1,25 @@ package main import ( + "encoding/binary" "errors" "flag" "io/ioutil" "log" + "os" + "strings" "github.com/miekg/dns" ) func main() { file := flag.String("f", "", "file path to read hexa decimal wireformat data") + domainToResolve := flag.String("s", "-", "Server adress to resolve") flag.Parse() + // remove timestamp prefix from log out + log.SetFlags(0) + var body []byte var err error if *file != "" { @@ -31,6 +38,28 @@ func main() { } log.Println(msg) + } else if *domainToResolve != "" { + if *domainToResolve == "-" { + readDomainToResolve, err := ioutil.ReadFile("/dev/stdin") + if err != nil { + log.Fatalln(err) + } + *domainToResolve = strings.TrimSuffix(string(readDomainToResolve), "\n") + } + msg := &dns.Msg{ + Question: []dns.Question{ + dns.Question{ + dns.Fqdn(*domainToResolve), + dns.TypeANY, + dns.ClassINET, + }, + }, + } + b, err := msg.Pack() + if err != nil { + log.Fatalln(err) + } + binary.Write(os.Stdout, binary.LittleEndian, b) } else { log.Println(errors.New("Too few argument pass")) flag.Usage() diff --git a/readme.md b/readme.md index 3a0563f..0331db6 100644 --- a/readme.md +++ b/readme.md @@ -16,3 +16,8 @@ With post resolution `curl -s -H accept: application/dns-message https://cloudflare-dns.com/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB | ./bin/wireformat -f -` +Wireformat encode and decode + +`./bin/wireformat -s 'www.example.com' | ./bin/wireformat -f -` + +`echo 'www.example.com' | ./bin/wireformat | ./bin/wireformat -f -`