-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnscrypt_client.go
69 lines (52 loc) · 1.18 KB
/
dnscrypt_client.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"log"
"sync"
"time"
"github.com/ameshkov/dnscrypt/v2"
"github.com/miekg/dns"
)
type DCryptClient struct {
client *dnscrypt.Client
resolverInfos []*dnscrypt.ResolverInfo
// Last used index
lIndex int
logQueries bool
sync.Mutex
}
func (c *DCryptClient) GetDNSResponse(msg *dns.Msg) (*dns.Msg, error) {
if len(msg.Question) == 0 {
return nil, nil
}
c.Lock()
rinfo := c.resolverInfos[c.lIndex]
// Increase last index
c.lIndex++
if c.lIndex == len(c.resolverInfos) {
c.lIndex = 0
}
c.Unlock()
if c.logQueries {
log.Printf("Sending to %s for query: %s", rinfo.ProviderName, msg.Question[0].String())
}
return c.client.Exchange(msg, rinfo)
}
func NewDNSCryptClient(stamps []string, logQueries bool) (*DCryptClient, error) {
c := dnscrypt.Client{
Net: "udp",
Timeout: 10 * time.Second,
}
client := DCryptClient{
client: &c,
resolverInfos: make([]*dnscrypt.ResolverInfo, 0, len(stamps)),
logQueries: logQueries,
}
for _, stamp := range stamps {
resolverInfo, err := c.Dial(stamp)
if err != nil {
return nil, err
}
client.resolverInfos = append(client.resolverInfos, resolverInfo)
}
return &client, nil
}