main
fangdingjun 6 years ago
parent dc19c58c54
commit 29ed6389b4

@ -1,19 +1,19 @@
package main package main
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func TestCFDns(t *testing.T) { func TestCFDns(t *testing.T) {
cf := &CloudflareHTTPDns{} cf := &CloudflareHTTPDns{}
m := new(dns.Msg) m := new(dns.Msg)
m.SetQuestion("www.google.com.", dns.TypeA) m.SetQuestion("www.google.com.", dns.TypeA)
m1, _, err := cf.Exchange(m, "1.1.1.1") m1, _, err := cf.Exchange(m, "1.1.1.1")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fmt.Println(m1.String()) fmt.Println(m1.String())
} }

@ -1,59 +1,59 @@
package main package main
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time" "time"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
var httpclientCF = &http.Client{ var httpclientCF = &http.Client{
Timeout: 8 * time.Second, Timeout: 8 * time.Second,
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{ServerName: "cloudflare-dns.com"}, TLSClientConfig: &tls.Config{ServerName: "cloudflare-dns.com"},
TLSHandshakeTimeout: 5 * time.Second, TLSHandshakeTimeout: 5 * time.Second,
}, },
} }
// CloudflareHTTPDns cloudflare http dns // CloudflareHTTPDns cloudflare http dns
type CloudflareHTTPDns struct { type CloudflareHTTPDns struct {
} }
// Exchange send request to server and get result // Exchange send request to server and get result
func (cf *CloudflareHTTPDns) Exchange(m *dns.Msg, addr string) (m1 *dns.Msg, d time.Duration, err error) { func (cf *CloudflareHTTPDns) Exchange(m *dns.Msg, addr string) (m1 *dns.Msg, d time.Duration, err error) {
u := fmt.Sprintf("https://%s/dns-query", addr) u := fmt.Sprintf("https://%s/dns-query", addr)
data, err := m.Pack() data, err := m.Pack()
if err != nil { if err != nil {
return return
} }
body := bytes.NewBuffer(data) body := bytes.NewBuffer(data)
req, err := http.NewRequest("POST", u, body) req, err := http.NewRequest("POST", u, body)
if err != nil { if err != nil {
return return
} }
req.Header.Set("Content-Type", "application/dns-udpwireformat") req.Header.Set("Content-Type", "application/dns-udpwireformat")
resp, err := httpclientCF.Do(req) resp, err := httpclientCF.Do(req)
if err != nil { if err != nil {
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body) data, err = ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return return
} }
m1 = new(dns.Msg) m1 = new(dns.Msg)
if err = m1.Unpack(data); err != nil { if err = m1.Unpack(data); err != nil {
return return
} }
return return
} }

Loading…
Cancel
Save