-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremotes.go
75 lines (60 loc) · 1.34 KB
/
remotes.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
package dnsproxy
import (
"sync"
"github.com/miekg/dns"
)
// Remotes contains a mapping from domain names to remote DNS servers
type Remotes struct {
sync.RWMutex
m map[string][]string
}
// Add adds a mapping from domain to servers
func (r *Remotes) Add(domain string, servers []string) {
r.Lock()
defer r.Unlock()
r.m[domain] = servers
}
// Remove removes a mapping from domain to servers
func (r *Remotes) Remove(domain string) {
r.Lock()
defer r.Unlock()
delete(r.m, domain)
}
// Flush removes all mappings
func (r *Remotes) Flush() {
r.Lock()
defer r.Unlock()
r.m = make(map[string][]string)
}
// Get returns the servers for domain
func (r *Remotes) Get(domain string) []string {
r.RLock()
defer r.RUnlock()
// only handle domain names
if _, ok := dns.IsDomainName(domain); !ok {
// no domain name, return default remote
return r.m["."]
}
// get label indexes and find matching remotes
labels := dns.Split(domain)
if labels == nil {
// root domain, return default remote
return r.m["."]
}
// try finding longest matching domain name
for _, i := range labels {
d := domain[i:]
s := r.m[d]
if len(s) != 0 {
return s
}
}
// did not find anything, return default remote
return r.m["."]
}
// NewRemotes returns a new Remotes
func NewRemotes() *Remotes {
return &Remotes{
m: make(map[string][]string),
}
}