From 8b45c99dfc1b075e9504ee47dcd7cdb8725074fc Mon Sep 17 00:00:00 2001 From: Star Brilliant Date: Sun, 1 Apr 2018 22:57:18 +0800 Subject: [PATCH] Adapt for CloudFlare DNS service --- doh-client/client.go | 8 ++++---- doh-client/doh-client.conf | 4 ++++ doh-client/google.go | 2 +- doh-server/server.go | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/doh-client/client.go b/doh-client/client.go index 54653dc..be8c3c2 100644 --- a/doh-client/client.go +++ b/doh-client/client.go @@ -178,21 +178,21 @@ func (c *Client) handlerFunc(w dns.ResponseWriter, r *dns.Msg, isTCP bool) { requestType := "" if len(c.conf.UpstreamIETF) == 0 { - requestType = "application/x-www-form-urlencoded" + requestType = "application/dns-json" } else if len(c.conf.UpstreamGoogle) == 0 { requestType = "application/dns-udpwireformat" } else { numServers := len(c.conf.UpstreamGoogle) + len(c.conf.UpstreamIETF) random := rand.Intn(numServers) if random < len(c.conf.UpstreamGoogle) { - requestType = "application/x-www-form-urlencoded" + requestType = "application/dns-json" } else { requestType = "application/dns-udpwireformat" } } var req *DNSRequest - if requestType == "application/x-www-form-urlencoded" { + if requestType == "application/dns-json" { req = c.generateRequestGoogle(w, r, isTCP) } else if requestType == "application/dns-udpwireformat" { req = c.generateRequestIETF(w, r, isTCP) @@ -211,7 +211,7 @@ func (c *Client) handlerFunc(w dns.ResponseWriter, r *dns.Msg, isTCP bool) { } else if candidateType == "application/dns-udpwireformat" { contentType = "application/dns-udpwireformat" } else { - if requestType == "application/x-www-form-urlencoded" { + if requestType == "application/dns-json" { contentType = "application/json" } else if requestType == "application/dns-udpwireformat" { contentType = "application/dns-udpwireformat" diff --git a/doh-client/doh-client.conf b/doh-client/doh-client.conf index da003a8..aa0d2b1 100644 --- a/doh-client/doh-client.conf +++ b/doh-client/doh-client.conf @@ -5,9 +5,11 @@ listen = "127.0.0.1:53" # If multiple servers are specified, a random one will be chosen each time. upstream_google = [ "https://dns.google.com/resolve", + #"https://cloudflare-dns.com/dns-query", ] upstream_ietf = [ #"https://dns.google.com/experimental", + #"https://cloudflare-dns.com/dns-query", ] # Bootstrap DNS server to resolve the address of the upstream resolver @@ -16,6 +18,8 @@ upstream_ietf = [ bootstrap = [ "8.8.8.8:53", "8.8.4.4:53", + #"1.1.1.1:53", + #"1.0.0.1:53", ] # Timeout for upstream request diff --git a/doh-client/google.go b/doh-client/google.go index c3a212a..7fe6686 100644 --- a/doh-client/google.go +++ b/doh-client/google.go @@ -66,7 +66,7 @@ func (c *Client) generateRequestGoogle(w dns.ResponseWriter, r *dns.Msg, isTCP b numServers := len(c.conf.UpstreamGoogle) upstream := c.conf.UpstreamGoogle[rand.Intn(numServers)] - requestURL := fmt.Sprintf("%s?name=%s&type=%s", upstream, url.QueryEscape(questionName), url.QueryEscape(questionType)) + requestURL := fmt.Sprintf("%s?ct=application/dns-json&name=%s&type=%s", upstream, url.QueryEscape(questionName), url.QueryEscape(questionType)) if r.CheckingDisabled { requestURL += "&cd=1" diff --git a/doh-server/server.go b/doh-server/server.go index 6446b27..6981482 100644 --- a/doh-server/server.go +++ b/doh-server/server.go @@ -96,7 +96,7 @@ func (s *Server) handlerFunc(w http.ResponseWriter, r *http.Request) { if contentType == "" { // Guess request Content-Type based on other parameters if r.FormValue("name") != "" { - contentType = "application/x-www-form-urlencoded" + contentType = "application/dns-json" } else if r.FormValue("dns") != "" { contentType = "application/dns-udpwireformat" } @@ -114,7 +114,7 @@ func (s *Server) handlerFunc(w http.ResponseWriter, r *http.Request) { } if responseType == "" { // Guess response Content-Type based on request Content-Type - if contentType == "application/x-www-form-urlencoded" { + if contentType == "application/dns-json" { responseType = "application/json" } else if contentType == "application/dns-udpwireformat" { responseType = "application/dns-udpwireformat" @@ -122,7 +122,7 @@ func (s *Server) handlerFunc(w http.ResponseWriter, r *http.Request) { } var req *DNSRequest - if contentType == "application/x-www-form-urlencoded" { + if contentType == "application/dns-json" { req = s.parseRequestGoogle(w, r) } else if contentType == "application/dns-udpwireformat" { req = s.parseRequestIETF(w, r)