Local DoH: add support for request using the GET method

Fixes #2012
This commit is contained in:
Frank Denis 2022-01-31 14:56:46 +01:00
parent e6089449b6
commit c10e6e0635
1 changed files with 24 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package main
import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net"
@ -29,7 +31,27 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
writer.WriteHeader(404)
return
}
if request.Header.Get("Content-Type") != dataType {
packet := []byte{}
var err error
start := time.Now()
if request.Method == "POST" &&
request.Header.Get("Content-Type") == dataType {
packet, err = ioutil.ReadAll(io.LimitReader(request.Body, int64(MaxDNSPacketSize)))
if err != nil {
dlog.Warnf("No body in a local DoH query")
return
}
} else if request.Method == "GET" && request.Header.Get("Accept") == dataType {
encodedPacket := request.URL.Query().Get("dns")
if len(encodedPacket) >= MinDNSPacketSize*4/3 && len(encodedPacket) <= MaxDNSPacketSize*4/3 {
packet, err = base64.RawURLEncoding.DecodeString(encodedPacket)
if err != nil {
dlog.Warnf("Invalid base64 in a local DoH query")
return
}
}
}
if len(packet) < MinDNSPacketSize {
writer.Header().Set("Content-Type", "text/plain")
writer.WriteHeader(400)
writer.Write([]byte("dnscrypt-proxy local DoH server\n"))
@ -41,12 +63,6 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
return
}
xClientAddr := net.Addr(clientAddr)
start := time.Now()
packet, err := ioutil.ReadAll(io.LimitReader(request.Body, MaxHTTPBodyLength))
if err != nil {
dlog.Warnf("No body in a local DoH query")
return
}
hasEDNS0Padding, err := hasEDNS0Padding(packet)
if err != nil {
writer.WriteHeader(400)
@ -76,6 +92,7 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
writer.Header().Set("X-Pad", pad)
}
writer.Header().Set("Content-Type", dataType)
writer.Header().Set("Content-Length", fmt.Sprint(len(response)))
writer.WriteHeader(200)
writer.Write(response)
}