/
picker.go
84 lines (75 loc) · 2.06 KB
/
picker.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package picker
import (
"fmt"
"math/rand"
"net/netip"
"github.com/qdm12/dns/v2/pkg/provider"
)
type Picker struct {
rand *rand.Rand
}
// New returns a new fast thread safe random picker
// to use for DNS servers and their IP addresses.
func New() *Picker {
source := newMaphashSource()
return &Picker{
rand: rand.New(source), //nolint:gosec
}
}
func (p *Picker) DoHServer(servers []provider.DoHServer) provider.DoHServer {
index := 0
if nServers := len(servers); nServers > 1 {
index = p.rand.Intn(nServers)
}
return servers[index]
}
func (p *Picker) DoTServer(servers []provider.DoTServer) provider.DoTServer {
index := 0
if nServers := len(servers); nServers > 1 {
index = p.rand.Intn(nServers)
}
return servers[index]
}
// DotAddrPort returns a randomly picked IP address and port
// from the given DoT server. If ipv6 is true, IPv6 addresses
// are added to the pool of IP addresses to pick from, on top
// of all IPv4 addresses.
// Note IPv4 addresses are always in the pool of addresses,
// because some providers only have IPv4 addresses, and IPv4
// usually works on an IPv6 network, which is not true the other
// way around.
func (p *Picker) DoTAddrPort(server provider.DoTServer, ipv6 bool) netip.AddrPort {
totalSize := len(server.IPv4)
if ipv6 {
totalSize += len(server.IPv6)
}
serverIPs := make([]netip.AddrPort, 0, totalSize)
serverIPs = append(serverIPs, server.IPv4...)
if ipv6 {
serverIPs = append(serverIPs, server.IPv6...)
}
addrPort := p.addrPort(serverIPs)
if addrPort.IsValid() {
return addrPort
}
// this should never happen since every servers
// should have at least one IP address matching the
// IP version given. This is more of a programming
// safety.
ipVersions := "IPv4"
if ipv6 {
ipVersions += " or IPv6"
}
panic(fmt.Sprintf("no valid " + ipVersions + " address found"))
}
func (p *Picker) addrPort(addrPorts []netip.AddrPort) netip.AddrPort {
switch len(addrPorts) {
case 0:
return netip.AddrPort{}
case 1:
return addrPorts[0]
default:
index := p.rand.Intn(len(addrPorts))
return addrPorts[index]
}
}