-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.go
47 lines (39 loc) · 912 Bytes
/
resolver.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
package main
import (
"errors"
"fmt"
"net"
"strings"
"time"
)
type DnsResolver struct {
db *LogDb
}
func NewDnsResolver(db *LogDb) (*DnsResolver, error) {
return &DnsResolver{db: db}, nil
}
func (t *DnsResolver) ResolveDomain(ipAddr string) (string, error) {
if ipAddr == "<empty>" {
return "<empty>", nil
}
return t.db.GetCached("DnsResolver:ResolveDomain:"+ipAddr, 24*time.Hour, func() (string, error) {
finalName := fmt.Sprintf("<Unresolved: %s>", ipAddr)
vals, err := net.LookupAddr(ipAddr)
if err != nil {
if strings.Contains(err.Error(), "no such host") {
return finalName, nil
}
return finalName, errors.Join(errors.New("unable to resolve domain"), err)
}
if len(vals) == 0 {
return finalName, nil
}
finalName = vals[0]
for idx, val := range vals {
if idx > 0 && len(val) < len(finalName) {
finalName = val
}
}
return finalName, nil
})
}