-
Notifications
You must be signed in to change notification settings - Fork 0
/
dial.go
46 lines (38 loc) · 1.22 KB
/
dial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package doh
import (
"bytes"
"context"
"net"
"sync"
"github.com/shplack/dns/pkg/dot"
"github.com/shplack/dns/pkg/provider"
)
type dialFunc func(ctx context.Context, _, _ string) (net.Conn, error)
func newDoHDial(settings ResolverSettings) dialFunc {
dohServers := make([]provider.DoHServer, len(settings.DoHProviders))
for i := range settings.DoHProviders {
dohServers[i] = settings.DoHProviders[i].DoH()
}
// DoT HTTP client to resolve the DoH URL hostname
DoTSettings := dot.ResolverSettings{
DoTProviders: settings.SelfDNS.DoTProviders,
DNSProviders: settings.SelfDNS.DNSProviders,
Timeout: settings.Timeout, // http client timeout really
IPv6: settings.SelfDNS.IPv6,
}
dotClient := newDoTClient(DoTSettings)
// HTTP bodies buffer pool
bufferPool := &sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
}
picker := newPicker() // fast thread safe random picker
return func(ctx context.Context, _, _ string) (conn net.Conn, err error) {
// Pick DoH server pseudo-randomly from the chosen providers
DoHServer := picker.DoHServer(dohServers)
// Create connection object (no actual IO yet)
conn = newDoHConn(ctx, dotClient, bufferPool, DoHServer.URL)
return conn, nil
}
}