aboutsummaryrefslogtreecommitdiff
path: root/lib/dns/server.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-01-27 14:18:03 +0700
committerShulhan <ms@kilabit.info>2026-02-02 16:28:11 +0700
commit93149aeb09a1641be37d6fc62846a244d957b81a (patch)
tree7fa8144dd82f7a3aba1227949cc1c827928d4511 /lib/dns/server.go
parentec3f606769672da427080d0ccdd5dcd100d63f17 (diff)
downloadpakakeh.go-93149aeb09a1641be37d6fc62846a244d957b81a.tar.xz
lib/dns: use separate ServeMux for handling DoH
Using the [http.DefaultServeMux] will cause panic when the server restarted automatically.
Diffstat (limited to 'lib/dns/server.go')
-rw-r--r--lib/dns/server.go32
1 files changed, 12 insertions, 20 deletions
diff --git a/lib/dns/server.go b/lib/dns/server.go
index 075eecb9..32fd8241 100644
--- a/lib/dns/server.go
+++ b/lib/dns/server.go
@@ -15,7 +15,6 @@ import (
"log"
"net"
"net/http"
- "strings"
"sync"
"time"
)
@@ -246,18 +245,20 @@ func (srv *Server) serveDoH() {
var (
logp = `serveDoH`
addr = srv.opts.getHTTPAddress().String()
-
- err error
)
+ var mux = http.NewServeMux()
+
+ mux.Handle(`/dns-query`, srv)
+
srv.doh = &http.Server{
Addr: addr,
IdleTimeout: srv.opts.HTTPIdleTimeout,
ReadHeaderTimeout: 5 * time.Second,
+ Handler: mux,
}
- http.Handle("/dns-query", srv)
-
+ var err error
if srv.tlsConfig != nil && !srv.opts.DoHBehindProxy {
log.Printf(`%s: listening at %s`, logp, addr)
srv.doh.TLSConfig = srv.tlsConfig
@@ -399,26 +400,17 @@ func (srv *Server) serveUDP() {
}
}
+// ServeHTTP the main handle for DNS-over-HTTPS.
func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- var (
- hdr = w.Header()
-
- hdrAcceptValue string
- )
-
- hdr.Set(dohHeaderKeyContentType, dohHeaderValDNSMessage)
-
- hdrAcceptValue = r.Header.Get(dohHeaderKeyAccept)
- if len(hdrAcceptValue) == 0 {
+ const acceptDNSMessage = `application/dns-message`
+ var hdrAcceptValue = r.Header.Get(`Accept`)
+ if hdrAcceptValue != acceptDNSMessage {
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}
- hdrAcceptValue = strings.ToLower(hdrAcceptValue)
- if hdrAcceptValue != dohHeaderValDNSMessage {
- w.WriteHeader(http.StatusUnsupportedMediaType)
- return
- }
+ var hdr = w.Header()
+ hdr.Set(`Content-Type`, acceptDNSMessage)
if r.Method == http.MethodGet {
srv.handleDoHGet(w, r)