feat : add http dns resolver with cloudflare

This commit is contained in:
Antoine 2020-11-09 03:35:09 +01:00
джерело a19dbb1808
коміт aa3931e6df
Підписано: antoine
Ідентифікатор GPG ключа: 098FB66FC0475E70
3 змінених файлів з 57 додано та 1 видалено

@ -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 ./...

44
cmd/dns/http-resolver.go Normal file

@ -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)
}

@ -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 -`