mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
make CoreDNS DoH Server (#1619)
* WIP: make CoreDNS DoH Server * It works * Fix tests * Review from Tom - on diff. PR * correct mime type * Cleanups and use the pkg/nonwriter * rename and updates * implement get * implement GET * Code review comments * correct context * tweaks * code review
This commit is contained in:
56
core/dnsserver/https.go
Normal file
56
core/dnsserver/https.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package dnsserver
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// mimeTypeDOH is the DoH mimetype that should be used.
|
||||
const mimeTypeDOH = "application/dns-message"
|
||||
|
||||
// pathDOH is the URL path that should be used.
|
||||
const pathDOH = "/dns-query"
|
||||
|
||||
// postRequestToMsg extracts the dns message from the request body.
|
||||
func postRequestToMsg(req *http.Request) (*dns.Msg, error) {
|
||||
defer req.Body.Close()
|
||||
|
||||
buf, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := new(dns.Msg)
|
||||
err = m.Unpack(buf)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// getRequestToMsg extract the dns message from the GET request.
|
||||
func getRequestToMsg(req *http.Request) (*dns.Msg, error) {
|
||||
values := req.URL.Query()
|
||||
b64, ok := values["dns"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no 'dns' query parameter found")
|
||||
}
|
||||
if len(b64) != 1 {
|
||||
return nil, fmt.Errorf("multiple 'dns' query values found")
|
||||
}
|
||||
return base64ToMsg(b64[0])
|
||||
}
|
||||
|
||||
func base64ToMsg(b64 string) (*dns.Msg, error) {
|
||||
buf, err := b64Enc.DecodeString(b64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := new(dns.Msg)
|
||||
err = m.Unpack(buf)
|
||||
|
||||
return m, err
|
||||
}
|
||||
|
||||
var b64Enc = base64.RawURLEncoding
|
||||
Reference in New Issue
Block a user