2019-11-24 22:45:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-26 01:36:35 +01:00
|
|
|
"io/ioutil"
|
2019-11-24 22:45:43 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-11-26 01:36:35 +01:00
|
|
|
"time"
|
2019-11-24 22:45:43 +01:00
|
|
|
|
|
|
|
"github.com/jedisct1/dlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type localDoHHandler struct {
|
2019-11-26 01:36:35 +01:00
|
|
|
proxy *Proxy
|
2019-11-24 22:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
dataType := "application/dns-message"
|
|
|
|
if request.Header.Get("Content-Type") != dataType {
|
|
|
|
writer.WriteHeader(400)
|
|
|
|
return
|
|
|
|
}
|
2019-11-26 01:36:35 +01:00
|
|
|
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
|
|
|
|
}
|
2019-11-24 22:45:43 +01:00
|
|
|
writer.WriteHeader(200)
|
|
|
|
writer.Header().Add("Server", "dnscrypt-proxy")
|
|
|
|
writer.Header().Add("Content-Type", "application/dns-message")
|
2019-11-26 01:36:35 +01:00
|
|
|
writer.Write(response)
|
2019-11-24 22:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (proxy *Proxy) localDoHListener(acceptPc *net.TCPListener) {
|
|
|
|
defer acceptPc.Close()
|
2019-11-26 01:36:35 +01:00
|
|
|
httpServer := &http.Server{
|
|
|
|
ReadTimeout: proxy.timeout,
|
|
|
|
WriteTimeout: proxy.timeout,
|
|
|
|
Handler: localDoHHandler{proxy: proxy},
|
|
|
|
}
|
2019-11-24 22:45:43 +01:00
|
|
|
if err := httpServer.Serve(acceptPc); err != nil {
|
|
|
|
dlog.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|