2024-07-23 00:01:41 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
func dohRouter() http.Handler {
|
|
|
|
return http.HandlerFunc(dohHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dohHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if resolver.DefaultResolver == nil {
|
|
|
|
render.Status(r, http.StatusInternalServerError)
|
2024-07-23 08:46:27 +08:00
|
|
|
render.PlainText(w, r, "DNS section is disabled")
|
2024-07-23 00:01:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var dnsData []byte
|
|
|
|
var err error
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
dnsData, err = base64.RawURLEncoding.DecodeString(r.URL.Query().Get("dns"))
|
|
|
|
case "POST":
|
|
|
|
if r.Header.Get("Content-Type") != "application/dns-message" {
|
|
|
|
render.Status(r, http.StatusInternalServerError)
|
2024-07-23 08:46:27 +08:00
|
|
|
render.PlainText(w, r, "invalid content-type")
|
2024-07-23 00:01:41 +08:00
|
|
|
return
|
|
|
|
}
|
2024-07-23 08:46:27 +08:00
|
|
|
reader := io.LimitReader(r.Body, 65535) // according to rfc8484, the maximum size of the DNS message is 65535 bytes
|
|
|
|
dnsData, err = io.ReadAll(reader)
|
2024-07-23 00:01:41 +08:00
|
|
|
_ = r.Body.Close()
|
|
|
|
default:
|
|
|
|
render.Status(r, http.StatusMethodNotAllowed)
|
2024-07-23 08:46:27 +08:00
|
|
|
render.PlainText(w, r, "method not allowed")
|
2024-07-23 00:01:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
render.Status(r, http.StatusInternalServerError)
|
2024-07-23 08:46:27 +08:00
|
|
|
render.PlainText(w, r, err.Error())
|
2024-07-23 00:01:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
dnsData, err = resolver.RelayDnsPacket(ctx, dnsData, dnsData)
|
|
|
|
if err != nil {
|
|
|
|
render.Status(r, http.StatusInternalServerError)
|
2024-07-23 08:46:27 +08:00
|
|
|
render.PlainText(w, r, err.Error())
|
2024-07-23 00:01:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-23 08:46:27 +08:00
|
|
|
w.Header().Set("Content-Type", "application/dns-message")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write(dnsData)
|
2024-07-23 00:01:41 +08:00
|
|
|
}
|