mirror of
https://github.com/DNSCrypt/dnscrypt-proxy.git
synced 2025-04-05 06:07:36 +03:00
up
This commit is contained in:
parent
f249813cc5
commit
1966a8604b
2 changed files with 30 additions and 5 deletions
|
@ -1,13 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jedisct1/dlog"
|
"github.com/jedisct1/dlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
type localDoHHandler struct {
|
type localDoHHandler struct {
|
||||||
|
proxy *Proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
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)
|
writer.WriteHeader(400)
|
||||||
return
|
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.WriteHeader(200)
|
||||||
writer.Header().Add("Server", "dnscrypt-proxy")
|
writer.Header().Add("Server", "dnscrypt-proxy")
|
||||||
writer.Header().Add("Content-Type", "application/dns-message")
|
writer.Header().Add("Content-Type", "application/dns-message")
|
||||||
writer.Write([]byte("OK\n"))
|
writer.Write(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy *Proxy) localDoHListener(acceptPc *net.TCPListener) {
|
func (proxy *Proxy) localDoHListener(acceptPc *net.TCPListener) {
|
||||||
defer acceptPc.Close()
|
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 {
|
if err := httpServer.Serve(acceptPc); err != nil {
|
||||||
dlog.Fatal(err)
|
dlog.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
if len(query) < MinDNSPacketSize {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -427,7 +427,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
|
||||||
if len(query) < MinDNSPacketSize || len(query) > MaxDNSPacketSize {
|
if len(query) < MinDNSPacketSize || len(query) > MaxDNSPacketSize {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var response []byte
|
|
||||||
var err error
|
var err error
|
||||||
if pluginsState.action != PluginsActionForward {
|
if pluginsState.action != PluginsActionForward {
|
||||||
if pluginsState.synthResponse != nil {
|
if pluginsState.synthResponse != nil {
|
||||||
|
@ -549,7 +548,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
|
||||||
} else {
|
} else {
|
||||||
proxy.questionSizeEstimator.adjust(ResponseOverhead + len(response))
|
proxy.questionSizeEstimator.adjust(ResponseOverhead + len(response))
|
||||||
}
|
}
|
||||||
} else {
|
} else if clientProto == "tcp" {
|
||||||
response, err = PrefixWithSize(response)
|
response, err = PrefixWithSize(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pluginsState.returnCode = PluginsReturnCodeParseError
|
pluginsState.returnCode = PluginsReturnCodeParseError
|
||||||
|
@ -562,6 +561,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
|
||||||
clientPc.Write(response)
|
clientPc.Write(response)
|
||||||
}
|
}
|
||||||
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxy() *Proxy {
|
func NewProxy() *Proxy {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue