Skip to content

Commit

Permalink
fix: better doh server compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Jul 23, 2024
1 parent de61e81 commit 13b7ab8
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions hub/route/doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ func dohRouter() http.Handler {
func dohHandler(w http.ResponseWriter, r *http.Request) {
if resolver.DefaultResolver == nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError("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"))
render.PlainText(w, r, "DNS section is disabled")
return
}

Expand All @@ -36,19 +30,20 @@ func dohHandler(w http.ResponseWriter, r *http.Request) {
case "POST":
if r.Header.Get("Content-Type") != "application/dns-message" {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError("invalid content-type"))
render.PlainText(w, r, "invalid content-type")
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()
default:
render.Status(r, http.StatusMethodNotAllowed)
render.JSON(w, r, newError("method not allowed"))
render.PlainText(w, r, "method not allowed")
return
}
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(err.Error()))
render.PlainText(w, r, err.Error())
return
}

Expand All @@ -58,10 +53,11 @@ func dohHandler(w http.ResponseWriter, r *http.Request) {
dnsData, err = resolver.RelayDnsPacket(ctx, dnsData, dnsData)
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(err.Error()))
render.PlainText(w, r, err.Error())
return
}

render.Status(r, http.StatusOK)
render.Data(w, r, dnsData)
w.Header().Set("Content-Type", "application/dns-message")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(dnsData)
}

0 comments on commit 13b7ab8

Please sign in to comment.