Local DoH support, continued

This commit is contained in:
Frank Denis 2019-11-28 16:46:25 +01:00
parent 1966a8604b
commit be996c486f
3 changed files with 18 additions and 5 deletions

View File

@ -34,6 +34,8 @@ type Config struct {
DisabledServerNames []string `toml:"disabled_server_names"` DisabledServerNames []string `toml:"disabled_server_names"`
ListenAddresses []string `toml:"listen_addresses"` ListenAddresses []string `toml:"listen_addresses"`
LocalDoHListenAddresses []string `toml:"local_doh_listen_addresses"` LocalDoHListenAddresses []string `toml:"local_doh_listen_addresses"`
LocalDoHCertFile string `toml:"local_doh_cert_file"`
LocalDoHCertKeyFile string `toml:"local_doh_cert_key_file"`
Daemonize bool Daemonize bool
UserName string `toml:"user_name"` UserName string `toml:"user_name"`
ForceTCP bool `toml:"force_tcp"` ForceTCP bool `toml:"force_tcp"`
@ -96,6 +98,8 @@ func newConfig() Config {
LogLevel: int(dlog.LogLevel()), LogLevel: int(dlog.LogLevel()),
ListenAddresses: []string{"127.0.0.1:53"}, ListenAddresses: []string{"127.0.0.1:53"},
LocalDoHListenAddresses: []string{"127.0.0.1:443"}, LocalDoHListenAddresses: []string{"127.0.0.1:443"},
LocalDoHCertFile: "localhost.pem",
LocalDoHCertKeyFile: "localhost.pem",
Timeout: 5000, Timeout: 5000,
KeepAlive: 5, KeepAlive: 5,
CertRefreshDelay: 240, CertRefreshDelay: 240,
@ -352,6 +356,8 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.listenAddresses = config.ListenAddresses proxy.listenAddresses = config.ListenAddresses
proxy.localDoHListenAddresses = config.LocalDoHListenAddresses proxy.localDoHListenAddresses = config.LocalDoHListenAddresses
proxy.localDoHCertFile = config.LocalDoHCertFile
proxy.localDoHCertKeyFile = config.LocalDoHCertKeyFile
proxy.daemonize = config.Daemonize proxy.daemonize = config.Daemonize
proxy.pluginBlockIPv6 = config.BlockIPv6 proxy.pluginBlockIPv6 = config.BlockIPv6
proxy.cache = config.Cache proxy.cache = config.Cache

View File

@ -15,8 +15,10 @@ type localDoHHandler struct {
func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
dataType := "application/dns-message" dataType := "application/dns-message"
writer.Header().Set("Server", "dnscrypt-proxy")
if request.Header.Get("Content-Type") != dataType { if request.Header.Get("Content-Type") != dataType {
writer.WriteHeader(400) writer.WriteHeader(400)
writer.Write([]byte("Unexpected Content-Type\n"))
return return
} }
proxy := handler.proxy proxy := handler.proxy
@ -32,14 +34,14 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
dlog.Warnf("No body in a local DoH query") dlog.Warnf("No body in a local DoH query")
return return
} }
response := proxy.processIncomingQuery(proxy.serversInfo.getOne(), "tcp", "tcp", packet, &xClientAddr, nil, start) response := proxy.processIncomingQuery(proxy.serversInfo.getOne(), "http", proxy.mainProto, packet, &xClientAddr, nil, start)
if len(response) == 0 { if len(response) == 0 {
writer.WriteHeader(500) writer.WriteHeader(500)
return return
} }
writer.Header().Set("Content-Type", "application/dns-message")
writer.Header().Set("Content-Length", string(len(response)))
writer.WriteHeader(200) writer.WriteHeader(200)
writer.Header().Add("Server", "dnscrypt-proxy")
writer.Header().Add("Content-Type", "application/dns-message")
writer.Write(response) writer.Write(response)
} }
@ -50,7 +52,7 @@ func (proxy *Proxy) localDoHListener(acceptPc *net.TCPListener) {
WriteTimeout: proxy.timeout, WriteTimeout: proxy.timeout,
Handler: localDoHHandler{proxy: proxy}, Handler: localDoHHandler{proxy: proxy},
} }
if err := httpServer.Serve(acceptPc); err != nil { if err := httpServer.ServeTLS(acceptPc, proxy.localDoHCertFile, proxy.localDoHCertKeyFile); err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
} }

View File

@ -32,6 +32,8 @@ type Proxy struct {
mainProto string mainProto string
listenAddresses []string listenAddresses []string
localDoHListenAddresses []string localDoHListenAddresses []string
localDoHCertFile string
localDoHCertKeyFile string
daemonize bool daemonize bool
registeredServers []RegisteredServer registeredServers []RegisteredServer
registeredRelays []RegisteredServer registeredRelays []RegisteredServer
@ -558,9 +560,12 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} }
return return
} }
clientPc.Write(response) if clientPc != nil {
clientPc.Write(response)
}
} }
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals) pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return response return response
} }