Skip to content

Commit be996c4

Browse files
committed
Local DoH support, continued
1 parent 1966a86 commit be996c4

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

dnscrypt-proxy/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Config struct {
3434
DisabledServerNames []string `toml:"disabled_server_names"`
3535
ListenAddresses []string `toml:"listen_addresses"`
3636
LocalDoHListenAddresses []string `toml:"local_doh_listen_addresses"`
37+
LocalDoHCertFile string `toml:"local_doh_cert_file"`
38+
LocalDoHCertKeyFile string `toml:"local_doh_cert_key_file"`
3739
Daemonize bool
3840
UserName string `toml:"user_name"`
3941
ForceTCP bool `toml:"force_tcp"`
@@ -96,6 +98,8 @@ func newConfig() Config {
9698
LogLevel: int(dlog.LogLevel()),
9799
ListenAddresses: []string{"127.0.0.1:53"},
98100
LocalDoHListenAddresses: []string{"127.0.0.1:443"},
101+
LocalDoHCertFile: "localhost.pem",
102+
LocalDoHCertKeyFile: "localhost.pem",
99103
Timeout: 5000,
100104
KeepAlive: 5,
101105
CertRefreshDelay: 240,
@@ -352,6 +356,8 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
352356

353357
proxy.listenAddresses = config.ListenAddresses
354358
proxy.localDoHListenAddresses = config.LocalDoHListenAddresses
359+
proxy.localDoHCertFile = config.LocalDoHCertFile
360+
proxy.localDoHCertKeyFile = config.LocalDoHCertKeyFile
355361
proxy.daemonize = config.Daemonize
356362
proxy.pluginBlockIPv6 = config.BlockIPv6
357363
proxy.cache = config.Cache

dnscrypt-proxy/local-doh.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ type localDoHHandler struct {
1515

1616
func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
1717
dataType := "application/dns-message"
18+
writer.Header().Set("Server", "dnscrypt-proxy")
1819
if request.Header.Get("Content-Type") != dataType {
1920
writer.WriteHeader(400)
21+
writer.Write([]byte("Unexpected Content-Type\n"))
2022
return
2123
}
2224
proxy := handler.proxy
@@ -32,14 +34,14 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
3234
dlog.Warnf("No body in a local DoH query")
3335
return
3436
}
35-
response := proxy.processIncomingQuery(proxy.serversInfo.getOne(), "tcp", "tcp", packet, &xClientAddr, nil, start)
37+
response := proxy.processIncomingQuery(proxy.serversInfo.getOne(), "http", proxy.mainProto, packet, &xClientAddr, nil, start)
3638
if len(response) == 0 {
3739
writer.WriteHeader(500)
3840
return
3941
}
42+
writer.Header().Set("Content-Type", "application/dns-message")
43+
writer.Header().Set("Content-Length", string(len(response)))
4044
writer.WriteHeader(200)
41-
writer.Header().Add("Server", "dnscrypt-proxy")
42-
writer.Header().Add("Content-Type", "application/dns-message")
4345
writer.Write(response)
4446
}
4547

@@ -50,7 +52,7 @@ func (proxy *Proxy) localDoHListener(acceptPc *net.TCPListener) {
5052
WriteTimeout: proxy.timeout,
5153
Handler: localDoHHandler{proxy: proxy},
5254
}
53-
if err := httpServer.Serve(acceptPc); err != nil {
55+
if err := httpServer.ServeTLS(acceptPc, proxy.localDoHCertFile, proxy.localDoHCertKeyFile); err != nil {
5456
dlog.Fatal(err)
5557
}
5658
}

dnscrypt-proxy/proxy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Proxy struct {
3232
mainProto string
3333
listenAddresses []string
3434
localDoHListenAddresses []string
35+
localDoHCertFile string
36+
localDoHCertKeyFile string
3537
daemonize bool
3638
registeredServers []RegisteredServer
3739
registeredRelays []RegisteredServer
@@ -558,9 +560,12 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
558560
}
559561
return
560562
}
561-
clientPc.Write(response)
563+
if clientPc != nil {
564+
clientPc.Write(response)
565+
}
562566
}
563567
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
568+
564569
return response
565570
}
566571

0 commit comments

Comments
 (0)