-
Notifications
You must be signed in to change notification settings - Fork 62
/
example_test.go
59 lines (46 loc) · 1.63 KB
/
example_test.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
package rdns_test
import (
"fmt"
rdns "github.com/folbricht/routedns"
"github.com/miekg/dns"
)
func Example_resolver() {
// Define resolver
r, _ := rdns.NewDoTClient("test-dot", "dns.google:853", rdns.DoTClientOptions{})
// Build a query
q := new(dns.Msg)
q.SetQuestion("google.com.", dns.TypeA)
// Resolve the query
a, _ := r.Resolve(q, rdns.ClientInfo{})
fmt.Println(a)
}
func Example_group() {
// Define resolvers
r1, _ := rdns.NewDNSClient("google1", "8.8.8.8:53", "udp", rdns.DNSClientOptions{})
r2, _ := rdns.NewDNSClient("google2", "8.8.4.4:53", "udp", rdns.DNSClientOptions{})
// Combine them int a group that does round-robin over the two resolvers
g := rdns.NewRoundRobin("test-rr", r1, r2)
// Build a query
q := new(dns.Msg)
q.SetQuestion("google.com.", dns.TypeA)
// Resolve the query
a, _ := g.Resolve(q, rdns.ClientInfo{})
fmt.Println(a)
}
func Example_router() {
// Define resolvers
google, _ := rdns.NewDNSClient("g-dns", "8.8.8.8:53", "udp", rdns.DNSClientOptions{})
cloudflare, _ := rdns.NewDNSClient("cf-dns", "1.1.1.1:53", "udp", rdns.DNSClientOptions{})
// Build a router that will send all "*.cloudflare.com" to the cloudflare
// resolver while everything else goes to the google resolver (default)
route1, _ := rdns.NewRoute(`\.cloudflare\.com\.$`, "", nil, nil, "", "", "", "", "", "", cloudflare)
route2, _ := rdns.NewRoute("", "", nil, nil, "", "", "", "", "", "", google)
r := rdns.NewRouter("my-router")
r.Add(route1, route2)
// Build a query
q := new(dns.Msg)
q.SetQuestion("www.cloudflare.com.", dns.TypeA)
// Resolve the query
a, _ := r.Resolve(q, rdns.ClientInfo{})
fmt.Println(a)
}