Skip to content

Commit 1966a86

Browse files
committed
up
1 parent f249813 commit 1966a86

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

dnscrypt-proxy/local-doh.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"net"
56
"net/http"
7+
"time"
68

79
"github.com/jedisct1/dlog"
810
)
911

1012
type localDoHHandler struct {
13+
proxy *Proxy
1114
}
1215

1316
func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
@@ -16,15 +19,37 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
1619
writer.WriteHeader(400)
1720
return
1821
}
22+
proxy := handler.proxy
23+
start := time.Now()
24+
clientAddr, err := net.ResolveTCPAddr("tcp", request.RemoteAddr)
25+
if err != nil {
26+
dlog.Errorf("Unable to get the client address: [%v]", err)
27+
return
28+
}
29+
xClientAddr := net.Addr(clientAddr)
30+
packet, err := ioutil.ReadAll(request.Body)
31+
if err != nil {
32+
dlog.Warnf("No body in a local DoH query")
33+
return
34+
}
35+
response := proxy.processIncomingQuery(proxy.serversInfo.getOne(), "tcp", "tcp", packet, &xClientAddr, nil, start)
36+
if len(response) == 0 {
37+
writer.WriteHeader(500)
38+
return
39+
}
1940
writer.WriteHeader(200)
2041
writer.Header().Add("Server", "dnscrypt-proxy")
2142
writer.Header().Add("Content-Type", "application/dns-message")
22-
writer.Write([]byte("OK\n"))
43+
writer.Write(response)
2344
}
2445

2546
func (proxy *Proxy) localDoHListener(acceptPc *net.TCPListener) {
2647
defer acceptPc.Close()
27-
httpServer := &http.Server{ReadTimeout: proxy.timeout, WriteTimeout: proxy.timeout, Handler: localDoHHandler{}}
48+
httpServer := &http.Server{
49+
ReadTimeout: proxy.timeout,
50+
WriteTimeout: proxy.timeout,
51+
Handler: localDoHHandler{proxy: proxy},
52+
}
2853
if err := httpServer.Serve(acceptPc); err != nil {
2954
dlog.Fatal(err)
3055
}

dnscrypt-proxy/proxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func (proxy *Proxy) clientsCountDec() {
414414
}
415415
}
416416

417-
func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time) {
417+
func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto string, serverProto string, query []byte, clientAddr *net.Addr, clientPc net.Conn, start time.Time) (response []byte) {
418418
if len(query) < MinDNSPacketSize {
419419
return
420420
}
@@ -427,7 +427,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
427427
if len(query) < MinDNSPacketSize || len(query) > MaxDNSPacketSize {
428428
return
429429
}
430-
var response []byte
431430
var err error
432431
if pluginsState.action != PluginsActionForward {
433432
if pluginsState.synthResponse != nil {
@@ -549,7 +548,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
549548
} else {
550549
proxy.questionSizeEstimator.adjust(ResponseOverhead + len(response))
551550
}
552-
} else {
551+
} else if clientProto == "tcp" {
553552
response, err = PrefixWithSize(response)
554553
if err != nil {
555554
pluginsState.returnCode = PluginsReturnCodeParseError
@@ -562,6 +561,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
562561
clientPc.Write(response)
563562
}
564563
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
564+
return response
565565
}
566566

567567
func NewProxy() *Proxy {

0 commit comments

Comments
 (0)