-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.go
More file actions
45 lines (38 loc) · 903 Bytes
/
Copy pathresolver.go
File metadata and controls
45 lines (38 loc) · 903 Bytes
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
package main
import (
"errors"
"log"
"strings"
)
type Resolver struct {
conf *Config
sys *System
}
var ErrHostNotFound = errors.New("Host not found")
func NewResolver(system *System) *Resolver {
re := Resolver{
conf: system.config,
sys: system,
}
log.Printf("Resolving the following:\n")
for _, r := range system.config.Resolve {
log.Printf("%s -> %s\n", r.Name, r.IpV4)
}
return &re
}
func (r *Resolver) Resolve(domain string) (string, error) {
for _, r := range r.conf.Resolve {
log.Printf("testing: %s", r.Name)
if WildcardPatternMatch(strings.ToLower(domain), strings.ToLower(r.Name)) {
return r.IpV4, nil
}
}
log.Printf("Resolve with device cache")
domain = strings.TrimSuffix(domain, ".")
ip, err := r.sys.DeviceCache().NameToIP(domain)
if err != nil {
log.Printf("Name %s not found\n", domain)
return "", ErrHostNotFound
}
return ip.String(), nil
}