This commit is contained in:
Frank Denis 2019-11-26 01:36:35 +01:00
parent f249813cc5
commit 1966a8604b
2 changed files with 30 additions and 5 deletions

View File

@ -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)
}

View File

@ -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 {