feat : add http dns resolver with cloudflare

这个提交包含在:
Antoine 2020-11-09 03:35:09 +01:00
父节点 a19dbb1808
当前提交 aa3931e6df
签署人:: antoine
GPG 密钥 ID: 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 普通文件
查看文件

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