fix: better doh server compatibility

This commit is contained in:
wwqgtxx 2024-07-23 08:46:27 +08:00
parent de61e81ff7
commit 13b7ab8da3

View File

@ -18,13 +18,7 @@ func dohRouter() http.Handler {
func dohHandler(w http.ResponseWriter, r *http.Request) { func dohHandler(w http.ResponseWriter, r *http.Request) {
if resolver.DefaultResolver == nil { if resolver.DefaultResolver == nil {
render.Status(r, http.StatusInternalServerError) render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError("DNS section is disabled")) render.PlainText(w, r, "DNS section is disabled")
return
}
if r.Header.Get("Accept") != "application/dns-message" {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError("invalid accept header"))
return return
} }
@ -36,19 +30,20 @@ func dohHandler(w http.ResponseWriter, r *http.Request) {
case "POST": case "POST":
if r.Header.Get("Content-Type") != "application/dns-message" { if r.Header.Get("Content-Type") != "application/dns-message" {
render.Status(r, http.StatusInternalServerError) render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError("invalid content-type")) render.PlainText(w, r, "invalid content-type")
return return
} }
dnsData, err = io.ReadAll(r.Body) reader := io.LimitReader(r.Body, 65535) // according to rfc8484, the maximum size of the DNS message is 65535 bytes
dnsData, err = io.ReadAll(reader)
_ = r.Body.Close() _ = r.Body.Close()
default: default:
render.Status(r, http.StatusMethodNotAllowed) render.Status(r, http.StatusMethodNotAllowed)
render.JSON(w, r, newError("method not allowed")) render.PlainText(w, r, "method not allowed")
return return
} }
if err != nil { if err != nil {
render.Status(r, http.StatusInternalServerError) render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(err.Error())) render.PlainText(w, r, err.Error())
return return
} }
@ -58,10 +53,11 @@ func dohHandler(w http.ResponseWriter, r *http.Request) {
dnsData, err = resolver.RelayDnsPacket(ctx, dnsData, dnsData) dnsData, err = resolver.RelayDnsPacket(ctx, dnsData, dnsData)
if err != nil { if err != nil {
render.Status(r, http.StatusInternalServerError) render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(err.Error())) render.PlainText(w, r, err.Error())
return return
} }
render.Status(r, http.StatusOK) w.Header().Set("Content-Type", "application/dns-message")
render.Data(w, r, dnsData) w.WriteHeader(http.StatusOK)
_, _ = w.Write(dnsData)
} }