Workaround a bug causing Firefox 61-62 to reject responses with Content-Type = application/dns-message

This commit is contained in:
Star Brilliant
2018-07-02 17:49:34 +08:00
parent a64df3f048
commit e19250dc99

View File

@@ -31,6 +31,7 @@ import (
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/m13253/dns-over-https/json-dns"
@@ -62,7 +63,7 @@ func (s *Server) parseRequestIETF(w http.ResponseWriter, r *http.Request) *DNSRe
}
}
if s.patchDNSCryptProxyReqID(requestBinary, w) {
if s.patchDNSCryptProxyReqID(w, r, requestBinary) {
return &DNSRequest{
errcode: 444,
}
@@ -166,6 +167,9 @@ func (s *Server) generateResponseIETF(w http.ResponseWriter, r *http.Request, re
}
w.Header().Set("Expires", respJSON.EarliestExpires.Format(http.TimeFormat))
}
_ = s.patchFirefoxContentType(w, r)
if respJSON.Status == dns.RcodeServerFailure {
w.WriteHeader(503)
}
@@ -173,8 +177,8 @@ func (s *Server) generateResponseIETF(w http.ResponseWriter, r *http.Request, re
}
// Workaround a bug causing DNSCrypt-Proxy to expect a response with TransactionID = 0xcafe
func (s *Server) patchDNSCryptProxyReqID(requestBinary []byte, w http.ResponseWriter) bool {
if bytes.Equal(requestBinary, []byte("\xca\xfe\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x00\x00\x02\x00\x01\x00\x00\x29\x10\x00\x00\x00\x80\x00\x00\x00")) {
func (s *Server) patchDNSCryptProxyReqID(w http.ResponseWriter, r *http.Request, requestBinary []byte) bool {
if strings.Contains(r.UserAgent(), "dnscrypt-proxy") && bytes.Equal(requestBinary, []byte("\xca\xfe\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x00\x00\x02\x00\x01\x00\x00\x29\x10\x00\x00\x00\x80\x00\x00\x00")) {
log.Println("DNSCrypt-Proxy detected. Patching response.")
w.Header().Set("Content-Type", "application/dns-message")
now := time.Now().UTC().Format(http.TimeFormat)
@@ -184,3 +188,12 @@ func (s *Server) patchDNSCryptProxyReqID(requestBinary []byte, w http.ResponseWr
}
return false
}
// Workaround a bug causing Firefox 61-62 to reject responses with Content-Type = application/dns-message
func (s *Server) patchFirefoxContentType(w http.ResponseWriter, r *http.Request) bool {
if strings.Contains(r.UserAgent(), "Firefox") && strings.Contains(r.Header.Get("Accept"), "application/dns-udpwireformat") && !strings.Contains(r.Header.Get("Accept"), "application/dns-message") {
w.Header().Set("Content-Type", "application/dns-udpwireformat")
return true
}
return false
}