diff --git a/dnscrypt-proxy/local-doh.go b/dnscrypt-proxy/local-doh.go index 67a4b57c..b30832ab 100644 --- a/dnscrypt-proxy/local-doh.go +++ b/dnscrypt-proxy/local-doh.go @@ -1,13 +1,16 @@ package main import ( + "io/ioutil" "net" "net/http" + "time" "github.com/jedisct1/dlog" ) type localDoHHandler struct { + proxy *Proxy } func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { @@ -16,15 +19,37 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht writer.WriteHeader(400) return } + proxy := handler.proxy + start := time.Now() + clientAddr, err := net.ResolveTCPAddr("tcp", request.RemoteAddr) + if err != nil { + dlog.Errorf("Unable to get the client address: [%v]", err) + return + } + xClientAddr := net.Addr(clientAddr) + packet, err := ioutil.ReadAll(request.Body) + if err != nil { + dlog.Warnf("No body in a local DoH query") + return + } + response := proxy.processIncomingQuery(proxy.serversInfo.getOne(), "tcp", "tcp", packet, &xClientAddr, nil, start) + if len(response) == 0 { + writer.WriteHeader(500) + return + } writer.WriteHeader(200) writer.Header().Add("Server", "dnscrypt-proxy") writer.Header().Add("Content-Type", "application/dns-message") - writer.Write([]byte("OK\n")) + writer.Write(response) } func (proxy *Proxy) localDoHListener(acceptPc *net.TCPListener) { defer acceptPc.Close() - httpServer := &http.Server{ReadTimeout: proxy.timeout, WriteTimeout: proxy.timeout, Handler: localDoHHandler{}} + httpServer := &http.Server{ + ReadTimeout: proxy.timeout, + WriteTimeout: proxy.timeout, + Handler: localDoHHandler{proxy: proxy}, + } if err := httpServer.Serve(acceptPc); err != nil { dlog.Fatal(err) } diff --git a/dnscrypt-proxy/proxy.go b/dnscrypt-proxy/proxy.go index bae19298..2b3d2ed4 100644 --- a/dnscrypt-proxy/proxy.go +++ b/dnscrypt-proxy/proxy.go @@ -414,7 +414,7 @@ func (proxy *Proxy) clientsCountDec() { } } -func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time) { +func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time) (response []byte) { if len(query) < MinDNSPacketSize { return } @@ -427,7 +427,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str if len(query) < MinDNSPacketSize || len(query) > MaxDNSPacketSize { return } - var response []byte var err error if pluginsState.action != PluginsActionForward { if pluginsState.synthResponse != nil { @@ -549,7 +548,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str } else { proxy.questionSizeEstimator.adjust(ResponseOverhead + len(response)) } - } else { + } else if clientProto == "tcp" { response, err = PrefixWithSize(response) if err != nil { pluginsState.returnCode = PluginsReturnCodeParseError @@ -562,6 +561,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str clientPc.Write(response) } pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals) + return response } func NewProxy() *Proxy {